2018-05-16 23:05:00 -05:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
|
|
|
// Copyright 2018 The Gitea 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 setting
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2020-09-24 23:09:23 -05:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-05-16 23:05:00 -05:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/auth"
|
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
2019-08-23 11:40:30 -05:00
|
|
|
"github.com/unknwon/i18n"
|
2018-05-16 23:05:00 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
tplSettingsProfile base.TplName = "user/settings/profile"
|
|
|
|
tplSettingsOrganization base.TplName = "user/settings/organization"
|
|
|
|
tplSettingsRepositories base.TplName = "user/settings/repos"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Profile render user's profile page
|
|
|
|
func Profile(ctx *context.Context) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("settings")
|
|
|
|
ctx.Data["PageIsSettingsProfile"] = true
|
2018-06-18 13:24:45 -05:00
|
|
|
|
2018-05-16 23:05:00 -05:00
|
|
|
ctx.HTML(200, tplSettingsProfile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleUsernameChange(ctx *context.Context, newName string) {
|
|
|
|
// Non-local users are not allowed to change their username.
|
|
|
|
if len(newName) == 0 || !ctx.User.IsLocal() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if user name has been changed
|
|
|
|
if ctx.User.LowerName != strings.ToLower(newName) {
|
|
|
|
if err := models.ChangeUserName(ctx.User, newName); err != nil {
|
|
|
|
switch {
|
|
|
|
case models.IsErrUserAlreadyExist(err):
|
|
|
|
ctx.Flash.Error(ctx.Tr("form.username_been_taken"))
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings")
|
|
|
|
case models.IsErrEmailAlreadyUsed(err):
|
|
|
|
ctx.Flash.Error(ctx.Tr("form.email_been_used"))
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings")
|
|
|
|
case models.IsErrNameReserved(err):
|
|
|
|
ctx.Flash.Error(ctx.Tr("user.form.name_reserved", newName))
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings")
|
|
|
|
case models.IsErrNamePatternNotAllowed(err):
|
|
|
|
ctx.Flash.Error(ctx.Tr("user.form.name_pattern_not_allowed", newName))
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings")
|
2020-02-23 14:52:05 -05:00
|
|
|
case models.IsErrNameCharsNotAllowed(err):
|
|
|
|
ctx.Flash.Error(ctx.Tr("user.form.name_chars_not_allowed", newName))
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings")
|
2018-05-16 23:05:00 -05:00
|
|
|
default:
|
|
|
|
ctx.ServerError("ChangeUserName", err)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProfilePost response for change user's profile
|
|
|
|
func ProfilePost(ctx *context.Context, form auth.UpdateProfileForm) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("settings")
|
|
|
|
ctx.Data["PageIsSettingsProfile"] = true
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
ctx.HTML(200, tplSettingsProfile)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
handleUsernameChange(ctx, form.Name)
|
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.User.FullName = form.FullName
|
|
|
|
ctx.User.KeepEmailPrivate = form.KeepEmailPrivate
|
|
|
|
ctx.User.Website = form.Website
|
|
|
|
ctx.User.Location = form.Location
|
|
|
|
ctx.User.Language = form.Language
|
2019-03-18 21:28:10 -05:00
|
|
|
ctx.User.Description = form.Description
|
2020-06-05 15:01:53 -05:00
|
|
|
ctx.User.KeepActivityPrivate = form.KeepActivityPrivate
|
2018-05-16 23:05:00 -05:00
|
|
|
if err := models.UpdateUserSetting(ctx.User); err != nil {
|
|
|
|
if _, ok := err.(models.ErrEmailAlreadyUsed); ok {
|
|
|
|
ctx.Flash.Error(ctx.Tr("form.email_been_used"))
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.ServerError("UpdateUser", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the language to the one we just set
|
2019-07-12 08:57:31 -05:00
|
|
|
ctx.SetCookie("lang", ctx.User.Language, nil, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
|
2018-05-16 23:05:00 -05:00
|
|
|
|
|
|
|
log.Trace("User settings updated: %s", ctx.User.Name)
|
|
|
|
ctx.Flash.Success(i18n.Tr(ctx.User.Language, "settings.update_profile_success"))
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings")
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateAvatarSetting update user's avatar
|
|
|
|
// FIXME: limit size.
|
|
|
|
func UpdateAvatarSetting(ctx *context.Context, form auth.AvatarForm, ctxUser *models.User) error {
|
|
|
|
ctxUser.UseCustomAvatar = form.Source == auth.AvatarLocal
|
|
|
|
if len(form.Gravatar) > 0 {
|
2020-10-23 12:55:10 -05:00
|
|
|
if form.Avatar != nil {
|
|
|
|
ctxUser.Avatar = base.EncodeMD5(form.Gravatar)
|
|
|
|
} else {
|
|
|
|
ctxUser.Avatar = ""
|
|
|
|
}
|
2018-05-16 23:05:00 -05:00
|
|
|
ctxUser.AvatarEmail = form.Gravatar
|
|
|
|
}
|
|
|
|
|
2018-08-01 04:38:56 -05:00
|
|
|
if form.Avatar != nil && form.Avatar.Filename != "" {
|
2018-05-16 23:05:00 -05:00
|
|
|
fr, err := form.Avatar.Open()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Avatar.Open: %v", err)
|
|
|
|
}
|
|
|
|
defer fr.Close()
|
|
|
|
|
2020-10-14 08:07:51 -05:00
|
|
|
if form.Avatar.Size > setting.Avatar.MaxFileSize {
|
2019-05-29 21:22:26 -05:00
|
|
|
return errors.New(ctx.Tr("settings.uploaded_avatar_is_too_big"))
|
|
|
|
}
|
|
|
|
|
2018-05-16 23:05:00 -05:00
|
|
|
data, err := ioutil.ReadAll(fr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("ioutil.ReadAll: %v", err)
|
|
|
|
}
|
|
|
|
if !base.IsImageFile(data) {
|
|
|
|
return errors.New(ctx.Tr("settings.uploaded_avatar_not_a_image"))
|
|
|
|
}
|
|
|
|
if err = ctxUser.UploadAvatar(data); err != nil {
|
|
|
|
return fmt.Errorf("UploadAvatar: %v", err)
|
|
|
|
}
|
2020-10-14 08:07:51 -05:00
|
|
|
} else if ctxUser.UseCustomAvatar && ctxUser.Avatar == "" {
|
2018-05-16 23:05:00 -05:00
|
|
|
// No avatar is uploaded but setting has been changed to enable,
|
|
|
|
// generate a random one when needed.
|
2019-06-12 14:41:28 -05:00
|
|
|
if err := ctxUser.GenerateRandomAvatar(); err != nil {
|
|
|
|
log.Error("GenerateRandomAvatar[%d]: %v", ctxUser.ID, err)
|
2018-05-16 23:05:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := models.UpdateUserCols(ctxUser, "avatar", "avatar_email", "use_custom_avatar"); err != nil {
|
|
|
|
return fmt.Errorf("UpdateUser: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AvatarPost response for change user's avatar request
|
|
|
|
func AvatarPost(ctx *context.Context, form auth.AvatarForm) {
|
|
|
|
if err := UpdateAvatarSetting(ctx, form, ctx.User); err != nil {
|
|
|
|
ctx.Flash.Error(err.Error())
|
|
|
|
} else {
|
|
|
|
ctx.Flash.Success(ctx.Tr("settings.update_avatar_success"))
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings")
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteAvatar render delete avatar page
|
|
|
|
func DeleteAvatar(ctx *context.Context) {
|
|
|
|
if err := ctx.User.DeleteAvatar(); err != nil {
|
|
|
|
ctx.Flash.Error(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Organization render all the organization of the user
|
|
|
|
func Organization(ctx *context.Context) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("settings")
|
|
|
|
ctx.Data["PageIsSettingsOrganization"] = true
|
|
|
|
orgs, err := models.GetOrgsByUserID(ctx.User.ID, ctx.IsSigned)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetOrgsByUserID", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["Orgs"] = orgs
|
|
|
|
ctx.HTML(200, tplSettingsOrganization)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Repos display a list of all repositories of the user
|
|
|
|
func Repos(ctx *context.Context) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("settings")
|
|
|
|
ctx.Data["PageIsSettingsRepos"] = true
|
2020-09-24 23:09:23 -05:00
|
|
|
ctx.Data["allowAdopt"] = ctx.IsUserSiteAdmin() || setting.Repository.AllowAdoptionOfUnadoptedRepositories
|
|
|
|
ctx.Data["allowDelete"] = ctx.IsUserSiteAdmin() || setting.Repository.AllowDeleteOfUnadoptedRepositories
|
2018-05-16 23:05:00 -05:00
|
|
|
|
2020-09-24 23:09:23 -05:00
|
|
|
opts := models.ListOptions{
|
|
|
|
PageSize: setting.UI.Admin.UserPagingNum,
|
|
|
|
Page: ctx.QueryInt("page"),
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Page <= 0 {
|
|
|
|
opts.Page = 1
|
2018-05-16 23:05:00 -05:00
|
|
|
}
|
2020-09-24 23:09:23 -05:00
|
|
|
start := (opts.Page - 1) * opts.PageSize
|
|
|
|
end := start + opts.PageSize
|
2018-05-16 23:05:00 -05:00
|
|
|
|
2020-09-24 23:09:23 -05:00
|
|
|
adoptOrDelete := ctx.IsUserSiteAdmin() || (setting.Repository.AllowAdoptionOfUnadoptedRepositories && setting.Repository.AllowDeleteOfUnadoptedRepositories)
|
|
|
|
|
|
|
|
ctxUser := ctx.User
|
|
|
|
count := 0
|
|
|
|
|
|
|
|
if adoptOrDelete {
|
|
|
|
repoNames := make([]string, 0, setting.UI.Admin.UserPagingNum)
|
|
|
|
repos := map[string]*models.Repository{}
|
|
|
|
// We're going to iterate by pagesize.
|
|
|
|
root := filepath.Join(models.UserPath(ctxUser.Name))
|
|
|
|
if err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
|
2018-05-16 23:05:00 -05:00
|
|
|
if err != nil {
|
2020-09-24 23:09:23 -05:00
|
|
|
return err
|
2018-05-16 23:05:00 -05:00
|
|
|
}
|
2020-09-24 23:09:23 -05:00
|
|
|
if !info.IsDir() || path == root {
|
|
|
|
return nil
|
2018-05-16 23:05:00 -05:00
|
|
|
}
|
2020-09-24 23:09:23 -05:00
|
|
|
name := info.Name()
|
|
|
|
if !strings.HasSuffix(name, ".git") {
|
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
|
|
|
name = name[:len(name)-4]
|
|
|
|
if models.IsUsableRepoName(name) != nil || strings.ToLower(name) != name {
|
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
|
|
|
if count >= start && count < end {
|
|
|
|
repoNames = append(repoNames, name)
|
|
|
|
}
|
|
|
|
count++
|
|
|
|
return filepath.SkipDir
|
|
|
|
}); err != nil {
|
|
|
|
ctx.ServerError("filepath.Walk", err)
|
|
|
|
return
|
2018-05-16 23:05:00 -05:00
|
|
|
}
|
|
|
|
|
2020-09-24 23:09:23 -05:00
|
|
|
if err := ctxUser.GetRepositories(models.ListOptions{Page: 1, PageSize: setting.UI.Admin.UserPagingNum}, repoNames...); err != nil {
|
|
|
|
ctx.ServerError("GetRepositories", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, repo := range ctxUser.Repos {
|
|
|
|
if repo.IsFork {
|
|
|
|
if err := repo.GetBaseRepo(); err != nil {
|
|
|
|
ctx.ServerError("GetBaseRepo", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
repos[repo.LowerName] = repo
|
|
|
|
}
|
|
|
|
ctx.Data["Dirs"] = repoNames
|
|
|
|
ctx.Data["ReposMap"] = repos
|
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
var count64 int64
|
|
|
|
ctxUser.Repos, count64, err = models.GetUserRepositories(&models.SearchRepoOptions{Actor: ctxUser, Private: true, ListOptions: opts})
|
2018-05-16 23:05:00 -05:00
|
|
|
|
2020-09-24 23:09:23 -05:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetRepositories", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
count = int(count64)
|
|
|
|
repos := ctxUser.Repos
|
|
|
|
|
|
|
|
for i := range repos {
|
|
|
|
if repos[i].IsFork {
|
|
|
|
if err := repos[i].GetBaseRepo(); err != nil {
|
|
|
|
ctx.ServerError("GetBaseRepo", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Data["Repos"] = repos
|
|
|
|
}
|
|
|
|
ctx.Data["Owner"] = ctxUser
|
|
|
|
pager := context.NewPagination(int(count), opts.PageSize, opts.Page, 5)
|
|
|
|
pager.SetDefaultParams(ctx)
|
|
|
|
ctx.Data["Page"] = pager
|
2018-05-16 23:05:00 -05:00
|
|
|
ctx.HTML(200, tplSettingsRepositories)
|
|
|
|
}
|