2017-01-20 02:31:46 +00:00
|
|
|
// Copyright 2017 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 (
|
2019-12-20 17:07:12 +00:00
|
|
|
"net/http"
|
2021-11-16 18:18:25 +00:00
|
|
|
"net/url"
|
2017-01-20 02:31:46 +00:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
2019-11-10 04:41:51 +00:00
|
|
|
"code.gitea.io/gitea/modules/convert"
|
2017-01-20 02:31:46 +00:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-08-23 16:40:30 +00:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2017-01-20 02:31:46 +00:00
|
|
|
"code.gitea.io/gitea/routers/api/v1/user"
|
2020-01-24 19:00:29 +00:00
|
|
|
"code.gitea.io/gitea/routers/api/v1/utils"
|
2017-01-20 02:31:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// listMembers list an organization's members
|
|
|
|
func listMembers(ctx *context.APIContext, publicOnly bool) {
|
2021-08-12 12:43:08 +00:00
|
|
|
opts := &models.FindOrgMembersOpts{
|
2020-01-24 19:00:29 +00:00
|
|
|
OrgID: ctx.Org.Organization.ID,
|
|
|
|
PublicOnly: publicOnly,
|
|
|
|
ListOptions: utils.GetListOptions(ctx),
|
2021-08-12 12:43:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
count, err := models.CountOrgMembers(opts)
|
|
|
|
if err != nil {
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
members, _, err := models.FindOrgMembers(opts)
|
2019-12-06 05:34:54 +00:00
|
|
|
if err != nil {
|
2021-08-12 12:43:08 +00:00
|
|
|
ctx.InternalServerError(err)
|
2019-12-06 05:34:54 +00:00
|
|
|
return
|
2017-01-20 02:31:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
apiMembers := make([]*api.User, len(members))
|
|
|
|
for i, member := range members {
|
2021-03-27 16:45:26 +00:00
|
|
|
apiMembers[i] = convert.ToUser(member, ctx.User)
|
2017-01-20 02:31:46 +00:00
|
|
|
}
|
2020-06-21 08:22:06 +00:00
|
|
|
|
2021-08-12 12:43:08 +00:00
|
|
|
ctx.SetTotalCountHeader(count)
|
2019-12-20 17:07:12 +00:00
|
|
|
ctx.JSON(http.StatusOK, apiMembers)
|
2017-01-20 02:31:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListMembers list an organization's members
|
|
|
|
func ListMembers(ctx *context.APIContext) {
|
2017-11-13 07:02:25 +00:00
|
|
|
// swagger:operation GET /orgs/{org}/members organization orgListMembers
|
|
|
|
// ---
|
|
|
|
// summary: List an organization's members
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: org
|
|
|
|
// in: path
|
|
|
|
// description: name of the organization
|
|
|
|
// type: string
|
|
|
|
// required: true
|
2020-01-24 19:00:29 +00:00
|
|
|
// - name: page
|
|
|
|
// in: query
|
|
|
|
// description: page number of results to return (1-based)
|
|
|
|
// type: integer
|
|
|
|
// - name: limit
|
|
|
|
// in: query
|
2020-06-09 04:57:38 +00:00
|
|
|
// description: page size of results
|
2020-01-24 19:00:29 +00:00
|
|
|
// type: integer
|
2017-11-13 07:02:25 +00:00
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/UserList"
|
2019-12-20 17:07:12 +00:00
|
|
|
|
2017-12-21 07:43:26 +00:00
|
|
|
publicOnly := true
|
|
|
|
if ctx.User != nil {
|
|
|
|
isMember, err := ctx.Org.Organization.IsOrgMember(ctx.User.ID)
|
|
|
|
if err != nil {
|
2019-12-20 17:07:12 +00:00
|
|
|
ctx.Error(http.StatusInternalServerError, "IsOrgMember", err)
|
2017-12-21 07:43:26 +00:00
|
|
|
return
|
|
|
|
}
|
2020-03-28 19:45:55 +00:00
|
|
|
publicOnly = !isMember && !ctx.User.IsAdmin
|
2017-12-21 07:43:26 +00:00
|
|
|
}
|
2017-06-07 16:10:35 +00:00
|
|
|
listMembers(ctx, publicOnly)
|
2017-01-20 02:31:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListPublicMembers list an organization's public members
|
|
|
|
func ListPublicMembers(ctx *context.APIContext) {
|
2017-11-13 07:02:25 +00:00
|
|
|
// swagger:operation GET /orgs/{org}/public_members organization orgListPublicMembers
|
|
|
|
// ---
|
|
|
|
// summary: List an organization's public members
|
|
|
|
// parameters:
|
|
|
|
// - name: org
|
|
|
|
// in: path
|
|
|
|
// description: name of the organization
|
|
|
|
// type: string
|
|
|
|
// required: true
|
2020-01-24 19:00:29 +00:00
|
|
|
// - name: page
|
|
|
|
// in: query
|
|
|
|
// description: page number of results to return (1-based)
|
|
|
|
// type: integer
|
|
|
|
// - name: limit
|
|
|
|
// in: query
|
2020-06-09 04:57:38 +00:00
|
|
|
// description: page size of results
|
2020-01-24 19:00:29 +00:00
|
|
|
// type: integer
|
2017-11-13 07:02:25 +00:00
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/UserList"
|
2019-12-20 17:07:12 +00:00
|
|
|
|
2017-01-20 02:31:46 +00:00
|
|
|
listMembers(ctx, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsMember check if a user is a member of an organization
|
|
|
|
func IsMember(ctx *context.APIContext) {
|
2017-11-13 07:02:25 +00:00
|
|
|
// swagger:operation GET /orgs/{org}/members/{username} organization orgIsMember
|
|
|
|
// ---
|
|
|
|
// summary: Check if a user is a member of an organization
|
|
|
|
// parameters:
|
|
|
|
// - name: org
|
|
|
|
// in: path
|
|
|
|
// description: name of the organization
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: username
|
|
|
|
// in: path
|
|
|
|
// description: username of the user
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "204":
|
|
|
|
// description: user is a member
|
2019-12-20 17:07:12 +00:00
|
|
|
// "302":
|
|
|
|
// description: redirection to /orgs/{org}/public_members/{username}
|
2017-11-13 07:02:25 +00:00
|
|
|
// "404":
|
|
|
|
// description: user is not a member
|
2019-12-20 17:07:12 +00:00
|
|
|
|
2017-01-20 02:31:46 +00:00
|
|
|
userToCheck := user.GetUserByParams(ctx)
|
2017-06-07 16:10:35 +00:00
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
2017-12-21 07:43:26 +00:00
|
|
|
if ctx.User != nil {
|
|
|
|
userIsMember, err := ctx.Org.Organization.IsOrgMember(ctx.User.ID)
|
|
|
|
if err != nil {
|
2019-12-20 17:07:12 +00:00
|
|
|
ctx.Error(http.StatusInternalServerError, "IsOrgMember", err)
|
2017-12-21 07:43:26 +00:00
|
|
|
return
|
2020-02-09 21:54:53 +00:00
|
|
|
} else if userIsMember || ctx.User.IsAdmin {
|
2018-07-05 07:14:56 +00:00
|
|
|
userToCheckIsMember, err := ctx.Org.Organization.IsOrgMember(userToCheck.ID)
|
2017-12-21 07:43:26 +00:00
|
|
|
if err != nil {
|
2019-12-20 17:07:12 +00:00
|
|
|
ctx.Error(http.StatusInternalServerError, "IsOrgMember", err)
|
2017-12-21 07:43:26 +00:00
|
|
|
} else if userToCheckIsMember {
|
2019-12-20 17:07:12 +00:00
|
|
|
ctx.Status(http.StatusNoContent)
|
2017-12-21 07:43:26 +00:00
|
|
|
} else {
|
2019-03-19 02:29:43 +00:00
|
|
|
ctx.NotFound()
|
2017-12-21 07:43:26 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
} else if ctx.User.ID == userToCheck.ID {
|
2019-03-19 02:29:43 +00:00
|
|
|
ctx.NotFound()
|
2017-12-21 07:43:26 +00:00
|
|
|
return
|
2017-01-20 02:31:46 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-21 07:43:26 +00:00
|
|
|
|
2021-11-16 18:18:25 +00:00
|
|
|
redirectURL := setting.AppSubURL + "/api/v1/orgs/" + url.PathEscape(ctx.Org.Organization.Name) + "/public_members/" + url.PathEscape(userToCheck.Name)
|
2017-12-21 07:43:26 +00:00
|
|
|
ctx.Redirect(redirectURL, 302)
|
2017-01-20 02:31:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsPublicMember check if a user is a public member of an organization
|
|
|
|
func IsPublicMember(ctx *context.APIContext) {
|
2017-11-13 07:02:25 +00:00
|
|
|
// swagger:operation GET /orgs/{org}/public_members/{username} organization orgIsPublicMember
|
|
|
|
// ---
|
|
|
|
// summary: Check if a user is a public member of an organization
|
|
|
|
// parameters:
|
|
|
|
// - name: org
|
|
|
|
// in: path
|
|
|
|
// description: name of the organization
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: username
|
|
|
|
// in: path
|
|
|
|
// description: username of the user
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "204":
|
|
|
|
// description: user is a public member
|
|
|
|
// "404":
|
|
|
|
// description: user is not a public member
|
2019-12-20 17:07:12 +00:00
|
|
|
|
2017-01-20 02:31:46 +00:00
|
|
|
userToCheck := user.GetUserByParams(ctx)
|
2017-06-07 16:10:35 +00:00
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
2021-11-22 15:21:55 +00:00
|
|
|
is, err := models.IsPublicMembership(ctx.Org.Organization.ID, userToCheck.ID)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "IsPublicMembership", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if is {
|
2019-12-20 17:07:12 +00:00
|
|
|
ctx.Status(http.StatusNoContent)
|
2017-01-20 02:31:46 +00:00
|
|
|
} else {
|
2019-03-19 02:29:43 +00:00
|
|
|
ctx.NotFound()
|
2017-01-20 02:31:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PublicizeMember make a member's membership public
|
|
|
|
func PublicizeMember(ctx *context.APIContext) {
|
2017-11-13 07:02:25 +00:00
|
|
|
// swagger:operation PUT /orgs/{org}/public_members/{username} organization orgPublicizeMember
|
|
|
|
// ---
|
|
|
|
// summary: Publicize a user's membership
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: org
|
|
|
|
// in: path
|
|
|
|
// description: name of the organization
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: username
|
|
|
|
// in: path
|
|
|
|
// description: username of the user
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "204":
|
|
|
|
// description: membership publicized
|
2019-12-20 17:07:12 +00:00
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/forbidden"
|
|
|
|
|
2017-01-20 02:31:46 +00:00
|
|
|
userToPublicize := user.GetUserByParams(ctx)
|
2017-06-07 16:10:35 +00:00
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
2017-01-20 02:31:46 +00:00
|
|
|
if userToPublicize.ID != ctx.User.ID {
|
2019-12-20 17:07:12 +00:00
|
|
|
ctx.Error(http.StatusForbidden, "", "Cannot publicize another member")
|
2017-01-20 02:31:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
err := models.ChangeOrgUserStatus(ctx.Org.Organization.ID, userToPublicize.ID, true)
|
|
|
|
if err != nil {
|
2019-12-20 17:07:12 +00:00
|
|
|
ctx.Error(http.StatusInternalServerError, "ChangeOrgUserStatus", err)
|
2017-01-20 02:31:46 +00:00
|
|
|
return
|
|
|
|
}
|
2019-12-20 17:07:12 +00:00
|
|
|
ctx.Status(http.StatusNoContent)
|
2017-01-20 02:31:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ConcealMember make a member's membership not public
|
|
|
|
func ConcealMember(ctx *context.APIContext) {
|
2017-11-13 07:02:25 +00:00
|
|
|
// swagger:operation DELETE /orgs/{org}/public_members/{username} organization orgConcealMember
|
|
|
|
// ---
|
|
|
|
// summary: Conceal a user's membership
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: org
|
|
|
|
// in: path
|
|
|
|
// description: name of the organization
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: username
|
|
|
|
// in: path
|
|
|
|
// description: username of the user
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "204":
|
|
|
|
// "$ref": "#/responses/empty"
|
2019-12-20 17:07:12 +00:00
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/forbidden"
|
|
|
|
|
2017-01-20 02:31:46 +00:00
|
|
|
userToConceal := user.GetUserByParams(ctx)
|
2017-06-07 16:10:35 +00:00
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
2017-01-20 02:31:46 +00:00
|
|
|
if userToConceal.ID != ctx.User.ID {
|
2019-12-20 17:07:12 +00:00
|
|
|
ctx.Error(http.StatusForbidden, "", "Cannot conceal another member")
|
2017-01-20 02:31:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
err := models.ChangeOrgUserStatus(ctx.Org.Organization.ID, userToConceal.ID, false)
|
|
|
|
if err != nil {
|
2019-12-20 17:07:12 +00:00
|
|
|
ctx.Error(http.StatusInternalServerError, "ChangeOrgUserStatus", err)
|
2017-01-20 02:31:46 +00:00
|
|
|
return
|
|
|
|
}
|
2019-12-20 17:07:12 +00:00
|
|
|
ctx.Status(http.StatusNoContent)
|
2017-01-20 02:31:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteMember remove a member from an organization
|
|
|
|
func DeleteMember(ctx *context.APIContext) {
|
2017-11-13 07:02:25 +00:00
|
|
|
// swagger:operation DELETE /orgs/{org}/members/{username} organization orgDeleteMember
|
|
|
|
// ---
|
|
|
|
// summary: Remove a member from an organization
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: org
|
|
|
|
// in: path
|
|
|
|
// description: name of the organization
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: username
|
|
|
|
// in: path
|
|
|
|
// description: username of the user
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "204":
|
|
|
|
// description: member removed
|
2019-12-20 17:07:12 +00:00
|
|
|
|
2017-06-07 16:10:35 +00:00
|
|
|
member := user.GetUserByParams(ctx)
|
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := ctx.Org.Organization.RemoveMember(member.ID); err != nil {
|
2019-12-20 17:07:12 +00:00
|
|
|
ctx.Error(http.StatusInternalServerError, "RemoveMember", err)
|
2017-01-20 02:31:46 +00:00
|
|
|
}
|
2019-12-20 17:07:12 +00:00
|
|
|
ctx.Status(http.StatusNoContent)
|
2017-01-20 02:31:46 +00:00
|
|
|
}
|