Skip to content

Commit

Permalink
Added outOfContext method.
Browse files Browse the repository at this point in the history
  • Loading branch information
jasny committed Apr 10, 2020
1 parent c797f87 commit 2e75f94
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 2 deletions.
5 changes: 5 additions & 0 deletions docs/authorization.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions src/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions src/Authz/StateTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
7 changes: 7 additions & 0 deletions src/AuthzInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
14 changes: 13 additions & 1 deletion tests/AuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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()
{
Expand Down
17 changes: 16 additions & 1 deletion tests/Authz/LevelsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -98,7 +99,7 @@ public function testWithInvalidUserRole()
$this->authz = $this->authz->forUser($user);
}

public function testContext()
public function testInContextOf()
{
$this->assertNull($this->authz->context());

Expand All @@ -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());
}


Expand Down

0 comments on commit 2e75f94

Please sign in to comment.