gitea-1120-rc1/routers/api/v1/org/org_ext.go

161 lines
4.7 KiB
Go

// Copyright 2015 The Gogs Authors. All rights reserved.
// Copyright 2018 The Gitea 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 org
import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert"
api "code.gitea.io/gitea/modules/structs"
"fmt"
"net/http"
"strings"
)
// Edit change an organization's information modified on 2021/01/14
func Edit_Ext(ctx *context.APIContext, form api.EditOrgOptionExt) {
// swagger:operation PATCH /orgs/{org} organization orgEdit
// ---
// summary: Edit an organization
// consumes:
// - application/json
// produces:
// - application/json
// parameters:
// - name: org
// in: path
// description: name of the organization to edit
// type: string
// required: true
// - name: body
// in: body
// required: true
// schema:
// "$ref": "#/definitions/EditOrgOption"
// responses:
// "200":
// "$ref": "#/responses/Organization"
org := ctx.Org.Organization
if org.LowerName!=strings.ToLower(form.Name) && form.Name!="" {
if len(form.Name)>40 {
ctx.Error(http.StatusBadRequest, "EditOrganization", "name长度不能超过40个字符")
return
}
org.Name=form.Name //add by hcxm 2021/01/14
org.LowerName=strings.ToLower(org.Name) //add by hcxm 2021/01/14
}
org.RepoAdminChangeTeamAccess=form.RepoAdminChangeTeamAccess //add by hcxm 2021/01/14
org.FullName = form.FullName
org.Description = form.Description
org.Website = form.Website
org.Location = form.Location
var visibilityChanged bool=false
if form.Visibility != "" {
visibilityChanged = form.Visibility != org.Visibility.String()
fmt.Println("***打印参数: visibilityChanged:",visibilityChanged," form.Visibility:",form.Visibility," org.Visibility:",org.Visibility.String())
org.Visibility = api.VisibilityModes[form.Visibility]
}
if err := models.UpdateUserCols(org, "full_name", "description", "website", "location", "visibility","name","lower_name","repo_admin_change_team_access"); err != nil {
ctx.Error(http.StatusInternalServerError, "EditOrganization", err)
return
}
/*
// update forks visibility 更新库的访问权限;
if visibilityChanged {
if err := org.GetRepositories(models.ListOptions{Page: 1, PageSize: org.NumRepos}); err != nil {
ctx.ServerError("GetRepositories", err)
return
}
for _, repo := range org.Repos {
if err := models.UpdateRepository(repo, true); err != nil {
ctx.ServerError("UpdateRepository", err)
return
}
}
}
*/
ctx.JSON(http.StatusOK, convert.ToOrganization(org))
}
// Create api for create organization ext inferface
func CreateExt(ctx *context.APIContext, form api.CreateOrgOption) {
// swagger:operation POST /orgs organization orgCreate
// ---
// summary: Create an organization
// consumes:
// - application/json
// produces:
// - application/json
// parameters:
// - name: organization
// in: body
// required: true
// schema: { "$ref": "#/definitions/CreateOrgOption" }
// responses:
// "201":
// "$ref": "#/responses/Organization"
// "403":
// "$ref": "#/responses/forbidden"
// "422":
// "$ref": "#/responses/validationError"
if !ctx.User.CanCreateOrganization() {
ctx.Error(http.StatusForbidden, "Create organization not allowed", nil)
return
}
visibility := api.VisibleTypePublic
if form.Visibility != "" {
visibility = api.VisibilityModes[form.Visibility]
}
org := &models.User{
Name: form.UserName,
FullName: form.FullName,
Description: form.Description,
Website: form.Website,
Location: form.Location,
IsActive: true,
Type: models.UserTypeOrganization,
Visibility: visibility,
RepoAdminChangeTeamAccess: form.RepoAdminChangeTeamAccess,
}
if err := models.CreateOrganization(org, ctx.User); err != nil {
if models.IsErrUserAlreadyExist(err) ||
models.IsErrNameReserved(err) ||
models.IsErrNameCharsNotAllowed(err) ||
models.IsErrNamePatternNotAllowed(err) {
ctx.Error(http.StatusUnprocessableEntity, "", err)
} else {
ctx.Error(http.StatusInternalServerError, "CreateOrganization", err)
}
return
}
// 根据业务需要 自定义创建组织时将默认团队同时返回.
Team, err := models.GetTeam(org.ID,"")
if err != nil {
if models.IsErrUserNotExist(err) {
ctx.NotFound()
} else {
ctx.Error(http.StatusInternalServerError, "GetTeam", err)
}
return
}
apiOrg:=convert.ToOrganizationExt(org)
apiOrg.OwnerTeam=convert.ToTeam(Team)
ctx.JSON(http.StatusCreated, apiOrg)
}