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.

auth.go 1.3 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 middleware
  5. import (
  6. "net/url"
  7. "github.com/go-martini/martini"
  8. "github.com/gogits/gogs/modules/base"
  9. )
  10. type ToggleOptions struct {
  11. SignInRequire bool
  12. SignOutRequire bool
  13. AdminRequire bool
  14. DisableCsrf bool
  15. }
  16. func Toggle(options *ToggleOptions) martini.Handler {
  17. return func(ctx *Context) {
  18. if !base.InstallLock {
  19. ctx.Redirect("/install")
  20. return
  21. }
  22. if options.SignOutRequire && ctx.IsSigned && ctx.Req.RequestURI != "/" {
  23. ctx.Redirect("/")
  24. return
  25. }
  26. if !options.DisableCsrf {
  27. if ctx.Req.Method == "POST" {
  28. if !ctx.CsrfTokenValid() {
  29. ctx.Error(403, "CSRF token does not match")
  30. return
  31. }
  32. }
  33. }
  34. if options.SignInRequire {
  35. if !ctx.IsSigned {
  36. ctx.SetCookie("redirect_to", "/"+url.QueryEscape(ctx.Req.RequestURI))
  37. ctx.Redirect("/user/login")
  38. return
  39. } else if !ctx.User.IsActive && base.Service.RegisterEmailConfirm {
  40. ctx.Data["Title"] = "Activate Your Account"
  41. ctx.HTML(200, "user/active")
  42. return
  43. }
  44. }
  45. if options.AdminRequire {
  46. if !ctx.User.IsAdmin {
  47. ctx.Error(403)
  48. return
  49. }
  50. ctx.Data["PageIsAdmin"] = true
  51. }
  52. }
  53. }