added RBAC
This commit is contained in:
25
lib/User/Validator/RbacItemsValidator.php
Normal file
25
lib/User/Validator/RbacItemsValidator.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
namespace Da\User\Validator;
|
||||
|
||||
use Da\User\Traits\AuthManagerTrait;
|
||||
use Yii;
|
||||
use yii\validators\Validator;
|
||||
|
||||
class RbacItemsValidator extends Validator
|
||||
{
|
||||
use AuthManagerTrait;
|
||||
|
||||
protected function validateValue($value)
|
||||
{
|
||||
if (!is_array($value)) {
|
||||
return [Yii::t('item', 'Invalid value'), []];
|
||||
}
|
||||
|
||||
foreach ($value as $item) {
|
||||
if ($this->getAuthManager()->getItem($item) == null) {
|
||||
return [Yii::t('user', 'There is neither role nor permission with name "{0}"', [$item]), []];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
26
lib/User/Validator/RbacRuleValidator.php
Normal file
26
lib/User/Validator/RbacRuleValidator.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
namespace Da\User\Validator;
|
||||
|
||||
use ReflectionClass;
|
||||
use Exception;
|
||||
use yii\validators\Validator;
|
||||
use Yii;
|
||||
|
||||
class RbacRuleValidator extends Validator
|
||||
{
|
||||
protected function validateValue($value)
|
||||
{
|
||||
try {
|
||||
$class = new ReflectionClass($value);
|
||||
} catch (Exception $e) {
|
||||
return [Yii::t('user', 'Class "{0}" does not exist', $value), []];
|
||||
}
|
||||
|
||||
if ($class->isInstantiable() == false) {
|
||||
return [Yii::t('user', 'Rule class can not be instantiated'), []];
|
||||
}
|
||||
if ($class->isSubclassOf('\yii\rbac\Rule') == false) {
|
||||
return [Yii::t('user', 'Rule class must extend "yii\rbac\Rule"'), []];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user