2017-09-19 22:36:16 +05:30
|
|
|
<?php
|
2019-08-02 05:59:20 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace api\rbac\rules;
|
2017-09-19 22:36:16 +05:30
|
|
|
|
|
|
|
use common\models\Account;
|
2019-08-02 05:59:20 +05:30
|
|
|
use Webmozart\Assert\Assert;
|
2017-09-19 22:36:16 +05:30
|
|
|
use Yii;
|
|
|
|
use yii\rbac\Rule;
|
|
|
|
|
2020-06-12 02:57:02 +05:30
|
|
|
final class AccountOwner extends Rule {
|
2017-09-19 22:36:16 +05:30
|
|
|
|
|
|
|
public $name = 'account_owner';
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
*
|
|
|
|
* @param string|int $accessToken
|
|
|
|
* @param \yii\rbac\Item $item
|
2019-07-15 04:29:56 +05:30
|
|
|
* @param array $params the "accountId" parameter must be passed as the id of the account
|
|
|
|
* to which the request is made
|
|
|
|
* the "optionalRules" parameter allows you to disable the mandatory acceptance
|
|
|
|
* of the latest version of the rules
|
2017-09-19 22:36:16 +05:30
|
|
|
*
|
|
|
|
* @return bool a value indicating whether the rule permits the auth item it is associated with.
|
|
|
|
*/
|
|
|
|
public function execute($accessToken, $item, $params): bool {
|
2019-08-02 05:59:20 +05:30
|
|
|
Assert::keyExists($params, 'accountId');
|
2017-09-19 22:36:16 +05:30
|
|
|
$accountId = $params['accountId'] ?? null;
|
|
|
|
|
2019-08-02 05:59:20 +05:30
|
|
|
$identity = Yii::$app->user->getIdentity();
|
2017-12-23 03:40:54 +05:30
|
|
|
if ($identity === null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
$account = $identity->getAccount();
|
|
|
|
if ($account === null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($account->id !== (int)$accountId) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-12 02:57:02 +05:30
|
|
|
$allowDeleted = $params['allowDeleted'] ?? false;
|
|
|
|
if ($account->status !== Account::STATUS_ACTIVE
|
|
|
|
// if deleted accounts are allowed, but the passed one is not in deleted state
|
|
|
|
&& (!$allowDeleted || $account->status !== Account::STATUS_DELETED)
|
|
|
|
) {
|
2017-09-19 22:36:16 +05:30
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$actualRulesOptional = $params['optionalRules'] ?? false;
|
|
|
|
if (!$actualRulesOptional && !$account->isAgreedWithActualRules()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|