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.

auths.go 6.3KB

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