bench-forgejo/routers/admin/user.go

180 lines
4.6 KiB
Go
Raw Normal View History

2014-03-21 12:57:59 +05:30
// 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 (
2014-05-05 14:10:25 +05:30
"fmt"
"strconv"
2014-03-21 12:57:59 +05:30
"strings"
2014-03-30 21:41:28 +05:30
"github.com/go-martini/martini"
2014-03-21 15:45:58 +05:30
2014-03-21 12:57:59 +05:30
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/auth"
2014-03-21 15:45:58 +05:30
"github.com/gogits/gogs/modules/base"
2014-03-21 12:57:59 +05:30
"github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/middleware"
)
2014-04-11 03:39:57 +05:30
func NewUser(ctx *middleware.Context) {
2014-03-21 12:57:59 +05:30
ctx.Data["Title"] = "New Account"
2014-03-21 18:36:05 +05:30
ctx.Data["PageIsUsers"] = true
2014-05-05 14:10:25 +05:30
auths, err := models.GetAuths()
if err != nil {
ctx.Handle(500, "admin.user.NewUser", err)
return
}
ctx.Data["LoginSources"] = auths
2014-04-11 03:39:57 +05:30
ctx.HTML(200, "admin/users/new")
}
2014-03-21 12:57:59 +05:30
2014-04-11 03:39:57 +05:30
func NewUserPost(ctx *middleware.Context, form auth.RegisterForm) {
ctx.Data["Title"] = "New Account"
ctx.Data["PageIsUsers"] = true
2014-03-21 12:57:59 +05:30
2014-05-06 05:28:13 +05:30
if ctx.HasError() {
ctx.HTML(200, "admin/users/new")
return
}
2014-03-21 12:57:59 +05:30
if form.Password != form.RetypePasswd {
ctx.Data["Err_Password"] = true
ctx.Data["Err_RetypePasswd"] = true
2014-05-06 05:28:13 +05:30
ctx.RenderWithErr("Password and re-type password are not same.", "admin/users/new", &form)
2014-03-21 12:57:59 +05:30
return
}
u := &models.User{
2014-05-05 14:10:25 +05:30
Name: form.UserName,
Email: form.Email,
Passwd: form.Password,
IsActive: true,
LoginType: models.PLAIN,
2014-05-05 14:10:25 +05:30
}
if len(form.LoginType) > 0 {
fields := strings.Split(form.LoginType, "-")
tp, _ := strconv.Atoi(fields[0])
u.LoginType = models.LoginType(tp)
2014-05-05 14:10:25 +05:30
u.LoginSource, _ = strconv.ParseInt(fields[1], 10, 64)
2014-05-11 11:42:45 +05:30
u.LoginName = form.LoginName
fmt.Println(u.LoginType, u.LoginSource, u.LoginName)
2014-03-21 12:57:59 +05:30
}
var err error
if u, err = models.RegisterUser(u); err != nil {
switch err {
case models.ErrUserAlreadyExist:
ctx.RenderWithErr("Username has been already taken", "admin/users/new", &form)
case models.ErrEmailAlreadyUsed:
ctx.RenderWithErr("E-mail address has been already used", "admin/users/new", &form)
case models.ErrUserNameIllegal:
ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), "admin/users/new", &form)
default:
2014-04-11 03:39:57 +05:30
ctx.Handle(500, "admin.user.NewUser", err)
2014-03-21 12:57:59 +05:30
}
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 15:45:58 +05:30
2014-04-11 03:39:57 +05:30
func EditUser(ctx *middleware.Context, params martini.Params) {
2014-03-21 15:45:58 +05:30
ctx.Data["Title"] = "Edit Account"
2014-03-21 18:36:05 +05:30
ctx.Data["PageIsUsers"] = true
2014-03-21 15:45:58 +05:30
uid, err := base.StrTo(params["userid"]).Int()
if err != nil {
2014-04-11 03:39:57 +05:30
ctx.Handle(404, "admin.user.EditUser", err)
2014-03-21 15:45:58 +05:30
return
}
u, err := models.GetUserById(int64(uid))
if err != nil {
2014-04-11 03:39:57 +05:30
ctx.Handle(500, "admin.user.EditUser", err)
2014-03-21 15:45:58 +05:30
return
}
2014-04-11 03:39:57 +05:30
ctx.Data["User"] = u
2014-05-05 14:10:25 +05:30
auths, err := models.GetAuths()
if err != nil {
ctx.Handle(500, "admin.user.NewUser", err)
return
}
ctx.Data["LoginSources"] = auths
2014-04-11 03:39:57 +05:30
ctx.HTML(200, "admin/users/edit")
}
func EditUserPost(ctx *middleware.Context, params martini.Params, form auth.AdminEditUserForm) {
ctx.Data["Title"] = "Edit Account"
ctx.Data["PageIsUsers"] = true
uid, err := base.StrTo(params["userid"]).Int()
if err != nil {
ctx.Handle(404, "admin.user.EditUser", err)
return
}
u, err := models.GetUserById(int64(uid))
if err != nil {
ctx.Handle(500, "admin.user.EditUser", err)
2014-03-21 15:45:58 +05:30
return
}
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 15:02:47 +05:30
u.IsActive = form.Active
u.IsAdmin = form.Admin
2014-03-21 15:45:58 +05:30
if err := models.UpdateUser(u); err != nil {
2014-04-11 03:39:57 +05:30
ctx.Handle(500, "admin.user.EditUser", err)
2014-03-21 15:45:58 +05:30
return
}
log.Trace("%s User profile updated by admin(%s): %s", ctx.Req.RequestURI,
ctx.User.LowerName, ctx.User.LowerName)
2014-04-11 03:39:57 +05:30
ctx.Data["User"] = u
ctx.Flash.Success("Account profile has been successfully updated.")
ctx.Redirect("/admin/users/" + params["userid"])
2014-03-21 15:45:58 +05:30
}
2014-03-22 15:50:00 +05:30
func DeleteUser(ctx *middleware.Context, params martini.Params) {
2014-04-11 03:39:57 +05:30
ctx.Data["Title"] = "Delete Account"
2014-03-22 15:50:00 +05:30
ctx.Data["PageIsUsers"] = true
2014-05-05 14:10:25 +05:30
//log.Info("delete")
2014-03-22 15:50:00 +05:30
uid, err := base.StrTo(params["userid"]).Int()
if err != nil {
2014-04-11 03:39:57 +05:30
ctx.Handle(404, "admin.user.EditUser", err)
2014-03-22 15:50:00 +05:30
return
}
u, err := models.GetUserById(int64(uid))
if err != nil {
2014-04-11 03:39:57 +05:30
ctx.Handle(500, "admin.user.EditUser", err)
2014-03-22 15:50:00 +05:30
return
}
if err = models.DeleteUser(u); err != nil {
switch err {
case models.ErrUserOwnRepos:
2014-04-11 03:39:57 +05:30
ctx.Flash.Error("This account still has ownership of repository, owner has to delete or transfer them first.")
ctx.Redirect("/admin/users/" + params["userid"])
2014-03-22 15:50:00 +05:30
default:
2014-04-11 03:39:57 +05:30
ctx.Handle(500, "admin.user.DeleteUser", err)
2014-03-22 15:50:00 +05:30
}
return
}
log.Trace("%s User deleted by admin(%s): %s", ctx.Req.RequestURI,
ctx.User.LowerName, ctx.User.LowerName)
2014-03-23 03:29:22 +05:30
ctx.Redirect("/admin/users")
2014-03-22 15:50:00 +05:30
}