auths.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. "fmt"
  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/context"
  14. "github.com/gogits/gogs/modules/log"
  15. "github.com/gogits/gogs/modules/setting"
  16. )
  17. const (
  18. AUTHS base.TplName = "admin/auth/list"
  19. AUTH_NEW base.TplName = "admin/auth/new"
  20. AUTH_EDIT base.TplName = "admin/auth/edit"
  21. )
  22. func Authentications(ctx *context.Context) {
  23. ctx.Data["Title"] = ctx.Tr("admin.authentication")
  24. ctx.Data["PageIsAdmin"] = true
  25. ctx.Data["PageIsAdminAuthentications"] = true
  26. var err error
  27. ctx.Data["Sources"], err = models.LoginSources()
  28. if err != nil {
  29. ctx.Handle(500, "LoginSources", err)
  30. return
  31. }
  32. ctx.Data["Total"] = models.CountLoginSources()
  33. ctx.HTML(200, AUTHS)
  34. }
  35. type AuthSource struct {
  36. Name string
  37. Type models.LoginType
  38. }
  39. var authSources = []AuthSource{
  40. {models.LoginNames[models.LOGIN_LDAP], models.LOGIN_LDAP},
  41. {models.LoginNames[models.LOGIN_DLDAP], models.LOGIN_DLDAP},
  42. {models.LoginNames[models.LOGIN_SMTP], models.LOGIN_SMTP},
  43. {models.LoginNames[models.LOGIN_PAM], models.LOGIN_PAM},
  44. }
  45. func NewAuthSource(ctx *context.Context) {
  46. ctx.Data["Title"] = ctx.Tr("admin.auths.new")
  47. ctx.Data["PageIsAdmin"] = true
  48. ctx.Data["PageIsAdminAuthentications"] = true
  49. ctx.Data["type"] = models.LOGIN_LDAP
  50. ctx.Data["CurTypeName"] = models.LoginNames[models.LOGIN_LDAP]
  51. ctx.Data["smtp_auth"] = "PLAIN"
  52. ctx.Data["is_active"] = true
  53. ctx.Data["AuthSources"] = authSources
  54. ctx.Data["SMTPAuths"] = models.SMTPAuths
  55. ctx.HTML(200, AUTH_NEW)
  56. }
  57. func parseLDAPConfig(form auth.AuthenticationForm) *models.LDAPConfig {
  58. return &models.LDAPConfig{
  59. Source: &ldap.Source{
  60. Name: form.Name,
  61. Host: form.Host,
  62. Port: form.Port,
  63. UseSSL: form.TLS,
  64. SkipVerify: form.SkipVerify,
  65. BindDN: form.BindDN,
  66. UserDN: form.UserDN,
  67. BindPassword: form.BindPassword,
  68. UserBase: form.UserBase,
  69. AttributeUsername: form.AttributeUsername,
  70. AttributeName: form.AttributeName,
  71. AttributeSurname: form.AttributeSurname,
  72. AttributeMail: form.AttributeMail,
  73. AttributesInBind: form.AttributesInBind,
  74. Filter: form.Filter,
  75. AdminFilter: form.AdminFilter,
  76. Enabled: true,
  77. },
  78. }
  79. }
  80. func parseSMTPConfig(form auth.AuthenticationForm) *models.SMTPConfig {
  81. return &models.SMTPConfig{
  82. Auth: form.SMTPAuth,
  83. Host: form.SMTPHost,
  84. Port: form.SMTPPort,
  85. AllowedDomains: form.AllowedDomains,
  86. TLS: form.TLS,
  87. SkipVerify: form.SkipVerify,
  88. }
  89. }
  90. func NewAuthSourcePost(ctx *context.Context, form auth.AuthenticationForm) {
  91. ctx.Data["Title"] = ctx.Tr("admin.auths.new")
  92. ctx.Data["PageIsAdmin"] = true
  93. ctx.Data["PageIsAdminAuthentications"] = true
  94. ctx.Data["CurTypeName"] = models.LoginNames[models.LoginType(form.Type)]
  95. ctx.Data["AuthSources"] = authSources
  96. ctx.Data["SMTPAuths"] = models.SMTPAuths
  97. if ctx.HasError() {
  98. ctx.HTML(200, AUTH_NEW)
  99. return
  100. }
  101. var config core.Conversion
  102. switch models.LoginType(form.Type) {
  103. case models.LOGIN_LDAP, models.LOGIN_DLDAP:
  104. config = parseLDAPConfig(form)
  105. case models.LOGIN_SMTP:
  106. config = parseSMTPConfig(form)
  107. case models.LOGIN_PAM:
  108. config = &models.PAMConfig{
  109. ServiceName: form.PAMServiceName,
  110. }
  111. default:
  112. ctx.Error(400)
  113. return
  114. }
  115. if err := models.CreateSource(&models.LoginSource{
  116. Type: models.LoginType(form.Type),
  117. Name: form.Name,
  118. IsActived: form.IsActive,
  119. Cfg: config,
  120. }); err != nil {
  121. ctx.Handle(500, "CreateSource", err)
  122. return
  123. }
  124. log.Trace("Authentication created by admin(%s): %s", ctx.User.Name, form.Name)
  125. ctx.Flash.Success(ctx.Tr("admin.auths.new_success", form.Name))
  126. ctx.Redirect(setting.AppSubUrl + "/admin/auths")
  127. }
  128. func EditAuthSource(ctx *context.Context) {
  129. ctx.Data["Title"] = ctx.Tr("admin.auths.edit")
  130. ctx.Data["PageIsAdmin"] = true
  131. ctx.Data["PageIsAdminAuthentications"] = true
  132. ctx.Data["SMTPAuths"] = models.SMTPAuths
  133. source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid"))
  134. if err != nil {
  135. ctx.Handle(500, "GetLoginSourceByID", err)
  136. return
  137. }
  138. ctx.Data["Source"] = source
  139. ctx.HTML(200, AUTH_EDIT)
  140. }
  141. func EditAuthSourcePost(ctx *context.Context, form auth.AuthenticationForm) {
  142. ctx.Data["Title"] = ctx.Tr("admin.auths.edit")
  143. ctx.Data["PageIsAdmin"] = true
  144. ctx.Data["PageIsAdminAuthentications"] = true
  145. ctx.Data["SMTPAuths"] = models.SMTPAuths
  146. source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid"))
  147. if err != nil {
  148. ctx.Handle(500, "GetLoginSourceByID", err)
  149. return
  150. }
  151. ctx.Data["Source"] = source
  152. if ctx.HasError() {
  153. ctx.HTML(200, AUTH_EDIT)
  154. return
  155. }
  156. var config core.Conversion
  157. switch models.LoginType(form.Type) {
  158. case models.LOGIN_LDAP, models.LOGIN_DLDAP:
  159. config = parseLDAPConfig(form)
  160. case models.LOGIN_SMTP:
  161. config = parseSMTPConfig(form)
  162. case models.LOGIN_PAM:
  163. config = &models.PAMConfig{
  164. ServiceName: form.PAMServiceName,
  165. }
  166. default:
  167. ctx.Error(400)
  168. return
  169. }
  170. source.Name = form.Name
  171. source.IsActived = form.IsActive
  172. source.Cfg = config
  173. if err := models.UpdateSource(source); err != nil {
  174. ctx.Handle(500, "UpdateSource", err)
  175. return
  176. }
  177. log.Trace("Authentication changed by admin(%s): %s", ctx.User.Name, source.ID)
  178. ctx.Flash.Success(ctx.Tr("admin.auths.update_success"))
  179. ctx.Redirect(setting.AppSubUrl + "/admin/auths/" + com.ToStr(form.ID))
  180. }
  181. func DeleteAuthSource(ctx *context.Context) {
  182. source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid"))
  183. if err != nil {
  184. ctx.Handle(500, "GetLoginSourceByID", err)
  185. return
  186. }
  187. if err = models.DeleteSource(source); err != nil {
  188. switch err {
  189. case models.ErrAuthenticationUserUsed:
  190. ctx.Flash.Error(ctx.Tr("admin.auths.still_in_used"))
  191. default:
  192. ctx.Flash.Error(fmt.Sprintf("DeleteSource: %v", err))
  193. }
  194. ctx.JSON(200, map[string]interface{}{
  195. "redirect": setting.AppSubUrl + "/admin/auths/" + ctx.Params(":authid"),
  196. })
  197. return
  198. }
  199. log.Trace("Authentication deleted by admin(%s): %d", ctx.User.Name, source.ID)
  200. ctx.Flash.Success(ctx.Tr("admin.auths.deletion_success"))
  201. ctx.JSON(200, map[string]interface{}{
  202. "redirect": setting.AppSubUrl + "/admin/auths",
  203. })
  204. }
PANIC: session(release): write data/sessions/d/6/d60d988c16e58c47: no space left on device

PANIC

session(release): write data/sessions/d/6/d60d988c16e58c47: no space left on device
github.com/go-macaron/session@v0.0.0-20190805070824-1a3cdc6f5659/session.go:199 (0x8b2934)
gopkg.in/macaron.v1@v1.3.9/context.go:79 (0x83d0a0)
github.com/go-macaron/inject@v0.0.0-20160627170012-d8a0b8677191/inject.go:157 (0x80ab07)
github.com/go-macaron/inject@v0.0.0-20160627170012-d8a0b8677191/inject.go:135 (0x80a8a8)
gopkg.in/macaron.v1@v1.3.9/context.go:121 (0x83d1f8)
gopkg.in/macaron.v1@v1.3.9/context.go:112 (0x84fdb5)
gopkg.in/macaron.v1@v1.3.9/recovery.go:161 (0x84fda8)
gopkg.in/macaron.v1@v1.3.9/logger.go:40 (0x840c73)
github.com/go-macaron/inject@v0.0.0-20160627170012-d8a0b8677191/inject.go:157 (0x80ab07)
github.com/go-macaron/inject@v0.0.0-20160627170012-d8a0b8677191/inject.go:135 (0x80a8a8)
gopkg.in/macaron.v1@v1.3.9/context.go:121 (0x83d1f8)
gopkg.in/macaron.v1@v1.3.9/router.go:187 (0x850fc6)
gopkg.in/macaron.v1@v1.3.9/router.go:303 (0x8493e5)
gopkg.in/macaron.v1@v1.3.9/macaron.go:220 (0x841fca)
net/http/server.go:2836 (0x7a79b2)
net/http/server.go:1924 (0x7a341b)
runtime/asm_amd64.s:1373 (0x46f9f0)