You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

232 lines
6.1 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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.LoginSources()
  27. if err != nil {
  28. ctx.Handle(500, "LoginSources", 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. Source: &ldap.Source{
  59. Name: form.Name,
  60. Host: form.Host,
  61. Port: form.Port,
  62. UseSSL: form.TLS,
  63. SkipVerify: form.SkipVerify,
  64. BindDN: form.BindDN,
  65. UserDN: form.UserDN,
  66. BindPassword: form.BindPassword,
  67. UserBase: form.UserBase,
  68. AttributeName: form.AttributeName,
  69. AttributeSurname: form.AttributeSurname,
  70. AttributeMail: form.AttributeMail,
  71. Filter: form.Filter,
  72. AdminFilter: form.AdminFilter,
  73. Enabled: true,
  74. },
  75. }
  76. }
  77. func parseSMTPConfig(form auth.AuthenticationForm) *models.SMTPConfig {
  78. return &models.SMTPConfig{
  79. Auth: form.SMTPAuth,
  80. Host: form.SMTPHost,
  81. Port: form.SMTPPort,
  82. AllowedDomains: form.AllowedDomains,
  83. TLS: form.TLS,
  84. SkipVerify: form.SkipVerify,
  85. }
  86. }
  87. func NewAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
  88. ctx.Data["Title"] = ctx.Tr("admin.auths.new")
  89. ctx.Data["PageIsAdmin"] = true
  90. ctx.Data["PageIsAdminAuthentications"] = true
  91. ctx.Data["CurTypeName"] = models.LoginNames[models.LoginType(form.Type)]
  92. ctx.Data["AuthSources"] = authSources
  93. ctx.Data["SMTPAuths"] = models.SMTPAuths
  94. if ctx.HasError() {
  95. ctx.HTML(200, AUTH_NEW)
  96. return
  97. }
  98. var config core.Conversion
  99. switch models.LoginType(form.Type) {
  100. case models.LDAP, models.DLDAP:
  101. config = parseLDAPConfig(form)
  102. case models.SMTP:
  103. config = parseSMTPConfig(form)
  104. case models.PAM:
  105. config = &models.PAMConfig{
  106. ServiceName: form.PAMServiceName,
  107. }
  108. default:
  109. ctx.Error(400)
  110. return
  111. }
  112. if err := models.CreateSource(&models.LoginSource{
  113. Type: models.LoginType(form.Type),
  114. Name: form.Name,
  115. IsActived: form.IsActive,
  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.Cfg = config
  170. if err := models.UpdateSource(source); err != nil {
  171. ctx.Handle(500, "UpdateSource", err)
  172. return
  173. }
  174. log.Trace("Authentication changed by admin(%s): %s", ctx.User.Name, source.ID)
  175. ctx.Flash.Success(ctx.Tr("admin.auths.update_success"))
  176. ctx.Redirect(setting.AppSubUrl + "/admin/auths/" + com.ToStr(form.ID))
  177. }
  178. func DeleteAuthSource(ctx *middleware.Context) {
  179. source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid"))
  180. if err != nil {
  181. ctx.Handle(500, "GetLoginSourceByID", err)
  182. return
  183. }
  184. if err = models.DeleteSource(source); err != nil {
  185. switch err {
  186. case models.ErrAuthenticationUserUsed:
  187. ctx.Flash.Error("form.still_own_user")
  188. ctx.Redirect(setting.AppSubUrl + "/admin/auths/" + ctx.Params(":authid"))
  189. default:
  190. ctx.Handle(500, "DeleteSource", err)
  191. }
  192. return
  193. }
  194. log.Trace("Authentication deleted by admin(%s): %d", ctx.User.Name, source.ID)
  195. ctx.Flash.Success(ctx.Tr("admin.auths.deletion_success"))
  196. ctx.JSON(200, map[string]interface{}{
  197. "redirect": setting.AppSubUrl + "/admin/auths",
  198. })
  199. }