-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Yii authorization integration with AuthManager and Behavior…
…s methods (#18) * feat: add Yii authorization integration with AuthManager and Behaviors methods * docs: update README with new Yii Authorization features and usage examples
- Loading branch information
Showing
9 changed files
with
638 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace yii\permission\components; | ||
|
||
use Yii; | ||
use yii\rbac\CheckAccessInterface; | ||
|
||
class PermissionChecker implements CheckAccessInterface | ||
{ | ||
/** | ||
* Checks if the user has access to a certain policy. | ||
* | ||
* @param int $userId The ID of the user to check. | ||
* @param string $policy The policy to check access for. | ||
* @param array $guards Optional guards to check, not supported yet. | ||
* | ||
* @return bool Whether the user has access to the policy. | ||
*/ | ||
public function checkAccess($userId, $policy, $guards = []) | ||
{ | ||
$params = explode(',', $policy); | ||
return Yii::$app->permission->enforce($userId, ...$params); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
namespace yii\permission\components; | ||
|
||
use Yii; | ||
use yii\base\ActionFilter; | ||
use yii\di\Instance; | ||
use yii\web\ForbiddenHttpException; | ||
use yii\web\User; | ||
|
||
class PermissionControl extends ActionFilter | ||
{ | ||
/** | ||
* @var User|array|string|false the user object. | ||
*/ | ||
public $user = 'user'; | ||
|
||
/** | ||
* @var callable|null a callback that will be called if the access should be denied | ||
*/ | ||
public $denyCallback; | ||
|
||
/** | ||
* @var array the default configuration of the policy | ||
*/ | ||
public $policyConfig = ['class' => 'yii\permission\components\PermissionPolicy']; | ||
|
||
/** | ||
* @var array the policies. | ||
*/ | ||
public $policy = []; | ||
|
||
/** | ||
* Initializes the PermissionControl component. | ||
* | ||
* @return void | ||
*/ | ||
public function init() | ||
{ | ||
parent::init(); | ||
if ($this->user !== false) { | ||
$this->user = Instance::ensure($this->user, User::class); | ||
} | ||
foreach ($this->policy as $i => $policy) { | ||
if (is_array($policy)) { | ||
$this->policy[$i] = Yii::createObject(array_merge($this->policyConfig, $policy)); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Checks if the current user has permission to perform the given action. | ||
* | ||
* @param Action $action the action to be performed | ||
* @throws ForbiddenHttpException if the user does not have permission | ||
* @return bool true if the user has permission, false otherwise | ||
*/ | ||
public function beforeAction($action) | ||
{ | ||
$user = $this->user; | ||
foreach ($this->policy as $policy) { | ||
if ($allow = $policy->allows($action, $user)) { | ||
return true; | ||
} elseif ($allow === false) { | ||
if (isset($policy->denyCallback)) { | ||
call_user_func($policy->denyCallback, $policy, $action); | ||
} elseif ($this->denyCallback !== null) { | ||
call_user_func($this->denyCallback, $policy, $action); | ||
} else { | ||
$this->denyAccess($user); | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
if ($this->denyCallback !== null) { | ||
call_user_func($this->denyCallback, null, $action); | ||
} else { | ||
$this->denyAccess($user); | ||
} | ||
return false; | ||
} | ||
/** | ||
* Denies the access of the user. | ||
* The default implementation will redirect the user to the login page if he is a guest; | ||
* if the user is already logged, a 403 HTTP exception will be thrown. | ||
* | ||
* @param User|false $user the current user or boolean `false` in case of detached User component | ||
* @throws ForbiddenHttpException if the user is already logged in or in case of detached User component. | ||
*/ | ||
protected function denyAccess($user) | ||
{ | ||
if ($user !== false && $user->getIsGuest()) { | ||
$user->loginRequired(); | ||
} else { | ||
throw new ForbiddenHttpException(Yii::t('yii', 'You are not allowed to perform this action.')); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace yii\permission\components; | ||
|
||
use Yii; | ||
use yii\base\Component; | ||
use yii\web\User; | ||
|
||
class PermissionPolicy extends Component | ||
{ | ||
/** | ||
* @var bool whether this is an 'allow' rule or 'deny' rule. | ||
*/ | ||
public $allow = false; | ||
|
||
/** | ||
* @var array|null list of the controller IDs that this rule applies to. | ||
*/ | ||
public $actions = []; | ||
|
||
/** | ||
* @var array|null list of params that passed to Casbin. | ||
*/ | ||
public $enforce = []; | ||
|
||
/** | ||
* @var callable|null a callback that will be called if the access should be denied | ||
*/ | ||
public $denyCallback; | ||
|
||
/** | ||
* Checks whether the given action is allowed for the specified user. | ||
* | ||
* @param string $action the action to be checked | ||
* @param User $user the user to be checked | ||
* | ||
* @return bool|null true if the action is allowed, false if not, null if the rule does not apply | ||
*/ | ||
public function allows($action, $user) | ||
{ | ||
if ( | ||
$this->matchAction($action) | ||
&& $this->matchEnforce($user, $this->enforce) | ||
) { | ||
return $this->allow ? true : false; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Checks if the rule applies to the specified action. | ||
* | ||
* @param Action $action the action | ||
* @return bool whether the rule applies to the action | ||
*/ | ||
protected function matchAction($action) | ||
{ | ||
return empty($this->actions) || in_array($action->id, $this->actions, true); | ||
} | ||
|
||
/** | ||
* Checks if the rule applies to the specified user. | ||
* | ||
* @param User $user | ||
* @param array $params | ||
* | ||
* @return bool | ||
*/ | ||
protected function matchEnforce($user, $params) | ||
{ | ||
return Yii::$app->permission->enforce($user->getId(), ...$params); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.