auths.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package admin
  2. import (
  3. "strings"
  4. "github.com/go-martini/martini"
  5. "github.com/gogits/gogs/models"
  6. "github.com/gogits/gogs/modules/auth"
  7. "github.com/gogits/gogs/modules/auth/ldap"
  8. "github.com/gogits/gogs/modules/base"
  9. "github.com/gogits/gogs/modules/middleware"
  10. "github.com/gpmgo/gopm/log"
  11. )
  12. func NewAuthSource(ctx *middleware.Context) {
  13. ctx.Data["Title"] = "New Authentication"
  14. ctx.Data["PageIsAuths"] = true
  15. ctx.Data["LoginTypes"] = models.LoginTypes
  16. ctx.HTML(200, "admin/auths/new")
  17. }
  18. func NewAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
  19. ctx.Data["Title"] = "New Authentication"
  20. ctx.Data["PageIsAuths"] = true
  21. if ctx.HasError() {
  22. ctx.HTML(200, "admin/auths/new")
  23. return
  24. }
  25. u := &models.LDAPConfig{
  26. Ldapsource: ldap.Ldapsource{
  27. Host: form.Host,
  28. Port: form.Port,
  29. BaseDN: form.BaseDN,
  30. Attributes: form.Attributes,
  31. Filter: form.Filter,
  32. MsAdSAFormat: form.MsAdSA,
  33. Enabled: true,
  34. Name: form.Name,
  35. },
  36. }
  37. if err := models.AddLDAPSource(form.Name, u); err != nil {
  38. switch err {
  39. default:
  40. ctx.Handle(500, "admin.auths.NewAuth", err)
  41. }
  42. return
  43. }
  44. log.Trace("%s Authentication created by admin(%s): %s", ctx.Req.RequestURI,
  45. ctx.User.LowerName, strings.ToLower(form.Name))
  46. ctx.Redirect("/admin/auths")
  47. }
  48. func EditAuthSource(ctx *middleware.Context, params martini.Params) {
  49. ctx.Data["Title"] = "Edit Authentication"
  50. ctx.Data["PageIsAuths"] = true
  51. id, err := base.StrTo(params["authid"]).Int64()
  52. if err != nil {
  53. ctx.Handle(404, "admin.auths.EditAuthSource", err)
  54. return
  55. }
  56. u, err := models.GetLoginSourceById(id)
  57. if err != nil {
  58. ctx.Handle(500, "admin.user.EditUser", err)
  59. return
  60. }
  61. ctx.Data["Source"] = u
  62. ctx.Data["LoginTypes"] = models.LoginTypes
  63. ctx.HTML(200, "admin/auths/edit")
  64. }
  65. func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
  66. ctx.Data["Title"] = "Edit Authentication"
  67. ctx.Data["PageIsAuths"] = true
  68. if ctx.HasError() {
  69. ctx.HTML(200, "admin/auths/edit")
  70. return
  71. }
  72. u := models.LoginSource{
  73. Name: form.Name,
  74. IsActived: form.IsActived,
  75. Type: models.LT_LDAP,
  76. Cfg: &models.LDAPConfig{
  77. Ldapsource: ldap.Ldapsource{
  78. Host: form.Host,
  79. Port: form.Port,
  80. BaseDN: form.BaseDN,
  81. Attributes: form.Attributes,
  82. Filter: form.Filter,
  83. MsAdSAFormat: form.MsAdSA,
  84. Enabled: true,
  85. Name: form.Name,
  86. },
  87. },
  88. }
  89. if err := models.UpdateLDAPSource(&u); err != nil {
  90. switch err {
  91. default:
  92. ctx.Handle(500, "admin.auths.EditAuth", err)
  93. }
  94. return
  95. }
  96. log.Trace("%s Authentication changed by admin(%s): %s", ctx.Req.RequestURI,
  97. ctx.User.LowerName, strings.ToLower(form.Name))
  98. ctx.Redirect("/admin/auths")
  99. }
  100. func DeleteAuthSource(ctx *middleware.Context, params martini.Params) {
  101. ctx.Data["Title"] = "Delete Authentication"
  102. ctx.Data["PageIsAuths"] = true
  103. id, err := base.StrTo(params["authid"]).Int64()
  104. if err != nil {
  105. ctx.Handle(404, "admin.auths.DeleteAuth", err)
  106. return
  107. }
  108. a, err := models.GetLoginSourceById(id)
  109. if err != nil {
  110. ctx.Handle(500, "admin.auths.DeleteAuth", err)
  111. return
  112. }
  113. if err = models.DelLoginSource(a); err != nil {
  114. switch err {
  115. case models.ErrAuthenticationUserUsed:
  116. ctx.Flash.Error("This authentication still has used by some users, you should move them and then delete again.")
  117. ctx.Redirect("/admin/auths/" + params["authid"])
  118. default:
  119. ctx.Handle(500, "admin.auths.DeleteAuth", err)
  120. }
  121. return
  122. }
  123. log.Trace("%s Authentication deleted by admin(%s): %s", ctx.Req.RequestURI,
  124. ctx.User.LowerName, ctx.User.LowerName)
  125. ctx.Redirect("/admin/auths")
  126. }