func (this *SocialAuthController) ConnectPost() { token := this.Ctx.GetCookie("epic_user_token") ok, userId := tools.VerifyToken(token) if !ok || len(userId) == 0 { this.Redirect("/", 302) return } password := this.GetString("Password") userName := this.GetString("UserName") if len(userId) == 0 || len(password) == 0 || len(userName) == 0 { this.Data["userId"] = userId this.TplNames = "connect.html" this.Data["error"] = "[用户名]或者[密码]为空" this.Data["state"] = "注册失败" this.Data["msg"] = "[用户名]或者[邮箱]已被注册" return } isExist := auth.UserIsExists(userName, userName) if isExist { this.Data["userId"] = userId this.TplNames = "connect.html" this.Data["error"] = "[用户名]或者[邮箱]已被注册" this.Data["state"] = "注册失败" this.Data["msg"] = "[用户名]或者[邮箱]已被注册" return } user := models.User{} user.Password = password user.UserName = userName user.Id, _ = strconv.Atoi(userId) err := auth.ConnectUpdateUser(&user, password) if err != nil { this.Data["userId"] = userId this.TplNames = "connect.html" this.Data["error"] = err.Error() this.Data["state"] = "注册失败" beego.Error("注册失败-插入数据库出错", err) this.Data["msg"] = err.Error() return } subSitesConf := config.GetSubSites() this.Data["srcs"] = strings.Split(subSitesConf, ",") this.Data["token"] = token this.Data["state"] = "注册成功" this.Data["msg"] = "3秒后自动跳转!!" this.Data["succ"] = true this.Data["redirectURL"] = config.GetRedirectURL() this.TplNames = "succeed.html" }
func VerifyUser(username, password string) (bool, *models.User) { isExists := UserIsExists(username, username) user := models.User{} if !isExists { return false, &user } var err error qs := orm.NewOrm() if strings.IndexRune(username, '@') == -1 { user.UserName = username err = qs.Read(&user, "UserName") } else { user.Email = username err = qs.Read(&user, "Email") } if err != nil { fmt.Println("用户登录读取用户信息失败" + err.Error()) return false, &user } ok := VerifyPassword(password, user.Password) return ok, &user }
func RegisterUser(user *models.User, username, email, password string) error { // use random salt encode password salt := models.GetUserSalt() pwd := tools.EncodePassword(password, salt) user.UserName = strings.ToLower(username) user.Email = strings.ToLower(email) // save salt and encode password, use $ as split char user.Password = fmt.Sprintf("%s$%s", salt, pwd) // save md5 email value for gravatar user.GrEmail = tools.EncodeMd5(user.Email) // Use username as default nickname. user.NickName = user.UserName //设置用户默认激活 user.IsActive = true return user.Insert() }
func InitConnect(identify string) (string, bool) { user := models.User{} user.Identify = identify err := user.Read("Identify") if err != nil { err = user.Insert() if err != nil { fmt.Println("connect创建用户失败-" + err.Error()) } } id := user.Id password := user.Password if len(password) == 0 { return strconv.Itoa(id), false } else { return strconv.Itoa(id), true } }
func UserIsExists(username, email string) bool { user := models.User{} user.UserName = strings.ToLower(username) user.Email = strings.ToLower(email) return user.Exists() }
func ConnectUpdateUser(user *models.User, password string) error { salt := models.GetUserSalt() pwd := tools.EncodePassword(password, salt) user.Password = fmt.Sprintf("%s$%s", salt, pwd) return user.Update("UserName", "Password") }
func SaveNewPassword(user *models.User, password string) error { salt := models.GetUserSalt() user.Password = fmt.Sprintf("%s$%s", salt, tools.EncodePassword(password, salt)) return user.Update("Password", "Rands", "Updated") }