auth.go 1.1 KB

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