Browse Source

user/setting: simplify code

pull/3974/merge
Unknwon 8 years ago
parent
commit
fda4b1106e
No known key found for this signature in database
GPG Key ID: 25B575AE3213B2B3
  1. 2
      gogs.go
  2. 23
      pkg/context/context.go
  3. 501
      routers/user/setting.go
  4. 2
      templates/.VERSION

2
gogs.go

@ -16,7 +16,7 @@ import (
"github.com/gogits/gogs/pkg/setting" "github.com/gogits/gogs/pkg/setting"
) )
const APP_VER = "0.11.6.0406" const APP_VER = "0.11.6.0407"
func init() { func init() {
setting.AppVer = APP_VER setting.AppVer = APP_VER

23
pkg/context/context.go

@ -41,6 +41,23 @@ type Context struct {
Org *Organization Org *Organization
} }
// Title sets "Title" field in template data.
func (c *Context) Title(locale string) {
c.Data["Title"] = c.Tr(locale)
}
// PageIs sets "PageIsxxx" field in template data.
func (c *Context) PageIs(name string) {
c.Data["PageIs"+name] = true
}
// FormErr sets "Err_xxx" field in template data.
func (c *Context) FormErr(names ...string) {
for i := range names {
c.Data["Err_"+names[i]] = true
}
}
// UserID returns ID of current logged in user. // UserID returns ID of current logged in user.
// It returns 0 if visitor is anonymous. // It returns 0 if visitor is anonymous.
func (c *Context) UserID() int64 { func (c *Context) UserID() int64 {
@ -96,6 +113,12 @@ func (c *Context) JSONSuccess(data interface{}) {
c.JSON(http.StatusOK, data) c.JSON(http.StatusOK, data)
} }
// SubURLRedirect responses redirection wtih given location and status.
// It prepends setting.AppSubURL to the location string.
func (c *Context) SubURLRedirect(location string, status ...int) {
c.Redirect(setting.AppSubURL + location)
}
// RenderWithErr used for page has form validation but need to prompt error to users. // RenderWithErr used for page has form validation but need to prompt error to users.
func (ctx *Context) RenderWithErr(msg, tpl string, f interface{}) { func (ctx *Context) RenderWithErr(msg, tpl string, f interface{}) {
if f != nil { if f != nil {

501
routers/user/setting.go

@ -44,8 +44,8 @@ const (
) )
func Settings(c *context.Context) { func Settings(c *context.Context) {
c.Data["Title"] = c.Tr("settings") c.Title("settings.profile")
c.Data["PageIsSettingsProfile"] = true c.PageIs("SettingsProfile")
c.Data["origin_name"] = c.User.Name c.Data["origin_name"] = c.User.Name
c.Data["name"] = c.User.Name c.Data["name"] = c.User.Name
c.Data["full_name"] = c.User.FullName c.Data["full_name"] = c.User.FullName
@ -55,72 +55,64 @@ func Settings(c *context.Context) {
c.Success(SETTINGS_PROFILE) c.Success(SETTINGS_PROFILE)
} }
func handleUsernameChange(ctx *context.Context, newName string) { func SettingsPost(c *context.Context, f form.UpdateProfile) {
// Non-local users are not allowed to change their username. c.Title("settings.profile")
if len(newName) == 0 || !ctx.User.IsLocal() { c.PageIs("SettingsProfile")
return c.Data["origin_name"] = c.User.Name
}
// Check if user name has been changed if c.HasError() {
if ctx.User.LowerName != strings.ToLower(newName) { c.Success(SETTINGS_PROFILE)
if err := models.ChangeUserName(ctx.User, newName); err != nil { return
switch { }
case models.IsErrUserAlreadyExist(err):
ctx.Flash.Error(ctx.Tr("newName_been_taken")) // Non-local users are not allowed to change their username
ctx.Redirect(setting.AppSubURL + "/user/settings") if c.User.IsLocal() {
case models.IsErrEmailAlreadyUsed(err): // Check if username characters have been changed
ctx.Flash.Error(ctx.Tr("form.email_been_used")) if c.User.LowerName != strings.ToLower(f.Name) {
ctx.Redirect(setting.AppSubURL + "/user/settings") if err := models.ChangeUserName(c.User, f.Name); err != nil {
case models.IsErrNameReserved(err): c.FormErr("Name")
ctx.Flash.Error(ctx.Tr("user.newName_reserved")) var msg string
ctx.Redirect(setting.AppSubURL + "/user/settings") switch {
case models.IsErrNamePatternNotAllowed(err): case models.IsErrUserAlreadyExist(err):
ctx.Flash.Error(ctx.Tr("user.newName_pattern_not_allowed")) msg = c.Tr("form.username_been_taken")
ctx.Redirect(setting.AppSubURL + "/user/settings") case models.IsErrEmailAlreadyUsed(err):
default: msg = c.Tr("form.email_been_used")
ctx.Handle(500, "ChangeUserName", err) case models.IsErrNameReserved(err):
msg = c.Tr("form.name_reserved")
case models.IsErrNamePatternNotAllowed(err):
msg = c.Tr("form.name_pattern_not_allowed")
default:
c.ServerError("ChangeUserName", err)
return
}
c.RenderWithErr(msg, SETTINGS_PROFILE, &f)
return
} }
return
}
log.Trace("User name changed: %s -> %s", ctx.User.Name, newName)
}
// In case it's just a case change
ctx.User.Name = newName
ctx.User.LowerName = strings.ToLower(newName)
}
func SettingsPost(ctx *context.Context, f form.UpdateProfile) { log.Trace("Username changed: %s -> %s", c.User.Name, f.Name)
ctx.Data["Title"] = ctx.Tr("settings") }
ctx.Data["PageIsSettingsProfile"] = true
ctx.Data["origin_name"] = ctx.User.Name
if ctx.HasError() {
ctx.HTML(200, SETTINGS_PROFILE)
return
}
handleUsernameChange(ctx, f.Name) // In case it's just a case change
if ctx.Written() { c.User.Name = f.Name
return c.User.LowerName = strings.ToLower(f.Name)
} }
ctx.User.FullName = f.FullName c.User.FullName = f.FullName
ctx.User.Email = f.Email c.User.Email = f.Email
ctx.User.Website = f.Website c.User.Website = f.Website
ctx.User.Location = f.Location c.User.Location = f.Location
if err := models.UpdateUser(ctx.User); err != nil { if err := models.UpdateUser(c.User); err != nil {
ctx.Handle(500, "UpdateUser", err) c.ServerError("UpdateUser", err)
return return
} }
log.Trace("User settings updated: %s", ctx.User.Name) c.Flash.Success(c.Tr("settings.update_profile_success"))
ctx.Flash.Success(ctx.Tr("settings.update_profile_success")) c.SubURLRedirect("/user/settings")
ctx.Redirect(setting.AppSubURL + "/user/settings")
} }
// FIXME: limit size. // FIXME: limit size.
func UpdateAvatarSetting(ctx *context.Context, f form.Avatar, ctxUser *models.User) error { func UpdateAvatarSetting(c *context.Context, f form.Avatar, ctxUser *models.User) error {
ctxUser.UseCustomAvatar = f.Source == form.AVATAR_LOCAL ctxUser.UseCustomAvatar = f.Source == form.AVATAR_LOCAL
if len(f.Gravatar) > 0 { if len(f.Gravatar) > 0 {
ctxUser.Avatar = tool.MD5(f.Gravatar) ctxUser.Avatar = tool.MD5(f.Gravatar)
@ -128,18 +120,18 @@ func UpdateAvatarSetting(ctx *context.Context, f form.Avatar, ctxUser *models.Us
} }
if f.Avatar != nil { if f.Avatar != nil {
fr, err := f.Avatar.Open() r, err := f.Avatar.Open()
if err != nil { if err != nil {
return fmt.Errorf("Avatar.Open: %v", err) return fmt.Errorf("Avatar.Open: %v", err)
} }
defer fr.Close() defer r.Close()
data, err := ioutil.ReadAll(fr) data, err := ioutil.ReadAll(r)
if err != nil { if err != nil {
return fmt.Errorf("ioutil.ReadAll: %v", err) return fmt.Errorf("ioutil.ReadAll: %v", err)
} }
if !tool.IsImageFile(data) { if !tool.IsImageFile(data) {
return errors.New(ctx.Tr("settings.uploaded_avatar_not_a_image")) return errors.New(c.Tr("settings.uploaded_avatar_not_a_image"))
} }
if err = ctxUser.UploadAvatar(data); err != nil { if err = ctxUser.UploadAvatar(data); err != nil {
return fmt.Errorf("UploadAvatar: %v", err) return fmt.Errorf("UploadAvatar: %v", err)
@ -161,232 +153,228 @@ func UpdateAvatarSetting(ctx *context.Context, f form.Avatar, ctxUser *models.Us
return nil return nil
} }
func SettingsAvatar(ctx *context.Context) { func SettingsAvatar(c *context.Context) {
ctx.Data["Title"] = ctx.Tr("settings") c.Title("settings.avatar")
ctx.Data["PageIsSettingsAvatar"] = true c.PageIs("SettingsAvatar")
ctx.HTML(200, SETTINGS_AVATAR) c.Success(SETTINGS_AVATAR)
} }
func SettingsAvatarPost(ctx *context.Context, f form.Avatar) { func SettingsAvatarPost(c *context.Context, f form.Avatar) {
if err := UpdateAvatarSetting(ctx, f, ctx.User); err != nil { if err := UpdateAvatarSetting(c, f, c.User); err != nil {
ctx.Flash.Error(err.Error()) c.Flash.Error(err.Error())
} else { } else {
ctx.Flash.Success(ctx.Tr("settings.update_avatar_success")) c.Flash.Success(c.Tr("settings.update_avatar_success"))
} }
ctx.Redirect(setting.AppSubURL + "/user/settings/avatar") c.SubURLRedirect("/user/settings/avatar")
} }
func SettingsDeleteAvatar(ctx *context.Context) { func SettingsDeleteAvatar(c *context.Context) {
if err := ctx.User.DeleteAvatar(); err != nil { if err := c.User.DeleteAvatar(); err != nil {
ctx.Flash.Error(err.Error()) c.Flash.Error(fmt.Sprintf("DeleteAvatar: %v", err))
} }
ctx.Redirect(setting.AppSubURL + "/user/settings/avatar") c.SubURLRedirect("/user/settings/avatar")
} }
func SettingsPassword(ctx *context.Context) { func SettingsPassword(c *context.Context) {
ctx.Data["Title"] = ctx.Tr("settings") c.Title("settings.password")
ctx.Data["PageIsSettingsPassword"] = true c.PageIs("SettingsPassword")
ctx.HTML(200, SETTINGS_PASSWORD) c.Success(SETTINGS_PASSWORD)
} }
func SettingsPasswordPost(ctx *context.Context, f form.ChangePassword) { func SettingsPasswordPost(c *context.Context, f form.ChangePassword) {
ctx.Data["Title"] = ctx.Tr("settings") c.Title("settings.password")
ctx.Data["PageIsSettingsPassword"] = true c.PageIs("SettingsPassword")
if ctx.HasError() { if c.HasError() {
ctx.HTML(200, SETTINGS_PASSWORD) c.Success(SETTINGS_PASSWORD)
return return
} }
if !ctx.User.ValidatePassword(f.OldPassword) { if !c.User.ValidatePassword(f.OldPassword) {
ctx.Flash.Error(ctx.Tr("settings.password_incorrect")) c.Flash.Error(c.Tr("settings.password_incorrect"))
} else if f.Password != f.Retype { } else if f.Password != f.Retype {
ctx.Flash.Error(ctx.Tr("form.password_not_match")) c.Flash.Error(c.Tr("form.password_not_match"))
} else { } else {
ctx.User.Passwd = f.Password c.User.Passwd = f.Password
var err error var err error
if ctx.User.Salt, err = models.GetUserSalt(); err != nil { if c.User.Salt, err = models.GetUserSalt(); err != nil {
ctx.Handle(500, "UpdateUser", err) c.ServerError("GetUserSalt", err)
return return
} }
ctx.User.EncodePasswd() c.User.EncodePasswd()
if err := models.UpdateUser(ctx.User); err != nil { if err := models.UpdateUser(c.User); err != nil {
ctx.Handle(500, "UpdateUser", err) c.ServerError("UpdateUser", err)
return return
} }
log.Trace("User password updated: %s", ctx.User.Name) c.Flash.Success(c.Tr("settings.change_password_success"))
ctx.Flash.Success(ctx.Tr("settings.change_password_success"))
} }
ctx.Redirect(setting.AppSubURL + "/user/settings/password") c.SubURLRedirect("/user/settings/password")
} }
func SettingsEmails(ctx *context.Context) { func SettingsEmails(c *context.Context) {
ctx.Data["Title"] = ctx.Tr("settings") c.Title("settings.emails")
ctx.Data["PageIsSettingsEmails"] = true c.PageIs("SettingsEmails")
emails, err := models.GetEmailAddresses(ctx.User.ID) emails, err := models.GetEmailAddresses(c.User.ID)
if err != nil { if err != nil {
ctx.Handle(500, "GetEmailAddresses", err) c.ServerError("GetEmailAddresses", err)
return return
} }
ctx.Data["Emails"] = emails c.Data["Emails"] = emails
ctx.HTML(200, SETTINGS_EMAILS) c.Success(SETTINGS_EMAILS)
} }
func SettingsEmailPost(ctx *context.Context, f form.AddEmail) { func SettingsEmailPost(c *context.Context, f form.AddEmail) {
ctx.Data["Title"] = ctx.Tr("settings") c.Title("settings.emails")
ctx.Data["PageIsSettingsEmails"] = true c.PageIs("SettingsEmails")
// Make emailaddress primary. // Make emailaddress primary.
if ctx.Query("_method") == "PRIMARY" { if c.Query("_method") == "PRIMARY" {
if err := models.MakeEmailPrimary(&models.EmailAddress{ID: ctx.QueryInt64("id")}); err != nil { if err := models.MakeEmailPrimary(&models.EmailAddress{ID: c.QueryInt64("id")}); err != nil {
ctx.Handle(500, "MakeEmailPrimary", err) c.ServerError("MakeEmailPrimary", err)
return return
} }
log.Trace("Email made primary: %s", ctx.User.Name) c.SubURLRedirect("/user/settings/email")
ctx.Redirect(setting.AppSubURL + "/user/settings/email")
return return
} }
// Add Email address. // Add Email address.
emails, err := models.GetEmailAddresses(ctx.User.ID) emails, err := models.GetEmailAddresses(c.User.ID)
if err != nil { if err != nil {
ctx.Handle(500, "GetEmailAddresses", err) c.ServerError("GetEmailAddresses", err)
return return
} }
ctx.Data["Emails"] = emails c.Data["Emails"] = emails
if ctx.HasError() { if c.HasError() {
ctx.HTML(200, SETTINGS_EMAILS) c.Success(SETTINGS_EMAILS)
return return
} }
email := &models.EmailAddress{ email := &models.EmailAddress{
UID: ctx.User.ID, UID: c.User.ID,
Email: f.Email, Email: f.Email,
IsActivated: !setting.Service.RegisterEmailConfirm, IsActivated: !setting.Service.RegisterEmailConfirm,
} }
if err := models.AddEmailAddress(email); err != nil { if err := models.AddEmailAddress(email); err != nil {
if models.IsErrEmailAlreadyUsed(err) { if models.IsErrEmailAlreadyUsed(err) {
ctx.RenderWithErr(ctx.Tr("form.email_been_used"), SETTINGS_EMAILS, &f) c.RenderWithErr(c.Tr("form.email_been_used"), SETTINGS_EMAILS, &f)
return } else {
c.ServerError("AddEmailAddress", err)
} }
ctx.Handle(500, "AddEmailAddress", err)
return return
} }
// Send confirmation email // Send confirmation email
if setting.Service.RegisterEmailConfirm { if setting.Service.RegisterEmailConfirm {
mailer.SendActivateEmailMail(ctx.Context, models.NewMailerUser(ctx.User), email.Email) mailer.SendActivateEmailMail(c.Context, models.NewMailerUser(c.User), email.Email)
if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil { if err := c.Cache.Put("MailResendLimit_"+c.User.LowerName, c.User.LowerName, 180); err != nil {
log.Error(4, "Set cache(MailResendLimit) fail: %v", err) log.Error(2, "Set cache 'MailResendLimit' failed: %v", err)
} }
ctx.Flash.Info(ctx.Tr("settings.add_email_confirmation_sent", email.Email, setting.Service.ActiveCodeLives/60)) c.Flash.Info(c.Tr("settings.add_email_confirmation_sent", email.Email, setting.Service.ActiveCodeLives/60))
} else { } else {
ctx.Flash.Success(ctx.Tr("settings.add_email_success")) c.Flash.Success(c.Tr("settings.add_email_success"))
} }
log.Trace("Email address added: %s", email.Email) c.SubURLRedirect("/user/settings/email")
ctx.Redirect(setting.AppSubURL + "/user/settings/email")
} }
func DeleteEmail(ctx *context.Context) { func DeleteEmail(c *context.Context) {
if err := models.DeleteEmailAddress(&models.EmailAddress{ if err := models.DeleteEmailAddress(&models.EmailAddress{
ID: ctx.QueryInt64("id"), ID: c.QueryInt64("id"),
UID: ctx.User.ID, UID: c.User.ID,
}); err != nil { }); err != nil {
ctx.Handle(500, "DeleteEmail", err) c.ServerError("DeleteEmailAddress", err)
return return
} }
log.Trace("Email address deleted: %s", ctx.User.Name)
ctx.Flash.Success(ctx.Tr("settings.email_deletion_success")) c.Flash.Success(c.Tr("settings.email_deletion_success"))
ctx.JSON(200, map[string]interface{}{ c.JSONSuccess(map[string]interface{}{
"redirect": setting.AppSubURL + "/user/settings/email", "redirect": setting.AppSubURL + "/user/settings/email",
}) })
} }
func SettingsSSHKeys(ctx *context.Context) { func SettingsSSHKeys(c *context.Context) {
ctx.Data["Title"] = ctx.Tr("settings") c.Title("settings.ssh_keys")
ctx.Data["PageIsSettingsSSHKeys"] = true c.PageIs("SettingsSSHKeys")
keys, err := models.ListPublicKeys(ctx.User.ID) keys, err := models.ListPublicKeys(c.User.ID)
if err != nil { if err != nil {
ctx.Handle(500, "ListPublicKeys", err) c.ServerError("ListPublicKeys", err)
return return
} }
ctx.Data["Keys"] = keys c.Data["Keys"] = keys
ctx.HTML(200, SETTINGS_SSH_KEYS) c.Success(SETTINGS_SSH_KEYS)
} }
func SettingsSSHKeysPost(ctx *context.Context, f form.AddSSHKey) { func SettingsSSHKeysPost(c *context.Context, f form.AddSSHKey) {
ctx.Data["Title"] = ctx.Tr("settings") c.Title("settings.ssh_keys")
ctx.Data["PageIsSettingsSSHKeys"] = true c.PageIs("SettingsSSHKeys")
keys, err := models.ListPublicKeys(ctx.User.ID) keys, err := models.ListPublicKeys(c.User.ID)
if err != nil { if err != nil {
ctx.Handle(500, "ListPublicKeys", err) c.ServerError("ListPublicKeys", err)
return return
} }
ctx.Data["Keys"] = keys c.Data["Keys"] = keys
if ctx.HasError() { if c.HasError() {
ctx.HTML(200, SETTINGS_SSH_KEYS) c.Success(SETTINGS_SSH_KEYS)
return return
} }
content, err := models.CheckPublicKeyString(f.Content) content, err := models.CheckPublicKeyString(f.Content)
if err != nil { if err != nil {
if models.IsErrKeyUnableVerify(err) { if models.IsErrKeyUnableVerify(err) {
ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key")) c.Flash.Info(c.Tr("form.unable_verify_ssh_key"))
} else { } else {
ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error())) c.Flash.Error(c.Tr("form.invalid_ssh_key", err.Error()))
ctx.Redirect(setting.AppSubURL + "/user/settings/ssh") c.SubURLRedirect("/user/settings/ssh")
return return
} }
} }
if _, err = models.AddPublicKey(ctx.User.ID, f.Title, content); err != nil { if _, err = models.AddPublicKey(c.User.ID, f.Title, content); err != nil {
ctx.Data["HasError"] = true c.Data["HasError"] = true
switch { switch {
case models.IsErrKeyAlreadyExist(err): case models.IsErrKeyAlreadyExist(err):
ctx.Data["Err_Content"] = true c.FormErr("Content")
ctx.RenderWithErr(ctx.Tr("settings.ssh_key_been_used"), SETTINGS_SSH_KEYS, &f) c.RenderWithErr(c.Tr("settings.ssh_key_been_used"), SETTINGS_SSH_KEYS, &f)
case models.IsErrKeyNameAlreadyUsed(err): case models.IsErrKeyNameAlreadyUsed(err):
ctx.Data["Err_Title"] = true c.FormErr("Title")
ctx.RenderWithErr(ctx.Tr("settings.ssh_key_name_used"), SETTINGS_SSH_KEYS, &f) c.RenderWithErr(c.Tr("settings.ssh_key_name_used"), SETTINGS_SSH_KEYS, &f)
default: default:
ctx.Handle(500, "AddPublicKey", err) c.ServerError("AddPublicKey", err)
} }
return return
} }
ctx.Flash.Success(ctx.Tr("settings.add_key_success", f.Title)) c.Flash.Success(c.Tr("settings.add_key_success", f.Title))
ctx.Redirect(setting.AppSubURL + "/user/settings/ssh") c.SubURLRedirect("/user/settings/ssh")
} }
func DeleteSSHKey(ctx *context.Context) { func DeleteSSHKey(c *context.Context) {
if err := models.DeletePublicKey(ctx.User, ctx.QueryInt64("id")); err != nil { if err := models.DeletePublicKey(c.User, c.QueryInt64("id")); err != nil {
ctx.Flash.Error("DeletePublicKey: " + err.Error()) c.Flash.Error("DeletePublicKey: " + err.Error())
} else { } else {
ctx.Flash.Success(ctx.Tr("settings.ssh_key_deletion_success")) c.Flash.Success(c.Tr("settings.ssh_key_deletion_success"))
} }
ctx.JSON(200, map[string]interface{}{ c.JSONSuccess(map[string]interface{}{
"redirect": setting.AppSubURL + "/user/settings/ssh", "redirect": setting.AppSubURL + "/user/settings/ssh",
}) })
} }
func SettingsSecurity(c *context.Context) { func SettingsSecurity(c *context.Context) {
c.Data["Title"] = c.Tr("settings") c.Title("settings.security")
c.Data["PageIsSettingsSecurity"] = true c.PageIs("SettingsSecurity")
t, err := models.GetTwoFactorByUserID(c.UserID()) t, err := models.GetTwoFactorByUserID(c.UserID())
if err != nil && !errors.IsTwoFactorNotFound(err) { if err != nil && !errors.IsTwoFactorNotFound(err) {
@ -404,8 +392,8 @@ func SettingsTwoFactorEnable(c *context.Context) {
return return
} }
c.Data["Title"] = c.Tr("settings") c.Title("settings.two_factor_enable_title")
c.Data["PageIsSettingsSecurity"] = true c.PageIs("SettingsSecurity")
var key *otp.Key var key *otp.Key
var err error var err error
@ -452,20 +440,20 @@ func SettingsTwoFactorEnablePost(c *context.Context) {
if !totp.Validate(c.Query("passcode"), secret) { if !totp.Validate(c.Query("passcode"), secret) {
c.Flash.Error(c.Tr("settings.two_factor_invalid_passcode")) c.Flash.Error(c.Tr("settings.two_factor_invalid_passcode"))
c.Redirect(setting.AppSubURL + "/user/settings/security/two_factor_enable") c.SubURLRedirect("/user/settings/security/two_factor_enable")
return return
} }
if err := models.NewTwoFactor(c.UserID(), secret); err != nil { if err := models.NewTwoFactor(c.UserID(), secret); err != nil {
c.Flash.Error(c.Tr("settings.two_factor_enable_error", err)) c.Flash.Error(c.Tr("settings.two_factor_enable_error", err))
c.Redirect(setting.AppSubURL + "/user/settings/security/two_factor_enable") c.SubURLRedirect("/user/settings/security/two_factor_enable")
return return
} }
c.Session.Delete("twoFactorSecret") c.Session.Delete("twoFactorSecret")
c.Session.Delete("twoFactorURL") c.Session.Delete("twoFactorURL")
c.Flash.Success(c.Tr("settings.two_factor_enable_success")) c.Flash.Success(c.Tr("settings.two_factor_enable_success"))
c.Redirect(setting.AppSubURL + "/user/settings/security/two_factor_recovery_codes") c.SubURLRedirect("/user/settings/security/two_factor_recovery_codes")
} }
func SettingsTwoFactorRecoveryCodes(c *context.Context) { func SettingsTwoFactorRecoveryCodes(c *context.Context) {
@ -474,8 +462,8 @@ func SettingsTwoFactorRecoveryCodes(c *context.Context) {
return return
} }
c.Data["Title"] = c.Tr("settings") c.Title("settings.two_factor_recovery_codes_title")
c.Data["PageIsSettingsSecurity"] = true c.PageIs("SettingsSecurity")
recoveryCodes, err := models.GetRecoveryCodesByUserID(c.UserID()) recoveryCodes, err := models.GetRecoveryCodesByUserID(c.UserID())
if err != nil { if err != nil {
@ -499,7 +487,7 @@ func SettingsTwoFactorRecoveryCodesPost(c *context.Context) {
c.Flash.Success(c.Tr("settings.two_factor_regenerate_recovery_codes_success")) c.Flash.Success(c.Tr("settings.two_factor_regenerate_recovery_codes_success"))
} }
c.Redirect(setting.AppSubURL + "/user/settings/security/two_factor_recovery_codes") c.SubURLRedirect("/user/settings/security/two_factor_recovery_codes")
} }
func SettingsTwoFactorDisable(c *context.Context) { func SettingsTwoFactorDisable(c *context.Context) {
@ -519,159 +507,158 @@ func SettingsTwoFactorDisable(c *context.Context) {
}) })
} }
func SettingsApplications(ctx *context.Context) { func SettingsRepos(c *context.Context) {
ctx.Data["Title"] = ctx.Tr("settings") c.Title("settings.repos")
ctx.Data["PageIsSettingsApplications"] = true c.PageIs("SettingsRepositories")
tokens, err := models.ListAccessTokens(ctx.User.ID) repos, err := models.GetUserAndCollaborativeRepositories(c.User.ID)
if err != nil { if err != nil {
ctx.Handle(500, "ListAccessTokens", err) c.ServerError("GetUserAndCollaborativeRepositories", err)
return return
} }
ctx.Data["Tokens"] = tokens if err = models.RepositoryList(repos).LoadAttributes(); err != nil {
c.ServerError("LoadAttributes", err)
return
}
c.Data["Repos"] = repos
ctx.HTML(200, SETTINGS_APPLICATIONS) c.Success(SETTINGS_REPOSITORIES)
} }
func SettingsRepos(ctx *context.Context) { func SettingsLeaveRepo(c *context.Context) {
ctx.Data["Title"] = ctx.Tr("settings") repo, err := models.GetRepositoryByID(c.QueryInt64("id"))
ctx.Data["PageIsSettingsRepositories"] = true
repos, err := models.GetUserAndCollaborativeRepositories(ctx.User.ID)
if err != nil { if err != nil {
ctx.Handle(500, "GetUserAndCollaborativeRepositories", err) c.NotFoundOrServerError("GetRepositoryByID", errors.IsRepoNotExist, err)
return return
} }
if err = models.RepositoryList(repos).LoadAttributes(); err != nil {
ctx.Handle(500, "LoadAttributes", err) if err = repo.DeleteCollaboration(c.User.ID); err != nil {
c.ServerError("DeleteCollaboration", err)
return return
} }
ctx.Data["Repos"] = repos
ctx.HTML(200, SETTINGS_REPOSITORIES) c.Flash.Success(c.Tr("settings.repos.leave_success", repo.FullName()))
c.JSONSuccess(map[string]interface{}{
"redirect": setting.AppSubURL + "/user/settings/repositories",
})
} }
func SettingsLeaveOrganization(ctx *context.Context) { func SettingsOrganizations(c *context.Context) {
err := models.RemoveOrgUser(ctx.QueryInt64("id"), ctx.User.ID) c.Title("settings.orgs")
c.PageIs("SettingsOrganizations")
orgs, err := models.GetOrgsByUserID(c.User.ID, true)
if err != nil { if err != nil {
c.ServerError("GetOrgsByUserID", err)
return
}
c.Data["Orgs"] = orgs
c.Success(SETTINGS_ORGANIZATIONS)
}
func SettingsLeaveOrganization(c *context.Context) {
if err := models.RemoveOrgUser(c.QueryInt64("id"), c.User.ID); err != nil {
if models.IsErrLastOrgOwner(err) { if models.IsErrLastOrgOwner(err) {
ctx.Flash.Error(ctx.Tr("form.last_org_owner")) c.Flash.Error(c.Tr("form.last_org_owner"))
} else { } else {
ctx.Handle(500, "RemoveOrgUser", err) c.ServerError("RemoveOrgUser", err)
return return
} }
} }
ctx.JSON(200, map[string]interface{}{ c.JSONSuccess(map[string]interface{}{
"redirect": setting.AppSubURL + "/user/settings/organizations", "redirect": setting.AppSubURL + "/user/settings/organizations",
}) })
} }
func SettingsLeaveRepo(ctx *context.Context) { func SettingsApplications(c *context.Context) {
repo, err := models.GetRepositoryByID(ctx.QueryInt64("id")) c.Title("settings.applications")
if err != nil { c.PageIs("SettingsApplications")
ctx.NotFoundOrServerError("GetRepositoryByID", errors.IsRepoNotExist, err)
return
}
if err = repo.DeleteCollaboration(ctx.User.ID); err != nil { tokens, err := models.ListAccessTokens(c.User.ID)
ctx.Handle(500, "DeleteCollaboration", err) if err != nil {
c.ServerError("ListAccessTokens", err)
return return
} }
c.Data["Tokens"] = tokens
ctx.Flash.Success(ctx.Tr("settings.repos.leave_success", repo.FullName())) c.Success(SETTINGS_APPLICATIONS)
ctx.JSON(200, map[string]interface{}{
"redirect": setting.AppSubURL + "/user/settings/repositories",
})
} }
func SettingsApplicationsPost(ctx *context.Context, f form.NewAccessToken) { func SettingsApplicationsPost(c *context.Context, f form.NewAccessToken) {
ctx.Data["Title"] = ctx.Tr("settings") c.Title("settings.applications")
ctx.Data["PageIsSettingsApplications"] = true c.PageIs("SettingsApplications")
if ctx.HasError() { if c.HasError() {
tokens, err := models.ListAccessTokens(ctx.User.ID) tokens, err := models.ListAccessTokens(c.User.ID)
if err != nil { if err != nil {
ctx.Handle(500, "ListAccessTokens", err) c.ServerError("ListAccessTokens", err)
return return
} }
ctx.Data["Tokens"] = tokens
ctx.HTML(200, SETTINGS_APPLICATIONS) c.Data["Tokens"] = tokens
c.Success(SETTINGS_APPLICATIONS)
return return
} }
t := &models.AccessToken{ t := &models.AccessToken{
UID: ctx.User.ID, UID: c.User.ID,
Name: f.Name, Name: f.Name,
} }
if err := models.NewAccessToken(t); err != nil { if err := models.NewAccessToken(t); err != nil {
ctx.Handle(500, "NewAccessToken", err) c.ServerError("NewAccessToken", err)
return return
} }
ctx.Flash.Success(ctx.Tr("settings.generate_token_succees")) c.Flash.Success(c.Tr("settings.generate_token_succees"))
ctx.Flash.Info(t.Sha1) c.Flash.Info(t.Sha1)
c.SubURLRedirect("/user/settings/applications")
ctx.Redirect(setting.AppSubURL + "/user/settings/applications")
} }
func SettingsDeleteApplication(ctx *context.Context) { func SettingsDeleteApplication(c *context.Context) {
if err := models.DeleteAccessTokenOfUserByID(ctx.User.ID, ctx.QueryInt64("id")); err != nil { if err := models.DeleteAccessTokenOfUserByID(c.User.ID, c.QueryInt64("id")); err != nil {
ctx.Flash.Error("DeleteAccessTokenByID: " + err.Error()) c.Flash.Error("DeleteAccessTokenByID: " + err.Error())
} else { } else {
ctx.Flash.Success(ctx.Tr("settings.delete_token_success")) c.Flash.Success(c.Tr("settings.delete_token_success"))
} }
ctx.JSON(200, map[string]interface{}{ c.JSONSuccess(map[string]interface{}{
"redirect": setting.AppSubURL + "/user/settings/applications", "redirect": setting.AppSubURL + "/user/settings/applications",
}) })
} }
func SettingsOrganizations(ctx *context.Context) { func SettingsDelete(c *context.Context) {
ctx.Data["Title"] = ctx.Tr("settings") c.Title("settings.delete")
ctx.Data["PageIsSettingsOrganizations"] = true c.PageIs("SettingsDelete")
orgs, err := models.GetOrgsByUserID(ctx.User.ID, true)
if err != nil {
ctx.Handle(500, "GetOrgsByUserID", err)
return
}
ctx.Data["Orgs"] = orgs
ctx.HTML(200, SETTINGS_ORGANIZATIONS)
}
func SettingsDelete(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("settings")
ctx.Data["PageIsSettingsDelete"] = true
if ctx.Req.Method == "POST" { if c.Req.Method == "POST" {
if _, err := models.UserSignIn(ctx.User.Name, ctx.Query("password")); err != nil { if _, err := models.UserSignIn(c.User.Name, c.Query("password")); err != nil {
if errors.IsUserNotExist(err) { if errors.IsUserNotExist(err) {
ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_password"), SETTINGS_DELETE, nil) c.RenderWithErr(c.Tr("form.enterred_invalid_password"), SETTINGS_DELETE, nil)
} else { } else {
ctx.Handle(500, "UserSignIn", err) c.ServerError("UserSignIn", err)
} }
return return
} }
if err := models.DeleteUser(ctx.User); err != nil { if err := models.DeleteUser(c.User); err != nil {
switch { switch {
case models.IsErrUserOwnRepos(err): case models.IsErrUserOwnRepos(err):
ctx.Flash.Error(ctx.Tr("form.still_own_repo")) c.Flash.Error(c.Tr("form.still_own_repo"))
ctx.Redirect(setting.AppSubURL + "/user/settings/delete") c.Redirect(setting.AppSubURL + "/user/settings/delete")
case models.IsErrUserHasOrgs(err): case models.IsErrUserHasOrgs(err):
ctx.Flash.Error(ctx.Tr("form.still_has_org")) c.Flash.Error(c.Tr("form.still_has_org"))
ctx.Redirect(setting.AppSubURL + "/user/settings/delete") c.Redirect(setting.AppSubURL + "/user/settings/delete")
default: default:
ctx.Handle(500, "DeleteUser", err) c.ServerError("DeleteUser", err)
} }
} else { } else {
log.Trace("Account deleted: %s", ctx.User.Name) log.Trace("Account deleted: %s", c.User.Name)
ctx.Redirect(setting.AppSubURL + "/") c.Redirect(setting.AppSubURL + "/")
} }
return return
} }
ctx.HTML(200, SETTINGS_DELETE) c.Success(SETTINGS_DELETE)
} }

2
templates/.VERSION

@ -1 +1 @@
0.11.6.0406 0.11.6.0407
Loading…
Cancel
Save