mirror of https://github.com/gogits/gogs.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
665 lines
17 KiB
665 lines
17 KiB
11 years ago
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
|
package user
|
||
|
|
||
|
import (
|
||
8 years ago
|
"bytes"
|
||
|
"encoding/base64"
|
||
9 years ago
|
"fmt"
|
||
8 years ago
|
"html/template"
|
||
|
"image/png"
|
||
10 years ago
|
"io/ioutil"
|
||
10 years ago
|
"strings"
|
||
|
|
||
10 years ago
|
"github.com/Unknwon/com"
|
||
8 years ago
|
"github.com/pquerna/otp"
|
||
|
"github.com/pquerna/otp/totp"
|
||
8 years ago
|
log "gopkg.in/clog.v1"
|
||
11 years ago
|
|
||
11 years ago
|
"github.com/gogits/gogs/models"
|
||
8 years ago
|
"github.com/gogits/gogs/models/errors"
|
||
8 years ago
|
"github.com/gogits/gogs/pkg/context"
|
||
|
"github.com/gogits/gogs/pkg/form"
|
||
|
"github.com/gogits/gogs/pkg/mailer"
|
||
|
"github.com/gogits/gogs/pkg/setting"
|
||
8 years ago
|
"github.com/gogits/gogs/pkg/tool"
|
||
11 years ago
|
)
|
||
|
|
||
11 years ago
|
const (
|
||
8 years ago
|
SETTINGS_PROFILE = "user/settings/profile"
|
||
|
SETTINGS_AVATAR = "user/settings/avatar"
|
||
|
SETTINGS_PASSWORD = "user/settings/password"
|
||
|
SETTINGS_EMAILS = "user/settings/email"
|
||
|
SETTINGS_SSH_KEYS = "user/settings/sshkeys"
|
||
|
SETTINGS_SECURITY = "user/settings/security"
|
||
|
SETTINGS_TWO_FACTOR_ENABLE = "user/settings/two_factor_enable"
|
||
|
SETTINGS_TWO_FACTOR_RECOVERY_CODES = "user/settings/two_factor_recovery_codes"
|
||
|
SETTINGS_REPOSITORIES = "user/settings/repositories"
|
||
|
SETTINGS_ORGANIZATIONS = "user/settings/organizations"
|
||
|
SETTINGS_APPLICATIONS = "user/settings/applications"
|
||
|
SETTINGS_DELETE = "user/settings/delete"
|
||
|
NOTIFICATION = "user/notification"
|
||
11 years ago
|
)
|
||
|
|
||
8 years ago
|
func Settings(c *context.Context) {
|
||
8 years ago
|
c.Title("settings.profile")
|
||
|
c.PageIs("SettingsProfile")
|
||
8 years ago
|
c.Data["origin_name"] = c.User.Name
|
||
|
c.Data["name"] = c.User.Name
|
||
|
c.Data["full_name"] = c.User.FullName
|
||
|
c.Data["email"] = c.User.Email
|
||
|
c.Data["website"] = c.User.Website
|
||
|
c.Data["location"] = c.User.Location
|
||
|
c.Success(SETTINGS_PROFILE)
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
func SettingsPost(c *context.Context, f form.UpdateProfile) {
|
||
|
c.Title("settings.profile")
|
||
|
c.PageIs("SettingsProfile")
|
||
|
c.Data["origin_name"] = c.User.Name
|
||
11 years ago
|
|
||
8 years ago
|
if c.HasError() {
|
||
|
c.Success(SETTINGS_PROFILE)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// Non-local users are not allowed to change their username
|
||
|
if c.User.IsLocal() {
|
||
|
// Check if username characters have been changed
|
||
|
if c.User.LowerName != strings.ToLower(f.Name) {
|
||
|
if err := models.ChangeUserName(c.User, f.Name); err != nil {
|
||
|
c.FormErr("Name")
|
||
|
var msg string
|
||
|
switch {
|
||
|
case models.IsErrUserAlreadyExist(err):
|
||
|
msg = c.Tr("form.username_been_taken")
|
||
|
case models.IsErrEmailAlreadyUsed(err):
|
||
|
msg = c.Tr("form.email_been_used")
|
||
|
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
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
log.Trace("Username changed: %s -> %s", c.User.Name, f.Name)
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
// In case it's just a case change
|
||
|
c.User.Name = f.Name
|
||
|
c.User.LowerName = strings.ToLower(f.Name)
|
||
9 years ago
|
}
|
||
11 years ago
|
|
||
8 years ago
|
c.User.FullName = f.FullName
|
||
|
c.User.Email = f.Email
|
||
|
c.User.Website = f.Website
|
||
|
c.User.Location = f.Location
|
||
|
if err := models.UpdateUser(c.User); err != nil {
|
||
|
c.ServerError("UpdateUser", err)
|
||
11 years ago
|
return
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
c.Flash.Success(c.Tr("settings.update_profile_success"))
|
||
|
c.SubURLRedirect("/user/settings")
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
// FIXME: limit size.
|
||
8 years ago
|
func UpdateAvatarSetting(c *context.Context, f form.Avatar, ctxUser *models.User) error {
|
||
8 years ago
|
ctxUser.UseCustomAvatar = f.Source == form.AVATAR_LOCAL
|
||
|
if len(f.Gravatar) > 0 {
|
||
8 years ago
|
ctxUser.Avatar = tool.MD5(f.Gravatar)
|
||
8 years ago
|
ctxUser.AvatarEmail = f.Gravatar
|
||
8 years ago
|
}
|
||
10 years ago
|
|
||
8 years ago
|
if f.Avatar != nil {
|
||
8 years ago
|
r, err := f.Avatar.Open()
|
||
10 years ago
|
if err != nil {
|
||
9 years ago
|
return fmt.Errorf("Avatar.Open: %v", err)
|
||
10 years ago
|
}
|
||
8 years ago
|
defer r.Close()
|
||
10 years ago
|
|
||
8 years ago
|
data, err := ioutil.ReadAll(r)
|
||
10 years ago
|
if err != nil {
|
||
9 years ago
|
return fmt.Errorf("ioutil.ReadAll: %v", err)
|
||
10 years ago
|
}
|
||
8 years ago
|
if !tool.IsImageFile(data) {
|
||
8 years ago
|
return errors.New(c.Tr("settings.uploaded_avatar_not_a_image"))
|
||
10 years ago
|
}
|
||
9 years ago
|
if err = ctxUser.UploadAvatar(data); err != nil {
|
||
|
return fmt.Errorf("UploadAvatar: %v", err)
|
||
10 years ago
|
}
|
||
10 years ago
|
} else {
|
||
9 years ago
|
// No avatar is uploaded but setting has been changed to enable,
|
||
|
// generate a random one when needed.
|
||
8 years ago
|
if ctxUser.UseCustomAvatar && !com.IsFile(ctxUser.CustomAvatarPath()) {
|
||
9 years ago
|
if err := ctxUser.GenerateRandomAvatar(); err != nil {
|
||
8 years ago
|
log.Error(4, "GenerateRandomAvatar[%d]: %v", ctxUser.ID, err)
|
||
9 years ago
|
}
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
9 years ago
|
if err := models.UpdateUser(ctxUser); err != nil {
|
||
|
return fmt.Errorf("UpdateUser: %v", err)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
8 years ago
|
func SettingsAvatar(c *context.Context) {
|
||
|
c.Title("settings.avatar")
|
||
|
c.PageIs("SettingsAvatar")
|
||
|
c.Success(SETTINGS_AVATAR)
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
func SettingsAvatarPost(c *context.Context, f form.Avatar) {
|
||
|
if err := UpdateAvatarSetting(c, f, c.User); err != nil {
|
||
|
c.Flash.Error(err.Error())
|
||
9 years ago
|
} else {
|
||
8 years ago
|
c.Flash.Success(c.Tr("settings.update_avatar_success"))
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
c.SubURLRedirect("/user/settings/avatar")
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
func SettingsDeleteAvatar(c *context.Context) {
|
||
|
if err := c.User.DeleteAvatar(); err != nil {
|
||
|
c.Flash.Error(fmt.Sprintf("DeleteAvatar: %v", err))
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
c.SubURLRedirect("/user/settings/avatar")
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func SettingsPassword(c *context.Context) {
|
||
|
c.Title("settings.password")
|
||
|
c.PageIs("SettingsPassword")
|
||
|
c.Success(SETTINGS_PASSWORD)
|
||
9 years ago
|
}
|
||
10 years ago
|
|
||
8 years ago
|
func SettingsPasswordPost(c *context.Context, f form.ChangePassword) {
|
||
|
c.Title("settings.password")
|
||
|
c.PageIs("SettingsPassword")
|
||
9 years ago
|
|
||
8 years ago
|
if c.HasError() {
|
||
|
c.Success(SETTINGS_PASSWORD)
|
||
10 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
if !c.User.ValidatePassword(f.OldPassword) {
|
||
|
c.Flash.Error(c.Tr("settings.password_incorrect"))
|
||
8 years ago
|
} else if f.Password != f.Retype {
|
||
8 years ago
|
c.Flash.Error(c.Tr("form.password_not_match"))
|
||
9 years ago
|
} else {
|
||
8 years ago
|
c.User.Passwd = f.Password
|
||
8 years ago
|
var err error
|
||
8 years ago
|
if c.User.Salt, err = models.GetUserSalt(); err != nil {
|
||
|
c.ServerError("GetUserSalt", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.User.EncodePasswd()
|
||
|
if err := models.UpdateUser(c.User); err != nil {
|
||
|
c.ServerError("UpdateUser", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Flash.Success(c.Tr("settings.change_password_success"))
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
c.SubURLRedirect("/user/settings/password")
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
func SettingsEmails(c *context.Context) {
|
||
|
c.Title("settings.emails")
|
||
|
c.PageIs("SettingsEmails")
|
||
10 years ago
|
|
||
8 years ago
|
emails, err := models.GetEmailAddresses(c.User.ID)
|
||
10 years ago
|
if err != nil {
|
||
8 years ago
|
c.ServerError("GetEmailAddresses", err)
|
||
10 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["Emails"] = emails
|
||
10 years ago
|
|
||
8 years ago
|
c.Success(SETTINGS_EMAILS)
|
||
9 years ago
|
}
|
||
10 years ago
|
|
||
8 years ago
|
func SettingsEmailPost(c *context.Context, f form.AddEmail) {
|
||
|
c.Title("settings.emails")
|
||
|
c.PageIs("SettingsEmails")
|
||
10 years ago
|
|
||
|
// Make emailaddress primary.
|
||
8 years ago
|
if c.Query("_method") == "PRIMARY" {
|
||
|
if err := models.MakeEmailPrimary(&models.EmailAddress{ID: c.QueryInt64("id")}); err != nil {
|
||
|
c.ServerError("MakeEmailPrimary", err)
|
||
10 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.SubURLRedirect("/user/settings/email")
|
||
10 years ago
|
return
|
||
|
}
|
||
|
|
||
|
// Add Email address.
|
||
8 years ago
|
emails, err := models.GetEmailAddresses(c.User.ID)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.ServerError("GetEmailAddresses", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["Emails"] = emails
|
||
9 years ago
|
|
||
8 years ago
|
if c.HasError() {
|
||
|
c.Success(SETTINGS_EMAILS)
|
||
10 years ago
|
return
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
email := &models.EmailAddress{
|
||
8 years ago
|
UID: c.User.ID,
|
||
8 years ago
|
Email: f.Email,
|
||
10 years ago
|
IsActivated: !setting.Service.RegisterEmailConfirm,
|
||
|
}
|
||
8 years ago
|
if err := models.AddEmailAddress(email); err != nil {
|
||
10 years ago
|
if models.IsErrEmailAlreadyUsed(err) {
|
||
8 years ago
|
c.RenderWithErr(c.Tr("form.email_been_used"), SETTINGS_EMAILS, &f)
|
||
|
} else {
|
||
|
c.ServerError("AddEmailAddress", err)
|
||
10 years ago
|
}
|
||
|
return
|
||
9 years ago
|
}
|
||
10 years ago
|
|
||
8 years ago
|
// Send confirmation email
|
||
9 years ago
|
if setting.Service.RegisterEmailConfirm {
|
||
8 years ago
|
mailer.SendActivateEmailMail(c.Context, models.NewMailerUser(c.User), email.Email)
|
||
10 years ago
|
|
||
8 years ago
|
if err := c.Cache.Put("MailResendLimit_"+c.User.LowerName, c.User.LowerName, 180); err != nil {
|
||
|
log.Error(2, "Set cache 'MailResendLimit' failed: %v", err)
|
||
9 years ago
|
}
|
||
8 years ago
|
c.Flash.Info(c.Tr("settings.add_email_confirmation_sent", email.Email, setting.Service.ActiveCodeLives/60))
|
||
9 years ago
|
} else {
|
||
8 years ago
|
c.Flash.Success(c.Tr("settings.add_email_success"))
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
c.SubURLRedirect("/user/settings/email")
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
func DeleteEmail(c *context.Context) {
|
||
8 years ago
|
if err := models.DeleteEmailAddress(&models.EmailAddress{
|
||
8 years ago
|
ID: c.QueryInt64("id"),
|
||
|
UID: c.User.ID,
|
||
8 years ago
|
}); err != nil {
|
||
8 years ago
|
c.ServerError("DeleteEmailAddress", err)
|
||
11 years ago
|
return
|
||
|
}
|
||
11 years ago
|
|
||
8 years ago
|
c.Flash.Success(c.Tr("settings.email_deletion_success"))
|
||
|
c.JSONSuccess(map[string]interface{}{
|
||
8 years ago
|
"redirect": setting.AppSubURL + "/user/settings/email",
|
||
9 years ago
|
})
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
func SettingsSSHKeys(c *context.Context) {
|
||
|
c.Title("settings.ssh_keys")
|
||
|
c.PageIs("SettingsSSHKeys")
|
||
10 years ago
|
|
||
8 years ago
|
keys, err := models.ListPublicKeys(c.User.ID)
|
||
10 years ago
|
if err != nil {
|
||
8 years ago
|
c.ServerError("ListPublicKeys", err)
|
||
10 years ago
|
return
|
||
10 years ago
|
}
|
||
8 years ago
|
c.Data["Keys"] = keys
|
||
10 years ago
|
|
||
8 years ago
|
c.Success(SETTINGS_SSH_KEYS)
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
func SettingsSSHKeysPost(c *context.Context, f form.AddSSHKey) {
|
||
|
c.Title("settings.ssh_keys")
|
||
|
c.PageIs("SettingsSSHKeys")
|
||
10 years ago
|
|
||
8 years ago
|
keys, err := models.ListPublicKeys(c.User.ID)
|
||
10 years ago
|
if err != nil {
|
||
8 years ago
|
c.ServerError("ListPublicKeys", err)
|
||
10 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["Keys"] = keys
|
||
11 years ago
|
|
||
8 years ago
|
if c.HasError() {
|
||
|
c.Success(SETTINGS_SSH_KEYS)
|
||
9 years ago
|
return
|
||
|
}
|
||
11 years ago
|
|
||
8 years ago
|
content, err := models.CheckPublicKeyString(f.Content)
|
||
9 years ago
|
if err != nil {
|
||
9 years ago
|
if models.IsErrKeyUnableVerify(err) {
|
||
8 years ago
|
c.Flash.Info(c.Tr("form.unable_verify_ssh_key"))
|
||
11 years ago
|
} else {
|
||
8 years ago
|
c.Flash.Error(c.Tr("form.invalid_ssh_key", err.Error()))
|
||
|
c.SubURLRedirect("/user/settings/ssh")
|
||
9 years ago
|
return
|
||
11 years ago
|
}
|
||
|
}
|
||
11 years ago
|
|
||
8 years ago
|
if _, err = models.AddPublicKey(c.User.ID, f.Title, content); err != nil {
|
||
|
c.Data["HasError"] = true
|
||
9 years ago
|
switch {
|
||
|
case models.IsErrKeyAlreadyExist(err):
|
||
8 years ago
|
c.FormErr("Content")
|
||
|
c.RenderWithErr(c.Tr("settings.ssh_key_been_used"), SETTINGS_SSH_KEYS, &f)
|
||
9 years ago
|
case models.IsErrKeyNameAlreadyUsed(err):
|
||
8 years ago
|
c.FormErr("Title")
|
||
|
c.RenderWithErr(c.Tr("settings.ssh_key_name_used"), SETTINGS_SSH_KEYS, &f)
|
||
9 years ago
|
default:
|
||
8 years ago
|
c.ServerError("AddPublicKey", err)
|
||
11 years ago
|
}
|
||
9 years ago
|
return
|
||
|
}
|
||
11 years ago
|
|
||
8 years ago
|
c.Flash.Success(c.Tr("settings.add_key_success", f.Title))
|
||
|
c.SubURLRedirect("/user/settings/ssh")
|
||
9 years ago
|
}
|
||
11 years ago
|
|
||
8 years ago
|
func DeleteSSHKey(c *context.Context) {
|
||
|
if err := models.DeletePublicKey(c.User, c.QueryInt64("id")); err != nil {
|
||
|
c.Flash.Error("DeletePublicKey: " + err.Error())
|
||
9 years ago
|
} else {
|
||
8 years ago
|
c.Flash.Success(c.Tr("settings.ssh_key_deletion_success"))
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
8 years ago
|
c.JSONSuccess(map[string]interface{}{
|
||
8 years ago
|
"redirect": setting.AppSubURL + "/user/settings/ssh",
|
||
9 years ago
|
})
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
8 years ago
|
func SettingsSecurity(c *context.Context) {
|
||
8 years ago
|
c.Title("settings.security")
|
||
|
c.PageIs("SettingsSecurity")
|
||
8 years ago
|
|
||
|
t, err := models.GetTwoFactorByUserID(c.UserID())
|
||
|
if err != nil && !errors.IsTwoFactorNotFound(err) {
|
||
|
c.ServerError("GetTwoFactorByUserID", err)
|
||
|
return
|
||
|
}
|
||
|
c.Data["TwoFactor"] = t
|
||
|
|
||
|
c.Success(SETTINGS_SECURITY)
|
||
|
}
|
||
|
|
||
|
func SettingsTwoFactorEnable(c *context.Context) {
|
||
|
if c.User.IsEnabledTwoFactor() {
|
||
|
c.NotFound()
|
||
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Title("settings.two_factor_enable_title")
|
||
|
c.PageIs("SettingsSecurity")
|
||
8 years ago
|
|
||
|
var key *otp.Key
|
||
|
var err error
|
||
|
keyURL := c.Session.Get("twoFactorURL")
|
||
|
if keyURL != nil {
|
||
|
key, _ = otp.NewKeyFromURL(keyURL.(string))
|
||
|
}
|
||
|
if key == nil {
|
||
|
key, err = totp.Generate(totp.GenerateOpts{
|
||
|
Issuer: setting.AppName,
|
||
|
AccountName: c.User.Email,
|
||
|
})
|
||
|
if err != nil {
|
||
|
c.ServerError("Generate", err)
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
c.Data["TwoFactorSecret"] = key.Secret()
|
||
|
|
||
|
img, err := key.Image(240, 240)
|
||
|
if err != nil {
|
||
|
c.ServerError("Image", err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
var buf bytes.Buffer
|
||
|
if err = png.Encode(&buf, img); err != nil {
|
||
|
c.ServerError("Encode", err)
|
||
|
return
|
||
|
}
|
||
|
c.Data["QRCode"] = template.URL("data:image/png;base64," + base64.StdEncoding.EncodeToString(buf.Bytes()))
|
||
|
|
||
|
c.Session.Set("twoFactorSecret", c.Data["TwoFactorSecret"])
|
||
|
c.Session.Set("twoFactorURL", key.String())
|
||
|
c.Success(SETTINGS_TWO_FACTOR_ENABLE)
|
||
|
}
|
||
|
|
||
|
func SettingsTwoFactorEnablePost(c *context.Context) {
|
||
|
secret, ok := c.Session.Get("twoFactorSecret").(string)
|
||
|
if !ok {
|
||
|
c.NotFound()
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if !totp.Validate(c.Query("passcode"), secret) {
|
||
|
c.Flash.Error(c.Tr("settings.two_factor_invalid_passcode"))
|
||
8 years ago
|
c.SubURLRedirect("/user/settings/security/two_factor_enable")
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
|
if err := models.NewTwoFactor(c.UserID(), secret); err != nil {
|
||
|
c.Flash.Error(c.Tr("settings.two_factor_enable_error", err))
|
||
8 years ago
|
c.SubURLRedirect("/user/settings/security/two_factor_enable")
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
|
c.Session.Delete("twoFactorSecret")
|
||
|
c.Session.Delete("twoFactorURL")
|
||
|
c.Flash.Success(c.Tr("settings.two_factor_enable_success"))
|
||
8 years ago
|
c.SubURLRedirect("/user/settings/security/two_factor_recovery_codes")
|
||
8 years ago
|
}
|
||
|
|
||
|
func SettingsTwoFactorRecoveryCodes(c *context.Context) {
|
||
|
if !c.User.IsEnabledTwoFactor() {
|
||
|
c.NotFound()
|
||
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Title("settings.two_factor_recovery_codes_title")
|
||
|
c.PageIs("SettingsSecurity")
|
||
8 years ago
|
|
||
|
recoveryCodes, err := models.GetRecoveryCodesByUserID(c.UserID())
|
||
|
if err != nil {
|
||
|
c.ServerError("GetRecoveryCodesByUserID", err)
|
||
|
return
|
||
|
}
|
||
|
c.Data["RecoveryCodes"] = recoveryCodes
|
||
|
|
||
|
c.Success(SETTINGS_TWO_FACTOR_RECOVERY_CODES)
|
||
|
}
|
||
|
|
||
|
func SettingsTwoFactorRecoveryCodesPost(c *context.Context) {
|
||
|
if !c.User.IsEnabledTwoFactor() {
|
||
|
c.NotFound()
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if err := models.RegenerateRecoveryCodes(c.UserID()); err != nil {
|
||
|
c.Flash.Error(c.Tr("settings.two_factor_regenerate_recovery_codes_error", err))
|
||
|
} else {
|
||
|
c.Flash.Success(c.Tr("settings.two_factor_regenerate_recovery_codes_success"))
|
||
|
}
|
||
|
|
||
8 years ago
|
c.SubURLRedirect("/user/settings/security/two_factor_recovery_codes")
|
||
8 years ago
|
}
|
||
|
|
||
|
func SettingsTwoFactorDisable(c *context.Context) {
|
||
|
if !c.User.IsEnabledTwoFactor() {
|
||
|
c.NotFound()
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if err := models.DeleteTwoFactor(c.UserID()); err != nil {
|
||
|
c.ServerError("DeleteTwoFactor", err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
c.Flash.Success(c.Tr("settings.two_factor_disable_success"))
|
||
|
c.JSONSuccess(map[string]interface{}{
|
||
8 years ago
|
"redirect": setting.AppSubURL + "/user/settings/security",
|
||
8 years ago
|
})
|
||
|
}
|
||
|
|
||
8 years ago
|
func SettingsRepos(c *context.Context) {
|
||
|
c.Title("settings.repos")
|
||
|
c.PageIs("SettingsRepositories")
|
||
10 years ago
|
|
||
8 years ago
|
repos, err := models.GetUserAndCollaborativeRepositories(c.User.ID)
|
||
10 years ago
|
if err != nil {
|
||
8 years ago
|
c.ServerError("GetUserAndCollaborativeRepositories", err)
|
||
10 years ago
|
return
|
||
|
}
|
||
8 years ago
|
if err = models.RepositoryList(repos).LoadAttributes(); err != nil {
|
||
|
c.ServerError("LoadAttributes", err)
|
||
|
return
|
||
|
}
|
||
|
c.Data["Repos"] = repos
|
||
10 years ago
|
|
||
8 years ago
|
c.Success(SETTINGS_REPOSITORIES)
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
func SettingsLeaveRepo(c *context.Context) {
|
||
|
repo, err := models.GetRepositoryByID(c.QueryInt64("id"))
|
||
8 years ago
|
if err != nil {
|
||
8 years ago
|
c.NotFoundOrServerError("GetRepositoryByID", errors.IsRepoNotExist, err)
|
||
8 years ago
|
return
|
||
|
}
|
||
8 years ago
|
|
||
|
if err = repo.DeleteCollaboration(c.User.ID); err != nil {
|
||
|
c.ServerError("DeleteCollaboration", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Flash.Success(c.Tr("settings.repos.leave_success", repo.FullName()))
|
||
|
c.JSONSuccess(map[string]interface{}{
|
||
|
"redirect": setting.AppSubURL + "/user/settings/repositories",
|
||
|
})
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
func SettingsOrganizations(c *context.Context) {
|
||
|
c.Title("settings.orgs")
|
||
|
c.PageIs("SettingsOrganizations")
|
||
|
|
||
|
orgs, err := models.GetOrgsByUserID(c.User.ID, true)
|
||
8 years ago
|
if err != nil {
|
||
8 years ago
|
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 {
|
||
8 years ago
|
if models.IsErrLastOrgOwner(err) {
|
||
8 years ago
|
c.Flash.Error(c.Tr("form.last_org_owner"))
|
||
8 years ago
|
} else {
|
||
8 years ago
|
c.ServerError("RemoveOrgUser", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
c.JSONSuccess(map[string]interface{}{
|
||
8 years ago
|
"redirect": setting.AppSubURL + "/user/settings/organizations",
|
||
8 years ago
|
})
|
||
|
}
|
||
|
|
||
8 years ago
|
func SettingsApplications(c *context.Context) {
|
||
|
c.Title("settings.applications")
|
||
|
c.PageIs("SettingsApplications")
|
||
8 years ago
|
|
||
8 years ago
|
tokens, err := models.ListAccessTokens(c.User.ID)
|
||
|
if err != nil {
|
||
|
c.ServerError("ListAccessTokens", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["Tokens"] = tokens
|
||
8 years ago
|
|
||
8 years ago
|
c.Success(SETTINGS_APPLICATIONS)
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
func SettingsApplicationsPost(c *context.Context, f form.NewAccessToken) {
|
||
|
c.Title("settings.applications")
|
||
|
c.PageIs("SettingsApplications")
|
||
10 years ago
|
|
||
8 years ago
|
if c.HasError() {
|
||
|
tokens, err := models.ListAccessTokens(c.User.ID)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.ServerError("ListAccessTokens", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
|
||
|
c.Data["Tokens"] = tokens
|
||
|
c.Success(SETTINGS_APPLICATIONS)
|
||
9 years ago
|
return
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
t := &models.AccessToken{
|
||
8 years ago
|
UID: c.User.ID,
|
||
8 years ago
|
Name: f.Name,
|
||
10 years ago
|
}
|
||
9 years ago
|
if err := models.NewAccessToken(t); err != nil {
|
||
8 years ago
|
c.ServerError("NewAccessToken", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Flash.Success(c.Tr("settings.generate_token_succees"))
|
||
|
c.Flash.Info(t.Sha1)
|
||
|
c.SubURLRedirect("/user/settings/applications")
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
func SettingsDeleteApplication(c *context.Context) {
|
||
|
if err := models.DeleteAccessTokenOfUserByID(c.User.ID, c.QueryInt64("id")); err != nil {
|
||
|
c.Flash.Error("DeleteAccessTokenByID: " + err.Error())
|
||
9 years ago
|
} else {
|
||
8 years ago
|
c.Flash.Success(c.Tr("settings.delete_token_success"))
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
c.JSONSuccess(map[string]interface{}{
|
||
8 years ago
|
"redirect": setting.AppSubURL + "/user/settings/applications",
|
||
9 years ago
|
})
|
||
|
}
|
||
|
|
||
8 years ago
|
func SettingsDelete(c *context.Context) {
|
||
|
c.Title("settings.delete")
|
||
|
c.PageIs("SettingsDelete")
|
||
10 years ago
|
|
||
8 years ago
|
if c.Req.Method == "POST" {
|
||
|
if _, err := models.UserSignIn(c.User.Name, c.Query("password")); err != nil {
|
||
8 years ago
|
if errors.IsUserNotExist(err) {
|
||
8 years ago
|
c.RenderWithErr(c.Tr("form.enterred_invalid_password"), SETTINGS_DELETE, nil)
|
||
9 years ago
|
} else {
|
||
8 years ago
|
c.ServerError("UserSignIn", err)
|
||
9 years ago
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
if err := models.DeleteUser(c.User); err != nil {
|
||
10 years ago
|
switch {
|
||
|
case models.IsErrUserOwnRepos(err):
|
||
8 years ago
|
c.Flash.Error(c.Tr("form.still_own_repo"))
|
||
|
c.Redirect(setting.AppSubURL + "/user/settings/delete")
|
||
10 years ago
|
case models.IsErrUserHasOrgs(err):
|
||
8 years ago
|
c.Flash.Error(c.Tr("form.still_has_org"))
|
||
|
c.Redirect(setting.AppSubURL + "/user/settings/delete")
|
||
10 years ago
|
default:
|
||
8 years ago
|
c.ServerError("DeleteUser", err)
|
||
10 years ago
|
}
|
||
|
} else {
|
||
8 years ago
|
log.Trace("Account deleted: %s", c.User.Name)
|
||
|
c.Redirect(setting.AppSubURL + "/")
|
||
10 years ago
|
}
|
||
10 years ago
|
return
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
c.Success(SETTINGS_DELETE)
|
||
11 years ago
|
}
|