123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <?php
- namespace common\modules\rbac\models;
- use Yii;
- use yii\rbac\Rule;
- /**
- * BizRule.
- *
- * @author Misbahul D Munir <misbahuldmunir@gmail.com>
- *
- * @since 1.0
- */
- class BizRule extends \yii\base\Model
- {
- /**
- * @var string name of the rule
- */
- public $name;
- /**
- * @var int UNIX timestamp representing the rule creation time
- */
- public $createdAt;
- /**
- * @var int UNIX timestamp representing the rule updating time
- */
- public $updatedAt;
- /**
- * @var string Rule classname.
- */
- public $className;
- /**
- * @var Rule
- */
- private $_item;
- /**
- * Initilaize object.
- *
- * @param \yii\rbac\Rule $item
- * @param array $config
- */
- public function __construct($item, $config = [])
- {
- $this->_item = $item;
- if ($item !== null) {
- $this->name = $item->name;
- $this->className = get_class($item);
- }
- parent::__construct($config);
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['name', 'className'], 'required'],
- [['className'], 'string'],
- [['className'], 'classExists'],
- ];
- }
- /**
- * Validate class exists.
- */
- public function classExists()
- {
- if (!class_exists($this->className) || !is_subclass_of($this->className, Rule::className())) {
- $this->addError('className', "Unknown Class: {$this->className}");
- }
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'name' => Yii::t('rbac', 'Name'),
- 'className' => Yii::t('rbac', 'Class Name'),
- ];
- }
- /**
- * Check if new record.
- *
- * @return bool
- */
- public function getIsNewRecord()
- {
- return $this->_item === null;
- }
- /**
- * Find model by id.
- *
- * @param type $id
- *
- * @return null|static
- */
- public static function find($id)
- {
- $item = Yii::$app->authManager->getRule($id);
- if ($item !== null) {
- return new static($item);
- }
- return;
- }
- /**
- * Save model to authManager.
- *
- * @return bool
- */
- public function save()
- {
- if ($this->validate()) {
- $manager = Yii::$app->authManager;
- $class = $this->className;
- if ($this->_item === null) {
- $this->_item = new $class();
- $isNew = true;
- } else {
- $isNew = false;
- $oldName = $this->_item->name;
- }
- $this->_item->name = $this->name;
- 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;
- }
- }
|