auth.go 5.0 KB

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