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.

94 lines
2.2 KiB

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. )
  17. // Members render orgnization users page
  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.ServerError("GetMembers", err)
  24. return
  25. }
  26. ctx.Data["Members"] = org.Members
  27. ctx.HTML(200, tplMembers)
  28. }
  29. // MembersAction response for operation to a member of orgnization
  30. func MembersAction(ctx *context.Context) {
  31. uid := com.StrTo(ctx.Query("uid")).MustInt64()
  32. if uid == 0 {
  33. ctx.Redirect(ctx.Org.OrgLink + "/members")
  34. return
  35. }
  36. org := ctx.Org.Organization
  37. var err error
  38. switch ctx.Params(":action") {
  39. case "private":
  40. if ctx.User.ID != uid && !ctx.Org.IsOwner {
  41. ctx.Error(404)
  42. return
  43. }
  44. err = models.ChangeOrgUserStatus(org.ID, uid, false)
  45. case "public":
  46. if ctx.User.ID != uid && !ctx.Org.IsOwner {
  47. ctx.Error(404)
  48. return
  49. }
  50. err = models.ChangeOrgUserStatus(org.ID, uid, true)
  51. case "remove":
  52. if !ctx.Org.IsOwner {
  53. ctx.Error(404)
  54. return
  55. }
  56. err = org.RemoveMember(uid)
  57. if models.IsErrLastOrgOwner(err) {
  58. ctx.Flash.Error(ctx.Tr("form.last_org_owner"))
  59. ctx.Redirect(ctx.Org.OrgLink + "/members")
  60. return
  61. }
  62. case "leave":
  63. err = org.RemoveMember(ctx.User.ID)
  64. if models.IsErrLastOrgOwner(err) {
  65. ctx.Flash.Error(ctx.Tr("form.last_org_owner"))
  66. ctx.Redirect(ctx.Org.OrgLink + "/members")
  67. return
  68. }
  69. }
  70. if err != nil {
  71. log.Error(4, "Action(%s): %v", ctx.Params(":action"), err)
  72. ctx.JSON(200, map[string]interface{}{
  73. "ok": false,
  74. "err": err.Error(),
  75. })
  76. return
  77. }
  78. if ctx.Params(":action") != "leave" {
  79. ctx.Redirect(ctx.Org.OrgLink + "/members")
  80. } else {
  81. ctx.Redirect(setting.AppSubURL + "/")
  82. }
  83. }