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