re #25 allow rules edition + assignment
This commit is contained in:
30
src/User/Validator/RbacRuleExistsValidator.php
Normal file
30
src/User/Validator/RbacRuleExistsValidator.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the 2amigos/yii2-usuario project.
|
||||
*
|
||||
* (c) 2amigOS! <http://2amigos.us/>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Da\User\Validator;
|
||||
|
||||
use Da\User\Traits\AuthManagerAwareTrait;
|
||||
use Yii;
|
||||
use yii\validators\Validator;
|
||||
|
||||
class RbacRuleExistsValidator extends Validator
|
||||
{
|
||||
use AuthManagerAwareTrait;
|
||||
|
||||
protected function validateValue($value)
|
||||
{
|
||||
$rule = $this->getAuthManager()->getRule($value);
|
||||
|
||||
if (!$rule) {
|
||||
return [Yii::t('usuario', 'Rule {0} does not exists', $value), []];
|
||||
}
|
||||
}
|
||||
}
|
||||
42
src/User/Validator/RbacRuleNameValidator.php
Normal file
42
src/User/Validator/RbacRuleNameValidator.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the 2amigos/yii2-usuario project.
|
||||
*
|
||||
* (c) 2amigOS! <http://2amigos.us/>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Da\User\Validator;
|
||||
|
||||
use Da\User\Traits\AuthManagerAwareTrait;
|
||||
use Yii;
|
||||
use yii\rbac\Rule;
|
||||
use yii\validators\Validator;
|
||||
|
||||
class RbacRuleNameValidator extends Validator
|
||||
{
|
||||
use AuthManagerAwareTrait;
|
||||
|
||||
/**
|
||||
* @var
|
||||
*/
|
||||
public $previousName;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function validateValue($value)
|
||||
{
|
||||
if ($this->previousName != $value) {
|
||||
$rule = $this->getAuthManager()->getRule($value);
|
||||
|
||||
if ($rule instanceof Rule) {
|
||||
return [Yii::t('usuario', 'Rule name {0} is already in use', $value), []];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -11,26 +11,26 @@
|
||||
|
||||
namespace Da\User\Validator;
|
||||
|
||||
use Da\User\Traits\ContainerAwareTrait;
|
||||
use Exception;
|
||||
use ReflectionClass;
|
||||
use Yii;
|
||||
use yii\rbac\Rule;
|
||||
use yii\validators\Validator;
|
||||
|
||||
class RbacRuleValidator extends Validator
|
||||
{
|
||||
use ContainerAwareTrait;
|
||||
|
||||
protected function validateValue($value)
|
||||
{
|
||||
try {
|
||||
$class = new ReflectionClass($value);
|
||||
} catch (Exception $e) {
|
||||
return [Yii::t('usuario', 'Class "{0}" does not exist', $value), []];
|
||||
}
|
||||
$rule = $this->make($value);
|
||||
|
||||
if ($class->isInstantiable() == false) {
|
||||
return [Yii::t('usuario', 'Rule class can not be instantiated'), []];
|
||||
}
|
||||
if ($class->isSubclassOf('\yii\rbac\Rule') == false) {
|
||||
return [Yii::t('usuario', 'Rule class must extend "yii\\rbac\\Rule"'), []];
|
||||
if (!($rule instanceof Rule)) {
|
||||
return [Yii::t('usuario', 'Rule class must extend "yii\\rbac\\Rule".'), []];
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
return [Yii::t('usuario', 'Authentication rule class {0} can not be instantiated', $value), []];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user