2017-09-19 22:36:16 +05:30
|
|
|
<?php
|
|
|
|
namespace common\rbac;
|
|
|
|
|
|
|
|
use Yii;
|
|
|
|
use yii\rbac\PhpManager;
|
|
|
|
|
|
|
|
class Manager extends PhpManager {
|
|
|
|
|
|
|
|
/**
|
2019-07-15 04:29:56 +05:30
|
|
|
* In our application the permissions are given not to users but to tokens,
|
|
|
|
* so we receive $accessToken here and extract all the assigned scopes from it.
|
2017-09-19 22:36:16 +05:30
|
|
|
*
|
2019-07-15 04:29:56 +05:30
|
|
|
* In Yii2, the mechanism of recursive permissions checking requires that the array with permissions
|
|
|
|
* is indexed by the keys of these rights, so at the end we turn the array inside out.
|
2017-09-19 22:36:16 +05:30
|
|
|
*
|
|
|
|
* @param string $accessToken
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getAssignments($accessToken): array {
|
|
|
|
$identity = Yii::$app->user->findIdentityByAccessToken($accessToken);
|
2017-09-30 03:14:05 +05:30
|
|
|
if ($identity === null) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
/** @noinspection NullPointerExceptionInspection */
|
|
|
|
$permissions = $identity->getAssignedPermissions();
|
|
|
|
if (empty($permissions)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_flip($permissions);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|