From 6cfe0108f7eb97cb6babf9802665ceee4330f2bb Mon Sep 17 00:00:00 2001 From: Anton Komarev <1849174+antonkomarev@users.noreply.github.com> Date: Thu, 28 Dec 2023 19:46:25 +0300 Subject: [PATCH] [10.x] Expand Gate::allows & Gate::denies signature (#49503) * [10.x] Expand Gate::allows & Gate::denies signature * [10.x] Expand Gate::allows & Gate::denies signature * [10.x] Expand Gate::allows & Gate::denies signature --------- Co-authored-by: Anton Komarev --- src/Illuminate/Auth/Access/Gate.php | 16 ++++---- src/Illuminate/Contracts/Auth/Access/Gate.php | 12 +++--- tests/Auth/AuthAccessGateTest.php | 41 +++++++++++++++++++ 3 files changed, 55 insertions(+), 14 deletions(-) diff --git a/src/Illuminate/Auth/Access/Gate.php b/src/Illuminate/Auth/Access/Gate.php index e8ef93b64271..c757714c28cc 100644 --- a/src/Illuminate/Auth/Access/Gate.php +++ b/src/Illuminate/Auth/Access/Gate.php @@ -318,27 +318,27 @@ public function after(callable $callback) } /** - * Determine if the given ability should be granted for the current user. + * Determine if all of the given abilities should be granted for the current user. * - * @param string $ability + * @param iterable|string $abilities * @param array|mixed $arguments * @return bool */ - public function allows($ability, $arguments = []) + public function allows($abilities, $arguments = []) { - return $this->check($ability, $arguments); + return $this->check($abilities, $arguments); } /** - * Determine if the given ability should be denied for the current user. + * Determine if any of the given abilities should be denied for the current user. * - * @param string $ability + * @param iterable|string $abilities * @param array|mixed $arguments * @return bool */ - public function denies($ability, $arguments = []) + public function denies($abilities, $arguments = []) { - return ! $this->allows($ability, $arguments); + return ! $this->allows($abilities, $arguments); } /** diff --git a/src/Illuminate/Contracts/Auth/Access/Gate.php b/src/Illuminate/Contracts/Auth/Access/Gate.php index b88ab17965ed..1e8b8f87041b 100644 --- a/src/Illuminate/Contracts/Auth/Access/Gate.php +++ b/src/Illuminate/Contracts/Auth/Access/Gate.php @@ -57,22 +57,22 @@ public function before(callable $callback); public function after(callable $callback); /** - * Determine if the given ability should be granted for the current user. + * Determine if all of the given abilities should be granted for the current user. * - * @param string $ability + * @param iterable|string $abilities * @param array|mixed $arguments * @return bool */ - public function allows($ability, $arguments = []); + public function allows($abilities, $arguments = []); /** - * Determine if the given ability should be denied for the current user. + * Determine if any of the given abilities should be denied for the current user. * - * @param string $ability + * @param iterable|string $abilities * @param array|mixed $arguments * @return bool */ - public function denies($ability, $arguments = []); + public function denies($abilities, $arguments = []); /** * Determine if all of the given abilities should be granted for the current user. diff --git a/tests/Auth/AuthAccessGateTest.php b/tests/Auth/AuthAccessGateTest.php index c6ed6c2cbcc4..ad817dc3d374 100644 --- a/tests/Auth/AuthAccessGateTest.php +++ b/tests/Auth/AuthAccessGateTest.php @@ -325,6 +325,47 @@ public function testAfterCallbacksDoNotOverrideEachOther() $this->assertTrue($gate->denies('deny')); } + public function testArrayAbilitiesInAllows() + { + $gate = $this->getBasicGate(); + + $gate->define('allow_1', function ($user) { + return true; + }); + $gate->define('allow_2', function ($user) { + return true; + }); + $gate->define('deny', function ($user) { + return false; + }); + + $this->assertTrue($gate->allows(['allow_1'])); + $this->assertTrue($gate->allows(['allow_1', 'allow_2'])); + $this->assertFalse($gate->allows(['allow_1', 'allow_2', 'deny'])); + $this->assertFalse($gate->allows(['deny', 'allow_1', 'allow_2'])); + } + + public function testArrayAbilitiesInDenies() + { + $gate = $this->getBasicGate(); + + $gate->define('deny_1', function ($user) { + return false; + }); + $gate->define('deny_2', function ($user) { + return false; + }); + $gate->define('allow', function ($user) { + return true; + }); + + $this->assertTrue($gate->denies(['deny_1'])); + $this->assertTrue($gate->denies(['deny_1', 'deny_2'])); + $this->assertTrue($gate->denies(['deny_1', 'allow'])); + $this->assertTrue($gate->denies(['allow', 'deny_1'])); + $this->assertFalse($gate->denies(['allow'])); + } + public function testCurrentUserThatIsOnGateAlwaysInjectedIntoClosureCallbacks() { $gate = $this->getBasicGate();