auths.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. AttributeUsername: form.AttributeUsername,
  61. AttributeName: form.AttributeName,
  62. AttributeSurname: form.AttributeSurname,
  63. AttributeMail: form.AttributeMail,
  64. Filter: form.Filter,
  65. MsAdSAFormat: form.MsAdSA,
  66. Enabled: true,
  67. Name: form.Name,
  68. },
  69. }
  70. case models.SMTP:
  71. u = &models.SMTPConfig{
  72. Auth: form.SMTPAuth,
  73. Host: form.SMTPHost,
  74. Port: form.SMTPPort,
  75. TLS: form.TLS,
  76. }
  77. default:
  78. ctx.Error(400)
  79. return
  80. }
  81. var source = &models.LoginSource{
  82. Type: models.LoginType(form.Type),
  83. Name: form.Name,
  84. IsActived: true,
  85. AllowAutoRegister: form.AllowAutoRegister,
  86. Cfg: u,
  87. }
  88. if err := models.CreateSource(source); err != nil {
  89. ctx.Handle(500, "CreateSource", err)
  90. return
  91. }
  92. log.Trace("Authentication created by admin(%s): %s", ctx.User.Name, form.Name)
  93. ctx.Redirect(setting.AppSubUrl + "/admin/auths")
  94. }
  95. func EditAuthSource(ctx *middleware.Context) {
  96. ctx.Data["Title"] = ctx.Tr("admin.auths.edit")
  97. ctx.Data["PageIsAdmin"] = true
  98. ctx.Data["PageIsAdminAuthentications"] = true
  99. ctx.Data["LoginTypes"] = models.LoginTypes
  100. ctx.Data["SMTPAuths"] = models.SMTPAuths
  101. id := com.StrTo(ctx.Params(":authid")).MustInt64()
  102. if id == 0 {
  103. ctx.Handle(404, "EditAuthSource", nil)
  104. return
  105. }
  106. u, err := models.GetLoginSourceById(id)
  107. if err != nil {
  108. ctx.Handle(500, "GetLoginSourceById", err)
  109. return
  110. }
  111. ctx.Data["Source"] = u
  112. ctx.HTML(200, AUTH_EDIT)
  113. }
  114. func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
  115. ctx.Data["Title"] = ctx.Tr("admin.auths.edit")
  116. ctx.Data["PageIsAdmin"] = true
  117. ctx.Data["PageIsAdminAuthentications"] = true
  118. ctx.Data["PageIsAuths"] = true
  119. ctx.Data["LoginTypes"] = models.LoginTypes
  120. ctx.Data["SMTPAuths"] = models.SMTPAuths
  121. if ctx.HasError() {
  122. ctx.HTML(200, AUTH_EDIT)
  123. return
  124. }
  125. var config core.Conversion
  126. switch models.LoginType(form.Type) {
  127. case models.LDAP:
  128. config = &models.LDAPConfig{
  129. Ldapsource: ldap.Ldapsource{
  130. Host: form.Host,
  131. Port: form.Port,
  132. UseSSL: form.UseSSL,
  133. BaseDN: form.BaseDN,
  134. AttributeUsername: form.AttributeUsername,
  135. AttributeName: form.AttributeName,
  136. AttributeSurname: form.AttributeSurname,
  137. AttributeMail: form.AttributeMail,
  138. Filter: form.Filter,
  139. MsAdSAFormat: form.MsAdSA,
  140. Enabled: true,
  141. Name: form.Name,
  142. },
  143. }
  144. case models.SMTP:
  145. config = &models.SMTPConfig{
  146. Auth: form.SMTPAuth,
  147. Host: form.SMTPHost,
  148. Port: form.SMTPPort,
  149. TLS: form.TLS,
  150. }
  151. default:
  152. ctx.Error(400)
  153. return
  154. }
  155. u := models.LoginSource{
  156. Id: form.ID,
  157. Name: form.Name,
  158. IsActived: form.IsActived,
  159. Type: models.LoginType(form.Type),
  160. AllowAutoRegister: form.AllowAutoRegister,
  161. Cfg: config,
  162. }
  163. if err := models.UpdateSource(&u); err != nil {
  164. ctx.Handle(500, "UpdateSource", err)
  165. return
  166. }
  167. log.Trace("Authentication changed by admin(%s): %s", ctx.User.Name, form.Name)
  168. ctx.Flash.Success(ctx.Tr("admin.auths.update_success"))
  169. ctx.Redirect(setting.AppSubUrl + "/admin/auths/" + ctx.Params(":authid"))
  170. }
  171. func DeleteAuthSource(ctx *middleware.Context) {
  172. id := com.StrTo(ctx.Params(":authid")).MustInt64()
  173. if id == 0 {
  174. ctx.Handle(404, "DeleteAuthSource", nil)
  175. return
  176. }
  177. a, err := models.GetLoginSourceById(id)
  178. if err != nil {
  179. ctx.Handle(500, "GetLoginSourceById", err)
  180. return
  181. }
  182. if err = models.DelLoginSource(a); err != nil {
  183. switch err {
  184. case models.ErrAuthenticationUserUsed:
  185. ctx.Flash.Error("form.still_own_user")
  186. ctx.Redirect(setting.AppSubUrl + "/admin/auths/" + ctx.Params(":authid"))
  187. default:
  188. ctx.Handle(500, "DelLoginSource", err)
  189. }
  190. return
  191. }
  192. log.Trace("Authentication deleted by admin(%s): %s", ctx.User.Name, a.Name)
  193. ctx.Redirect(setting.AppSubUrl + "/admin/auths")
  194. }