auths.go 6.3 KB

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