1
0
Fork 0
forgejo/routers/admin/user.go

145 lines
3.6 KiB
Go
Raw Normal View History

2014-03-21 03:27:59 -04: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-03-30 12:11:28 -04:00
"github.com/go-martini/martini"
2014-03-21 06:15:58 -04:00
2014-03-21 03:27:59 -04:00
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/auth"
2014-03-21 06:15:58 -04:00
"github.com/gogits/gogs/modules/base"
2014-03-21 03:27:59 -04:00
"github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/middleware"
)
func NewUser(ctx *middleware.Context, form auth.RegisterForm) {
ctx.Data["Title"] = "New Account"
2014-03-21 09:06:05 -04:00
ctx.Data["PageIsUsers"] = true
2014-03-21 03:27:59 -04:00
if ctx.Req.Method == "GET" {
ctx.HTML(200, "admin/users/new")
return
}
if form.Password != form.RetypePasswd {
ctx.Data["HasError"] = true
ctx.Data["Err_Password"] = true
ctx.Data["Err_RetypePasswd"] = true
ctx.Data["ErrorMsg"] = "Password and re-type password are not same"
auth.AssignForm(form, ctx.Data)
}
if ctx.HasError() {
ctx.HTML(200, "admin/users/new")
return
}
u := &models.User{
Name: form.UserName,
Email: form.Email,
Passwd: form.Password,
IsActive: true,
}
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:
ctx.Handle(200, "admin.user.NewUser", err)
}
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 06:15:58 -04:00
func EditUser(ctx *middleware.Context, params martini.Params, form auth.AdminEditUserForm) {
ctx.Data["Title"] = "Edit Account"
2014-03-21 09:06:05 -04:00
ctx.Data["PageIsUsers"] = true
2014-03-21 06:15:58 -04:00
uid, err := base.StrTo(params["userid"]).Int()
if err != nil {
ctx.Handle(200, "admin.user.EditUser", err)
return
}
u, err := models.GetUserById(int64(uid))
if err != nil {
ctx.Handle(200, "admin.user.EditUser", err)
return
}
if ctx.Req.Method == "GET" {
ctx.Data["User"] = u
ctx.HTML(200, "admin/users/edit")
return
}
u.Email = form.Email
u.Website = form.Website
u.Location = form.Location
u.Avatar = base.EncodeMd5(form.Avatar)
u.AvatarEmail = form.Avatar
u.IsActive = form.Active == "on"
u.IsAdmin = form.Admin == "on"
if err := models.UpdateUser(u); err != nil {
ctx.Handle(200, "admin.user.EditUser", err)
return
}
ctx.Data["IsSuccess"] = true
ctx.Data["User"] = u
ctx.HTML(200, "admin/users/edit")
log.Trace("%s User profile updated by admin(%s): %s", ctx.Req.RequestURI,
ctx.User.LowerName, ctx.User.LowerName)
}
2014-03-22 06:20:00 -04:00
func DeleteUser(ctx *middleware.Context, params martini.Params) {
ctx.Data["Title"] = "Edit Account"
ctx.Data["PageIsUsers"] = true
uid, err := base.StrTo(params["userid"]).Int()
if err != nil {
ctx.Handle(200, "admin.user.EditUser", err)
return
}
u, err := models.GetUserById(int64(uid))
if err != nil {
ctx.Handle(200, "admin.user.EditUser", err)
return
}
if err = models.DeleteUser(u); err != nil {
ctx.Data["HasError"] = true
switch err {
case models.ErrUserOwnRepos:
ctx.Data["ErrorMsg"] = "This account still has ownership of repository, owner has to delete or transfer them first."
ctx.Data["User"] = u
ctx.HTML(200, "admin/users/edit")
default:
ctx.Handle(200, "admin.user.DeleteUser", err)
}
return
}
log.Trace("%s User deleted by admin(%s): %s", ctx.Req.RequestURI,
ctx.User.LowerName, ctx.User.LowerName)
2014-03-22 17:59:22 -04:00
ctx.Redirect("/admin/users")
2014-03-22 06:20:00 -04:00
}