members.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. log "unknwon.dev/clog/v2"
  8. "gogs.io/gogs/internal/conf"
  9. "gogs.io/gogs/internal/context"
  10. "gogs.io/gogs/internal/db"
  11. )
  12. const (
  13. MEMBERS = "org/member/members"
  14. MEMBER_INVITE = "org/member/invite"
  15. )
  16. func Members(c *context.Context) {
  17. org := c.Org.Organization
  18. c.Data["Title"] = org.FullName
  19. c.Data["PageIsOrgMembers"] = true
  20. if err := org.GetMembers(); err != nil {
  21. c.Error(err, "get members")
  22. return
  23. }
  24. c.Data["Members"] = org.Members
  25. c.Success(MEMBERS)
  26. }
  27. func MembersAction(c *context.Context) {
  28. uid := com.StrTo(c.Query("uid")).MustInt64()
  29. if uid == 0 {
  30. c.Redirect(c.Org.OrgLink + "/members")
  31. return
  32. }
  33. org := c.Org.Organization
  34. var err error
  35. switch c.Params(":action") {
  36. case "private":
  37. if c.User.ID != uid && !c.Org.IsOwner {
  38. c.NotFound()
  39. return
  40. }
  41. err = db.ChangeOrgUserStatus(org.ID, uid, false)
  42. case "public":
  43. if c.User.ID != uid && !c.Org.IsOwner {
  44. c.NotFound()
  45. return
  46. }
  47. err = db.ChangeOrgUserStatus(org.ID, uid, true)
  48. case "remove":
  49. if !c.Org.IsOwner {
  50. c.NotFound()
  51. return
  52. }
  53. err = org.RemoveMember(uid)
  54. if db.IsErrLastOrgOwner(err) {
  55. c.Flash.Error(c.Tr("form.last_org_owner"))
  56. c.Redirect(c.Org.OrgLink + "/members")
  57. return
  58. }
  59. case "leave":
  60. err = org.RemoveMember(c.User.ID)
  61. if db.IsErrLastOrgOwner(err) {
  62. c.Flash.Error(c.Tr("form.last_org_owner"))
  63. c.Redirect(c.Org.OrgLink + "/members")
  64. return
  65. }
  66. }
  67. if err != nil {
  68. log.Error("Action(%s): %v", c.Params(":action"), err)
  69. c.JSONSuccess( map[string]interface{}{
  70. "ok": false,
  71. "err": err.Error(),
  72. })
  73. return
  74. }
  75. if c.Params(":action") != "leave" {
  76. c.Redirect(c.Org.OrgLink + "/members")
  77. } else {
  78. c.Redirect(conf.Server.Subpath + "/")
  79. }
  80. }
  81. func Invitation(c *context.Context) {
  82. org := c.Org.Organization
  83. c.Data["Title"] = org.FullName
  84. c.Data["PageIsOrgMembers"] = true
  85. if c.Req.Method == "POST" {
  86. uname := c.Query("uname")
  87. u, err := db.GetUserByName(uname)
  88. if err != nil {
  89. if db.IsErrUserNotExist(err) {
  90. c.Flash.Error(c.Tr("form.user_not_exist"))
  91. c.Redirect(c.Org.OrgLink + "/invitations/new")
  92. } else {
  93. c.Error(err, "get user by name")
  94. }
  95. return
  96. }
  97. if err = org.AddMember(u.ID); err != nil {
  98. c.Error(err, "add member")
  99. return
  100. }
  101. log.Trace("New member added(%s): %s", org.Name, u.Name)
  102. c.Redirect(c.Org.OrgLink + "/members")
  103. return
  104. }
  105. c.Success(MEMBER_INVITE)
  106. }