auths.go 5.9 KB

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