本站源代码
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

288 lines
7.4KB

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