auths.go 5.4 KB

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