예제 #1
0
// Update method
func (c *UserController) Update() {
	userID, err := strconv.ParseInt(c.Ctx.Input.Param(":id"), 10, 0)
	if err != nil {
		logger.Critical("Parse user id error: %v", err)
		c.Abort("500")
		return
	}

	var input struct {
		Password        string `json:"password"`
		PasswordConfirm string `json:"password_confirm"`
	}

	if err = c.ParseJSON(&input); err != nil {
		logger.Critical("Parse JSON params error: %v", err)
		c.Abort("500")
		return
	}
	logger.Debug("input params: %+v", input)

	user := models.FindUserByID(userID)
	if user == nil {
		logger.Critical("User ID[%d] not found", userID)
		c.Abort("500")
		return
	}

	if err = user.Save(); err != nil {
		logger.Critical("Save user error: %v", err)
		c.Abort("500")
		return
	}

	c.Index()
}
예제 #2
0
// Create method
func (c *UserController) Create() {
	var input struct {
		Username        string `form:"username"`
		Password        string `form:"password"`
		PasswordConfirm string `form:"password_confirm"`
	}

	if err := c.ParseForm(&input); err != nil {
		logger.Critical("Register error: %v", err)
		c.Abort("500")
	}

	if input.Username == "" || input.Password == "" || input.PasswordConfirm == "" {
		c.RenderCreateError("请填入完整的注册信息", input.Username)
		return
	}

	if input.Password != input.PasswordConfirm {
		c.RenderCreateError("两次输入的密码不一致", input.Username)
		return
	}

	if user := models.FindUserByName(input.Username); user != nil {
		c.RenderCreateError("用户名已被占用", input.Username)
		return
	}

	user := &models.User{Name: input.Username, Password: input.Password, PasswordChanged: true}
	if err := user.Save(); err != nil {
		logger.Critical("Register error: %v", err)
		c.Abort("500")
	}

	c.Redirect("/pending", 302)
}
예제 #3
0
func (c *UserController) findUser() (*models.User, error) {
	userID, err := strconv.ParseInt(c.Ctx.Input.Param(":id"), 10, 0)
	if err != nil {
		logger.Critical("Parse user id error: %v", err)
		return nil, err
	}

	user := models.FindUserByID(userID)
	if user == nil {
		logger.Critical("User ID[%d] not found", userID)
		return nil, errors.New("User not found in database")
	}

	return user, nil
}
예제 #4
0
// SyncCounter method
func SyncCounter() {
	counterMutex.Lock()
	if err := counterColl.Update(nil, Counter); err != nil {
		logger.Critical("SyncCounter Error: %v", err)
	}
	counterMutex.Unlock()
}
예제 #5
0
// CounterIncr method, if f() returns nil
func CounterIncr(key string, f CounterTransactionFunc) error {
	counterMutex.Lock()
	defer counterMutex.Unlock()

	if _, ok := Counter[key]; !ok {
		Counter[key] = 0
	}
	Counter[key]++

	err := f(Counter[key])
	if err == nil {
		if e := counterColl.Update(nil, Counter); e != nil {
			logger.Critical("SyncCounter Error: %v", e)
		}
	} else {
		Counter[key]-- // Roll back
	}

	return err
}
예제 #6
0
func (c *UserController) setUserActive(active bool) error {
	user, err := c.findUser()
	if err != nil {
		c.RenderCustomResponse(1, err.Error(), nil)
		return err
	}

	if err = c.adminRestrict(user); err != nil {
		return err
	}

	user.IsValid = active
	if err = user.Save(); err != nil {
		logger.Critical("Save user error: %v", err)
		c.RenderCustomResponse(1, err.Error(), nil)
		return err
	}

	c.RenderCustomResponse(0, "", nil)
	return nil
}