Example #1
0
// Login method
func (c *LoginController) Login() {
	if c.Ctx.Request.Method == "GET" {
		c.renderLoginLayout()
		c.Data["Title"] = "登录"
		c.Data["ShowError"] = false
		c.TplName = "login.tpl"
	} else if c.Ctx.Request.Method == "POST" {
		var input struct {
			Username string `form:"username"`
			Password string `form:"password"`
		}
		c.ParseForm(&input)
		logger.Debug("Input:%+v", input)

		if input.Username == "" || input.Password == "" {
			c.RenderLoginError("用户名或密码不能为空", input.Username)
			return
		}

		user := models.FindUserByName(input.Username)
		if user != nil && user.CheckPassword(input.Password) {
			c.SetSession("uid", user.ID)
			uid := c.Ctx.Input.Session("uid")
			// sessionID := c.GetSession("uid")
			logger.Debug("sessionID: %v", uid)
			c.Redirect("/dashboard", 302)
		} else {
			c.RenderLoginError("用户名或密码错误", input.Username)
		}
	}
}
Example #2
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()
}
Example #3
0
// FindUserByID method
func FindUserByID(id int64) *User {
	user := User{}
	err := userColl.FindId(id).One(&user)
	if err != nil {
		logger.Debug("Find user by id error:%v", err)
		return nil
	}
	return &user
}
Example #4
0
// FindUserByName method
func FindUserByName(name string) *User {
	user := User{}
	err := userColl.Find(bson.M{"name": name}).One(&user)
	if err != nil {
		logger.Debug("Find user[name:%s] error:%v", name, err)
		return nil
	}

	return &user
}
Example #5
0
// AllUsers method
func AllUsers() []*User {
	users := []*User{}

	err := userColl.Find(bson.M{}).All(&users)
	if err != nil {
		logger.Debug("Find all user error: %v", err)
		return nil
	}

	return users
}
Example #6
0
// Destroy method: json data returned
func (c *UserController) Destroy() {
	user, err := c.findUser()
	if err != nil {
		c.RenderCustomResponse(1, "没有找到该用户", nil)
		return
	}

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

	logger.Debug("Will delete user:%+v", user)
	err = models.DeleteUserByID(user.ID)
	if err != nil {
		c.RenderCustomResponse(1, err.Error(), nil)
	}

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