auths.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. Cfg: config,
  116. }); err != nil {
  117. ctx.Handle(500, "CreateSource", err)
  118. return
  119. }
  120. log.Trace("Authentication created by admin(%s): %s", ctx.User.Name, form.Name)
  121. ctx.Flash.Success(ctx.Tr("admin.auths.new_success", form.Name))
  122. ctx.Redirect(setting.AppSubUrl + "/admin/auths")
  123. }
  124. func EditAuthSource(ctx *middleware.Context) {
  125. ctx.Data["Title"] = ctx.Tr("admin.auths.edit")
  126. ctx.Data["PageIsAdmin"] = true
  127. ctx.Data["PageIsAdminAuthentications"] = true
  128. ctx.Data["SMTPAuths"] = models.SMTPAuths
  129. source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid"))
  130. if err != nil {
  131. ctx.Handle(500, "GetLoginSourceByID", err)
  132. return
  133. }
  134. ctx.Data["Source"] = source
  135. ctx.HTML(200, AUTH_EDIT)
  136. }
  137. func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
  138. ctx.Data["Title"] = ctx.Tr("admin.auths.edit")
  139. ctx.Data["PageIsAdmin"] = true
  140. ctx.Data["PageIsAdminAuthentications"] = true
  141. ctx.Data["SMTPAuths"] = models.SMTPAuths
  142. source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid"))
  143. if err != nil {
  144. ctx.Handle(500, "GetLoginSourceByID", err)
  145. return
  146. }
  147. ctx.Data["Source"] = source
  148. if ctx.HasError() {
  149. ctx.HTML(200, AUTH_EDIT)
  150. return
  151. }
  152. var config core.Conversion
  153. switch models.LoginType(form.Type) {
  154. case models.LDAP, models.DLDAP:
  155. config = parseLDAPConfig(form)
  156. case models.SMTP:
  157. config = parseSMTPConfig(form)
  158. case models.PAM:
  159. config = &models.PAMConfig{
  160. ServiceName: form.PAMServiceName,
  161. }
  162. default:
  163. ctx.Error(400)
  164. return
  165. }
  166. source.Name = form.Name
  167. source.IsActived = form.IsActive
  168. source.Cfg = config
  169. if err := models.UpdateSource(source); err != nil {
  170. ctx.Handle(500, "UpdateSource", err)
  171. return
  172. }
  173. log.Trace("Authentication changed by admin(%s): %s", ctx.User.Name, source.ID)
  174. ctx.Flash.Success(ctx.Tr("admin.auths.update_success"))
  175. ctx.Redirect(setting.AppSubUrl + "/admin/auths/" + com.ToStr(form.ID))
  176. }
  177. func DeleteAuthSource(ctx *middleware.Context) {
  178. source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid"))
  179. if err != nil {
  180. ctx.Handle(500, "GetLoginSourceByID", err)
  181. return
  182. }
  183. if err = models.DeleteSource(source); err != nil {
  184. switch err {
  185. case models.ErrAuthenticationUserUsed:
  186. ctx.Flash.Error("form.still_own_user")
  187. ctx.Redirect(setting.AppSubUrl + "/admin/auths/" + ctx.Params(":authid"))
  188. default:
  189. ctx.Handle(500, "DeleteSource", err)
  190. }
  191. return
  192. }
  193. log.Trace("Authentication deleted by admin(%s): %d", ctx.User.Name, source.ID)
  194. ctx.Flash.Success(ctx.Tr("admin.auths.deletion_success"))
  195. ctx.JSON(200, map[string]interface{}{
  196. "redirect": setting.AppSubUrl + "/admin/auths",
  197. })
  198. }