Files
Gogs/internal/route/admin/users.go

250 lines
6.7 KiB
Go
Raw Permalink Normal View History

2014-08-29 15:32:52 +08:00
package admin
import (
"net/http"
"strconv"
2014-08-29 15:32:52 +08:00
"strings"
log "unknwon.dev/clog/v2"
2014-08-29 15:32:52 +08:00
"gogs.io/gogs/internal/conf"
"gogs.io/gogs/internal/context"
"gogs.io/gogs/internal/database"
2020-02-25 00:35:35 +08:00
"gogs.io/gogs/internal/email"
"gogs.io/gogs/internal/form"
"gogs.io/gogs/internal/route"
2014-08-29 15:32:52 +08:00
)
const (
tmplAdminUserList = "admin/user/list"
tmplAdminUserNew = "admin/user/new"
tmplAdminUserEdit = "admin/user/edit"
2014-08-29 15:32:52 +08:00
)
2017-06-03 07:26:09 -04:00
func Users(c *context.Context) {
c.Data["Title"] = c.Tr("admin.users")
c.Data["PageIsAdmin"] = true
c.Data["PageIsAdminUsers"] = true
2014-08-29 20:50:43 +08:00
route.RenderUserSearch(c, &route.UserSearchOptions{
Type: database.UserTypeIndividual,
Counter: database.Handle.Users().Count,
Ranger: database.Handle.Users().List,
PageSize: conf.UI.Admin.UserPagingNum,
OrderBy: "id ASC",
TplName: tmplAdminUserList,
})
2014-08-29 15:32:52 +08:00
}
2017-06-03 07:26:09 -04:00
func NewUser(c *context.Context) {
c.Data["Title"] = c.Tr("admin.users.new_account")
c.Data["PageIsAdmin"] = true
c.Data["PageIsAdminUsers"] = true
2014-08-29 15:32:52 +08:00
2017-06-03 07:26:09 -04:00
c.Data["login_type"] = "0-0"
sources, err := database.Handle.LoginSources().List(c.Req.Context(), database.ListLoginSourceOptions{})
2014-08-29 15:32:52 +08:00
if err != nil {
c.Error(err, "list login sources")
2014-08-29 15:32:52 +08:00
return
}
2017-06-03 07:26:09 -04:00
c.Data["Sources"] = sources
2020-02-25 00:35:35 +08:00
c.Data["CanSendEmail"] = conf.Email.Enabled
c.Success(tmplAdminUserNew)
2014-08-29 15:32:52 +08:00
}
2017-06-03 07:26:09 -04:00
func NewUserPost(c *context.Context, f form.AdminCrateUser) {
c.Data["Title"] = c.Tr("admin.users.new_account")
c.Data["PageIsAdmin"] = true
c.Data["PageIsAdminUsers"] = true
2014-08-29 15:32:52 +08:00
sources, err := database.Handle.LoginSources().List(c.Req.Context(), database.ListLoginSourceOptions{})
if err != nil {
c.Error(err, "list login sources")
2014-08-29 15:32:52 +08:00
return
}
2017-06-03 07:26:09 -04:00
c.Data["Sources"] = sources
2014-08-29 15:32:52 +08:00
2020-02-25 00:35:35 +08:00
c.Data["CanSendEmail"] = conf.Email.Enabled
2017-06-03 07:26:09 -04:00
if c.HasError() {
c.HTML(http.StatusBadRequest, tmplAdminUserNew)
2014-08-29 15:32:52 +08:00
return
}
createUserOpts := database.CreateUserOptions{
Password: f.Password,
Activated: true,
2014-08-29 15:32:52 +08:00
}
if len(f.LoginType) > 0 {
fields := strings.Split(f.LoginType, "-")
if len(fields) == 2 {
createUserOpts.LoginSource, _ = strconv.ParseInt(fields[1], 10, 64)
createUserOpts.LoginName = f.LoginName
}
2014-08-29 15:32:52 +08:00
}
user, err := database.Handle.Users().Create(c.Req.Context(), f.UserName, f.Email, createUserOpts)
if err != nil {
switch {
case database.IsErrUserAlreadyExist(err):
2017-06-03 07:26:09 -04:00
c.Data["Err_UserName"] = true
c.RenderWithErr(c.Tr("form.username_been_taken"), http.StatusUnprocessableEntity, tmplAdminUserNew, &f)
case database.IsErrEmailAlreadyUsed(err):
2017-06-03 07:26:09 -04:00
c.Data["Err_Email"] = true
c.RenderWithErr(c.Tr("form.email_been_used"), http.StatusUnprocessableEntity, tmplAdminUserNew, &f)
case database.IsErrNameNotAllowed(err):
2017-06-03 07:26:09 -04:00
c.Data["Err_UserName"] = true
c.RenderWithErr(c.Tr("user.form.name_not_allowed", err.(database.ErrNameNotAllowed).Value()), http.StatusBadRequest, tmplAdminUserNew, &f)
2014-08-29 15:32:52 +08:00
default:
c.Error(err, "create user")
2014-08-29 15:32:52 +08:00
}
return
}
log.Trace("Account %q created by admin %q", user.Name, c.User.Name)
// Send email notification.
2020-02-25 00:35:35 +08:00
if f.SendNotify && conf.Email.Enabled {
email.SendRegisterNotifyMail(c.Context, database.NewMailerUser(user))
}
c.Flash.Success(c.Tr("admin.users.new_success", user.Name))
c.Redirect(conf.Server.Subpath + "/admin/users/" + strconv.FormatInt(user.ID, 10))
2014-08-29 15:32:52 +08:00
}
func prepareUserInfo(c *context.Context) *database.User {
u, err := database.Handle.Users().GetByID(c.Req.Context(), c.ParamsInt64(":userid"))
2014-08-29 15:32:52 +08:00
if err != nil {
c.Error(err, "get user by ID")
return nil
2014-08-29 15:32:52 +08:00
}
2017-06-03 07:26:09 -04:00
c.Data["User"] = u
if u.LoginSource > 0 {
c.Data["LoginSource"], err = database.Handle.LoginSources().GetByID(c.Req.Context(), u.LoginSource)
if err != nil {
c.Error(err, "get login source by ID")
return nil
}
} else {
c.Data["LoginSource"] = &database.LoginSource{}
}
sources, err := database.Handle.LoginSources().List(c.Req.Context(), database.ListLoginSourceOptions{})
2014-08-29 15:32:52 +08:00
if err != nil {
c.Error(err, "list login sources")
return nil
2014-08-29 15:32:52 +08:00
}
2017-06-03 07:26:09 -04:00
c.Data["Sources"] = sources
return u
2014-08-29 15:32:52 +08:00
}
2017-06-03 07:26:09 -04:00
func EditUser(c *context.Context) {
c.Data["Title"] = c.Tr("admin.users.edit_account")
c.Data["PageIsAdmin"] = true
c.Data["PageIsAdminUsers"] = true
c.Data["EnableLocalPathMigration"] = conf.Repository.EnableLocalPathMigration
2014-08-29 15:32:52 +08:00
2017-06-03 07:26:09 -04:00
prepareUserInfo(c)
if c.Written() {
2014-08-29 15:32:52 +08:00
return
}
c.Success(tmplAdminUserEdit)
}
2017-06-03 07:26:09 -04:00
func EditUserPost(c *context.Context, f form.AdminEditUser) {
c.Data["Title"] = c.Tr("admin.users.edit_account")
c.Data["PageIsAdmin"] = true
c.Data["PageIsAdminUsers"] = true
c.Data["EnableLocalPathMigration"] = conf.Repository.EnableLocalPathMigration
2017-06-03 07:26:09 -04:00
u := prepareUserInfo(c)
if c.Written() {
2014-08-29 15:32:52 +08:00
return
}
2017-06-03 07:26:09 -04:00
if c.HasError() {
c.HTML(http.StatusBadRequest, tmplAdminUserEdit)
2014-08-29 15:32:52 +08:00
return
}
opts := database.UpdateUserOptions{
LoginName: &f.LoginName,
FullName: &f.FullName,
Website: &f.Website,
Location: &f.Location,
MaxRepoCreation: &f.MaxRepoCreation,
IsActivated: &f.Active,
IsAdmin: &f.Admin,
AllowGitHook: &f.AllowGitHook,
AllowImportLocal: &f.AllowImportLocal,
ProhibitLogin: &f.ProhibitLogin,
}
fields := strings.Split(f.LoginType, "-")
if len(fields) == 2 {
loginSource, _ := strconv.ParseInt(fields[1], 10, 64)
if u.LoginSource != loginSource {
opts.LoginSource = &loginSource
}
}
if f.Password != "" {
opts.Password = &f.Password
}
if u.Email != f.Email {
opts.Email = &f.Email
}
err := database.Handle.Users().Update(c.Req.Context(), u.ID, opts)
if err != nil {
if database.IsErrEmailAlreadyUsed(err) {
2017-06-03 07:26:09 -04:00
c.Data["Err_Email"] = true
c.RenderWithErr(c.Tr("form.email_been_used"), http.StatusUnprocessableEntity, tmplAdminUserEdit, &f)
2014-11-30 18:29:16 -05:00
} else {
c.Error(err, "update user")
2014-11-30 18:29:16 -05:00
}
2014-08-29 15:32:52 +08:00
return
}
log.Trace("Account updated by admin %q: %s", c.User.Name, u.Name)
2017-06-03 07:26:09 -04:00
c.Flash.Success(c.Tr("admin.users.update_profile_success"))
c.Redirect(conf.Server.Subpath + "/admin/users/" + c.Params(":userid"))
2014-08-29 15:32:52 +08:00
}
2017-06-03 07:26:09 -04:00
func DeleteUser(c *context.Context) {
u, err := database.Handle.Users().GetByID(c.Req.Context(), c.ParamsInt64(":userid"))
2014-08-29 15:32:52 +08:00
if err != nil {
c.Error(err, "get user by ID")
2014-08-29 15:32:52 +08:00
return
}
if err = database.Handle.Users().DeleteByID(c.Req.Context(), u.ID, false); err != nil {
switch {
case database.IsErrUserOwnRepos(err):
2017-06-03 07:26:09 -04:00
c.Flash.Error(c.Tr("admin.users.still_own_repo"))
c.JSONSuccess(map[string]any{
"redirect": conf.Server.Subpath + "/admin/users/" + c.Params(":userid"),
2015-09-13 13:26:20 -04:00
})
case database.IsErrUserHasOrgs(err):
2017-06-03 07:26:09 -04:00
c.Flash.Error(c.Tr("admin.users.still_has_org"))
c.JSONSuccess(map[string]any{
"redirect": conf.Server.Subpath + "/admin/users/" + c.Params(":userid"),
2015-09-13 13:26:20 -04:00
})
2014-08-29 15:32:52 +08:00
default:
c.Error(err, "delete user")
2014-08-29 15:32:52 +08:00
}
return
}
2017-06-03 07:26:09 -04:00
log.Trace("Account deleted by admin (%s): %s", c.User.Name, u.Name)
2015-09-13 13:26:20 -04:00
2017-06-03 07:26:09 -04:00
c.Flash.Success(c.Tr("admin.users.deletion_success"))
c.JSONSuccess(map[string]any{
"redirect": conf.Server.Subpath + "/admin/users",
2015-09-13 13:26:20 -04:00
})
2014-08-29 15:32:52 +08:00
}