You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

member.go 7.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package org
  5. import (
  6. "fmt"
  7. "net/http"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/convert"
  11. "code.gitea.io/gitea/modules/setting"
  12. api "code.gitea.io/gitea/modules/structs"
  13. "code.gitea.io/gitea/routers/api/v1/user"
  14. )
  15. // listMembers list an organization's members
  16. func listMembers(ctx *context.APIContext, publicOnly bool) {
  17. var members []*models.User
  18. members, _, err := models.FindOrgMembers(models.FindOrgMembersOpts{
  19. OrgID: ctx.Org.Organization.ID,
  20. PublicOnly: publicOnly,
  21. })
  22. if err != nil {
  23. ctx.Error(http.StatusInternalServerError, "GetUsersByIDs", err)
  24. return
  25. }
  26. apiMembers := make([]*api.User, len(members))
  27. for i, member := range members {
  28. apiMembers[i] = convert.ToUser(member, ctx.IsSigned, ctx.User != nil && ctx.User.IsAdmin)
  29. }
  30. ctx.JSON(http.StatusOK, apiMembers)
  31. }
  32. // ListMembers list an organization's members
  33. func ListMembers(ctx *context.APIContext) {
  34. // swagger:operation GET /orgs/{org}/members organization orgListMembers
  35. // ---
  36. // summary: List an organization's members
  37. // produces:
  38. // - application/json
  39. // parameters:
  40. // - name: org
  41. // in: path
  42. // description: name of the organization
  43. // type: string
  44. // required: true
  45. // responses:
  46. // "200":
  47. // "$ref": "#/responses/UserList"
  48. publicOnly := true
  49. if ctx.User != nil {
  50. isMember, err := ctx.Org.Organization.IsOrgMember(ctx.User.ID)
  51. if err != nil {
  52. ctx.Error(http.StatusInternalServerError, "IsOrgMember", err)
  53. return
  54. }
  55. publicOnly = !isMember
  56. }
  57. listMembers(ctx, publicOnly)
  58. }
  59. // ListPublicMembers list an organization's public members
  60. func ListPublicMembers(ctx *context.APIContext) {
  61. // swagger:operation GET /orgs/{org}/public_members organization orgListPublicMembers
  62. // ---
  63. // summary: List an organization's public members
  64. // parameters:
  65. // - name: org
  66. // in: path
  67. // description: name of the organization
  68. // type: string
  69. // required: true
  70. // produces:
  71. // - application/json
  72. // responses:
  73. // "200":
  74. // "$ref": "#/responses/UserList"
  75. listMembers(ctx, true)
  76. }
  77. // IsMember check if a user is a member of an organization
  78. func IsMember(ctx *context.APIContext) {
  79. // swagger:operation GET /orgs/{org}/members/{username} organization orgIsMember
  80. // ---
  81. // summary: Check if a user is a member of an organization
  82. // parameters:
  83. // - name: org
  84. // in: path
  85. // description: name of the organization
  86. // type: string
  87. // required: true
  88. // - name: username
  89. // in: path
  90. // description: username of the user
  91. // type: string
  92. // required: true
  93. // responses:
  94. // "204":
  95. // description: user is a member
  96. // "302":
  97. // description: redirection to /orgs/{org}/public_members/{username}
  98. // "404":
  99. // description: user is not a member
  100. userToCheck := user.GetUserByParams(ctx)
  101. if ctx.Written() {
  102. return
  103. }
  104. if ctx.User != nil {
  105. userIsMember, err := ctx.Org.Organization.IsOrgMember(ctx.User.ID)
  106. if err != nil {
  107. ctx.Error(http.StatusInternalServerError, "IsOrgMember", err)
  108. return
  109. } else if userIsMember {
  110. userToCheckIsMember, err := ctx.Org.Organization.IsOrgMember(userToCheck.ID)
  111. if err != nil {
  112. ctx.Error(http.StatusInternalServerError, "IsOrgMember", err)
  113. } else if userToCheckIsMember {
  114. ctx.Status(http.StatusNoContent)
  115. } else {
  116. ctx.NotFound()
  117. }
  118. return
  119. } else if ctx.User.ID == userToCheck.ID {
  120. ctx.NotFound()
  121. return
  122. }
  123. }
  124. redirectURL := fmt.Sprintf("%sapi/v1/orgs/%s/public_members/%s",
  125. setting.AppURL, ctx.Org.Organization.Name, userToCheck.Name)
  126. ctx.Redirect(redirectURL, 302)
  127. }
  128. // IsPublicMember check if a user is a public member of an organization
  129. func IsPublicMember(ctx *context.APIContext) {
  130. // swagger:operation GET /orgs/{org}/public_members/{username} organization orgIsPublicMember
  131. // ---
  132. // summary: Check if a user is a public member of an organization
  133. // parameters:
  134. // - name: org
  135. // in: path
  136. // description: name of the organization
  137. // type: string
  138. // required: true
  139. // - name: username
  140. // in: path
  141. // description: username of the user
  142. // type: string
  143. // required: true
  144. // responses:
  145. // "204":
  146. // description: user is a public member
  147. // "404":
  148. // description: user is not a public member
  149. userToCheck := user.GetUserByParams(ctx)
  150. if ctx.Written() {
  151. return
  152. }
  153. if userToCheck.IsPublicMember(ctx.Org.Organization.ID) {
  154. ctx.Status(http.StatusNoContent)
  155. } else {
  156. ctx.NotFound()
  157. }
  158. }
  159. // PublicizeMember make a member's membership public
  160. func PublicizeMember(ctx *context.APIContext) {
  161. // swagger:operation PUT /orgs/{org}/public_members/{username} organization orgPublicizeMember
  162. // ---
  163. // summary: Publicize a user's membership
  164. // produces:
  165. // - application/json
  166. // parameters:
  167. // - name: org
  168. // in: path
  169. // description: name of the organization
  170. // type: string
  171. // required: true
  172. // - name: username
  173. // in: path
  174. // description: username of the user
  175. // type: string
  176. // required: true
  177. // responses:
  178. // "204":
  179. // description: membership publicized
  180. // "403":
  181. // "$ref": "#/responses/forbidden"
  182. userToPublicize := user.GetUserByParams(ctx)
  183. if ctx.Written() {
  184. return
  185. }
  186. if userToPublicize.ID != ctx.User.ID {
  187. ctx.Error(http.StatusForbidden, "", "Cannot publicize another member")
  188. return
  189. }
  190. err := models.ChangeOrgUserStatus(ctx.Org.Organization.ID, userToPublicize.ID, true)
  191. if err != nil {
  192. ctx.Error(http.StatusInternalServerError, "ChangeOrgUserStatus", err)
  193. return
  194. }
  195. ctx.Status(http.StatusNoContent)
  196. }
  197. // ConcealMember make a member's membership not public
  198. func ConcealMember(ctx *context.APIContext) {
  199. // swagger:operation DELETE /orgs/{org}/public_members/{username} organization orgConcealMember
  200. // ---
  201. // summary: Conceal a user's membership
  202. // produces:
  203. // - application/json
  204. // parameters:
  205. // - name: org
  206. // in: path
  207. // description: name of the organization
  208. // type: string
  209. // required: true
  210. // - name: username
  211. // in: path
  212. // description: username of the user
  213. // type: string
  214. // required: true
  215. // responses:
  216. // "204":
  217. // "$ref": "#/responses/empty"
  218. // "403":
  219. // "$ref": "#/responses/forbidden"
  220. userToConceal := user.GetUserByParams(ctx)
  221. if ctx.Written() {
  222. return
  223. }
  224. if userToConceal.ID != ctx.User.ID {
  225. ctx.Error(http.StatusForbidden, "", "Cannot conceal another member")
  226. return
  227. }
  228. err := models.ChangeOrgUserStatus(ctx.Org.Organization.ID, userToConceal.ID, false)
  229. if err != nil {
  230. ctx.Error(http.StatusInternalServerError, "ChangeOrgUserStatus", err)
  231. return
  232. }
  233. ctx.Status(http.StatusNoContent)
  234. }
  235. // DeleteMember remove a member from an organization
  236. func DeleteMember(ctx *context.APIContext) {
  237. // swagger:operation DELETE /orgs/{org}/members/{username} organization orgDeleteMember
  238. // ---
  239. // summary: Remove a member from an organization
  240. // produces:
  241. // - application/json
  242. // parameters:
  243. // - name: org
  244. // in: path
  245. // description: name of the organization
  246. // type: string
  247. // required: true
  248. // - name: username
  249. // in: path
  250. // description: username of the user
  251. // type: string
  252. // required: true
  253. // responses:
  254. // "204":
  255. // description: member removed
  256. member := user.GetUserByParams(ctx)
  257. if ctx.Written() {
  258. return
  259. }
  260. if err := ctx.Org.Organization.RemoveMember(member.ID); err != nil {
  261. ctx.Error(http.StatusInternalServerError, "RemoveMember", err)
  262. }
  263. ctx.Status(http.StatusNoContent)
  264. }