2015-12-17 12:58:47 +05:30
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
2019-01-24 04:00:19 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2015-12-17 12:58:47 +05:30
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
2019-12-20 22:37:12 +05:30
|
|
|
"net/http"
|
|
|
|
|
2016-11-10 21:54:48 +05:30
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
2019-11-10 10:11:51 +05:30
|
|
|
"code.gitea.io/gitea/modules/convert"
|
2019-08-23 22:10:30 +05:30
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2021-01-26 21:06:53 +05:30
|
|
|
"code.gitea.io/gitea/modules/web"
|
2016-11-10 21:54:48 +05:30
|
|
|
"code.gitea.io/gitea/routers/api/v1/user"
|
2020-01-25 00:30:29 +05:30
|
|
|
"code.gitea.io/gitea/routers/api/v1/utils"
|
2015-12-17 12:58:47 +05:30
|
|
|
)
|
|
|
|
|
2016-11-24 12:34:31 +05:30
|
|
|
// CreateOrg api for create organization
|
2021-01-26 21:06:53 +05:30
|
|
|
func CreateOrg(ctx *context.APIContext) {
|
2017-11-13 12:32:25 +05:30
|
|
|
// swagger:operation POST /admin/users/{username}/orgs admin adminCreateOrg
|
|
|
|
// ---
|
|
|
|
// summary: Create an organization
|
|
|
|
// consumes:
|
|
|
|
// - application/json
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: username
|
|
|
|
// in: path
|
|
|
|
// description: username of the user that will own the created organization
|
|
|
|
// type: string
|
|
|
|
// required: true
|
2018-10-21 09:10:42 +05:30
|
|
|
// - name: organization
|
|
|
|
// in: body
|
|
|
|
// required: true
|
|
|
|
// schema: { "$ref": "#/definitions/CreateOrgOption" }
|
2017-11-13 12:32:25 +05:30
|
|
|
// responses:
|
|
|
|
// "201":
|
|
|
|
// "$ref": "#/responses/Organization"
|
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/forbidden"
|
|
|
|
// "422":
|
|
|
|
// "$ref": "#/responses/validationError"
|
2021-01-26 21:06:53 +05:30
|
|
|
form := web.GetForm(ctx).(*api.CreateOrgOption)
|
2015-12-17 12:58:47 +05:30
|
|
|
u := user.GetUserByParams(ctx)
|
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-30 23:27:55 +05:30
|
|
|
visibility := api.VisibleTypePublic
|
|
|
|
if form.Visibility != "" {
|
|
|
|
visibility = api.VisibilityModes[form.Visibility]
|
|
|
|
}
|
|
|
|
|
2015-12-17 12:58:47 +05:30
|
|
|
org := &models.User{
|
|
|
|
Name: form.UserName,
|
|
|
|
FullName: form.FullName,
|
|
|
|
Description: form.Description,
|
|
|
|
Website: form.Website,
|
|
|
|
Location: form.Location,
|
|
|
|
IsActive: true,
|
2016-11-07 22:23:22 +05:30
|
|
|
Type: models.UserTypeOrganization,
|
2019-05-30 23:27:55 +05:30
|
|
|
Visibility: visibility,
|
2015-12-17 12:58:47 +05:30
|
|
|
}
|
2019-05-30 23:27:55 +05:30
|
|
|
|
2015-12-17 12:58:47 +05:30
|
|
|
if err := models.CreateOrganization(org, u); err != nil {
|
|
|
|
if models.IsErrUserAlreadyExist(err) ||
|
|
|
|
models.IsErrNameReserved(err) ||
|
2020-02-24 01:22:05 +05:30
|
|
|
models.IsErrNameCharsNotAllowed(err) ||
|
2015-12-17 12:58:47 +05:30
|
|
|
models.IsErrNamePatternNotAllowed(err) {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "", err)
|
2015-12-17 12:58:47 +05:30
|
|
|
} else {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "CreateOrganization", err)
|
2015-12-17 12:58:47 +05:30
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.JSON(http.StatusCreated, convert.ToOrganization(org))
|
2015-12-17 12:58:47 +05:30
|
|
|
}
|
2019-01-24 04:00:19 +05:30
|
|
|
|
|
|
|
//GetAllOrgs API for getting information of all the organizations
|
|
|
|
func GetAllOrgs(ctx *context.APIContext) {
|
|
|
|
// swagger:operation GET /admin/orgs admin adminGetAllOrgs
|
|
|
|
// ---
|
|
|
|
// summary: List all organizations
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
2019-08-05 00:03:36 +05:30
|
|
|
// parameters:
|
|
|
|
// - name: page
|
|
|
|
// in: query
|
|
|
|
// description: page number of results to return (1-based)
|
|
|
|
// type: integer
|
|
|
|
// - name: limit
|
|
|
|
// in: query
|
2020-06-09 10:27:38 +05:30
|
|
|
// description: page size of results
|
2019-08-05 00:03:36 +05:30
|
|
|
// type: integer
|
2019-01-24 04:00:19 +05:30
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/OrganizationList"
|
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/forbidden"
|
2019-12-20 22:37:12 +05:30
|
|
|
|
2020-06-21 13:52:06 +05:30
|
|
|
listOptions := utils.GetListOptions(ctx)
|
|
|
|
|
|
|
|
users, maxResults, err := models.SearchUsers(&models.SearchUserOptions{
|
2020-01-25 00:30:29 +05:30
|
|
|
Type: models.UserTypeOrganization,
|
|
|
|
OrderBy: models.SearchOrderByAlphabetically,
|
2020-06-21 13:52:06 +05:30
|
|
|
ListOptions: listOptions,
|
2020-01-25 00:30:29 +05:30
|
|
|
Visible: []api.VisibleType{api.VisibleTypePublic, api.VisibleTypeLimited, api.VisibleTypePrivate},
|
2019-01-24 04:00:19 +05:30
|
|
|
})
|
|
|
|
if err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "SearchOrganizations", err)
|
2019-01-24 04:00:19 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
orgs := make([]*api.Organization, len(users))
|
|
|
|
for i := range users {
|
|
|
|
orgs[i] = convert.ToOrganization(users[i])
|
|
|
|
}
|
2020-06-21 13:52:06 +05:30
|
|
|
|
|
|
|
ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)
|
2021-08-12 18:13:08 +05:30
|
|
|
ctx.SetTotalCountHeader(maxResults)
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.JSON(http.StatusOK, &orgs)
|
2019-01-24 04:00:19 +05:30
|
|
|
}
|