auths.go 3.8 KB

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