| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace app\models\entity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Yii; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use yii\behaviors\TimestampBehavior; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * This is the model class for table "{{%auth_item}}" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @property string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @property integer $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @property string $description | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @property string $rule_name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @property string $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @property integer $created_at | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @property integer $updated_at | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class AuthItem extends \yii\db\ActiveRecord | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     public $roles; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     public $permissions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 14 |  |     public static function tableName() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 14 |  |         return Yii::$app->authManager->itemTable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @inheritdoc | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 4 |  |     public function attributeLabels() | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         return [ | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 4 |  |             'name'        => Yii::t('app', 'Title'), | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 4 |  |             'description' => Yii::t('app', 'Description'), | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 4 |  |             'roles'       => Yii::t('app', 'Inherit role'), | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 4 |  |             'permissions' => Yii::t('app', 'Permissions'), | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 12 |  |     public function behaviors() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 12 |  |                 'class' => TimestampBehavior::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 'createdAtAttribute' => 'created_at', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 'updatedAtAttribute' => 'updated_at', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     public function events() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             ActiveRecord::EVENT_BEFORE_DELETE => 'beforeDelete', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @return \app\models\query\AuthItemQuery The active query used by this AR class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     public static function find() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 14 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         return new \app\models\query\AuthItemQuery(get_called_class()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 14 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * Whether the role is SuperUser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     public function isSuperUser(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 2 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         return $this->name === User::ROLE_SUPERUSER; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * Delete auth item. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * WARNING: Impossible delete a superuser. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     public function beforeDelete() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         return !$this->isSuperUser(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 104 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |  |