forgejo-federation/routers/api/v1/admin/user.go

212 lines
4.7 KiB
Go
Raw Normal View History

2015-12-06 03:43:13 +05:30
// Copyright 2015 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 (
api "code.gitea.io/sdk/gitea"
2015-12-06 03:43:13 +05:30
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/routers/api/v1/user"
2015-12-06 03:43:13 +05:30
)
func parseLoginSource(ctx *context.APIContext, u *models.User, sourceID int64, loginName string) {
2015-12-06 03:43:13 +05:30
if sourceID == 0 {
return
}
source, err := models.GetLoginSourceByID(sourceID)
if err != nil {
if models.IsErrLoginSourceNotExist(err) {
ctx.Error(422, "", err)
2015-12-06 03:43:13 +05:30
} else {
ctx.Error(500, "GetLoginSourceByID", err)
2015-12-06 03:43:13 +05:30
}
return
}
u.LoginType = source.Type
u.LoginSource = source.ID
u.LoginName = loginName
}
2016-11-24 12:34:31 +05:30
// CreateUser api for creating a user
func CreateUser(ctx *context.APIContext, form api.CreateUserOption) {
// swagger:route POST /admin/users admin adminCreateUser
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Responses:
// 201: User
// 403: forbidden
// 422: validationError
// 500: error
2015-12-06 03:43:13 +05:30
u := &models.User{
Name: form.Username,
FullName: form.FullName,
2015-12-06 03:43:13 +05:30
Email: form.Email,
Passwd: form.Password,
IsActive: true,
2016-11-07 22:00:04 +05:30
LoginType: models.LoginPlain,
2015-12-06 03:43:13 +05:30
}
parseLoginSource(ctx, u, form.SourceID, form.LoginName)
if ctx.Written() {
return
}
if err := models.CreateUser(u); err != nil {
if models.IsErrUserAlreadyExist(err) ||
models.IsErrEmailAlreadyUsed(err) ||
models.IsErrNameReserved(err) ||
models.IsErrNamePatternNotAllowed(err) {
ctx.Error(422, "", err)
2015-12-06 03:43:13 +05:30
} else {
ctx.Error(500, "CreateUser", err)
2015-12-06 03:43:13 +05:30
}
return
}
log.Trace("Account created by admin (%s): %s", ctx.User.Name, u.Name)
// Send email notification.
2015-12-06 03:43:13 +05:30
if form.SendNotify && setting.MailService != nil {
models.SendRegisterNotifyMail(ctx.Context.Context, u)
2015-12-06 03:43:13 +05:30
}
ctx.JSON(201, u.APIFormat())
2015-12-06 03:43:13 +05:30
}
2016-11-24 12:34:31 +05:30
// EditUser api for modifying a user's information
func EditUser(ctx *context.APIContext, form api.EditUserOption) {
// swagger:route PATCH /admin/users/{username} admin adminEditUser
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Responses:
// 200: User
// 403: forbidden
// 422: validationError
// 500: error
2015-12-06 03:43:13 +05:30
u := user.GetUserByParams(ctx)
if ctx.Written() {
return
}
parseLoginSource(ctx, u, form.SourceID, form.LoginName)
if ctx.Written() {
return
}
if len(form.Password) > 0 {
u.Passwd = form.Password
var err error
if u.Salt, err = models.GetUserSalt(); err != nil {
ctx.Error(500, "UpdateUser", err)
return
}
2015-12-06 03:43:13 +05:30
u.EncodePasswd()
}
u.LoginName = form.LoginName
u.FullName = form.FullName
u.Email = form.Email
u.Website = form.Website
u.Location = form.Location
if form.Active != nil {
u.IsActive = *form.Active
}
if form.Admin != nil {
u.IsAdmin = *form.Admin
}
if form.AllowGitHook != nil {
u.AllowGitHook = *form.AllowGitHook
}
if form.AllowImportLocal != nil {
u.AllowImportLocal = *form.AllowImportLocal
}
if form.MaxRepoCreation != nil {
u.MaxRepoCreation = *form.MaxRepoCreation
}
2015-12-06 03:43:13 +05:30
if err := models.UpdateUser(u); err != nil {
if models.IsErrEmailAlreadyUsed(err) {
ctx.Error(422, "", err)
2015-12-06 03:43:13 +05:30
} else {
ctx.Error(500, "UpdateUser", err)
2015-12-06 03:43:13 +05:30
}
return
}
log.Trace("Account profile updated by admin (%s): %s", ctx.User.Name, u.Name)
ctx.JSON(200, u.APIFormat())
2015-12-06 03:43:13 +05:30
}
2016-11-24 12:34:31 +05:30
// DeleteUser api for deleting a user
func DeleteUser(ctx *context.APIContext) {
// swagger:route DELETE /admin/users/{username} admin adminDeleteUser
//
// Produces:
// - application/json
//
// Responses:
// 204: empty
// 403: forbidden
// 422: validationError
// 500: error
2015-12-06 03:43:13 +05:30
u := user.GetUserByParams(ctx)
if ctx.Written() {
return
}
if err := models.DeleteUser(u); err != nil {
if models.IsErrUserOwnRepos(err) ||
models.IsErrUserHasOrgs(err) {
ctx.Error(422, "", err)
2015-12-06 03:43:13 +05:30
} else {
ctx.Error(500, "DeleteUser", err)
2015-12-06 03:43:13 +05:30
}
return
}
log.Trace("Account deleted by admin(%s): %s", ctx.User.Name, u.Name)
ctx.Status(204)
}
2016-11-24 12:34:31 +05:30
// CreatePublicKey api for creating a public key to a user
func CreatePublicKey(ctx *context.APIContext, form api.CreateKeyOption) {
// swagger:route POST /admin/users/{username}/keys admin adminCreatePublicKey
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Responses:
// 201: PublicKey
// 403: forbidden
// 422: validationError
// 500: error
2015-12-06 03:43:13 +05:30
u := user.GetUserByParams(ctx)
if ctx.Written() {
return
}
2016-07-23 22:38:22 +05:30
user.CreateUserPublicKey(ctx, form, u.ID)
2015-12-06 03:43:13 +05:30
}