members.go 2.8 KB

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