auths.go 6.2 KB

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