Skip to content

Commit

Permalink
Model binding in broadcasting channel definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
themsaid committed Oct 27, 2016
1 parent 78c1e5b commit 6f761aa
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 11 deletions.
51 changes: 40 additions & 11 deletions src/Illuminate/Broadcasting/Broadcasters/Broadcaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace Illuminate\Broadcasting\Broadcasters;

use ReflectionFunction;
use ReflectionParameter;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Model;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Illuminate\Contracts\Broadcasting\Broadcaster as BroadcasterContract;

Expand Down Expand Up @@ -39,11 +42,11 @@ public function channel($channel, callable $callback)
protected function verifyUserCanAccessChannel($request, $channel)
{
foreach ($this->channels as $pattern => $callback) {
if (! Str::is($pattern, $channel)) {
if (! Str::is(preg_replace('/\{(.*?)\}/', '*', $pattern), $channel)) {
continue;
}

$parameters = $this->extractAuthParameters($pattern, $channel);
$parameters = $this->extractAuthParameters($pattern, $channel, $callback);

if ($result = $callback($request->user(), ...$parameters)) {
return $this->validAuthenticationResponse($request, $result);
Expand All @@ -58,23 +61,29 @@ protected function verifyUserCanAccessChannel($request, $channel)
*
* @param string $pattern
* @param string $channel
* @param callable $callback
* @return array
*/
protected function extractAuthParameters($pattern, $channel)
protected function extractAuthParameters($pattern, $channel, $callback)
{
if (! Str::contains($pattern, '*')) {
return [];
}
$parameters = [];

$pattern = preg_replace('/\{(.*?)\}/', '([^\.]+)', $pattern);

$pattern = str_replace('\*', '([^\.]+)', preg_quote($pattern));
preg_match('/^'.$pattern.'/', $channel, $keys);

if (preg_match('/^'.$pattern.'/', $channel, $keys)) {
array_shift($keys);
$callbackParameters = (new ReflectionFunction($callback))->getParameters();

return $keys;
foreach ($callbackParameters as $parameter) {
if ($parameter->getPosition() === 0) {
continue;
}

$parameters[] = ! isset($keys[$parameter->getPosition()])
? null : $this->getAuthParameterFromKeys($parameter, $keys);
}

return [];
return $parameters;
}

/**
Expand All @@ -89,4 +98,24 @@ protected function formatChannels(array $channels)
return (string) $channel;
}, $channels);
}

/**
* Extract a parameter from the given keys.
*
* @param ReflectionParameter $parameter
* @param array $keys
* @return mixed
*/
protected function getAuthParameterFromKeys($parameter, $keys)
{
$key = $keys[$parameter->getPosition()];

if ($parameter->getClass() && $parameter->getClass()->isSubclassOf(Model::class)) {
$model = $parameter->getClass()->newInstance();

return $model->where($model->getRouteKeyName(), $key)->first();
}

return $key;
}
}
102 changes: 102 additions & 0 deletions tests/Broadcasting/BroadcasterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

use Mockery as m;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Broadcasting\Broadcasters\Broadcaster;

class BroadcasterTest extends PHPUnit_Framework_TestCase
{
public function tearDown()
{
m::close();
}

public function testExtractingParametersWhileCheckingForUserAccess()
{
$broadcaster = new FakeBroadcaster();

$callback = function ($user, BroadcasterTestEloquentModelStub $model, $nonModel) {
};
$parameters = $broadcaster->extractAuthParameters('asd.{model}.{nonModel}', 'asd.1.something', $callback);
$this->assertEquals(['model.1.instance', 'something'], $parameters);

$callback = function ($user, BroadcasterTestEloquentModelStub $model, BroadcasterTestEloquentModelStub $model2, $something) {
};
$parameters = $broadcaster->extractAuthParameters('asd.{model}.{model}.{nonModel}', 'asd.1.uid.something', $callback);
$this->assertEquals(['model.1.instance', 'model.uid.instance', 'something'], $parameters);

$callback = function ($user) {
};
$parameters = $broadcaster->extractAuthParameters('asd', 'asd', $callback);
$this->assertEquals([], $parameters);

$callback = function ($user, $something) {
};
$parameters = $broadcaster->extractAuthParameters('asd', 'asd', $callback);
$this->assertEquals([null], $parameters);

$callback = function ($user, BroadcasterTestEloquentModelNotFoundStub $model) {
};
$parameters = $broadcaster->extractAuthParameters('asd.{model}', 'asd.1', $callback);
$this->assertEquals([null], $parameters);
}
}

class FakeBroadcaster extends Broadcaster
{
public function auth($request)
{
}

public function validAuthenticationResponse($request, $result)
{
}

public function broadcast(array $channels, $event, array $payload = [])
{
}

public function extractAuthParameters($pattern, $channel, $callback)
{
return parent::extractAuthParameters($pattern, $channel, $callback);
}
}

class BroadcasterTestEloquentModelStub extends Model
{
public function getRouteKeyName()
{
return 'id';
}

public function where($key, $value)
{
$this->value = $value;

return $this;
}

public function first()
{
return "model.{$this->value}.instance";
}
}

class BroadcasterTestEloquentModelNotFoundStub extends Model
{
public function getRouteKeyName()
{
return 'id';
}

public function where($key, $value)
{
$this->value = $value;

return $this;
}

public function first()
{
}
}

0 comments on commit 6f761aa

Please sign in to comment.