mirror of https://github.com/gogits/gogs.git
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.
161 lines
3.8 KiB
161 lines
3.8 KiB
9 years ago
|
// 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 (
|
||
8 years ago
|
log "gopkg.in/clog.v1"
|
||
|
|
||
9 years ago
|
api "github.com/gogits/go-gogs-client"
|
||
|
|
||
|
"github.com/gogits/gogs/models"
|
||
8 years ago
|
"github.com/gogits/gogs/pkg/context"
|
||
|
"github.com/gogits/gogs/pkg/mailer"
|
||
|
"github.com/gogits/gogs/pkg/setting"
|
||
8 years ago
|
"github.com/gogits/gogs/routes/api/v1/user"
|
||
9 years ago
|
)
|
||
|
|
||
8 years ago
|
func parseLoginSource(c *context.APIContext, u *models.User, sourceID int64, loginName string) {
|
||
9 years ago
|
if sourceID == 0 {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
source, err := models.GetLoginSourceByID(sourceID)
|
||
|
if err != nil {
|
||
8 years ago
|
if models.IsErrLoginSourceNotExist(err) {
|
||
8 years ago
|
c.Error(422, "", err)
|
||
9 years ago
|
} else {
|
||
8 years ago
|
c.Error(500, "GetLoginSourceByID", err)
|
||
9 years ago
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
|
u.LoginType = source.Type
|
||
|
u.LoginSource = source.ID
|
||
|
u.LoginName = loginName
|
||
|
}
|
||
|
|
||
9 years ago
|
// https://github.com/gogits/go-gogs-client/wiki/Administration-Users#create-a-new-user
|
||
8 years ago
|
func CreateUser(c *context.APIContext, form api.CreateUserOption) {
|
||
9 years ago
|
u := &models.User{
|
||
|
Name: form.Username,
|
||
8 years ago
|
FullName: form.FullName,
|
||
9 years ago
|
Email: form.Email,
|
||
|
Passwd: form.Password,
|
||
|
IsActive: true,
|
||
9 years ago
|
LoginType: models.LOGIN_PLAIN,
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
parseLoginSource(c, u, form.SourceID, form.LoginName)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
|
if err := models.CreateUser(u); err != nil {
|
||
|
if models.IsErrUserAlreadyExist(err) ||
|
||
|
models.IsErrEmailAlreadyUsed(err) ||
|
||
|
models.IsErrNameReserved(err) ||
|
||
|
models.IsErrNamePatternNotAllowed(err) {
|
||
8 years ago
|
c.Error(422, "", err)
|
||
9 years ago
|
} else {
|
||
8 years ago
|
c.Error(500, "CreateUser", err)
|
||
9 years ago
|
}
|
||
|
return
|
||
|
}
|
||
8 years ago
|
log.Trace("Account created by admin (%s): %s", c.User.Name, u.Name)
|
||
9 years ago
|
|
||
8 years ago
|
// Send email notification.
|
||
9 years ago
|
if form.SendNotify && setting.MailService != nil {
|
||
8 years ago
|
mailer.SendRegisterNotifyMail(c.Context.Context, models.NewMailerUser(u))
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
c.JSON(201, u.APIFormat())
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// https://github.com/gogits/go-gogs-client/wiki/Administration-Users#edit-an-existing-user
|
||
8 years ago
|
func EditUser(c *context.APIContext, form api.EditUserOption) {
|
||
|
u := user.GetUserByParams(c)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
parseLoginSource(c, u, form.SourceID, form.LoginName)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
|
if len(form.Password) > 0 {
|
||
|
u.Passwd = form.Password
|
||
8 years ago
|
var err error
|
||
|
if u.Salt, err = models.GetUserSalt(); err != nil {
|
||
8 years ago
|
c.Error(500, "UpdateUser", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
9 years ago
|
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
|
||
|
}
|
||
8 years ago
|
if form.MaxRepoCreation != nil {
|
||
|
u.MaxRepoCreation = *form.MaxRepoCreation
|
||
|
}
|
||
9 years ago
|
|
||
|
if err := models.UpdateUser(u); err != nil {
|
||
|
if models.IsErrEmailAlreadyUsed(err) {
|
||
8 years ago
|
c.Error(422, "", err)
|
||
9 years ago
|
} else {
|
||
8 years ago
|
c.Error(500, "UpdateUser", err)
|
||
9 years ago
|
}
|
||
|
return
|
||
|
}
|
||
8 years ago
|
log.Trace("Account profile updated by admin (%s): %s", c.User.Name, u.Name)
|
||
9 years ago
|
|
||
8 years ago
|
c.JSON(200, u.APIFormat())
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// https://github.com/gogits/go-gogs-client/wiki/Administration-Users#delete-a-user
|
||
8 years ago
|
func DeleteUser(c *context.APIContext) {
|
||
|
u := user.GetUserByParams(c)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
|
if err := models.DeleteUser(u); err != nil {
|
||
|
if models.IsErrUserOwnRepos(err) ||
|
||
|
models.IsErrUserHasOrgs(err) {
|
||
8 years ago
|
c.Error(422, "", err)
|
||
9 years ago
|
} else {
|
||
8 years ago
|
c.Error(500, "DeleteUser", err)
|
||
9 years ago
|
}
|
||
|
return
|
||
|
}
|
||
8 years ago
|
log.Trace("Account deleted by admin(%s): %s", c.User.Name, u.Name)
|
||
9 years ago
|
|
||
8 years ago
|
c.Status(204)
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// https://github.com/gogits/go-gogs-client/wiki/Administration-Users#create-a-public-key-for-user
|
||
8 years ago
|
func CreatePublicKey(c *context.APIContext, form api.CreateKeyOption) {
|
||
|
u := user.GetUserByParams(c)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
user.CreateUserPublicKey(c, form, u.ID)
|
||
9 years ago
|
}
|