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.

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