auths.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 NewAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
  57. ctx.Data["Title"] = ctx.Tr("admin.auths.new")
  58. ctx.Data["PageIsAdmin"] = true
  59. ctx.Data["PageIsAdminAuthentications"] = true
  60. ctx.Data["CurTypeName"] = models.LoginNames[models.LoginType(form.Type)]
  61. ctx.Data["AuthSources"] = authSources
  62. ctx.Data["SMTPAuths"] = models.SMTPAuths
  63. if ctx.HasError() {
  64. ctx.HTML(200, AUTH_NEW)
  65. return
  66. }
  67. var u core.Conversion
  68. switch models.LoginType(form.Type) {
  69. case models.LDAP, models.DLDAP:
  70. u = &models.LDAPConfig{
  71. Ldapsource: ldap.Ldapsource{
  72. Name: form.Name,
  73. Host: form.Host,
  74. Port: form.Port,
  75. UseSSL: form.UseSSL,
  76. BindDN: form.BindDN,
  77. UserDN: form.UserDN,
  78. BindPassword: form.BindPassword,
  79. UserBase: form.UserBase,
  80. AttributeName: form.AttributeName,
  81. AttributeSurname: form.AttributeSurname,
  82. AttributeMail: form.AttributeMail,
  83. Filter: form.Filter,
  84. AdminFilter: form.AdminFilter,
  85. Enabled: true,
  86. },
  87. }
  88. case models.SMTP:
  89. u = &models.SMTPConfig{
  90. Auth: form.SMTPAuth,
  91. Host: form.SMTPHost,
  92. Port: form.SMTPPort,
  93. TLS: form.TLS,
  94. SkipVerify: form.SkipVerify,
  95. }
  96. case models.PAM:
  97. u = &models.PAMConfig{
  98. ServiceName: form.PAMServiceName,
  99. }
  100. default:
  101. ctx.Error(400)
  102. return
  103. }
  104. var source = &models.LoginSource{
  105. Type: models.LoginType(form.Type),
  106. Name: form.Name,
  107. IsActived: form.IsActive,
  108. AllowAutoRegister: form.AllowAutoRegister,
  109. Cfg: u,
  110. }
  111. if err := models.CreateSource(source); err != nil {
  112. ctx.Handle(500, "CreateSource", err)
  113. return
  114. }
  115. log.Trace("Authentication created by admin(%s): %s", ctx.User.Name, form.Name)
  116. ctx.Redirect(setting.AppSubUrl + "/admin/auths")
  117. }
  118. func EditAuthSource(ctx *middleware.Context) {
  119. ctx.Data["Title"] = ctx.Tr("admin.auths.edit")
  120. ctx.Data["PageIsAdmin"] = true
  121. ctx.Data["PageIsAdminAuthentications"] = true
  122. // ctx.Data["LoginTypes"] = models.LoginTypes
  123. ctx.Data["SMTPAuths"] = models.SMTPAuths
  124. id := com.StrTo(ctx.Params(":authid")).MustInt64()
  125. if id == 0 {
  126. ctx.Handle(404, "EditAuthSource", nil)
  127. return
  128. }
  129. u, err := models.GetLoginSourceByID(id)
  130. if err != nil {
  131. ctx.Handle(500, "GetLoginSourceById", err)
  132. return
  133. }
  134. ctx.Data["Source"] = u
  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["PageIsAuths"] = true
  142. // ctx.Data["LoginTypes"] = models.LoginTypes
  143. ctx.Data["SMTPAuths"] = models.SMTPAuths
  144. if ctx.HasError() {
  145. ctx.HTML(200, AUTH_EDIT)
  146. return
  147. }
  148. var config core.Conversion
  149. switch models.LoginType(form.Type) {
  150. case models.LDAP:
  151. fallthrough
  152. case models.DLDAP:
  153. config = &models.LDAPConfig{
  154. Ldapsource: ldap.Ldapsource{
  155. Name: form.Name,
  156. Host: form.Host,
  157. Port: form.Port,
  158. UseSSL: form.UseSSL,
  159. BindDN: form.BindDN,
  160. UserDN: form.UserDN,
  161. BindPassword: form.BindPassword,
  162. UserBase: form.UserBase,
  163. AttributeName: form.AttributeName,
  164. AttributeSurname: form.AttributeSurname,
  165. AttributeMail: form.AttributeMail,
  166. Filter: form.Filter,
  167. AdminFilter: form.AdminFilter,
  168. Enabled: true,
  169. },
  170. }
  171. case models.SMTP:
  172. config = &models.SMTPConfig{
  173. Auth: form.SMTPAuth,
  174. Host: form.SMTPHost,
  175. Port: form.SMTPPort,
  176. TLS: form.TLS,
  177. SkipVerify: form.SkipVerify,
  178. }
  179. case models.PAM:
  180. config = &models.PAMConfig{
  181. ServiceName: form.PAMServiceName,
  182. }
  183. default:
  184. ctx.Error(400)
  185. return
  186. }
  187. u := models.LoginSource{
  188. ID: form.ID,
  189. Name: form.Name,
  190. IsActived: form.IsActive,
  191. Type: models.LoginType(form.Type),
  192. AllowAutoRegister: form.AllowAutoRegister,
  193. Cfg: config,
  194. }
  195. if err := models.UpdateSource(&u); err != nil {
  196. ctx.Handle(500, "UpdateSource", err)
  197. return
  198. }
  199. log.Trace("Authentication changed by admin(%s): %s", ctx.User.Name, form.Name)
  200. ctx.Flash.Success(ctx.Tr("admin.auths.update_success"))
  201. ctx.Redirect(setting.AppSubUrl + "/admin/auths/" + ctx.Params(":authid"))
  202. }
  203. func DeleteAuthSource(ctx *middleware.Context) {
  204. id := com.StrTo(ctx.Params(":authid")).MustInt64()
  205. if id == 0 {
  206. ctx.Handle(404, "DeleteAuthSource", nil)
  207. return
  208. }
  209. a, err := models.GetLoginSourceByID(id)
  210. if err != nil {
  211. ctx.Handle(500, "GetLoginSourceById", err)
  212. return
  213. }
  214. if err = models.DelLoginSource(a); err != nil {
  215. switch err {
  216. case models.ErrAuthenticationUserUsed:
  217. ctx.Flash.Error("form.still_own_user")
  218. ctx.Redirect(setting.AppSubUrl + "/admin/auths/" + ctx.Params(":authid"))
  219. default:
  220. ctx.Handle(500, "DelLoginSource", err)
  221. }
  222. return
  223. }
  224. log.Trace("Authentication deleted by admin(%s): %s", ctx.User.Name, a.Name)
  225. ctx.Redirect(setting.AppSubUrl + "/admin/auths")
  226. }