auths.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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.Data["Total"] = models.CountLoginSources()
  32. ctx.HTML(200, AUTHS)
  33. }
  34. type AuthSource struct {
  35. Name string
  36. Type models.LoginType
  37. }
  38. var authSources = []AuthSource{
  39. {models.LoginNames[models.LDAP], models.LDAP},
  40. {models.LoginNames[models.DLDAP], models.DLDAP},
  41. {models.LoginNames[models.SMTP], models.SMTP},
  42. {models.LoginNames[models.PAM], models.PAM},
  43. }
  44. func NewAuthSource(ctx *middleware.Context) {
  45. ctx.Data["Title"] = ctx.Tr("admin.auths.new")
  46. ctx.Data["PageIsAdmin"] = true
  47. ctx.Data["PageIsAdminAuthentications"] = true
  48. ctx.Data["type"] = models.LDAP
  49. ctx.Data["CurTypeName"] = models.LoginNames[models.LDAP]
  50. ctx.Data["smtp_auth"] = "PLAIN"
  51. ctx.Data["is_active"] = true
  52. ctx.Data["AuthSources"] = authSources
  53. ctx.Data["SMTPAuths"] = models.SMTPAuths
  54. ctx.HTML(200, AUTH_NEW)
  55. }
  56. func parseLDAPConfig(form auth.AuthenticationForm) *models.LDAPConfig {
  57. return &models.LDAPConfig{
  58. Ldapsource: ldap.Ldapsource{
  59. Name: form.Name,
  60. Host: form.Host,
  61. Port: form.Port,
  62. UseSSL: form.TLS,
  63. BindDN: form.BindDN,
  64. UserDN: form.UserDN,
  65. BindPassword: form.BindPassword,
  66. UserBase: form.UserBase,
  67. AttributeName: form.AttributeName,
  68. AttributeSurname: form.AttributeSurname,
  69. AttributeMail: form.AttributeMail,
  70. Filter: form.Filter,
  71. AdminFilter: form.AdminFilter,
  72. Enabled: true,
  73. },
  74. }
  75. }
  76. func parseSMTPConfig(form auth.AuthenticationForm) *models.SMTPConfig {
  77. return &models.SMTPConfig{
  78. Auth: form.SMTPAuth,
  79. Host: form.SMTPHost,
  80. Port: form.SMTPPort,
  81. AllowedDomains: form.AllowedDomains,
  82. TLS: form.TLS,
  83. SkipVerify: form.SkipVerify,
  84. }
  85. }
  86. func NewAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
  87. ctx.Data["Title"] = ctx.Tr("admin.auths.new")
  88. ctx.Data["PageIsAdmin"] = true
  89. ctx.Data["PageIsAdminAuthentications"] = true
  90. ctx.Data["CurTypeName"] = models.LoginNames[models.LoginType(form.Type)]
  91. ctx.Data["AuthSources"] = authSources
  92. ctx.Data["SMTPAuths"] = models.SMTPAuths
  93. if ctx.HasError() {
  94. ctx.HTML(200, AUTH_NEW)
  95. return
  96. }
  97. var config core.Conversion
  98. switch models.LoginType(form.Type) {
  99. case models.LDAP, models.DLDAP:
  100. config = parseLDAPConfig(form)
  101. case models.SMTP:
  102. config = parseSMTPConfig(form)
  103. case models.PAM:
  104. config = &models.PAMConfig{
  105. ServiceName: form.PAMServiceName,
  106. }
  107. default:
  108. ctx.Error(400)
  109. return
  110. }
  111. if err := models.CreateSource(&models.LoginSource{
  112. Type: models.LoginType(form.Type),
  113. Name: form.Name,
  114. IsActived: form.IsActive,
  115. AllowAutoRegister: form.AllowAutoRegister,
  116. Cfg: config,
  117. }); err != nil {
  118. ctx.Handle(500, "CreateSource", err)
  119. return
  120. }
  121. log.Trace("Authentication created by admin(%s): %s", ctx.User.Name, form.Name)
  122. ctx.Flash.Success(ctx.Tr("admin.auths.new_success", form.Name))
  123. ctx.Redirect(setting.AppSubUrl + "/admin/auths")
  124. }
  125. func EditAuthSource(ctx *middleware.Context) {
  126. ctx.Data["Title"] = ctx.Tr("admin.auths.edit")
  127. ctx.Data["PageIsAdmin"] = true
  128. ctx.Data["PageIsAdminAuthentications"] = true
  129. ctx.Data["SMTPAuths"] = models.SMTPAuths
  130. source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid"))
  131. if err != nil {
  132. ctx.Handle(500, "GetLoginSourceByID", err)
  133. return
  134. }
  135. ctx.Data["Source"] = source
  136. ctx.HTML(200, AUTH_EDIT)
  137. }
  138. func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
  139. ctx.Data["Title"] = ctx.Tr("admin.auths.edit")
  140. ctx.Data["PageIsAdmin"] = true
  141. ctx.Data["PageIsAdminAuthentications"] = true
  142. ctx.Data["SMTPAuths"] = models.SMTPAuths
  143. source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid"))
  144. if err != nil {
  145. ctx.Handle(500, "GetLoginSourceByID", err)
  146. return
  147. }
  148. ctx.Data["Source"] = source
  149. if ctx.HasError() {
  150. ctx.HTML(200, AUTH_EDIT)
  151. return
  152. }
  153. var config core.Conversion
  154. switch models.LoginType(form.Type) {
  155. case models.LDAP, models.DLDAP:
  156. config = parseLDAPConfig(form)
  157. case models.SMTP:
  158. config = parseSMTPConfig(form)
  159. case models.PAM:
  160. config = &models.PAMConfig{
  161. ServiceName: form.PAMServiceName,
  162. }
  163. default:
  164. ctx.Error(400)
  165. return
  166. }
  167. source.Name = form.Name
  168. source.IsActived = form.IsActive
  169. source.AllowAutoRegister = form.AllowAutoRegister
  170. source.Cfg = config
  171. if err := models.UpdateSource(source); err != nil {
  172. ctx.Handle(500, "UpdateSource", err)
  173. return
  174. }
  175. log.Trace("Authentication changed by admin(%s): %s", ctx.User.Name, source.ID)
  176. ctx.Flash.Success(ctx.Tr("admin.auths.update_success"))
  177. ctx.Redirect(setting.AppSubUrl + "/admin/auths/" + com.ToStr(form.ID))
  178. }
  179. func DeleteAuthSource(ctx *middleware.Context) {
  180. source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid"))
  181. if err != nil {
  182. ctx.Handle(500, "GetLoginSourceByID", err)
  183. return
  184. }
  185. if err = models.DeleteSource(source); err != nil {
  186. switch err {
  187. case models.ErrAuthenticationUserUsed:
  188. ctx.Flash.Error("form.still_own_user")
  189. ctx.Redirect(setting.AppSubUrl + "/admin/auths/" + ctx.Params(":authid"))
  190. default:
  191. ctx.Handle(500, "DeleteSource", err)
  192. }
  193. return
  194. }
  195. log.Trace("Authentication deleted by admin(%s): %d", ctx.User.Name, source.ID)
  196. ctx.Flash.Success(ctx.Tr("admin.auths.deletion_success"))
  197. ctx.JSON(200, map[string]interface{}{
  198. "redirect": setting.AppSubUrl + "/admin/auths",
  199. })
  200. }