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 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. "strings"
  7. "github.com/go-martini/martini"
  8. "github.com/go-xorm/core"
  9. "github.com/gogits/gogs/models"
  10. "github.com/gogits/gogs/modules/auth"
  11. "github.com/gogits/gogs/modules/auth/ldap"
  12. "github.com/gogits/gogs/modules/base"
  13. "github.com/gogits/gogs/modules/log"
  14. "github.com/gogits/gogs/modules/middleware"
  15. )
  16. func NewAuthSource(ctx *middleware.Context) {
  17. ctx.Data["Title"] = "New Authentication"
  18. ctx.Data["PageIsAuths"] = true
  19. ctx.Data["LoginTypes"] = models.LoginTypes
  20. ctx.Data["SMTPAuths"] = models.SMTPAuths
  21. ctx.HTML(200, "admin/auths/new")
  22. }
  23. func NewAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
  24. ctx.Data["Title"] = "New Authentication"
  25. ctx.Data["PageIsAuths"] = true
  26. ctx.Data["LoginTypes"] = models.LoginTypes
  27. ctx.Data["SMTPAuths"] = models.SMTPAuths
  28. if ctx.HasError() {
  29. ctx.HTML(200, "admin/auths/new")
  30. return
  31. }
  32. var u core.Conversion
  33. switch form.Type {
  34. case models.LT_LDAP:
  35. u = &models.LDAPConfig{
  36. Ldapsource: ldap.Ldapsource{
  37. Host: form.Host,
  38. Port: form.Port,
  39. UseSSL: form.UseSSL,
  40. BaseDN: form.BaseDN,
  41. Attributes: form.Attributes,
  42. Filter: form.Filter,
  43. MsAdSAFormat: form.MsAdSA,
  44. Enabled: true,
  45. Name: form.AuthName,
  46. },
  47. }
  48. case models.LT_SMTP:
  49. u = &models.SMTPConfig{
  50. Auth: form.SmtpAuth,
  51. Host: form.SmtpHost,
  52. Port: form.SmtpPort,
  53. TLS: form.Tls,
  54. }
  55. default:
  56. ctx.Error(400)
  57. return
  58. }
  59. var source = &models.LoginSource{
  60. Type: form.Type,
  61. Name: form.AuthName,
  62. IsActived: true,
  63. AllowAutoRegister: form.AllowAutoRegister,
  64. Cfg: u,
  65. }
  66. if err := models.AddSource(source); err != nil {
  67. ctx.Handle(500, "admin.auths.NewAuth", err)
  68. return
  69. }
  70. log.Trace("%s Authentication created by admin(%s): %s", ctx.Req.RequestURI,
  71. ctx.User.LowerName, strings.ToLower(form.AuthName))
  72. ctx.Redirect("/admin/auths")
  73. }
  74. func EditAuthSource(ctx *middleware.Context, params martini.Params) {
  75. ctx.Data["Title"] = "Edit Authentication"
  76. ctx.Data["PageIsAuths"] = true
  77. ctx.Data["LoginTypes"] = models.LoginTypes
  78. ctx.Data["SMTPAuths"] = models.SMTPAuths
  79. id, err := base.StrTo(params["authid"]).Int64()
  80. if err != nil {
  81. ctx.Handle(404, "admin.auths.EditAuthSource", err)
  82. return
  83. }
  84. u, err := models.GetLoginSourceById(id)
  85. if err != nil {
  86. ctx.Handle(500, "admin.user.EditUser", err)
  87. return
  88. }
  89. ctx.Data["Source"] = u
  90. ctx.HTML(200, "admin/auths/edit")
  91. }
  92. func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
  93. ctx.Data["Title"] = "Edit Authentication"
  94. ctx.Data["PageIsAuths"] = true
  95. ctx.Data["LoginTypes"] = models.LoginTypes
  96. ctx.Data["SMTPAuths"] = models.SMTPAuths
  97. if ctx.HasError() {
  98. ctx.HTML(200, "admin/auths/edit")
  99. return
  100. }
  101. var config core.Conversion
  102. switch form.Type {
  103. case models.LT_LDAP:
  104. config = &models.LDAPConfig{
  105. Ldapsource: ldap.Ldapsource{
  106. Host: form.Host,
  107. Port: form.Port,
  108. UseSSL: form.UseSSL,
  109. BaseDN: form.BaseDN,
  110. Attributes: form.Attributes,
  111. Filter: form.Filter,
  112. MsAdSAFormat: form.MsAdSA,
  113. Enabled: true,
  114. Name: form.AuthName,
  115. },
  116. }
  117. case models.LT_SMTP:
  118. config = &models.SMTPConfig{
  119. Auth: form.SmtpAuth,
  120. Host: form.SmtpHost,
  121. Port: form.SmtpPort,
  122. TLS: form.Tls,
  123. }
  124. default:
  125. ctx.Error(400)
  126. return
  127. }
  128. u := models.LoginSource{
  129. Id: form.Id,
  130. Name: form.AuthName,
  131. IsActived: form.IsActived,
  132. Type: form.Type,
  133. AllowAutoRegister: form.AllowAutoRegister,
  134. Cfg: config,
  135. }
  136. if err := models.UpdateSource(&u); err != nil {
  137. ctx.Handle(500, "admin.auths.EditAuth", err)
  138. return
  139. }
  140. log.Trace("%s Authentication changed by admin(%s): %s", ctx.Req.RequestURI,
  141. ctx.User.LowerName, form.AuthName)
  142. ctx.Redirect("/admin/auths")
  143. }
  144. func DeleteAuthSource(ctx *middleware.Context, params martini.Params) {
  145. ctx.Data["Title"] = "Delete Authentication"
  146. ctx.Data["PageIsAuths"] = true
  147. id, err := base.StrTo(params["authid"]).Int64()
  148. if err != nil {
  149. ctx.Handle(404, "admin.auths.DeleteAuth", err)
  150. return
  151. }
  152. a, err := models.GetLoginSourceById(id)
  153. if err != nil {
  154. ctx.Handle(500, "admin.auths.DeleteAuth", err)
  155. return
  156. }
  157. if err = models.DelLoginSource(a); err != nil {
  158. switch err {
  159. case models.ErrAuthenticationUserUsed:
  160. ctx.Flash.Error("This authentication still has used by some users, you should move them and then delete again.")
  161. ctx.Redirect("/admin/auths/" + params["authid"])
  162. default:
  163. ctx.Handle(500, "admin.auths.DeleteAuth", err)
  164. }
  165. return
  166. }
  167. log.Trace("%s Authentication deleted by admin(%s): %s", ctx.Req.RequestURI,
  168. ctx.User.LowerName, ctx.User.LowerName)
  169. ctx.Redirect("/admin/auths")
  170. }