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

211 lines
5.2 KiB

  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. api "code.gitea.io/sdk/gitea"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/setting"
  11. "code.gitea.io/gitea/routers/api/v1/user"
  12. )
  13. // listMembers list an organization's members
  14. func listMembers(ctx *context.APIContext, publicOnly bool) {
  15. var members []*models.User
  16. if publicOnly {
  17. orgUsers, err := models.GetOrgUsersByOrgID(ctx.Org.Organization.ID)
  18. if err != nil {
  19. ctx.Error(500, "GetOrgUsersByOrgID", err)
  20. return
  21. }
  22. memberIDs := make([]int64, 0, len(orgUsers))
  23. for _, orgUser := range orgUsers {
  24. if orgUser.IsPublic {
  25. memberIDs = append(memberIDs, orgUser.UID)
  26. }
  27. }
  28. if members, err = models.GetUsersByIDs(memberIDs); err != nil {
  29. ctx.Error(500, "GetUsersByIDs", err)
  30. return
  31. }
  32. } else {
  33. if err := ctx.Org.Organization.GetMembers(); err != nil {
  34. ctx.Error(500, "GetMembers", err)
  35. return
  36. }
  37. members = ctx.Org.Organization.Members
  38. }
  39. apiMembers := make([]*api.User, len(members))
  40. for i, member := range members {
  41. apiMembers[i] = member.APIFormat()
  42. }
  43. ctx.JSON(200, apiMembers)
  44. }
  45. // ListMembers list an organization's members
  46. func ListMembers(ctx *context.APIContext) {
  47. // swagger:route GET /orgs/{orgname}/members organization orgListMembers
  48. //
  49. // Produces:
  50. // - application/json
  51. //
  52. // Responses:
  53. // 200: UserList
  54. // 500: error
  55. publicOnly := ctx.User == nil || !ctx.Org.Organization.IsOrgMember(ctx.User.ID)
  56. listMembers(ctx, publicOnly)
  57. }
  58. // ListPublicMembers list an organization's public members
  59. func ListPublicMembers(ctx *context.APIContext) {
  60. // swagger:route GET /orgs/{orgname}/public_members organization orgListPublicMembers
  61. //
  62. // Produces:
  63. // - application/json
  64. //
  65. // Responses:
  66. // 200: UserList
  67. // 500: error
  68. listMembers(ctx, true)
  69. }
  70. // IsMember check if a user is a member of an organization
  71. func IsMember(ctx *context.APIContext) {
  72. // swagger:route GET /orgs/{orgname}/members/{username} organization orgIsMember
  73. //
  74. // Produces:
  75. // - application/json
  76. //
  77. // Responses:
  78. // 204: empty
  79. // 302: redirect
  80. // 404: notFound
  81. userToCheck := user.GetUserByParams(ctx)
  82. if ctx.Written() {
  83. return
  84. }
  85. if ctx.User != nil && ctx.Org.Organization.IsOrgMember(ctx.User.ID) {
  86. if ctx.Org.Organization.IsOrgMember(userToCheck.ID) {
  87. ctx.Status(204)
  88. } else {
  89. ctx.Status(404)
  90. }
  91. } else if ctx.User != nil && ctx.User.ID == userToCheck.ID {
  92. ctx.Status(404)
  93. } else {
  94. redirectURL := fmt.Sprintf("%sapi/v1/orgs/%s/public_members/%s",
  95. setting.AppURL, ctx.Org.Organization.Name, userToCheck.Name)
  96. ctx.Redirect(redirectURL, 302)
  97. }
  98. }
  99. // IsPublicMember check if a user is a public member of an organization
  100. func IsPublicMember(ctx *context.APIContext) {
  101. // swagger:route GET /orgs/{orgname}/public_members/{username} organization orgIsPublicMember
  102. //
  103. // Produces:
  104. // - application/json
  105. //
  106. // Responses:
  107. // 204: empty
  108. // 404: notFound
  109. userToCheck := user.GetUserByParams(ctx)
  110. if ctx.Written() {
  111. return
  112. }
  113. if userToCheck.IsPublicMember(ctx.Org.Organization.ID) {
  114. ctx.Status(204)
  115. } else {
  116. ctx.Status(404)
  117. }
  118. }
  119. // PublicizeMember make a member's membership public
  120. func PublicizeMember(ctx *context.APIContext) {
  121. // swagger:route PUT /orgs/{orgname}/public_members/{username} organization orgPublicizeMember
  122. //
  123. // Produces:
  124. // - application/json
  125. //
  126. // Responses:
  127. // 204: empty
  128. // 403: forbidden
  129. // 500: error
  130. userToPublicize := user.GetUserByParams(ctx)
  131. if ctx.Written() {
  132. return
  133. }
  134. if userToPublicize.ID != ctx.User.ID {
  135. ctx.Error(403, "", "Cannot publicize another member")
  136. return
  137. }
  138. err := models.ChangeOrgUserStatus(ctx.Org.Organization.ID, userToPublicize.ID, true)
  139. if err != nil {
  140. ctx.Error(500, "ChangeOrgUserStatus", err)
  141. return
  142. }
  143. ctx.Status(204)
  144. }
  145. // ConcealMember make a member's membership not public
  146. func ConcealMember(ctx *context.APIContext) {
  147. // swagger:route DELETE /orgs/{orgname}/public_members/{username} organization orgConcealMember
  148. //
  149. // Produces:
  150. // - application/json
  151. //
  152. // Responses:
  153. // 204: empty
  154. // 403: forbidden
  155. // 500: error
  156. userToConceal := user.GetUserByParams(ctx)
  157. if ctx.Written() {
  158. return
  159. }
  160. if userToConceal.ID != ctx.User.ID {
  161. ctx.Error(403, "", "Cannot conceal another member")
  162. return
  163. }
  164. err := models.ChangeOrgUserStatus(ctx.Org.Organization.ID, userToConceal.ID, false)
  165. if err != nil {
  166. ctx.Error(500, "ChangeOrgUserStatus", err)
  167. return
  168. }
  169. ctx.Status(204)
  170. }
  171. // DeleteMember remove a member from an organization
  172. func DeleteMember(ctx *context.APIContext) {
  173. // swagger:route DELETE /orgs/{orgname}/members/{username} organization orgDeleteMember
  174. //
  175. // Produces:
  176. // - application/json
  177. //
  178. // Responses:
  179. // 204: empty
  180. // 500: error
  181. member := user.GetUserByParams(ctx)
  182. if ctx.Written() {
  183. return
  184. }
  185. if err := ctx.Org.Organization.RemoveMember(member.ID); err != nil {
  186. ctx.Error(500, "RemoveMember", err)
  187. }
  188. ctx.Status(204)
  189. }