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