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.

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