2014-03-21 16:27:59 +09:00
|
|
|
// 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"
|
|
|
|
|
2014-07-26 13:24:27 +09:00
|
|
|
"github.com/Unknwon/com"
|
2014-03-21 19:15:58 +09:00
|
|
|
|
2014-03-21 16:27:59 +09:00
|
|
|
"github.com/gogits/gogs/models"
|
|
|
|
"github.com/gogits/gogs/modules/auth"
|
2014-03-21 19:15:58 +09:00
|
|
|
"github.com/gogits/gogs/modules/base"
|
2014-03-21 16:27:59 +09:00
|
|
|
"github.com/gogits/gogs/modules/log"
|
|
|
|
"github.com/gogits/gogs/modules/middleware"
|
|
|
|
)
|
|
|
|
|
2014-06-23 02:14:03 +09:00
|
|
|
const (
|
|
|
|
USER_NEW base.TplName = "admin/user/new"
|
|
|
|
USER_EDIT base.TplName = "admin/user/edit"
|
|
|
|
)
|
|
|
|
|
2014-04-11 07:09:57 +09:00
|
|
|
func NewUser(ctx *middleware.Context) {
|
2014-03-21 16:27:59 +09:00
|
|
|
ctx.Data["Title"] = "New Account"
|
2014-03-21 22:06:05 +09:00
|
|
|
ctx.Data["PageIsUsers"] = true
|
2014-05-05 17:40:25 +09:00
|
|
|
auths, err := models.GetAuths()
|
|
|
|
if err != nil {
|
2014-06-23 02:14:03 +09:00
|
|
|
ctx.Handle(500, "admin.user.NewUser(GetAuths)", err)
|
2014-05-05 17:40:25 +09:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["LoginSources"] = auths
|
2014-06-23 02:14:03 +09:00
|
|
|
ctx.HTML(200, USER_NEW)
|
2014-04-11 07:09:57 +09:00
|
|
|
}
|
2014-03-21 16:27:59 +09:00
|
|
|
|
2014-04-11 07:09:57 +09:00
|
|
|
func NewUserPost(ctx *middleware.Context, form auth.RegisterForm) {
|
|
|
|
ctx.Data["Title"] = "New Account"
|
|
|
|
ctx.Data["PageIsUsers"] = true
|
2014-03-21 16:27:59 +09:00
|
|
|
|
2014-05-06 08:58:13 +09:00
|
|
|
if ctx.HasError() {
|
2014-06-23 02:14:03 +09:00
|
|
|
ctx.HTML(200, USER_NEW)
|
2014-05-06 08:58:13 +09:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-07-26 13:24:27 +09:00
|
|
|
if form.Password != form.Retype {
|
2014-03-21 16:27:59 +09:00
|
|
|
ctx.Data["Err_Password"] = true
|
|
|
|
ctx.Data["Err_RetypePasswd"] = true
|
2014-05-06 08:58:13 +09:00
|
|
|
ctx.RenderWithErr("Password and re-type password are not same.", "admin/users/new", &form)
|
2014-03-21 16:27:59 +09:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
u := &models.User{
|
2014-05-05 17:40:25 +09:00
|
|
|
Name: form.UserName,
|
|
|
|
Email: form.Email,
|
|
|
|
Passwd: form.Password,
|
|
|
|
IsActive: true,
|
2014-06-09 06:53:53 +09:00
|
|
|
LoginType: models.PLAIN,
|
2014-05-05 17:40:25 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(form.LoginType) > 0 {
|
2014-06-23 02:14:03 +09:00
|
|
|
// NOTE: need rewrite.
|
2014-05-05 17:40:25 +09:00
|
|
|
fields := strings.Split(form.LoginType, "-")
|
2014-07-26 13:24:27 +09:00
|
|
|
tp, _ := com.StrTo(fields[0]).Int()
|
2014-06-09 06:53:53 +09:00
|
|
|
u.LoginType = models.LoginType(tp)
|
2014-07-26 13:24:27 +09:00
|
|
|
u.LoginSource, _ = com.StrTo(fields[1]).Int64()
|
2014-05-11 15:12:45 +09:00
|
|
|
u.LoginName = form.LoginName
|
2014-03-21 16:27:59 +09:00
|
|
|
}
|
|
|
|
|
2014-07-26 13:24:27 +09:00
|
|
|
if err := models.CreateUser(u); err != nil {
|
2014-03-21 16:27:59 +09:00
|
|
|
switch err {
|
|
|
|
case models.ErrUserAlreadyExist:
|
2014-06-23 02:14:03 +09:00
|
|
|
ctx.RenderWithErr("Username has been already taken", USER_NEW, &form)
|
2014-03-21 16:27:59 +09:00
|
|
|
case models.ErrEmailAlreadyUsed:
|
2014-06-23 02:14:03 +09:00
|
|
|
ctx.RenderWithErr("E-mail address has been already used", USER_NEW, &form)
|
2014-03-21 16:27:59 +09:00
|
|
|
case models.ErrUserNameIllegal:
|
2014-06-23 02:14:03 +09:00
|
|
|
ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), USER_NEW, &form)
|
2014-03-21 16:27:59 +09:00
|
|
|
default:
|
2014-06-25 13:44:48 +09:00
|
|
|
ctx.Handle(500, "admin.user.NewUser(CreateUser)", err)
|
2014-03-21 16:27:59 +09:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace("%s User created by admin(%s): %s", ctx.Req.RequestURI,
|
|
|
|
ctx.User.LowerName, strings.ToLower(form.UserName))
|
|
|
|
|
|
|
|
ctx.Redirect("/admin/users")
|
|
|
|
}
|
2014-03-21 19:15:58 +09:00
|
|
|
|
2014-07-26 13:24:27 +09:00
|
|
|
func EditUser(ctx *middleware.Context) {
|
2014-03-21 19:15:58 +09:00
|
|
|
ctx.Data["Title"] = "Edit Account"
|
2014-03-21 22:06:05 +09:00
|
|
|
ctx.Data["PageIsUsers"] = true
|
2014-03-21 19:15:58 +09:00
|
|
|
|
2014-07-26 13:24:27 +09:00
|
|
|
uid, err := com.StrTo(ctx.Params(":userid")).Int()
|
2014-03-21 19:15:58 +09:00
|
|
|
if err != nil {
|
2014-04-11 07:09:57 +09:00
|
|
|
ctx.Handle(404, "admin.user.EditUser", err)
|
2014-03-21 19:15:58 +09:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := models.GetUserById(int64(uid))
|
|
|
|
if err != nil {
|
2014-06-23 02:14:03 +09:00
|
|
|
ctx.Handle(500, "admin.user.EditUser(GetUserById)", err)
|
2014-03-21 19:15:58 +09:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-04-11 07:09:57 +09:00
|
|
|
ctx.Data["User"] = u
|
2014-05-05 17:40:25 +09:00
|
|
|
auths, err := models.GetAuths()
|
|
|
|
if err != nil {
|
2014-06-23 02:14:03 +09:00
|
|
|
ctx.Handle(500, "admin.user.NewUser(GetAuths)", err)
|
2014-05-05 17:40:25 +09:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["LoginSources"] = auths
|
2014-06-23 02:14:03 +09:00
|
|
|
ctx.HTML(200, USER_EDIT)
|
2014-04-11 07:09:57 +09:00
|
|
|
}
|
|
|
|
|
2014-07-26 13:24:27 +09:00
|
|
|
func EditUserPost(ctx *middleware.Context, form auth.AdminEditUserForm) {
|
2014-04-11 07:09:57 +09:00
|
|
|
ctx.Data["Title"] = "Edit Account"
|
|
|
|
ctx.Data["PageIsUsers"] = true
|
|
|
|
|
2014-07-26 13:24:27 +09:00
|
|
|
uid, err := com.StrTo(ctx.Params(":userid")).Int()
|
2014-04-11 07:09:57 +09:00
|
|
|
if err != nil {
|
2014-06-23 02:14:03 +09:00
|
|
|
ctx.Handle(404, "admin.user.EditUserPost", err)
|
2014-04-11 07:09:57 +09:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := models.GetUserById(int64(uid))
|
|
|
|
if err != nil {
|
2014-06-23 02:14:03 +09:00
|
|
|
ctx.Handle(500, "admin.user.EditUserPost(GetUserById)", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
ctx.HTML(200, USER_EDIT)
|
2014-03-21 19:15:58 +09:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-07-26 13:24:27 +09:00
|
|
|
if form.Passwd != "" {
|
2014-07-18 11:11:40 +09:00
|
|
|
u.Passwd = form.Passwd
|
|
|
|
u.Rands = models.GetUserSalt()
|
|
|
|
u.Salt = models.GetUserSalt()
|
|
|
|
u.EncodePasswd()
|
|
|
|
}
|
|
|
|
|
2014-03-21 19:15:58 +09:00
|
|
|
u.Email = form.Email
|
|
|
|
u.Website = form.Website
|
|
|
|
u.Location = form.Location
|
|
|
|
u.Avatar = base.EncodeMd5(form.Avatar)
|
|
|
|
u.AvatarEmail = form.Avatar
|
2014-05-05 18:32:47 +09:00
|
|
|
u.IsActive = form.Active
|
|
|
|
u.IsAdmin = form.Admin
|
2014-03-21 19:15:58 +09:00
|
|
|
if err := models.UpdateUser(u); err != nil {
|
2014-06-23 02:14:03 +09:00
|
|
|
ctx.Handle(500, "admin.user.EditUserPost(UpdateUser)", err)
|
2014-03-21 19:15:58 +09:00
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Trace("%s User profile updated by admin(%s): %s", ctx.Req.RequestURI,
|
|
|
|
ctx.User.LowerName, ctx.User.LowerName)
|
2014-04-11 07:09:57 +09:00
|
|
|
|
|
|
|
ctx.Data["User"] = u
|
|
|
|
ctx.Flash.Success("Account profile has been successfully updated.")
|
2014-07-26 13:24:27 +09:00
|
|
|
ctx.Redirect("/admin/users/" + ctx.Params(":userid"))
|
2014-03-21 19:15:58 +09:00
|
|
|
}
|
2014-03-22 19:20:00 +09:00
|
|
|
|
2014-07-26 13:24:27 +09:00
|
|
|
func DeleteUser(ctx *middleware.Context) {
|
2014-04-11 07:09:57 +09:00
|
|
|
ctx.Data["Title"] = "Delete Account"
|
2014-03-22 19:20:00 +09:00
|
|
|
ctx.Data["PageIsUsers"] = true
|
|
|
|
|
2014-05-05 17:40:25 +09:00
|
|
|
//log.Info("delete")
|
2014-07-26 13:24:27 +09:00
|
|
|
uid, err := com.StrTo(ctx.Params(":userid")).Int()
|
2014-03-22 19:20:00 +09:00
|
|
|
if err != nil {
|
2014-06-23 02:14:03 +09:00
|
|
|
ctx.Handle(404, "admin.user.DeleteUser", err)
|
2014-03-22 19:20:00 +09:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := models.GetUserById(int64(uid))
|
|
|
|
if err != nil {
|
2014-06-23 02:14:03 +09:00
|
|
|
ctx.Handle(500, "admin.user.DeleteUser(GetUserById)", err)
|
2014-03-22 19:20:00 +09:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = models.DeleteUser(u); err != nil {
|
|
|
|
switch err {
|
|
|
|
case models.ErrUserOwnRepos:
|
2014-04-11 07:09:57 +09:00
|
|
|
ctx.Flash.Error("This account still has ownership of repository, owner has to delete or transfer them first.")
|
2014-07-26 13:24:27 +09:00
|
|
|
ctx.Redirect("/admin/users/" + ctx.Params(":userid"))
|
2014-03-22 19:20:00 +09:00
|
|
|
default:
|
2014-04-11 07:09:57 +09:00
|
|
|
ctx.Handle(500, "admin.user.DeleteUser", err)
|
2014-03-22 19:20:00 +09:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Trace("%s User deleted by admin(%s): %s", ctx.Req.RequestURI,
|
|
|
|
ctx.User.LowerName, ctx.User.LowerName)
|
|
|
|
|
2014-03-23 06:59:22 +09:00
|
|
|
ctx.Redirect("/admin/users")
|
2014-03-22 19:20:00 +09:00
|
|
|
}
|