Skip to content
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
4 changes: 3 additions & 1 deletion src/Illuminate/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,9 @@ public function setAction(array $action)
}

if (isset($this->action['can'])) {
$this->can($this->action['can']);
foreach ($this->action['can'] as $can) {
$this->can($can[0], $can[1] ?? []);
}
}

return $this;
Expand Down
4 changes: 4 additions & 0 deletions src/Illuminate/Routing/RouteRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,10 @@ public function __call($method, $parameters)
return $this->attribute($method, is_array($parameters[0]) ? $parameters[0] : $parameters);
}

if ($method === 'can') {
return $this->attribute($method, [$parameters]);
}

return $this->attribute($method, array_key_exists(0, $parameters) ? $parameters[0] : true);
}

Expand Down
4 changes: 4 additions & 0 deletions src/Illuminate/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -1516,6 +1516,10 @@ public function __call($method, $parameters)
return (new RouteRegistrar($this))->attribute($method, is_array($parameters[0]) ? $parameters[0] : $parameters);
}

if ($method === 'can') {
return (new RouteRegistrar($this))->attribute($method, [$parameters]);
}

if ($method !== 'where' && Str::startsWith($method, 'where')) {
return (new RouteRegistrar($this))->{$method}(...$parameters);
}
Expand Down
23 changes: 23 additions & 0 deletions tests/Routing/RouteRegistrarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,29 @@ public function testCanSetMiddlewareCanOnGroups()
$this->seeMiddleware('can:test');
}

public function testCanSetMiddlewareCanWithModelsOnGroups()
{
$this->router->can('view', 'post')->group(function ($router) {
$router->get('/post/{post}');
});

$this->seeMiddleware('can:view,post');
}

public function testCanSetMiddlewareCanNestedOnGroups()
{
$this->router->can('access-admin')->group(function ($router) {
$router->can('edit', 'post')->group(function ($router) {
$router->get('/post/{post}/edit');
});
});

$this->assertEquals([
'can:access-admin',
'can:edit,post',
], $this->getRoute()->middleware());
}

public function testCanSetMiddlewareForSpecifiedMethodsOnRegisteredResource()
{
$this->router->resource('users', RouteRegistrarControllerStub::class)
Expand Down
9 changes: 9 additions & 0 deletions tests/Routing/RoutingRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2219,6 +2219,15 @@ public function testRouteCanMiddlewareCanBeAssigned()
$this->assertEquals([
'can:create',
], $route->middleware());

$route = new Route(['GET'], '/', []);
$route->can('create');
$route->can('update');

$this->assertEquals([
'can:create',
'can:update',
], $route->middleware());
}

public function testItDispatchesEventsWhilePreparingRequest()
Expand Down