123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- // 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 admin
- import (
- "strings"
- "github.com/Unknwon/com"
- log "gopkg.in/clog.v1"
- "github.com/gogits/gogs/models"
- "github.com/gogits/gogs/modules/base"
- "github.com/gogits/gogs/modules/context"
- "github.com/gogits/gogs/modules/form"
- "github.com/gogits/gogs/modules/mailer"
- "github.com/gogits/gogs/modules/setting"
- "github.com/gogits/gogs/routers"
- )
- const (
- USERS base.TplName = "admin/user/list"
- USER_NEW base.TplName = "admin/user/new"
- USER_EDIT base.TplName = "admin/user/edit"
- )
- func Users(ctx *context.Context) {
- ctx.Data["Title"] = ctx.Tr("admin.users")
- ctx.Data["PageIsAdmin"] = true
- ctx.Data["PageIsAdminUsers"] = true
- routers.RenderUserSearch(ctx, &routers.UserSearchOptions{
- Type: models.USER_TYPE_INDIVIDUAL,
- Counter: models.CountUsers,
- Ranger: models.Users,
- PageSize: setting.UI.Admin.UserPagingNum,
- OrderBy: "id ASC",
- TplName: USERS,
- })
- }
- func NewUser(ctx *context.Context) {
- ctx.Data["Title"] = ctx.Tr("admin.users.new_account")
- ctx.Data["PageIsAdmin"] = true
- ctx.Data["PageIsAdminUsers"] = true
- ctx.Data["login_type"] = "0-0"
- sources, err := models.LoginSources()
- if err != nil {
- ctx.Handle(500, "LoginSources", err)
- return
- }
- ctx.Data["Sources"] = sources
- ctx.Data["CanSendEmail"] = setting.MailService != nil
- ctx.HTML(200, USER_NEW)
- }
- func NewUserPost(ctx *context.Context, f form.AdminCrateUser) {
- ctx.Data["Title"] = ctx.Tr("admin.users.new_account")
- ctx.Data["PageIsAdmin"] = true
- ctx.Data["PageIsAdminUsers"] = true
- sources, err := models.LoginSources()
- if err != nil {
- ctx.Handle(500, "LoginSources", err)
- return
- }
- ctx.Data["Sources"] = sources
- ctx.Data["CanSendEmail"] = setting.MailService != nil
- if ctx.HasError() {
- ctx.HTML(200, USER_NEW)
- return
- }
- u := &models.User{
- Name: f.UserName,
- Email: f.Email,
- Passwd: f.Password,
- IsActive: true,
- LoginType: models.LOGIN_PLAIN,
- }
- if len(f.LoginType) > 0 {
- fields := strings.Split(f.LoginType, "-")
- if len(fields) == 2 {
- u.LoginType = models.LoginType(com.StrTo(fields[0]).MustInt())
- u.LoginSource = com.StrTo(fields[1]).MustInt64()
- u.LoginName = f.LoginName
- }
- }
- if err := models.CreateUser(u); err != nil {
- switch {
- case models.IsErrUserAlreadyExist(err):
- ctx.Data["Err_UserName"] = true
- ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), USER_NEW, &f)
- case models.IsErrEmailAlreadyUsed(err):
- ctx.Data["Err_Email"] = true
- ctx.RenderWithErr(ctx.Tr("form.email_been_used"), USER_NEW, &f)
- case models.IsErrNameReserved(err):
- ctx.Data["Err_UserName"] = true
- ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), USER_NEW, &f)
- case models.IsErrNamePatternNotAllowed(err):
- ctx.Data["Err_UserName"] = true
- ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), USER_NEW, &f)
- default:
- ctx.Handle(500, "CreateUser", err)
- }
- return
- }
- log.Trace("Account created by admin (%s): %s", ctx.User.Name, u.Name)
- // Send email notification.
- if f.SendNotify && setting.MailService != nil {
- mailer.SendRegisterNotifyMail(ctx.Context, models.NewMailerUser(u))
- }
- ctx.Flash.Success(ctx.Tr("admin.users.new_success", u.Name))
- ctx.Redirect(setting.AppSubUrl + "/admin/users/" + com.ToStr(u.ID))
- }
- func prepareUserInfo(ctx *context.Context) *models.User {
- u, err := models.GetUserByID(ctx.ParamsInt64(":userid"))
- if err != nil {
- ctx.Handle(500, "GetUserByID", err)
- return nil
- }
- ctx.Data["User"] = u
- if u.LoginSource > 0 {
- ctx.Data["LoginSource"], err = models.GetLoginSourceByID(u.LoginSource)
- if err != nil {
- ctx.Handle(500, "GetLoginSourceByID", err)
- return nil
- }
- } else {
- ctx.Data["LoginSource"] = &models.LoginSource{}
- }
- sources, err := models.LoginSources()
- if err != nil {
- ctx.Handle(500, "LoginSources", err)
- return nil
- }
- ctx.Data["Sources"] = sources
- return u
- }
- func EditUser(ctx *context.Context) {
- ctx.Data["Title"] = ctx.Tr("admin.users.edit_account")
- ctx.Data["PageIsAdmin"] = true
- ctx.Data["PageIsAdminUsers"] = true
- ctx.Data["EnableLocalPathMigration"] = setting.Repository.EnableLocalPathMigration
- prepareUserInfo(ctx)
- if ctx.Written() {
- return
- }
- ctx.HTML(200, USER_EDIT)
- }
- func EditUserPost(ctx *context.Context, f form.AdminEditUser) {
- ctx.Data["Title"] = ctx.Tr("admin.users.edit_account")
- ctx.Data["PageIsAdmin"] = true
- ctx.Data["PageIsAdminUsers"] = true
- ctx.Data["EnableLocalPathMigration"] = setting.Repository.EnableLocalPathMigration
- u := prepareUserInfo(ctx)
- if ctx.Written() {
- return
- }
- if ctx.HasError() {
- ctx.HTML(200, USER_EDIT)
- return
- }
- fields := strings.Split(f.LoginType, "-")
- if len(fields) == 2 {
- loginType := models.LoginType(com.StrTo(fields[0]).MustInt())
- loginSource := com.StrTo(fields[1]).MustInt64()
- if u.LoginSource != loginSource {
- u.LoginSource = loginSource
- u.LoginType = loginType
- }
- }
- if len(f.Password) > 0 {
- u.Passwd = f.Password
- var err error
- if u.Salt, err = models.GetUserSalt(); err != nil {
- ctx.Handle(500, "UpdateUser", err)
- return
- }
- u.EncodePasswd()
- }
- u.LoginName = f.LoginName
- u.FullName = f.FullName
- u.Email = f.Email
- u.Website = f.Website
- u.Location = f.Location
- u.MaxRepoCreation = f.MaxRepoCreation
- u.IsActive = f.Active
- u.IsAdmin = f.Admin
- u.AllowGitHook = f.AllowGitHook
- u.AllowImportLocal = f.AllowImportLocal
- u.ProhibitLogin = f.ProhibitLogin
- if err := models.UpdateUser(u); err != nil {
- if models.IsErrEmailAlreadyUsed(err) {
- ctx.Data["Err_Email"] = true
- ctx.RenderWithErr(ctx.Tr("form.email_been_used"), USER_EDIT, &f)
- } else {
- ctx.Handle(500, "UpdateUser", err)
- }
- return
- }
- log.Trace("Account profile updated by admin (%s): %s", ctx.User.Name, u.Name)
- ctx.Flash.Success(ctx.Tr("admin.users.update_profile_success"))
- ctx.Redirect(setting.AppSubUrl + "/admin/users/" + ctx.Params(":userid"))
- }
- func DeleteUser(ctx *context.Context) {
- u, err := models.GetUserByID(ctx.ParamsInt64(":userid"))
- if err != nil {
- ctx.Handle(500, "GetUserByID", err)
- return
- }
- if err = models.DeleteUser(u); err != nil {
- switch {
- case models.IsErrUserOwnRepos(err):
- ctx.Flash.Error(ctx.Tr("admin.users.still_own_repo"))
- ctx.JSON(200, map[string]interface{}{
- "redirect": setting.AppSubUrl + "/admin/users/" + ctx.Params(":userid"),
- })
- case models.IsErrUserHasOrgs(err):
- ctx.Flash.Error(ctx.Tr("admin.users.still_has_org"))
- ctx.JSON(200, map[string]interface{}{
- "redirect": setting.AppSubUrl + "/admin/users/" + ctx.Params(":userid"),
- })
- default:
- ctx.Handle(500, "DeleteUser", err)
- }
- return
- }
- log.Trace("Account deleted by admin (%s): %s", ctx.User.Name, u.Name)
- ctx.Flash.Success(ctx.Tr("admin.users.deletion_success"))
- ctx.JSON(200, map[string]interface{}{
- "redirect": setting.AppSubUrl + "/admin/users",
- })
- }
|