代码之家  ›  专栏  ›  技术社区  ›  Hasan Teymoori

多守卫拉雷维尔广播

  •  2
  • Hasan Teymoori  · 技术社区  · 7 年前

    我有下面为我的应用程序定义的身份验证保护 admins , designers , customers 等等。默认的守卫是 designer guard

    我想要每一个 guard 拥有自己的 private channel 所以我在我的频道.php下面每种都有多个条目

    Broadcast::channel('private.admins.{id}', function ($admin, $id) {
    
    
        Log::info($admin);
        //logging the admin
    
    });
    

    但这总是 binding 具有 default guard 类,所以我的问题是如何告诉它在这里使用 Admin model 我哪儿也找不到。你们能给我指一下正确的方向吗

    警卫 私人频道 .

    1 回复  |  直到 7 年前
        1
  •  9
  •   rkj    5 年前

    试着换一下 BroadcastServiceProvider 文件 app\Providers\BroadcastServiceProvider.php

    每个卫兵的广播授权端点不同

    public function boot()
    {
       //Broadcast::routes();
       //match any of the 3 auth guards
       Broadcast::routes(['middleware' => ['web','auth:admins,designers,customers']]);
       require base_path('routes/channels.php');
    }
    

    Broadcast::channel('admins.channel.{id}', function ($model, $id) {
          return $model->id === $id && get_class($model) === 'App\Admin';
    });
    
    Broadcast::channel('designers.channel.{id}', function ($model, $id) {
          return $model->id === $id && get_class($model) === 'App\Designer';
    });
    
    Broadcast::channel('customers.channel.{id}', function ($model, $id) {
          return $model->id === $id && get_class($model) === 'App\Customer';
    });
    
        2
  •  1
  •   Hadi Aghandeh    5 年前

    我贴出这个答案,为每一个谁可能面临的问题没有几天。我用的是拉斐尔7和 beyondcode/laravel-websockets . 当我在Bora中挖掘指定midleware的源代码时dcastServiceProvider.php不起作用。为通道定义保护的唯一方法是为通道指定选项:

     Broadcast::channel('messaging.organ.{id}', function ($organ , $id) {
        return $organ->id == $id && get_class($organ) === "App\Organization";
    } , ['guards' => ['organ']]);
    

    : 因为我正在使用 所以我开始努力 src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php 在这个文件中 retrieveUser 方法将获取用户。在此文件中,如果为通道提供了选项,则将返回指定保护中的用户。您可以定义一个或多个保护程序,但是它只返回一个作为数组中第一个守护程序登录的用户。

        protected function retrieveUser($request, $channel)
    {
        $options = $this->retrieveChannelOptions($channel);
    
        $guards = $options['guards'] ?? null;
    
        if (is_null($guards)) {
            return $request->user();
        }
    
        foreach (Arr::wrap($guards) as $guard) {
            if ($user = $request->user($guard)) {
                return $user;
            }
        }
    }
    
    推荐文章