func SettingsPasswordPost(ctx *middleware.Context, form auth.ChangePasswordForm) { ctx.Data["Title"] = ctx.Tr("settings") ctx.Data["PageIsUserSettings"] = true ctx.Data["PageIsSettingsPassword"] = true if ctx.HasError() { ctx.HTML(200, SETTINGS_PASSWORD) return } tmpUser := &models.User{ Passwd: form.OldPassword, Salt: ctx.User.Salt, } tmpUser.EncodePasswd() if ctx.User.Passwd != tmpUser.Passwd { ctx.Flash.Error(ctx.Tr("settings.password_incorrect")) } else if form.Password != form.Retype { ctx.Flash.Error(ctx.Tr("form.password_not_match")) } else { ctx.User.Passwd = form.Password ctx.User.Salt = models.GetUserSalt() ctx.User.EncodePasswd() if err := models.UpdateUser(ctx.User); err != nil { ctx.Handle(500, "UpdateUser", err) return } log.Trace("User password updated: %s", ctx.User.Name) ctx.Flash.Success(ctx.Tr("settings.change_password_success")) } ctx.Redirect("/user/settings/password") }
func Activate(ctx *middleware.Context) { code := ctx.Query("code") if len(code) == 0 { ctx.Data["IsActivatePage"] = true if ctx.User.IsActive { ctx.Error(404) return } // Resend confirmation e-mail. if setting.Service.RegisterEmailConfirm { if ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName) { ctx.Data["ResendLimited"] = true } else { ctx.Data["Hours"] = setting.Service.ActiveCodeLives / 60 mailer.SendActiveMail(ctx.Render, ctx.User) if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil { log.Error(4, "Set cache(MailResendLimit) fail: %v", err) } } } else { ctx.Data["ServiceNotEnabled"] = true } ctx.HTML(200, ACTIVATE) return } // Verify code. if user := models.VerifyUserActiveCode(code); user != nil { user.IsActive = true user.Rands = models.GetUserSalt() if err := models.UpdateUser(user); err != nil { if err == models.ErrUserNotExist { ctx.Error(404) } else { ctx.Handle(500, "UpdateUser", err) } return } log.Trace("User activated: %s", user.Name) ctx.Session.Set("uid", user.Id) ctx.Session.Set("uname", user.Name) ctx.Redirect("/") return } ctx.Data["IsActivateFailed"] = true ctx.HTML(200, ACTIVATE) }
func EditUserPost(ctx *middleware.Context, form auth.AdminEditUserForm) { ctx.Data["Title"] = ctx.Tr("admin.users.edit_account") ctx.Data["PageIsAdmin"] = true ctx.Data["PageIsAdminUsers"] = true uid := com.StrTo(ctx.Params(":userid")).MustInt64() if uid == 0 { ctx.Handle(404, "EditUser", nil) return } u, err := models.GetUserById(uid) if err != nil { ctx.Handle(500, "GetUserById", err) return } if ctx.HasError() { ctx.HTML(200, USER_EDIT) return } // NOTE: need password length check? if len(form.Passwd) > 0 { u.Passwd = form.Passwd u.Salt = models.GetUserSalt() u.EncodePasswd() } u.Email = form.Email u.Website = form.Website u.Location = form.Location if len(form.Avatar) == 0 { form.Avatar = form.Email } u.Avatar = base.EncodeMd5(form.Avatar) u.AvatarEmail = form.Avatar u.IsActive = form.Active u.IsAdmin = form.Admin if err := models.UpdateUser(u); err != nil { ctx.Handle(500, "UpdateUser", err) return } log.Trace("Account profile updated by admin(%s): %s", ctx.User.Name, u.Name) ctx.Data["User"] = u ctx.Flash.Success(ctx.Tr("admin.users.update_profile_success")) ctx.Redirect("/admin/users/" + ctx.Params(":userid")) }
func SettingsPost(ctx *middleware.Context, form auth.UpdateProfileForm) { ctx.Data["Title"] = ctx.Tr("settings") ctx.Data["PageIsUserSettings"] = true ctx.Data["PageIsSettingsProfile"] = true if ctx.HasError() { ctx.HTML(200, SETTINGS_PROFILE) return } // Check if user name has been changed. if ctx.User.Name != form.UserName { isExist, err := models.IsUserExist(form.UserName) if err != nil { ctx.Handle(500, "IsUserExist", err) return } else if isExist { ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), SETTINGS_PROFILE, &form) return } else if err = models.ChangeUserName(ctx.User, form.UserName); err != nil { if err == models.ErrUserNameIllegal { ctx.Flash.Error(ctx.Tr("form.illegal_username")) ctx.Redirect("/user/settings") return } else { ctx.Handle(500, "ChangeUserName", err) } return } log.Trace("User name changed: %s -> %s", ctx.User.Name, form.UserName) ctx.User.Name = form.UserName } ctx.User.FullName = form.FullName ctx.User.Email = form.Email ctx.User.Website = form.Website ctx.User.Location = form.Location ctx.User.Avatar = base.EncodeMd5(form.Avatar) ctx.User.AvatarEmail = form.Avatar if err := models.UpdateUser(ctx.User); err != nil { ctx.Handle(500, "UpdateUser", err) return } log.Trace("User setting updated: %s", ctx.User.Name) ctx.Flash.Success(ctx.Tr("settings.update_profile_success")) ctx.Redirect("/user/settings") }
func ResetPasswdPost(ctx *middleware.Context) { ctx.Data["Title"] = ctx.Tr("auth.reset_password") code := ctx.Query("code") if len(code) == 0 { ctx.Error(404) return } ctx.Data["Code"] = code if u := models.VerifyUserActiveCode(code); u != nil { // Validate password length. passwd := ctx.Query("password") if len(passwd) < 6 { ctx.Data["IsResetForm"] = true ctx.Data["Err_Password"] = true ctx.RenderWithErr(ctx.Tr("auth.password_too_short"), RESET_PASSWORD, nil) return } u.Passwd = passwd u.Rands = models.GetUserSalt() u.Salt = models.GetUserSalt() u.EncodePasswd() if err := models.UpdateUser(u); err != nil { ctx.Handle(500, "UpdateUser", err) return } log.Trace("User password reset: %s", u.Name) ctx.Redirect("/user/login") return } ctx.Data["IsResetFailed"] = true ctx.HTML(200, RESET_PASSWORD) }