Skip to content

Commit 88cb5fa

Browse files
committed
updated token to return claims
1 parent 8138285 commit 88cb5fa

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

src/components/BaseTokenManager.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ public function setToken(UnencryptedToken $token): void
4646
*/
4747
public function getRoles(): array
4848
{
49-
return $this->getToken()->claims()->get($this->rolesClaimName, []);
49+
return $this->getClaim($this->rolesClaimName, []);
50+
}
51+
52+
/**
53+
* @inheritdoc
54+
*/
55+
public function getClaim(string $name, $default = null): mixed
56+
{
57+
return $this->getToken()->claims()->get($name, $default);
5058
}
5159
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace dmstr\tokenManager\exceptions;
6+
7+
class InvalidTokenManagerComponent extends \Exception
8+
{
9+
protected $message = 'Token manager is not instance of TokenManagerInterface';
10+
}

src/interfaces/TokenManagerInterface.php

+10
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,14 @@ public function setToken(UnencryptedToken $token): void;
2222
* @return array
2323
*/
2424
public function getRoles(): array;
25+
26+
/**
27+
* List of permissions assigned to user via token
28+
*
29+
* @param string $name
30+
* @param $default
31+
*
32+
* @return mixed
33+
*/
34+
public function getClaim(string $name, $default = null): mixed;
2535
}

src/rbac/TokenRoleRule.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace dmstr\tokenManager\rbac;
44

55
use dmstr\tokenManager\components\TokenManager;
6+
use dmstr\tokenManager\exceptions\InvalidTokenManagerComponent;
67
use dmstr\tokenManager\exceptions\LoadTokenException;
8+
use dmstr\tokenManager\interfaces\TokenManagerInterface;
79
use yii\base\InvalidConfigException;
810
use yii\rbac\Rule;
911
use Yii;
@@ -16,13 +18,18 @@ class TokenRoleRule extends Rule
1618
/**
1719
* @inheritdoc
1820
*
19-
* @throws InvalidConfigException
21+
* @throws InvalidConfigException|InvalidTokenManagerComponent
2022
*/
2123
public function execute($user, $item, $params)
2224
{
2325
try {
2426
/** @var TokenManager $tokenManager */
2527
$tokenManager = Yii::$app->get($this->tokenManager);
28+
// check if token manager not is instance of TokenManagerInterface
29+
if (!$tokenManager instanceof TokenManagerInterface) {
30+
throw new InvalidTokenManagerComponent();
31+
}
32+
2633
$roles = $tokenManager->getRoles();
2734
} catch (LoadTokenException $exception) {
2835
Yii::error($exception->getMessage());

0 commit comments

Comments
 (0)