BizRule.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace common\modules\rbac\models;
  3. use Yii;
  4. use yii\rbac\Rule;
  5. /**
  6. * BizRule.
  7. *
  8. * @author Misbahul D Munir <misbahuldmunir@gmail.com>
  9. *
  10. * @since 1.0
  11. */
  12. class BizRule extends \yii\base\Model
  13. {
  14. /**
  15. * @var string name of the rule
  16. */
  17. public $name;
  18. /**
  19. * @var int UNIX timestamp representing the rule creation time
  20. */
  21. public $createdAt;
  22. /**
  23. * @var int UNIX timestamp representing the rule updating time
  24. */
  25. public $updatedAt;
  26. /**
  27. * @var string Rule classname.
  28. */
  29. public $className;
  30. /**
  31. * @var Rule
  32. */
  33. private $_item;
  34. /**
  35. * Initilaize object.
  36. *
  37. * @param \yii\rbac\Rule $item
  38. * @param array $config
  39. */
  40. public function __construct($item, $config = [])
  41. {
  42. $this->_item = $item;
  43. if ($item !== null) {
  44. $this->name = $item->name;
  45. $this->className = get_class($item);
  46. }
  47. parent::__construct($config);
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function rules()
  53. {
  54. return [
  55. [['name', 'className'], 'required'],
  56. [['className'], 'string'],
  57. [['className'], 'classExists'],
  58. ];
  59. }
  60. /**
  61. * Validate class exists.
  62. */
  63. public function classExists()
  64. {
  65. if (!class_exists($this->className) || !is_subclass_of($this->className, Rule::className())) {
  66. $this->addError('className', "Unknown Class: {$this->className}");
  67. }
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function attributeLabels()
  73. {
  74. return [
  75. 'name' => Yii::t('rbac', 'Name'),
  76. 'className' => Yii::t('rbac', 'Class Name'),
  77. ];
  78. }
  79. /**
  80. * Check if new record.
  81. *
  82. * @return bool
  83. */
  84. public function getIsNewRecord()
  85. {
  86. return $this->_item === null;
  87. }
  88. /**
  89. * Find model by id.
  90. *
  91. * @param type $id
  92. *
  93. * @return null|static
  94. */
  95. public static function find($id)
  96. {
  97. $item = Yii::$app->authManager->getRule($id);
  98. if ($item !== null) {
  99. return new static($item);
  100. }
  101. return;
  102. }
  103. /**
  104. * Save model to authManager.
  105. *
  106. * @return bool
  107. */
  108. public function save()
  109. {
  110. if ($this->validate()) {
  111. $manager = Yii::$app->authManager;
  112. $class = $this->className;
  113. if ($this->_item === null) {
  114. $this->_item = new $class();
  115. $isNew = true;
  116. } else {
  117. $isNew = false;
  118. $oldName = $this->_item->name;
  119. }
  120. $this->_item->name = $this->name;
  121. if ($isNew) {
  122. $manager->add($this->_item);
  123. } else {
  124. $manager->update($oldName, $this->_item);
  125. }
  126. return true;
  127. } else {
  128. return false;
  129. }
  130. }
  131. /**
  132. * Get item.
  133. *
  134. * @return Item
  135. */
  136. public function getItem()
  137. {
  138. return $this->_item;
  139. }
  140. }