2018-02-28 03:57:35 +05:30
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-08-02 05:59:20 +05:30
|
|
|
namespace api\rbac\rules;
|
2018-02-28 03:57:35 +05:30
|
|
|
|
2019-08-02 05:59:20 +05:30
|
|
|
use api\rbac\Permissions as P;
|
2018-02-28 03:57:35 +05:30
|
|
|
use common\models\OauthClient;
|
2019-08-02 05:59:20 +05:30
|
|
|
use Webmozart\Assert\Assert;
|
2018-02-28 03:57:35 +05:30
|
|
|
use Yii;
|
|
|
|
use yii\rbac\Rule;
|
|
|
|
|
|
|
|
class OauthClientOwner extends Rule {
|
|
|
|
|
|
|
|
public $name = 'oauth_client_owner';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Accepts 2 params:
|
|
|
|
* - clientId - it's the client id, that user want access to.
|
|
|
|
* - accountId - if it is passed to check the VIEW_OAUTH_CLIENTS permission, then it will
|
|
|
|
* check, that current user have access to the provided account.
|
|
|
|
*
|
|
|
|
* @param string|int $accessToken
|
|
|
|
* @param \yii\rbac\Item $item
|
|
|
|
* @param array $params
|
|
|
|
*
|
|
|
|
* @return bool a value indicating whether the rule permits the auth item it is associated with.
|
|
|
|
*/
|
|
|
|
public function execute($accessToken, $item, $params): bool {
|
|
|
|
$accountId = $params['accountId'] ?? null;
|
|
|
|
if ($accountId !== null && $item->name === P::VIEW_OWN_OAUTH_CLIENTS) {
|
|
|
|
return (new AccountOwner())->execute($accessToken, $item, ['accountId' => $accountId]);
|
|
|
|
}
|
|
|
|
|
2019-08-02 05:59:20 +05:30
|
|
|
Assert::keyExists($params, 'clientId');
|
2018-02-28 03:57:35 +05:30
|
|
|
/** @var OauthClient|null $client */
|
2019-08-02 05:59:20 +05:30
|
|
|
$client = OauthClient::findOne(['id' => $params['clientId']]);
|
2018-02-28 03:57:35 +05:30
|
|
|
if ($client === null) {
|
2018-03-26 00:51:22 +05:30
|
|
|
return true;
|
2018-02-28 03:57:35 +05:30
|
|
|
}
|
|
|
|
|
2019-08-02 05:59:20 +05:30
|
|
|
$identity = Yii::$app->user->getIdentity();
|
2018-02-28 03:57:35 +05:30
|
|
|
if ($identity === null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$account = $identity->getAccount();
|
|
|
|
if ($account === null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($account->id !== $client->account_id) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|