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.

180 lines
4.6 KiB

// 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 (
"fmt"
"strconv"
"strings"
"github.com/go-martini/martini"
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/auth"
"github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/middleware"
)
11 years ago
func NewUser(ctx *middleware.Context) {
ctx.Data["Title"] = "New Account"
ctx.Data["PageIsUsers"] = true
auths, err := models.GetAuths()
if err != nil {
ctx.Handle(500, "admin.user.NewUser", err)
return
}
ctx.Data["LoginSources"] = auths
11 years ago
ctx.HTML(200, "admin/users/new")
}
11 years ago
func NewUserPost(ctx *middleware.Context, form auth.RegisterForm) {
ctx.Data["Title"] = "New Account"
ctx.Data["PageIsUsers"] = true
11 years ago
if ctx.HasError() {
ctx.HTML(200, "admin/users/new")
return
}
if form.Password != form.RetypePasswd {
ctx.Data["Err_Password"] = true
ctx.Data["Err_RetypePasswd"] = true
11 years ago
ctx.RenderWithErr("Password and re-type password are not same.", "admin/users/new", &form)
return
}
u := &models.User{
Name: form.UserName,
Email: form.Email,
Passwd: form.Password,
IsActive: true,
LoginType: models.PLAIN,
}
if len(form.LoginType) > 0 {
fields := strings.Split(form.LoginType, "-")
tp, _ := strconv.Atoi(fields[0])
u.LoginType = models.LoginType(tp)
u.LoginSource, _ = strconv.ParseInt(fields[1], 10, 64)
u.LoginName = form.LoginName
fmt.Println(u.LoginType, u.LoginSource, u.LoginName)
}
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:
11 years ago
ctx.Handle(500, "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")
}
11 years ago
func EditUser(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 {
11 years ago
ctx.Handle(404, "admin.user.EditUser", err)
return
}
u, err := models.GetUserById(int64(uid))
if err != nil {
11 years ago
ctx.Handle(500, "admin.user.EditUser", err)
return
}
11 years ago
ctx.Data["User"] = u
auths, err := models.GetAuths()
if err != nil {
ctx.Handle(500, "admin.user.NewUser", err)
return
}
ctx.Data["LoginSources"] = auths
11 years ago
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)
return
}
u.Email = form.Email
u.Website = form.Website
u.Location = form.Location
u.Avatar = base.EncodeMd5(form.Avatar)
u.AvatarEmail = form.Avatar
11 years ago
u.IsActive = form.Active
u.IsAdmin = form.Admin
if err := models.UpdateUser(u); err != nil {
11 years ago
ctx.Handle(500, "admin.user.EditUser", err)
return
}
log.Trace("%s User profile updated by admin(%s): %s", ctx.Req.RequestURI,
ctx.User.LowerName, ctx.User.LowerName)
11 years ago
ctx.Data["User"] = u
ctx.Flash.Success("Account profile has been successfully updated.")
ctx.Redirect("/admin/users/" + params["userid"])
}
func DeleteUser(ctx *middleware.Context, params martini.Params) {
11 years ago
ctx.Data["Title"] = "Delete Account"
ctx.Data["PageIsUsers"] = true
//log.Info("delete")
uid, err := base.StrTo(params["userid"]).Int()
if err != nil {
11 years ago
ctx.Handle(404, "admin.user.EditUser", err)
return
}
u, err := models.GetUserById(int64(uid))
if err != nil {
11 years ago
ctx.Handle(500, "admin.user.EditUser", err)
return
}
if err = models.DeleteUser(u); err != nil {
switch err {
case models.ErrUserOwnRepos:
11 years ago
ctx.Flash.Error("This account still has ownership of repository, owner has to delete or transfer them first.")
ctx.Redirect("/admin/users/" + params["userid"])
default:
11 years ago
ctx.Handle(500, "admin.user.DeleteUser", err)
}
return
}
log.Trace("%s User deleted by admin(%s): %s", ctx.Req.RequestURI,
ctx.User.LowerName, ctx.User.LowerName)
11 years ago
ctx.Redirect("/admin/users")
}