// Active implemented check Email actice code. func (this *RegisterActive) Get() { // no need active if this.CheckActiveRedirect(false) { return } code := this.Params().Get(":code") var user models.User if auth.VerifyUserActiveCode(&user, code) { user.IsActive = true user.Rands = models.GetUserSalt() if err := models.UpdateById(user.Id, user, models.Obj2Table([]string{"IsActive", "Rands", "Updated"})...); err != nil { log.Error("Active: user Update ", err) } if this.IsLogin { this.User = user } this.Redirect("/active/success", 302) } else { this.Data["Success"] = false } this.Render("auth/active.html", this.Data) }
func UserFollow(user *models.User, theUser *models.User) { if err := models.GetById(theUser.Id, theUser); err != nil { var mutual bool tFollow := models.Follow{UserId: theUser.Id, FollowUserId: user.Id} if err := models.GetByExample(&tFollow); err == nil { mutual = true } follow := models.Follow{UserId: user.Id, FollowUserId: theUser.Id, Mutual: mutual} if err := models.Insert(&follow); err == nil && mutual { tFollow.Mutual = mutual models.UpdateById(tFollow.Id, &tFollow, "mutual") } if nums, err := models.Count(&models.Follow{UserId: user.Id}); err == nil { user.Following = int(nums) models.UpdateById(user.Id, user, "following") } if nums, err := models.Count(&models.Follow{FollowUserId: theUser.Id}); err == nil { theUser.Followers = int(nums) models.UpdateById(theUser.Id, theUser, "followers") } } }
// Reset implemented user password reset. func (this *ResetRouter) Post() { code := this.GetString(":code") this.Data["Code"] = code var user models.User if auth.VerifyUserResetPwdCode(&user, code) { this.Data["Success"] = true form := auth.ResetPwdForm{} if this.ValidFormSets(&form) == false { return } user.IsActive = true user.Rands = models.GetUserSalt() if err := auth.SaveNewPassword(&user, form.Password); err != nil { log.Error("ResetPost Save New Password: "******"/login", 302, "ResetSuccess") } else { this.Data["Success"] = false } this.Render("auth/reset.html", this.Data) }
// register create user func RegisterUser(user *models.User, username, email, password string, locale i18n.Locale) error { // use random salt encode password salt := models.GetUserSalt() pwd := utils.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 = utils.EncodeMd5(user.Email) // Use username as default nickname. user.NickName = user.UserName //set default language if locale.Lang == "en-US" { user.Lang = setting.LangEnUS } else { user.Lang = setting.LangZhCN } //set default avatar user.AvatarType = setting.AvatarTypeGravatar return models.Insert(user) }
// check if exist user by username or email func HasUser(user *models.User, username string) bool { var userTemp models.User if strings.IndexRune(username, '@') == -1 { userTemp.UserName = username } else { userTemp.Email = username } err := models.GetByExample(&userTemp) if err == nil { *user = userTemp return true } return false }
func (this *UserRouter) getUser(user *models.User) bool { username := this.Params().Get(":username") user.UserName = username u, err := models.GetUserByName(username) if err != nil { this.NotFound() return true } *user = *u IsFollowed := false if this.IsLogin { if this.User.Id != user.Id { IsFollowed = models.IsExist(&models.Follow{ UserId: this.User.Id, FollowUserId: user.Id, }) } } this.Data["TheUser"] = &user this.Data["IsFollowed"] = IsFollowed return false }
func UserUnFollow(user *models.User, theUser *models.User) { follow := &models.Follow{UserId: user.Id, FollowUserId: theUser.Id} num, _ := models.ORM().Delete(follow) if num > 0 { models.ORM().UseBool().Update(&models.Follow{}, follow) if nums, err := models.Count(&models.Follow{UserId: user.Id}); err == nil { user.Following = int(nums) models.UpdateById(user.Id, user, "following") } if nums, err := models.Count(&models.Follow{FollowUserId: theUser.Id}); err == nil { theUser.Followers = int(nums) models.UpdateById(theUser.Id, theUser, "followers") } } }
// get user by erify code func getVerifyUser(user *models.User, code string) bool { if len(code) <= utils.TimeLimitCodeLength { return false } // use tail hex username query user hexStr := code[utils.TimeLimitCodeLength:] if b, err := hex.DecodeString(hexStr); err == nil { user.UserName = string(b) if u, err := models.GetUserByName(string(b)); err == nil { *user = *u return true } } return false }
func (form *ProfileForm) SaveUserProfile(user *models.User) error { // set md5 value if the value is an email if strings.IndexRune(form.GrEmail, '@') != -1 { form.GrEmail = utils.EncodeMd5(form.GrEmail) } changes := utils.FormChanges(user, form) if len(changes) > 0 { // if email changed then need re-active if user.Email != form.Email { user.IsActive = false changes = append(changes, "IsActive") } utils.SetFormValues(form, user) return models.UpdateById(user.Id, user, changes...) } return nil }
//upload user avatar func UploadUserAvatarToQiniu(r io.ReadSeeker, filename string, mime string, bucketName string, user *models.User) error { var ext string // test image mime type switch mime { case "image/jpeg": ext = ".jpg" case "image/png": ext = ".png" case "image/gif": ext = ".gif" default: ext = filepath.Ext(filename) switch ext { case ".jpg", ".png", ".gif": default: return fmt.Errorf("unsupport image format `%s`", filename) } } // decode image var err error switch ext { case ".jpg": _, err = jpeg.Decode(r) case ".png": _, err = png.Decode(r) case ".gif": _, err = gif.Decode(r) } if err != nil { return err } //reset reader pointer if _, err := r.Seek(0, 0); err != nil { return err } var data []byte if data, err = ioutil.ReadAll(r); err != nil { return err } if len(data) > setting.AvatarImageMaxLength { return fmt.Errorf("avatar image size too large", filename) } //reset reader pointer again if _, err := r.Seek(0, 0); err != nil { return err } //save to qiniu var uptoken = utils.GetQiniuUptoken(bucketName) var putRet qio.PutRet var putExtra = &qio.PutExtra{ MimeType: mime, } err = qio.PutWithoutKey(nil, &putRet, uptoken, r, putExtra) if err != nil { return err } //update user user.AvatarKey = putRet.Key if err := models.UpdateById(user.Id, user, "avatar_key", "updated"); err != nil { return err } return nil }
//set a new avatar type to user func SaveAvatarType(user *models.User, avatarType int) error { user.AvatarType = avatarType return models.UpdateById(user.Id, user, "avatar_type", "updated") }
// set a new password to user func SaveNewPassword(user *models.User, password string) error { salt := models.GetUserSalt() user.Password = fmt.Sprintf("%s$%s", salt, utils.EncodePassword(password, salt)) return models.UpdateById(user.Id, user, "password", "rands", "updated") }