Skip to content

Commit

Permalink
Firewall->hasRole()
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Nov 28, 2020
1 parent 29cbe58 commit c4dd8f0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ $firewall->login($identity);
$firewall->isLoggedIn(); // true
$firewall->getIdentity(); // $identity
$firewall->getAuthenticationTime(); // Instant
$firewall->hasRole($role); // bool
```

#### Set or remove login expiration
Expand Down
11 changes: 11 additions & 0 deletions src/Authentication/BaseFirewall.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@ public function getAuthenticationTime(): Instant
return $login->getAuthenticationTime();
}

public function hasRole(string $role): bool
{
$identity = $this->fetchIdentity();

if ($identity === null) {
return false;
}

return $identity->hasRole($role);
}

/**
* @throws CannotSetExpiration When expiration is set before user is logged in
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Authentication/Firewall.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public function logout(): void;
*/
public function getIdentity(): Identity;

public function hasRole(string $role): bool;

/**
* @throws CannotGetAuthenticationTime When user is not logged id
*/
Expand Down
14 changes: 14 additions & 0 deletions tests/Unit/Authentication/BaseFirewallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ public function testIdentityClassUpdate(): void
self::assertSame($renewedIdentity, $firewall->getLogins()->getCurrentLogin()->getIdentity());
}

public function testHasRole(): void
{
$identity = new IntIdentity(123, ['foo']);

$storage = new ArrayLoginStorage();
$firewall = new TestingFirewall($storage, $this->renewer());

self::assertFalse($firewall->hasRole('foo'));

$firewall->login($identity);
self::assertTrue($firewall->hasRole('foo'));
self::assertFalse($firewall->hasRole('bar'));
}

public function testExpiredIdentities(): void
{
$storage = new ArrayLoginStorage();
Expand Down

0 comments on commit c4dd8f0

Please sign in to comment.