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.7 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
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. "fmt"
  7. "net/url"
  8. "strings"
  9. "github.com/Unknwon/macaron"
  10. "github.com/macaron-contrib/csrf"
  11. "github.com/gogits/gogs/modules/setting"
  12. )
  13. type ToggleOptions struct {
  14. SignInRequire bool
  15. SignOutRequire bool
  16. AdminRequire bool
  17. DisableCsrf bool
  18. }
  19. func Toggle(options *ToggleOptions) macaron.Handler {
  20. return func(ctx *Context) {
  21. // Cannot view any page before installation.
  22. if !setting.InstallLock {
  23. ctx.Redirect("/install")
  24. return
  25. }
  26. // Redirect to dashboard if user tries to visit any non-login page.
  27. if options.SignOutRequire && ctx.IsSigned && ctx.Req.RequestURI != "/" {
  28. ctx.Redirect("/")
  29. return
  30. }
  31. if !options.SignOutRequire && !options.DisableCsrf && ctx.Req.Method == "POST" {
  32. csrf.Validate(ctx.Context, ctx.csrf)
  33. if ctx.Written() {
  34. return
  35. }
  36. }
  37. if options.SignInRequire {
  38. fmt.Println(ctx.User.IsActive, setting.Service.RegisterEmailConfirm)
  39. if !ctx.IsSigned {
  40. // Ignore watch repository operation.
  41. if strings.HasSuffix(ctx.Req.RequestURI, "watch") {
  42. return
  43. }
  44. ctx.SetCookie("redirect_to", "/"+url.QueryEscape(ctx.Req.RequestURI))
  45. ctx.Redirect("/user/login")
  46. return
  47. } else if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
  48. ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
  49. ctx.HTML(200, "user/activate")
  50. return
  51. }
  52. }
  53. if options.AdminRequire {
  54. if !ctx.User.IsAdmin {
  55. ctx.Error(403)
  56. return
  57. }
  58. ctx.Data["PageIsAdmin"] = true
  59. }
  60. }
  61. }