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.

193 lines
4.8 KiB

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