Skip to content

[12.x] Introduce excludeCan method #55382

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

Closed
wants to merge 2 commits into from
Closed
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
16 changes: 16 additions & 0 deletions src/Illuminate/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,22 @@ public function can($ability, $models = [])
: $this->middleware(['can:'.$ability.','.implode(',', Arr::wrap($models))]);
}

/**
* Specify that the "can" middleware for a specific ability should be removed from the route.
*
* @param \UnitEnum|string $ability
* @param array|string $models
* @return $this
*/
public function excludeCan($ability, $models = [])
{
$ability = enum_value($ability);

return empty($models)
? $this->withoutMiddleware(['can:'.$ability])
: $this->withoutMiddleware(['can:'.$ability.','.implode(',', Arr::wrap($models))]);
}

/**
* Get the middleware for the route's controller.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Routing/RouteRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* @method \Illuminate\Routing\Route put(string $uri, \Closure|array|string|null $action = null)
* @method \Illuminate\Routing\RouteRegistrar as(string $value)
* @method \Illuminate\Routing\RouteRegistrar can(\UnitEnum|string $ability, array|string $models = [])
* @method \Illuminate\Routing\RouteRegistrar excludeCan(\UnitEnum|string $ability, array|string $models = [])
* @method \Illuminate\Routing\RouteRegistrar controller(string $controller)
* @method \Illuminate\Routing\RouteRegistrar domain(\BackedEnum|string $value)
* @method \Illuminate\Routing\RouteRegistrar middleware(array|string|null $middleware)
Expand Down Expand Up @@ -66,6 +67,7 @@ class RouteRegistrar
protected $allowedAttributes = [
'as',
'can',
'excludeCan',
'controller',
'domain',
'middleware',
Expand Down
1 change: 1 addition & 0 deletions src/Illuminate/Support/Facades/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
* @method static \Illuminate\Routing\RouteRegistrar whereIn(array|string $parameters, array $values)
* @method static \Illuminate\Routing\RouteRegistrar as(string $value)
* @method static \Illuminate\Routing\RouteRegistrar can(\UnitEnum|string $ability, array|string $models = [])
* @method static \Illuminate\Routing\RouteRegistrar excludeCan(\UnitEnum|string $ability, array|string $models = [])
* @method static \Illuminate\Routing\RouteRegistrar controller(string $controller)
* @method static \Illuminate\Routing\RouteRegistrar domain(\BackedEnum|string $value)
* @method static \Illuminate\Routing\RouteRegistrar middleware(array|string|null $middleware)
Expand Down
94 changes: 94 additions & 0 deletions tests/Auth/AuthorizeMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,100 @@ public function testSimpleAbilityAuthorized()
$this->assertSame('success', $response->content());
}

public function testRouteCanHandleMultiplePermissionsInGroup()
{
$this->gate()->define('manage-company', function ($user) {
return true;
});

$this->gate()->define('manage-users', function ($user) {
return true;
});

$this->router->can('manage-company')->group(function () {
$this->router->get('company', [
'uses' => function () {
return 'Company Access Granted';
},
]);

$this->router->can('manage-users')->group(function () {
$this->router->get('users', [
'uses' => function () {
if (! app(GateContract::class)->allows('manage-company')) {
return 'Forbidden';
}

return 'Users Access Granted';
},
]);
});
});

$response = $this->router->dispatch(Request::create('company', 'GET'));
$this->assertSame('Company Access Granted', $response->content());

$response = $this->router->dispatch(Request::create('users', 'GET'));
$this->assertSame('Users Access Granted', $response->content());
}

public function testRouteCanOverrideMiddlewareInsideGroup()
{
$this->gate()->define('manage-company', function ($user) {
return true;
});

$this->gate()->define('manage-users', function ($user) {
return true;
});

$this->router->can('manage-company')->group(function () {
$this->router->can('manage-users')->get('overridden-resource', [
'uses' => function () {
return 'Overridden Access Granted';
},
]);
});

$response = $this->router->dispatch(Request::create('overridden-resource', 'GET'));
$this->assertSame('Overridden Access Granted', $response->content());
}

public function testRouteCanHandleMultiplePermissionsInGroupWithExcludeCan()
{
$this->gate()->define('manage-company', function ($user) {
return false;
});

$this->gate()->define('manage-users', function ($user) {
return true;
});

$this->router->can('manage-company')->group(function () {
$this->router->get('company', [
'uses' => function () {
return 'Company Access Denied';
},
]);

$this->router->excludeCan('manage-company')->can('manage-users')->get('users', [
'uses' => function () {
if (app(GateContract::class)->allows('manage-company')) {
return 'Forbidden';
}

return 'Users Access Granted Without Manage-Company';
},
]);
});

$response = $this->router->dispatch(Request::create('company', 'GET'));
$this->assertSame('Company Access Denied', $response->content());

$response = $this->router->dispatch(Request::create('users', 'GET'));
$this->assertSame('Users Access Granted Without Manage-Company', $response->content());
}

public function testSimpleAbilityWithStringParameter()
{
$this->gate()->define('view-dashboard', function ($user, $param) {
Expand Down
Loading