Browse Source

api: simple create user

pull/2039/head
Kim Lindhardt Madsen 9 years ago
parent
commit
fe61b0a23c
  1. 2
      cmd/web.go
  2. 45
      routers/api/v1/user.go

2
cmd/web.go

@ -205,6 +205,8 @@ func runWeb(ctx *cli.Context) {
// Users.
m.Group("/users", func() {
m.Post("", bind(api.CreateUserOption{}), v1.CreateUser)
m.Get("/search", v1.SearchUsers)
m.Group("/:username", func() {

45
routers/api/v1/user.go

@ -10,9 +10,16 @@ import (
api "github.com/gogits/go-gogs-client"
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/middleware"
)
const (
USERS base.TplName = "admin/user/list"
USER_NEW base.TplName = "admin/user/new"
USER_EDIT base.TplName = "admin/user/edit"
)
// ToApiUser converts user to API format.
func ToApiUser(u *models.User) *api.User {
return &api.User{
@ -79,3 +86,41 @@ func GetUserInfo(ctx *middleware.Context) {
}
ctx.JSON(200, &api.User{u.Id, u.Name, u.FullName, u.Email, u.AvatarLink()})
}
// POST /users
func CreateUser(ctx *middleware.Context, opt api.CreateUserOption) {
if !ctx.User.IsAdmin {
ctx.Error(403)
return
}
u := &models.User{
Name: opt.Name,
Email: opt.Email,
Passwd: opt.Password,
IsActive: true,
LoginType: models.PLAIN,
}
if err := models.CreateUser(u); err != nil {
switch {
case models.IsErrUserAlreadyExist(err):
ctx.Data["Err_UserName"] = true
ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), USER_NEW, &opt)
case models.IsErrEmailAlreadyUsed(err):
ctx.Data["Err_Email"] = true
ctx.RenderWithErr(ctx.Tr("form.email_been_used"), USER_NEW, &opt)
case models.IsErrNameReserved(err):
ctx.Data["Err_UserName"] = true
ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), USER_NEW, &opt)
case models.IsErrNamePatternNotAllowed(err):
ctx.Data["Err_UserName"] = true
ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), USER_NEW, &opt)
default:
ctx.Handle(500, "CreateUser", err)
}
return
}
ctx.JSON(201, &api.User{u.Id, u.Name, u.FullName, u.Email, u.AvatarLink()})
}

Loading…
Cancel
Save