【问题标题】:Routes horizon and websockets路由 Horizo​​n 和 websockets
【发布时间】:2020-10-21 03:45:40
【问题描述】:

我在 Laravel 中使用 Horizo​​n 和 web 套接字。如何只允许管理员访问这两项服务? 我只需要为此创建一个新的中间件吗?

示例:

/*
     * Dashboard Routes Middleware
     *
     * These middleware will be assigned to every dashboard route, giving you
     * the chance to add your own middleware to this list or change any of
     * the existing middleware. Or, you can simply stick with this list.
     */
    'middleware' => [
        'web',
        Authorize::class,
    ],

【问题讨论】:

    标签: laravel websocket routes laravel-middleware horizon


    【解决方案1】:

    为了查看 Horizo​​n 的仪表板,应该已经有一个配置。检查您的 config/app.php 在 providers 数组中是否有以下内容:

    '提供者' => [ ..., App\Providers\Horizo​​nServiceProvider::class ]

    稍后您可以在app/Providers/HorizonServiceProvider.php 中定义门访问逻辑,可能类似于:

    protected function gate()
    {
        Gate::define('viewHorizon', function ($user) {
            return $user->isAdmin();
        });
    }
    

    在您的用户模型中,为谁是管理员实现功能逻辑isAdmin()。这是一种方法。

    更多详情https://laravel.com/docs/7.x/horizon#dashboard-authorization

    对于web socket,你可以创建一个中间件就好了,类似于前面的例子来判断handle函数里面谁有访问权限。

    【讨论】:

      猜你喜欢
      • 2016-11-22
      • 1970-01-01
      • 2019-03-04
      • 2016-11-27
      • 2019-11-04
      • 1970-01-01
      • 2022-09-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多