func (u *User) Login(mData map[string]string) (int64, string) { username, ulen := mData["username"], len(mData["username"]) password, plen := mData["password"], len(mData["password"]) if ulen == 0 || plen == 0 { return u.code + 2, "" } result, err := u.Where("username =? ", username).Get() if err != nil { return u.code + 3, "" } flag, s := u.Password(password, fmt.Sprintf("%s", result["hash"])) if !flag { return u.code + 3, "" } if !bytes.Equal([]byte(s), utils.ItoByte(result["password"])) { return u.code + 4, "" } str := fmt.Sprintf("%d|%s|%s", result["uid"], result["username"], result["hash"]) cstr, err := utils.Encrypt(str, "12345678") if err != nil { return u.code, "" } return 0, cstr }
func (c *User) getGroupName(groupid []byte) string { //group list mgroup := models.NewGroupModel() groups, err := mgroup.Gets() if err != nil { return "" } for _, group := range groups { if bytes.Equal(groupid, utils.ItoByte(group["groupid"])) { return utils.ItoString(group["name"]) } } return "" }
func (u *User) Valid(mData *map[string]string) (int, string) { d := *mData username, ulen := d["username"], len(d["username"]) password, plen := d["password"], len(d["password"]) _, elen := d["email"], len(d["email"]) //groupid, _ := d["groupid"], len(d["groupid"]) r_password, rplen := d["r_password"], len(d["r_password"]) hash := utils.RandString(8) if username != "" && ulen == 0 { return -1, "用户不能为空." } if elen == 0 { return -1, "邮箱不能为空." } if plen == 0 || rplen == 0 { return -1, "密码不能为空." } if !bytes.Equal(utils.ItoByte(password), utils.ItoByte(r_password)) { return -1, "密码与确认密码不一致." } delete(d, "r_password") if password != "" { d["hash"] = hash flag, password := u.Password(d["password"], hash) if !flag { return -1, "密码操作失败." } d["password"] = password } return 0, "" }
func (c *User) Index() { user := models.NewUserModel() users, err := user.Gets() if err == nil { for _, user := range users { user["registertime"] = time.Unix(utils.ItoInt(user["registertime"]), 0).String() if utils.ItoInt(user["groupid"]) == 0 { user["groupname"] = "超级管理员" } else { user["groupname"] = c.getGroupName(utils.ItoByte(user["groupid"])) } } c.Assign("users", users) } }
func (u *User) IsLogin() (bool, map[string]interface{}) { flag, info := u.GetLoginUser() if !flag { return false, nil } result, err := u.Where("uid = ? AND username = ?", info[0], info[1]).Get() if err != nil { return false, nil } if !bytes.Equal(utils.ItoByte(result["hash"]), []byte(info[2])) { return false, nil } return true, result }
func (u *User) CheckPasswd(password string) (bool, string) { if len(password) == 0 { return false, "原始密码不能为空." } flag, info := u.GetLoginUser() if !flag { return false, "用户未登录." } result, err := u.Where("username =? ", info[1]).Get() if err != nil { return false, "获取用户信息失败." } flag, str := u.Password(password, fmt.Sprintf("%s", result["hash"])) if !flag { return false, "密码加密失败." } if !bytes.Equal([]byte(str), utils.ItoByte(result["password"])) { return false, "原始密码不正确." } return true, "" }