2017-01-20 08:01:46 +05:30
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2017-01-20 08:01:46 +05:30
|
|
|
|
|
|
|
package org
|
|
|
|
|
|
|
|
import (
|
2019-12-20 22:37:12 +05:30
|
|
|
"net/http"
|
2021-11-16 23:48:25 +05:30
|
|
|
"net/url"
|
2017-01-20 08:01:46 +05:30
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2022-03-29 11:59:02 +05:30
|
|
|
"code.gitea.io/gitea/models/organization"
|
2017-01-20 08:01:46 +05:30
|
|
|
"code.gitea.io/gitea/modules/context"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-08-23 22:10:30 +05:30
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2017-01-20 08:01:46 +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"
|
2022-12-29 08:27:15 +05:30
|
|
|
"code.gitea.io/gitea/services/convert"
|
2017-01-20 08:01:46 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// listMembers list an organization's members
|
|
|
|
func listMembers(ctx *context.APIContext, publicOnly bool) {
|
2022-03-29 11:59:02 +05:30
|
|
|
opts := &organization.FindOrgMembersOpts{
|
2020-01-25 00:30:29 +05:30
|
|
|
OrgID: ctx.Org.Organization.ID,
|
|
|
|
PublicOnly: publicOnly,
|
|
|
|
ListOptions: utils.GetListOptions(ctx),
|
2021-08-12 18:13:08 +05:30
|
|
|
}
|
|
|
|
|
2022-03-29 11:59:02 +05:30
|
|
|
count, err := organization.CountOrgMembers(opts)
|
2021-08-12 18:13:08 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-29 11:59:02 +05:30
|
|
|
members, _, err := organization.FindOrgMembers(opts)
|
2019-12-06 11:04:54 +05:30
|
|
|
if err != nil {
|
2021-08-12 18:13:08 +05:30
|
|
|
ctx.InternalServerError(err)
|
2019-12-06 11:04:54 +05:30
|
|
|
return
|
2017-01-20 08:01:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
apiMembers := make([]*api.User, len(members))
|
|
|
|
for i, member := range members {
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 19:07:34 +05:30
|
|
|
apiMembers[i] = convert.ToUser(ctx, member, ctx.Doer)
|
2017-01-20 08:01:46 +05:30
|
|
|
}
|
2020-06-21 13:52:06 +05:30
|
|
|
|
2021-08-12 18:13:08 +05:30
|
|
|
ctx.SetTotalCountHeader(count)
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.JSON(http.StatusOK, apiMembers)
|
2017-01-20 08:01:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// ListMembers list an organization's members
|
|
|
|
func ListMembers(ctx *context.APIContext) {
|
2017-11-13 12:32:25 +05:30
|
|
|
// 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-25 00:30:29 +05:30
|
|
|
// - 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
|
2020-01-25 00:30:29 +05:30
|
|
|
// type: integer
|
2017-11-13 12:32:25 +05:30
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/UserList"
|
2023-09-13 08:07:54 +05:30
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2019-12-20 22:37:12 +05:30
|
|
|
|
2017-12-21 13:13:26 +05:30
|
|
|
publicOnly := true
|
2022-03-22 12:33:22 +05:30
|
|
|
if ctx.Doer != nil {
|
|
|
|
isMember, err := ctx.Org.Organization.IsOrgMember(ctx.Doer.ID)
|
2017-12-21 13:13:26 +05:30
|
|
|
if err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "IsOrgMember", err)
|
2017-12-21 13:13:26 +05:30
|
|
|
return
|
|
|
|
}
|
2022-03-22 12:33:22 +05:30
|
|
|
publicOnly = !isMember && !ctx.Doer.IsAdmin
|
2017-12-21 13:13:26 +05:30
|
|
|
}
|
2017-06-07 21:40:35 +05:30
|
|
|
listMembers(ctx, publicOnly)
|
2017-01-20 08:01:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// ListPublicMembers list an organization's public members
|
|
|
|
func ListPublicMembers(ctx *context.APIContext) {
|
2017-11-13 12:32:25 +05:30
|
|
|
// 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-25 00:30:29 +05:30
|
|
|
// - 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
|
2020-01-25 00:30:29 +05:30
|
|
|
// type: integer
|
2017-11-13 12:32:25 +05:30
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/UserList"
|
2023-09-13 08:07:54 +05:30
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2019-12-20 22:37:12 +05:30
|
|
|
|
2017-01-20 08:01:46 +05:30
|
|
|
listMembers(ctx, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsMember check if a user is a member of an organization
|
|
|
|
func IsMember(ctx *context.APIContext) {
|
2017-11-13 12:32:25 +05:30
|
|
|
// 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
|
2022-03-23 10:24:07 +05:30
|
|
|
// "303":
|
2019-12-20 22:37:12 +05:30
|
|
|
// description: redirection to /orgs/{org}/public_members/{username}
|
2017-11-13 12:32:25 +05:30
|
|
|
// "404":
|
|
|
|
// description: user is not a member
|
2019-12-20 22:37:12 +05:30
|
|
|
|
2017-01-20 08:01:46 +05:30
|
|
|
userToCheck := user.GetUserByParams(ctx)
|
2017-06-07 21:40:35 +05:30
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
2022-03-22 12:33:22 +05:30
|
|
|
if ctx.Doer != nil {
|
|
|
|
userIsMember, err := ctx.Org.Organization.IsOrgMember(ctx.Doer.ID)
|
2017-12-21 13:13:26 +05:30
|
|
|
if err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "IsOrgMember", err)
|
2017-12-21 13:13:26 +05:30
|
|
|
return
|
2022-03-22 12:33:22 +05:30
|
|
|
} else if userIsMember || ctx.Doer.IsAdmin {
|
2018-07-05 12:44:56 +05:30
|
|
|
userToCheckIsMember, err := ctx.Org.Organization.IsOrgMember(userToCheck.ID)
|
2017-12-21 13:13:26 +05:30
|
|
|
if err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "IsOrgMember", err)
|
2017-12-21 13:13:26 +05:30
|
|
|
} else if userToCheckIsMember {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Status(http.StatusNoContent)
|
2017-12-21 13:13:26 +05:30
|
|
|
} else {
|
2019-03-19 07:59:43 +05:30
|
|
|
ctx.NotFound()
|
2017-12-21 13:13:26 +05:30
|
|
|
}
|
|
|
|
return
|
2022-03-22 12:33:22 +05:30
|
|
|
} else if ctx.Doer.ID == userToCheck.ID {
|
2019-03-19 07:59:43 +05:30
|
|
|
ctx.NotFound()
|
2017-12-21 13:13:26 +05:30
|
|
|
return
|
2017-01-20 08:01:46 +05:30
|
|
|
}
|
|
|
|
}
|
2017-12-21 13:13:26 +05:30
|
|
|
|
2021-11-16 23:48:25 +05:30
|
|
|
redirectURL := setting.AppSubURL + "/api/v1/orgs/" + url.PathEscape(ctx.Org.Organization.Name) + "/public_members/" + url.PathEscape(userToCheck.Name)
|
2022-03-23 10:24:07 +05:30
|
|
|
ctx.Redirect(redirectURL)
|
2017-01-20 08:01:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// IsPublicMember check if a user is a public member of an organization
|
|
|
|
func IsPublicMember(ctx *context.APIContext) {
|
2017-11-13 12:32:25 +05:30
|
|
|
// 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 22:37:12 +05:30
|
|
|
|
2017-01-20 08:01:46 +05:30
|
|
|
userToCheck := user.GetUserByParams(ctx)
|
2017-06-07 21:40:35 +05:30
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
2022-03-29 11:59:02 +05:30
|
|
|
is, err := organization.IsPublicMembership(ctx.Org.Organization.ID, userToCheck.ID)
|
2021-11-22 20:51:55 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "IsPublicMembership", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if is {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Status(http.StatusNoContent)
|
2017-01-20 08:01:46 +05:30
|
|
|
} else {
|
2019-03-19 07:59:43 +05:30
|
|
|
ctx.NotFound()
|
2017-01-20 08:01:46 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PublicizeMember make a member's membership public
|
|
|
|
func PublicizeMember(ctx *context.APIContext) {
|
2017-11-13 12:32:25 +05:30
|
|
|
// 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 22:37:12 +05:30
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/forbidden"
|
2023-09-13 08:07:54 +05:30
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2019-12-20 22:37:12 +05:30
|
|
|
|
2017-01-20 08:01:46 +05:30
|
|
|
userToPublicize := user.GetUserByParams(ctx)
|
2017-06-07 21:40:35 +05:30
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
2022-03-22 12:33:22 +05:30
|
|
|
if userToPublicize.ID != ctx.Doer.ID {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusForbidden, "", "Cannot publicize another member")
|
2017-01-20 08:01:46 +05:30
|
|
|
return
|
|
|
|
}
|
2022-03-29 11:59:02 +05:30
|
|
|
err := organization.ChangeOrgUserStatus(ctx.Org.Organization.ID, userToPublicize.ID, true)
|
2017-01-20 08:01:46 +05:30
|
|
|
if err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "ChangeOrgUserStatus", err)
|
2017-01-20 08:01:46 +05:30
|
|
|
return
|
|
|
|
}
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Status(http.StatusNoContent)
|
2017-01-20 08:01:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// ConcealMember make a member's membership not public
|
|
|
|
func ConcealMember(ctx *context.APIContext) {
|
2017-11-13 12:32:25 +05:30
|
|
|
// 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 22:37:12 +05:30
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/forbidden"
|
2023-09-13 08:07:54 +05:30
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2019-12-20 22:37:12 +05:30
|
|
|
|
2017-01-20 08:01:46 +05:30
|
|
|
userToConceal := user.GetUserByParams(ctx)
|
2017-06-07 21:40:35 +05:30
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
2022-03-22 12:33:22 +05:30
|
|
|
if userToConceal.ID != ctx.Doer.ID {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusForbidden, "", "Cannot conceal another member")
|
2017-01-20 08:01:46 +05:30
|
|
|
return
|
|
|
|
}
|
2022-03-29 11:59:02 +05:30
|
|
|
err := organization.ChangeOrgUserStatus(ctx.Org.Organization.ID, userToConceal.ID, false)
|
2017-01-20 08:01:46 +05:30
|
|
|
if err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "ChangeOrgUserStatus", err)
|
2017-01-20 08:01:46 +05:30
|
|
|
return
|
|
|
|
}
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Status(http.StatusNoContent)
|
2017-01-20 08:01:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteMember remove a member from an organization
|
|
|
|
func DeleteMember(ctx *context.APIContext) {
|
2017-11-13 12:32:25 +05:30
|
|
|
// 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
|
2023-09-13 08:07:54 +05:30
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2019-12-20 22:37:12 +05:30
|
|
|
|
2017-06-07 21:40:35 +05:30
|
|
|
member := user.GetUserByParams(ctx)
|
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
2022-03-29 11:59:02 +05:30
|
|
|
if err := models.RemoveOrgUser(ctx.Org.Organization.ID, member.ID); err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "RemoveOrgUser", err)
|
2017-01-20 08:01:46 +05:30
|
|
|
}
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Status(http.StatusNoContent)
|
2017-01-20 08:01:46 +05:30
|
|
|
}
|