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.

128 lines
3.0 KiB

10 years ago
10 years ago
10 years ago
  1. // Copyright 2014 The Gogs 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. "github.com/Unknwon/com"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/base"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/setting"
  12. )
  13. const (
  14. // tplMembers template for organization members page
  15. tplMembers base.TplName = "org/member/members"
  16. // tplMemberInvite template for orgnization invite page
  17. tplMemberInvite base.TplName = "org/member/invite"
  18. )
  19. // Members render orgnization users page
  20. func Members(ctx *context.Context) {
  21. org := ctx.Org.Organization
  22. ctx.Data["Title"] = org.FullName
  23. ctx.Data["PageIsOrgMembers"] = true
  24. if err := org.GetMembers(); err != nil {
  25. ctx.Handle(500, "GetMembers", err)
  26. return
  27. }
  28. ctx.Data["Members"] = org.Members
  29. ctx.HTML(200, tplMembers)
  30. }
  31. // MembersAction response for operation to a member of orgnization
  32. func MembersAction(ctx *context.Context) {
  33. uid := com.StrTo(ctx.Query("uid")).MustInt64()
  34. if uid == 0 {
  35. ctx.Redirect(ctx.Org.OrgLink + "/members")
  36. return
  37. }
  38. org := ctx.Org.Organization
  39. var err error
  40. switch ctx.Params(":action") {
  41. case "private":
  42. if ctx.User.ID != uid && !ctx.Org.IsOwner {
  43. ctx.Error(404)
  44. return
  45. }
  46. err = models.ChangeOrgUserStatus(org.ID, uid, false)
  47. case "public":
  48. if ctx.User.ID != uid && !ctx.Org.IsOwner {
  49. ctx.Error(404)
  50. return
  51. }
  52. err = models.ChangeOrgUserStatus(org.ID, uid, true)
  53. case "remove":
  54. if !ctx.Org.IsOwner {
  55. ctx.Error(404)
  56. return
  57. }
  58. err = org.RemoveMember(uid)
  59. if models.IsErrLastOrgOwner(err) {
  60. ctx.Flash.Error(ctx.Tr("form.last_org_owner"))
  61. ctx.Redirect(ctx.Org.OrgLink + "/members")
  62. return
  63. }
  64. case "leave":
  65. err = org.RemoveMember(ctx.User.ID)
  66. if models.IsErrLastOrgOwner(err) {
  67. ctx.Flash.Error(ctx.Tr("form.last_org_owner"))
  68. ctx.Redirect(ctx.Org.OrgLink + "/members")
  69. return
  70. }
  71. }
  72. if err != nil {
  73. log.Error(4, "Action(%s): %v", ctx.Params(":action"), err)
  74. ctx.JSON(200, map[string]interface{}{
  75. "ok": false,
  76. "err": err.Error(),
  77. })
  78. return
  79. }
  80. if ctx.Params(":action") != "leave" {
  81. ctx.Redirect(ctx.Org.OrgLink + "/members")
  82. } else {
  83. ctx.Redirect(setting.AppSubURL + "/")
  84. }
  85. }
  86. // Invitation render organization invitation page
  87. func Invitation(ctx *context.Context) {
  88. org := ctx.Org.Organization
  89. ctx.Data["Title"] = org.FullName
  90. ctx.Data["PageIsOrgMembers"] = true
  91. if ctx.Req.Method == "POST" {
  92. uname := ctx.Query("uname")
  93. u, err := models.GetUserByName(uname)
  94. if err != nil {
  95. if models.IsErrUserNotExist(err) {
  96. ctx.Flash.Error(ctx.Tr("form.user_not_exist"))
  97. ctx.Redirect(ctx.Org.OrgLink + "/invitations/new")
  98. } else {
  99. ctx.Handle(500, " GetUserByName", err)
  100. }
  101. return
  102. }
  103. if err = org.AddMember(u.ID); err != nil {
  104. ctx.Handle(500, " AddMember", err)
  105. return
  106. }
  107. log.Trace("New member added(%s): %s", org.Name, u.Name)
  108. ctx.Redirect(ctx.Org.OrgLink + "/members")
  109. return
  110. }
  111. ctx.HTML(200, tplMemberInvite)
  112. }