You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.4 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
  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 context
  5. import (
  6. "net/url"
  7. "github.com/go-macaron/csrf"
  8. "gopkg.in/macaron.v1"
  9. "github.com/gogits/gogs/modules/auth"
  10. "github.com/gogits/gogs/modules/setting"
  11. )
  12. type ToggleOptions struct {
  13. SignInRequired bool
  14. SignOutRequired bool
  15. AdminRequired bool
  16. DisableCSRF bool
  17. }
  18. func Toggle(options *ToggleOptions) macaron.Handler {
  19. return func(ctx *Context) {
  20. // Cannot view any page before installation.
  21. if !setting.InstallLock {
  22. ctx.Redirect(setting.AppSubUrl + "/install")
  23. return
  24. }
  25. // Checking non-logged users landing page.
  26. if !ctx.IsSigned && ctx.Req.RequestURI == "/" && setting.LandingPageUrl != setting.LANDING_PAGE_HOME {
  27. ctx.Redirect(setting.AppSubUrl + string(setting.LandingPageUrl))
  28. return
  29. }
  30. // Redirect to dashboard if user tries to visit any non-login page.
  31. if options.SignOutRequired && ctx.IsSigned && ctx.Req.RequestURI != "/" {
  32. ctx.Redirect(setting.AppSubUrl + "/")
  33. return
  34. }
  35. if !options.SignOutRequired && !options.DisableCSRF && ctx.Req.Method == "POST" && !auth.IsAPIPath(ctx.Req.URL.Path) {
  36. csrf.Validate(ctx.Context, ctx.csrf)
  37. if ctx.Written() {
  38. return
  39. }
  40. }
  41. if options.SignInRequired {
  42. if !ctx.IsSigned {
  43. // Restrict API calls with error message.
  44. if auth.IsAPIPath(ctx.Req.URL.Path) {
  45. ctx.JSON(403, map[string]string{
  46. "message": "Only signed in user is allowed to call APIs.",
  47. })
  48. return
  49. }
  50. ctx.SetCookie("redirect_to", url.QueryEscape(setting.AppSubUrl+ctx.Req.RequestURI), 0, setting.AppSubUrl)
  51. ctx.Redirect(setting.AppSubUrl + "/user/login")
  52. return
  53. } else if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm {
  54. ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
  55. ctx.HTML(200, "user/auth/activate")
  56. return
  57. }
  58. }
  59. // Redirect to log in page if auto-signin info is provided and has not signed in.
  60. if !options.SignOutRequired && !ctx.IsSigned && !auth.IsAPIPath(ctx.Req.URL.Path) &&
  61. len(ctx.GetCookie(setting.CookieUserName)) > 0 {
  62. ctx.SetCookie("redirect_to", url.QueryEscape(setting.AppSubUrl+ctx.Req.RequestURI), 0, setting.AppSubUrl)
  63. ctx.Redirect(setting.AppSubUrl + "/user/login")
  64. return
  65. }
  66. if options.AdminRequired {
  67. if !ctx.User.IsAdmin {
  68. ctx.Error(403)
  69. return
  70. }
  71. ctx.Data["PageIsAdmin"] = true
  72. }
  73. }
  74. }