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.

132 lines
3.5 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. listMembers(ctx, !ctx.Org.Organization.IsOrgMember(ctx.User.ID))
  48. }
  49. // ListPublicMembers list an organization's public members
  50. func ListPublicMembers(ctx *context.APIContext) {
  51. listMembers(ctx, true)
  52. }
  53. // IsMember check if a user is a member of an organization
  54. func IsMember(ctx *context.APIContext) {
  55. org := ctx.Org.Organization
  56. requester := ctx.User
  57. userToCheck := user.GetUserByParams(ctx)
  58. if org.IsOrgMember(requester.ID) {
  59. if org.IsOrgMember(userToCheck.ID) {
  60. ctx.Status(204)
  61. } else {
  62. ctx.Status(404)
  63. }
  64. } else if requester.ID == userToCheck.ID {
  65. ctx.Status(404)
  66. } else {
  67. redirectURL := fmt.Sprintf("%sapi/v1/orgs/%s/public_members/%s",
  68. setting.AppURL, org.Name, userToCheck.Name)
  69. ctx.Redirect(redirectURL, 302)
  70. }
  71. }
  72. // IsPublicMember check if a user is a public member of an organization
  73. func IsPublicMember(ctx *context.APIContext) {
  74. userToCheck := user.GetUserByParams(ctx)
  75. if userToCheck.IsPublicMember(ctx.Org.Organization.ID) {
  76. ctx.Status(204)
  77. } else {
  78. ctx.Status(404)
  79. }
  80. }
  81. // PublicizeMember make a member's membership public
  82. func PublicizeMember(ctx *context.APIContext) {
  83. userToPublicize := user.GetUserByParams(ctx)
  84. if userToPublicize.ID != ctx.User.ID {
  85. ctx.Error(403, "", "Cannot publicize another member")
  86. return
  87. }
  88. err := models.ChangeOrgUserStatus(ctx.Org.Organization.ID, userToPublicize.ID, true)
  89. if err != nil {
  90. ctx.Error(500, "ChangeOrgUserStatus", err)
  91. return
  92. }
  93. ctx.Status(204)
  94. }
  95. // ConcealMember make a member's membership not public
  96. func ConcealMember(ctx *context.APIContext) {
  97. userToConceal := user.GetUserByParams(ctx)
  98. if userToConceal.ID != ctx.User.ID {
  99. ctx.Error(403, "", "Cannot conceal another member")
  100. return
  101. }
  102. err := models.ChangeOrgUserStatus(ctx.Org.Organization.ID, userToConceal.ID, false)
  103. if err != nil {
  104. ctx.Error(500, "ChangeOrgUserStatus", err)
  105. return
  106. }
  107. ctx.Status(204)
  108. }
  109. // DeleteMember remove a member from an organization
  110. func DeleteMember(ctx *context.APIContext) {
  111. org := ctx.Org.Organization
  112. memberID := user.GetUserByParams(ctx).ID
  113. if err := org.RemoveMember(memberID); err != nil {
  114. ctx.Error(500, "RemoveMember", err)
  115. }
  116. ctx.Status(204)
  117. }