auths.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. "github.com/Unknwon/com"
  7. "github.com/go-xorm/core"
  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. const (
  16. AUTHS base.TplName = "admin/auth/list"
  17. AUTH_NEW base.TplName = "admin/auth/new"
  18. AUTH_EDIT base.TplName = "admin/auth/edit"
  19. )
  20. func Authentications(ctx *middleware.Context) {
  21. ctx.Data["Title"] = ctx.Tr("admin.authentication")
  22. ctx.Data["PageIsAdmin"] = true
  23. ctx.Data["PageIsAdminAuthentications"] = true
  24. var err error
  25. ctx.Data["Sources"], err = models.GetAuths()
  26. if err != nil {
  27. ctx.Handle(500, "GetAuths", err)
  28. return
  29. }
  30. ctx.HTML(200, AUTHS)
  31. }
  32. func NewAuthSource(ctx *middleware.Context) {
  33. ctx.Data["Title"] = ctx.Tr("admin.auths.new")
  34. ctx.Data["PageIsAdmin"] = true
  35. ctx.Data["PageIsAdminAuthentications"] = true
  36. ctx.Data["LoginTypes"] = models.LoginTypes
  37. ctx.Data["SMTPAuths"] = models.SMTPAuths
  38. ctx.HTML(200, AUTH_NEW)
  39. }
  40. func NewAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
  41. ctx.Data["Title"] = ctx.Tr("admin.auths.new")
  42. ctx.Data["PageIsAdmin"] = true
  43. ctx.Data["PageIsAdminAuthentications"] = true
  44. ctx.Data["LoginTypes"] = models.LoginTypes
  45. ctx.Data["SMTPAuths"] = models.SMTPAuths
  46. if ctx.HasError() {
  47. ctx.HTML(200, AUTH_NEW)
  48. return
  49. }
  50. var u core.Conversion
  51. switch models.LoginType(form.Type) {
  52. case models.LDAP:
  53. u = &models.LDAPConfig{
  54. Ldapsource: ldap.Ldapsource{
  55. Host: form.Host,
  56. Port: form.Port,
  57. UseSSL: form.UseSSL,
  58. BaseDN: form.BaseDN,
  59. Attributes: form.Attributes,
  60. Filter: form.Filter,
  61. MsAdSAFormat: form.MsAdSA,
  62. Enabled: true,
  63. Name: form.AuthName,
  64. },
  65. }
  66. case models.SMTP:
  67. u = &models.SMTPConfig{
  68. Auth: form.SmtpAuth,
  69. Host: form.SmtpHost,
  70. Port: form.SmtpPort,
  71. TLS: form.Tls,
  72. }
  73. default:
  74. ctx.Error(400)
  75. return
  76. }
  77. var source = &models.LoginSource{
  78. Type: models.LoginType(form.Type),
  79. Name: form.AuthName,
  80. IsActived: true,
  81. AllowAutoRegister: form.AllowAutoRegister,
  82. Cfg: u,
  83. }
  84. if err := models.CreateSource(source); err != nil {
  85. ctx.Handle(500, "CreateSource", err)
  86. return
  87. }
  88. log.Trace("Authentication created by admin(%s): %s", ctx.User.Name, form.AuthName)
  89. ctx.Redirect("/admin/auths")
  90. }
  91. func EditAuthSource(ctx *middleware.Context) {
  92. ctx.Data["Title"] = ctx.Tr("admin.auths.edit")
  93. ctx.Data["PageIsAdmin"] = true
  94. ctx.Data["PageIsAdminAuthentications"] = true
  95. ctx.Data["LoginTypes"] = models.LoginTypes
  96. ctx.Data["SMTPAuths"] = models.SMTPAuths
  97. id := com.StrTo(ctx.Params(":authid")).MustInt64()
  98. if id == 0 {
  99. ctx.Handle(404, "EditAuthSource", nil)
  100. return
  101. }
  102. u, err := models.GetLoginSourceById(id)
  103. if err != nil {
  104. ctx.Handle(500, "GetLoginSourceById", err)
  105. return
  106. }
  107. ctx.Data["Source"] = u
  108. ctx.HTML(200, AUTH_EDIT)
  109. }
  110. func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
  111. ctx.Data["Title"] = ctx.Tr("admin.auths.edit")
  112. ctx.Data["PageIsAdmin"] = true
  113. ctx.Data["PageIsAdminAuthentications"] = true
  114. ctx.Data["PageIsAuths"] = true
  115. ctx.Data["LoginTypes"] = models.LoginTypes
  116. ctx.Data["SMTPAuths"] = models.SMTPAuths
  117. if ctx.HasError() {
  118. ctx.HTML(200, AUTH_EDIT)
  119. return
  120. }
  121. var config core.Conversion
  122. switch models.LoginType(form.Type) {
  123. case models.LDAP:
  124. config = &models.LDAPConfig{
  125. Ldapsource: ldap.Ldapsource{
  126. Host: form.Host,
  127. Port: form.Port,
  128. UseSSL: form.UseSSL,
  129. BaseDN: form.BaseDN,
  130. Attributes: form.Attributes,
  131. Filter: form.Filter,
  132. MsAdSAFormat: form.MsAdSA,
  133. Enabled: true,
  134. Name: form.AuthName,
  135. },
  136. }
  137. case models.SMTP:
  138. config = &models.SMTPConfig{
  139. Auth: form.SmtpAuth,
  140. Host: form.SmtpHost,
  141. Port: form.SmtpPort,
  142. TLS: form.Tls,
  143. }
  144. default:
  145. ctx.Error(400)
  146. return
  147. }
  148. u := models.LoginSource{
  149. Id: form.Id,
  150. Name: form.AuthName,
  151. IsActived: form.IsActived,
  152. Type: models.LoginType(form.Type),
  153. AllowAutoRegister: form.AllowAutoRegister,
  154. Cfg: config,
  155. }
  156. if err := models.UpdateSource(&u); err != nil {
  157. ctx.Handle(500, "UpdateSource", err)
  158. return
  159. }
  160. log.Trace("Authentication changed by admin(%s): %s", ctx.User.Name, form.AuthName)
  161. ctx.Flash.Success(ctx.Tr("admin.auths.update_success"))
  162. ctx.Redirect("/admin/auths/" + ctx.Params(":authid"))
  163. }
  164. func DeleteAuthSource(ctx *middleware.Context) {
  165. id := com.StrTo(ctx.Params(":authid")).MustInt64()
  166. if id == 0 {
  167. ctx.Handle(404, "DeleteAuthSource", nil)
  168. return
  169. }
  170. a, err := models.GetLoginSourceById(id)
  171. if err != nil {
  172. ctx.Handle(500, "GetLoginSourceById", err)
  173. return
  174. }
  175. if err = models.DelLoginSource(a); err != nil {
  176. switch err {
  177. case models.ErrAuthenticationUserUsed:
  178. ctx.Flash.Error("form.still_own_user")
  179. ctx.Redirect("/admin/auths/" + ctx.Params(":authid"))
  180. default:
  181. ctx.Handle(500, "DelLoginSource", err)
  182. }
  183. return
  184. }
  185. log.Trace("Authentication deleted by admin(%s): %s", ctx.User.Name, a.Name)
  186. ctx.Redirect("/admin/auths")
  187. }