You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

auths.go 6.2 kB

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