auths.go 4.9 KB

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