vendredi 17 mai 2019

How to override/extend Broadcasters/Broadcaster.php from Laravel core source code?

I have written an extension of Illuminate/Broadcasting/Broadcasters/Broadcaster.php so that I can use a customized function (retrieveUser()).

How do I get Laravel to use it instead of the original?

As of now I have tried saving the class in the app/Providers directory and in a new app/Extensions directory.

I have tried calling the new class from config\app.php, but I either get Class does not exist or Call to undefined method isDeferred()

I have also tried creating a service provider to call it.

I have also tried creating a custom application that extends the default application as described here: https://mattstauffer.com/blog/extending-laravels-application/ However, there don't seem to be any references to Illuminate/Broadcasting in the default application which can even be overrode.

Here is the new class:

<?php

namespace App\Extensions;

use Illuminate\Support\Arr;
use Illuminate\Broadcasting\Broadcasters\Broadcaster as Broadcaster;
use App\User;
use Illuminate\Support\Facades\Crypt;

class EncryptedBroadcaster extends Broadcaster
{
    protected function retrieveUser($request, $channel)
    {
        $options = $this->retrieveChannelOptions($channel);

        $guards = $options['guards'] ?? null;

        if (is_null($guards)) {

            $authorization = $request->header('Authorization');
            $id = Crypt::decrypt($token);
            $user = User::find($id);
            return $user;
        }

        foreach (Arr::wrap($guards) as $guard) {
            if ($user = $request->user($guard)) {
                return $user;
            }
        }
    }


    public function auth($request)
    {
        $channelName = $this->normalizeChannelName($request->channel_name);

        if ($this->isGuardedChannel($request->channel_name) &&
            ! $this->retrieveUser($request, $channelName)) {
            throw new AccessDeniedHttpException;
        }

        return parent::verifyUserCanAccessChannel(
            $request, $channelName
        );
    }

    public function validAuthenticationResponse($request, $result)
    {
        if (is_bool($result)) {
            return json_encode($result);
        }

        $channelName = $this->normalizeChannelName($request->channel_name);

        return json_encode(['channel_data' => [
            'user_id' => $this->retrieveUser($request, $channelName)->getAuthIdentifier(),
            'user_info' => $result,
        ]]);
    }

    public function broadcast(array $channels, $event, array $payload = [])
    {
        $connection = $this->redis->connection($this->connection);

        $payload = json_encode([
            'event' => $event,
            'data' => $payload,
            'socket' => Arr::pull($payload, 'socket'),
        ]);

        foreach ($this->formatChannels($channels) as $channel) {
            $connection->publish($channel, $payload);
        }
    }

}

Here is my attempt at calling it in config/app.php:

'providers' => [

        // other existing providers ....

        App\Providers\EncryptedBroadcaster::class,

    ],



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire