auths.go 6.0 KB

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