0

我正在使用此软件包进行维护,但要定义哪些用户可以在停机期间访问该站点以及哪些用户不能有问题。

在过去的几天里,我一直在搜索并且阅读了很多内容,因为主控制器在中间件之后加载,它无法检测用户状态是否登录。(我不知道我只是反复看到

无论如何,这是问题所在:

我希望允许Admin具有访问该站点角色的用户在停机时间,并且visitorsother group of users不要。

到目前为止我做了什么

根据包文档,我App\Exemptions\AdminExemption.php用这些数据制作了客户文件:

<?php

namespace App\Exemptions;

use Auth; use Config; use App\User; use MisterPhilip\MaintenanceMode\Exemptions\MaintenanceModeExemption;

class AdminExemption extends MaintenanceModeExemption {
    public function isExempt()
    {

      if (Auth::check() && Auth::user()->role == 'Admin') {
          return true;
      }
      else {
         return false;
      }
        //if user is logged and it's admin show the site

        //if user is logged and isn't admin hide the site

        //if user isn't logged (is visitor) hide the site
    } }

我在包配置文件中注册了这个文件config\maintenancemode.php

'exemptions' => [
        App\Exemptions\AdminExemption::class,
],

并用 laravel 默认替换包类Kernel

protected $middleware = [
        // \App\Http\Middleware\CheckForMaintenanceMode::class,
        \MisterPhilip\MaintenanceMode\Http\Middleware\CheckForMaintenanceMode::class,
//others...
]

问题

Auth::check()或者auth()->user()这些Auth::user()都不能检测到登录的用户并假设用户没有登录(是访客)。因此,该网站对所有人甚至管理员都关闭。

问题

如何在AdminExemption.php文件中获取当前用户的真实状态?

地位

  1. 是用户(是管理员)show the site
  2. 是用户(不是管理员)don't show the site
  3. 不是用户(访客)don't show the site

任何的想法?

4

1 回答 1

1

您已经在主中间件堆栈中注册了中间件,这将在它到达中间件组之前发生。您可能希望将内核中的中间件向下移动到web中间件堆栈中。这是因为 Laravel 只会在有会话时才知道登录用户 - 并且会话仅在 Web 堆栈中启动。

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        // \Illuminate\Session\Middleware\AuthenticateSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],
];

您需要将中间件放在中间件之后StartSession- 但您可能最好将其弹出到最后,然后登录状态应该在您的中间件中可用。

于 2019-01-25T04:17:23.350 回答