members.go 2.6 KB

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