auths.go 6.3 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.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. Filter: form.Filter,
  73. AdminFilter: form.AdminFilter,
  74. Enabled: true,
  75. },
  76. }
  77. }
  78. func parseSMTPConfig(form auth.AuthenticationForm) *models.SMTPConfig {
  79. return &models.SMTPConfig{
  80. Auth: form.SMTPAuth,
  81. Host: form.SMTPHost,
  82. Port: form.SMTPPort,
  83. AllowedDomains: form.AllowedDomains,
  84. TLS: form.TLS,
  85. SkipVerify: form.SkipVerify,
  86. }
  87. }
  88. func NewAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
  89. ctx.Data["Title"] = ctx.Tr("admin.auths.new")
  90. ctx.Data["PageIsAdmin"] = true
  91. ctx.Data["PageIsAdminAuthentications"] = true
  92. ctx.Data["CurTypeName"] = models.LoginNames[models.LoginType(form.Type)]
  93. ctx.Data["AuthSources"] = authSources
  94. ctx.Data["SMTPAuths"] = models.SMTPAuths
  95. if ctx.HasError() {
  96. ctx.HTML(200, AUTH_NEW)
  97. return
  98. }
  99. var config core.Conversion
  100. switch models.LoginType(form.Type) {
  101. case models.LOGIN_LDAP, models.LOGIN_DLDAP:
  102. config = parseLDAPConfig(form)
  103. case models.LOGIN_SMTP:
  104. config = parseSMTPConfig(form)
  105. case models.LOGIN_PAM:
  106. config = &models.PAMConfig{
  107. ServiceName: form.PAMServiceName,
  108. }
  109. default:
  110. ctx.Error(400)
  111. return
  112. }
  113. if err := models.CreateSource(&models.LoginSource{
  114. Type: models.LoginType(form.Type),
  115. Name: form.Name,
  116. IsActived: form.IsActive,
  117. Cfg: config,
  118. }); err != nil {
  119. ctx.Handle(500, "CreateSource", err)
  120. return
  121. }
  122. log.Trace("Authentication created by admin(%s): %s", ctx.User.Name, form.Name)
  123. ctx.Flash.Success(ctx.Tr("admin.auths.new_success", form.Name))
  124. ctx.Redirect(setting.AppSubUrl + "/admin/auths")
  125. }
  126. func EditAuthSource(ctx *middleware.Context) {
  127. ctx.Data["Title"] = ctx.Tr("admin.auths.edit")
  128. ctx.Data["PageIsAdmin"] = true
  129. ctx.Data["PageIsAdminAuthentications"] = true
  130. ctx.Data["SMTPAuths"] = models.SMTPAuths
  131. source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid"))
  132. if err != nil {
  133. ctx.Handle(500, "GetLoginSourceByID", err)
  134. return
  135. }
  136. ctx.Data["Source"] = source
  137. ctx.HTML(200, AUTH_EDIT)
  138. }
  139. func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
  140. ctx.Data["Title"] = ctx.Tr("admin.auths.edit")
  141. ctx.Data["PageIsAdmin"] = true
  142. ctx.Data["PageIsAdminAuthentications"] = true
  143. ctx.Data["SMTPAuths"] = models.SMTPAuths
  144. source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid"))
  145. if err != nil {
  146. ctx.Handle(500, "GetLoginSourceByID", err)
  147. return
  148. }
  149. ctx.Data["Source"] = source
  150. if ctx.HasError() {
  151. ctx.HTML(200, AUTH_EDIT)
  152. return
  153. }
  154. var config core.Conversion
  155. switch models.LoginType(form.Type) {
  156. case models.LOGIN_LDAP, models.LOGIN_DLDAP:
  157. config = parseLDAPConfig(form)
  158. case models.LOGIN_SMTP:
  159. config = parseSMTPConfig(form)
  160. case models.LOGIN_PAM:
  161. config = &models.PAMConfig{
  162. ServiceName: form.PAMServiceName,
  163. }
  164. default:
  165. ctx.Error(400)
  166. return
  167. }
  168. source.Name = form.Name
  169. source.IsActived = form.IsActive
  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. }