AuthItem.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace common\modules\rbac\models;
  3. use Yii;
  4. use yii\helpers\Json;
  5. use yii\rbac\Item;
  6. /**
  7. * This is the model class for table "tbl_auth_item".
  8. *
  9. * @property string $name
  10. * @property int $type
  11. * @property string $description
  12. * @property string $ruleName
  13. * @property string $data
  14. * @property Item $item
  15. *
  16. * @author Misbahul D Munir <misbahuldmunir@gmail.com>
  17. *
  18. * @since 1.0
  19. */
  20. class AuthItem extends \yii\base\Model
  21. {
  22. public $name;
  23. public $type;
  24. public $description;
  25. public $ruleName;
  26. public $data;
  27. /**
  28. * @var Item
  29. */
  30. private $_item;
  31. /**
  32. * Initialize object.
  33. *
  34. * @param Item $item
  35. * @param array $config
  36. */
  37. public function __construct($item, $config = [])
  38. {
  39. $this->_item = $item;
  40. if ($item !== null) {
  41. $this->name = $item->name;
  42. $this->type = $item->type;
  43. $this->description = $item->description;
  44. $this->ruleName = $item->ruleName;
  45. $this->data = $item->data === null ? null : Json::encode($item->data);
  46. }
  47. parent::__construct($config);
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function rules()
  53. {
  54. return [
  55. [['ruleName'], 'in',
  56. 'range' => array_keys(Yii::$app->authManager->getRules()),
  57. 'message' => 'Rule not exists', ],
  58. [['name', 'type'], 'required'],
  59. [['name'], 'unique2', 'when' => function () {//Yii2新版本修改了Validator createValidator方法,导致inlineValidator无法和自带验证器重名
  60. return $this->isNewRecord || ($this->_item->name != $this->name);
  61. }],
  62. [['type'], 'integer'],
  63. [['description', 'data', 'ruleName'], 'default'],
  64. [['name'], 'string', 'max' => 64],
  65. ];
  66. }
  67. public function unique2()
  68. {
  69. $authManager = Yii::$app->authManager;
  70. $value = $this->name;
  71. if ($authManager->getRole($value) !== null || $authManager->getPermission($value) !== null) {
  72. $message = Yii::t('yii', '{attribute} "{value}" has already been taken.');
  73. $params = [
  74. 'attribute' => $this->getAttributeLabel('name'),
  75. 'value' => $value,
  76. ];
  77. $this->addError('name', Yii::$app->getI18n()->format($message, $params, Yii::$app->language));
  78. }
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function attributeLabels()
  84. {
  85. return [
  86. 'name' => Yii::t('rbac', 'Name'),
  87. 'type' => Yii::t('rbac', 'Type'),
  88. 'description' => Yii::t('rbac', 'Description'),
  89. 'ruleName' => Yii::t('rbac', 'Rule Name'),
  90. 'data' => Yii::t('rbac', 'Data'),
  91. ];
  92. }
  93. /**
  94. * Check if is new record.
  95. *
  96. * @return bool
  97. */
  98. public function getIsNewRecord()
  99. {
  100. return $this->_item === null;
  101. }
  102. /**
  103. * Find role.
  104. *
  105. * @param string $id
  106. *
  107. * @return null|\self
  108. */
  109. public static function find($id)
  110. {
  111. $item = Yii::$app->authManager->getRole($id);
  112. if ($item !== null) {
  113. return new self($item);
  114. }
  115. return;
  116. }
  117. /**
  118. * Save role to [[\yii\rbac\authManager]].
  119. *
  120. * @return bool
  121. */
  122. public function save()
  123. {
  124. if ($this->validate()) {
  125. $manager = Yii::$app->authManager;
  126. if ($this->_item === null) {
  127. if ($this->type == Item::TYPE_ROLE) {
  128. $this->_item = $manager->createRole($this->name);
  129. } else {
  130. $this->_item = $manager->createPermission($this->name);
  131. }
  132. $isNew = true;
  133. } else {
  134. $isNew = false;
  135. $oldName = $this->_item->name;
  136. }
  137. $this->_item->name = $this->name;
  138. $this->_item->description = $this->description;
  139. $this->_item->ruleName = $this->ruleName;
  140. $this->_item->data = $this->data === null || $this->data === '' ? null : Json::decode($this->data);
  141. if ($isNew) {
  142. $manager->add($this->_item);
  143. } else {
  144. $manager->update($oldName, $this->_item);
  145. }
  146. return true;
  147. } else {
  148. return false;
  149. }
  150. }
  151. /**
  152. * Get item.
  153. *
  154. * @return Item
  155. */
  156. public function getItem()
  157. {
  158. return $this->_item;
  159. }
  160. /**
  161. * Get type name.
  162. *
  163. * @param mixed $type
  164. *
  165. * @return string|array
  166. */
  167. public static function getTypeName($type = null)
  168. {
  169. $result = [
  170. Item::TYPE_PERMISSION => 'Permission',
  171. Item::TYPE_ROLE => 'Role',
  172. ];
  173. if ($type === null) {
  174. return $result;
  175. }
  176. return $result[$type];
  177. }
  178. }