Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.4] Model binding in broadcasting channel definitions #16120

Merged
merged 1 commit into from
Oct 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the first parameter is always the logged in user instance.

}

$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()
{
}
}