diff --git a/docs/authorization.md b/docs/authorization.md index b8e0775..dbc6e18 100644 --- a/docs/authorization.md +++ b/docs/authorization.md @@ -64,6 +64,11 @@ Returns a copy of the `Authz` service with the given user, in the current contex Returns a copy of the `Authz` service with the current user, in the given context. +#### outOfContext + + Auth::outOfContext(): Authz + +Alias of `Auth::inContextOf(null)`. ### Immutable state diff --git a/src/Auth.php b/src/Auth.php index 27b27ae..2b64346 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -512,6 +512,14 @@ public function inContextOf(?Context $context): Authz return $this->authz->inContextOf($context); } + /** + * Alias of `inContextOf(null)`. + */ + final public function outOfContext(): Authz + { + return $this->inContextOf(null); + } + /** * Get service to create or validate confirmation token. diff --git a/src/Authz/StateTrait.php b/src/Authz/StateTrait.php index 5312a84..9b68021 100644 --- a/src/Authz/StateTrait.php +++ b/src/Authz/StateTrait.php @@ -52,6 +52,16 @@ public function inContextOf(?Context $context): Authz return $this->withProperty('context', $context); } + /** + * Alias of `inContextOf(null)`. + * + * @return static&Authz + */ + final public function outOfContext(): Authz + { + return $this->inContextOf(null); + } + /** * Get current authenticated user. * diff --git a/src/AuthzInterface.php b/src/AuthzInterface.php index 6e2ae2b..d588872 100644 --- a/src/AuthzInterface.php +++ b/src/AuthzInterface.php @@ -36,6 +36,13 @@ public function forUser(?User $user): self; */ public function inContextOf(?Context $context): self; + /** + * Alias of `inContextOf(null)`. + * + * @return AuthzInterface + */ + public function outOfContext(): self; + /** * Get a copy, recalculating the authz role of the user. * diff --git a/tests/AuthTest.php b/tests/AuthTest.php index 95fd097..3917cc0 100644 --- a/tests/AuthTest.php +++ b/tests/AuthTest.php @@ -1182,7 +1182,7 @@ public function testForUser() $this->assertSame($this->authz, $this->service->authz()); // Not modified } - public function testForContext() + public function testInContextOf() { $context = $this->createMock(Context::class); $newAuthz = $this->createMock(Authz::class); @@ -1195,6 +1195,18 @@ public function testForContext() $this->assertSame($this->authz, $this->service->authz()); // Not modified } + public function testOutOfContext() + { + $newAuthz = $this->createMock(Authz::class); + + $this->authz->expects($this->once())->method('inContextOf') + ->with(null) + ->willReturn($newAuthz); + + $this->assertSame($newAuthz, $this->service->outOfContext()); + $this->assertSame($this->authz, $this->service->authz()); // Not modified + } + public function testConfirm() { diff --git a/tests/Authz/LevelsTest.php b/tests/Authz/LevelsTest.php index 65f9b39..4b10ea4 100644 --- a/tests/Authz/LevelsTest.php +++ b/tests/Authz/LevelsTest.php @@ -3,6 +3,7 @@ namespace Jasny\Auth\Tests\Authz; use Jasny\Auth\AuthException; +use Jasny\Auth\AuthzInterface; use Jasny\Auth\ContextInterface as Context; use Jasny\Auth\User\PartiallyLoggedIn; use Jasny\Auth\UserInterface as User; @@ -98,7 +99,7 @@ public function testWithInvalidUserRole() $this->authz = $this->authz->forUser($user); } - public function testContext() + public function testInContextOf() { $this->assertNull($this->authz->context()); @@ -108,6 +109,20 @@ public function testContext() $this->assertNotSame($this->authz, $contextAuthz); $this->assertNull($this->authz->context()); $this->assertSame($context, $contextAuthz->context()); + + return $contextAuthz; + } + + /** + * @depends testInContextOf + */ + public function testOutOfContext(AuthzInterface $contextAuthz) + { + $noContextAuthz = $contextAuthz->outOfContext(); + + $this->assertNotSame($contextAuthz, $noContextAuthz); + $this->assertNotNull($contextAuthz->context()); + $this->assertNull($noContextAuthz->context()); }