123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?php
- namespace common\modules\rbac\models;
- use Yii;
- use yii\helpers\Json;
- use yii\rbac\Item;
- /**
- * This is the model class for table "tbl_auth_item".
- *
- * @property string $name
- * @property int $type
- * @property string $description
- * @property string $ruleName
- * @property string $data
- * @property Item $item
- *
- * @author Misbahul D Munir <misbahuldmunir@gmail.com>
- *
- * @since 1.0
- */
- class AuthItem extends \yii\base\Model
- {
- public $name;
- public $type;
- public $description;
- public $ruleName;
- public $data;
- /**
- * @var Item
- */
- private $_item;
- /**
- * Initialize object.
- *
- * @param Item $item
- * @param array $config
- */
- public function __construct($item, $config = [])
- {
- $this->_item = $item;
- if ($item !== null) {
- $this->name = $item->name;
- $this->type = $item->type;
- $this->description = $item->description;
- $this->ruleName = $item->ruleName;
- $this->data = $item->data === null ? null : Json::encode($item->data);
- }
- parent::__construct($config);
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['ruleName'], 'in',
- 'range' => array_keys(Yii::$app->authManager->getRules()),
- 'message' => 'Rule not exists', ],
- [['name', 'type'], 'required'],
- [['name'], 'unique2', 'when' => function () {//Yii2新版本修改了Validator createValidator方法,导致inlineValidator无法和自带验证器重名
- return $this->isNewRecord || ($this->_item->name != $this->name);
- }],
- [['type'], 'integer'],
- [['description', 'data', 'ruleName'], 'default'],
- [['name'], 'string', 'max' => 64],
- ];
- }
- public function unique2()
- {
- $authManager = Yii::$app->authManager;
- $value = $this->name;
- if ($authManager->getRole($value) !== null || $authManager->getPermission($value) !== null) {
- $message = Yii::t('yii', '{attribute} "{value}" has already been taken.');
- $params = [
- 'attribute' => $this->getAttributeLabel('name'),
- 'value' => $value,
- ];
- $this->addError('name', Yii::$app->getI18n()->format($message, $params, Yii::$app->language));
- }
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'name' => Yii::t('rbac', 'Name'),
- 'type' => Yii::t('rbac', 'Type'),
- 'description' => Yii::t('rbac', 'Description'),
- 'ruleName' => Yii::t('rbac', 'Rule Name'),
- 'data' => Yii::t('rbac', 'Data'),
- ];
- }
- /**
- * Check if is new record.
- *
- * @return bool
- */
- public function getIsNewRecord()
- {
- return $this->_item === null;
- }
- /**
- * Find role.
- *
- * @param string $id
- *
- * @return null|\self
- */
- public static function find($id)
- {
- $item = Yii::$app->authManager->getRole($id);
- if ($item !== null) {
- return new self($item);
- }
- return;
- }
- /**
- * Save role to [[\yii\rbac\authManager]].
- *
- * @return bool
- */
- public function save()
- {
- if ($this->validate()) {
- $manager = Yii::$app->authManager;
- if ($this->_item === null) {
- if ($this->type == Item::TYPE_ROLE) {
- $this->_item = $manager->createRole($this->name);
- } else {
- $this->_item = $manager->createPermission($this->name);
- }
- $isNew = true;
- } else {
- $isNew = false;
- $oldName = $this->_item->name;
- }
- $this->_item->name = $this->name;
- $this->_item->description = $this->description;
- $this->_item->ruleName = $this->ruleName;
- $this->_item->data = $this->data === null || $this->data === '' ? null : Json::decode($this->data);
- if ($isNew) {
- $manager->add($this->_item);
- } else {
- $manager->update($oldName, $this->_item);
- }
- return true;
- } else {
- return false;
- }
- }
- /**
- * Get item.
- *
- * @return Item
- */
- public function getItem()
- {
- return $this->_item;
- }
- /**
- * Get type name.
- *
- * @param mixed $type
- *
- * @return string|array
- */
- public static function getTypeName($type = null)
- {
- $result = [
- Item::TYPE_PERMISSION => 'Permission',
- Item::TYPE_ROLE => 'Role',
- ];
- if ($type === null) {
- return $result;
- }
- return $result[$type];
- }
- }
|