Skip to content

Commit 309c087

Browse files
authored
[12.x] Fix nested can and inherit models on route groups (#57172)
* Add failing tests * Support multiple can() methods and models
1 parent f0b9e09 commit 309c087

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

src/Illuminate/Routing/Route.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,9 @@ public function setAction(array $action)
10081008
}
10091009

10101010
if (isset($this->action['can'])) {
1011-
$this->can($this->action['can']);
1011+
foreach ($this->action['can'] as $can) {
1012+
$this->can($can[0], $can[1] ?? []);
1013+
}
10121014
}
10131015

10141016
return $this;

src/Illuminate/Routing/RouteRegistrar.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,10 @@ public function __call($method, $parameters)
297297
return $this->attribute($method, is_array($parameters[0]) ? $parameters[0] : $parameters);
298298
}
299299

300+
if ($method === 'can') {
301+
return $this->attribute($method, [$parameters]);
302+
}
303+
300304
return $this->attribute($method, array_key_exists(0, $parameters) ? $parameters[0] : true);
301305
}
302306

src/Illuminate/Routing/Router.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,6 +1516,10 @@ public function __call($method, $parameters)
15161516
return (new RouteRegistrar($this))->attribute($method, is_array($parameters[0]) ? $parameters[0] : $parameters);
15171517
}
15181518

1519+
if ($method === 'can') {
1520+
return (new RouteRegistrar($this))->attribute($method, [$parameters]);
1521+
}
1522+
15191523
if ($method !== 'where' && Str::startsWith($method, 'where')) {
15201524
return (new RouteRegistrar($this))->{$method}(...$parameters);
15211525
}

tests/Routing/RouteRegistrarTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,29 @@ public function testCanSetMiddlewareCanOnGroups()
909909
$this->seeMiddleware('can:test');
910910
}
911911

912+
public function testCanSetMiddlewareCanWithModelsOnGroups()
913+
{
914+
$this->router->can('view', 'post')->group(function ($router) {
915+
$router->get('/post/{post}');
916+
});
917+
918+
$this->seeMiddleware('can:view,post');
919+
}
920+
921+
public function testCanSetMiddlewareCanNestedOnGroups()
922+
{
923+
$this->router->can('access-admin')->group(function ($router) {
924+
$router->can('edit', 'post')->group(function ($router) {
925+
$router->get('/post/{post}/edit');
926+
});
927+
});
928+
929+
$this->assertEquals([
930+
'can:access-admin',
931+
'can:edit,post',
932+
], $this->getRoute()->middleware());
933+
}
934+
912935
public function testCanSetMiddlewareForSpecifiedMethodsOnRegisteredResource()
913936
{
914937
$this->router->resource('users', RouteRegistrarControllerStub::class)

tests/Routing/RoutingRouteTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2219,6 +2219,15 @@ public function testRouteCanMiddlewareCanBeAssigned()
22192219
$this->assertEquals([
22202220
'can:create',
22212221
], $route->middleware());
2222+
2223+
$route = new Route(['GET'], '/', []);
2224+
$route->can('create');
2225+
$route->can('update');
2226+
2227+
$this->assertEquals([
2228+
'can:create',
2229+
'can:update',
2230+
], $route->middleware());
22222231
}
22232232

22242233
public function testItDispatchesEventsWhilePreparingRequest()

0 commit comments

Comments
 (0)