| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace modules\rbac\console; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Yii; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use yii\helpers\ArrayHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use yii\console\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use modules\rbac\components\AuthorRule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use console\components\helpers\Console; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use modules\rbac\models\Role; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use modules\rbac\models\Permission; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use modules\rbac\Module; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * Class RbacController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * Инициализатор RBAC выполняется в консоли php yii rbac/init | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @package console\controllers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | class InitController extends Controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     const TYPE_ROLE = 'Roles'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     const TYPE_PERMISSION = 'Permissions'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * Color | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public $color = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * Initialize RBAC | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public function actionIndex() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         if ($this->processInit()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             $this->stdout( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                 Console::convertEncoding(Module::translate('module', 'Success!')), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                 Console::FG_GREEN, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                 Console::BOLD | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             $this->stdout(PHP_EOL); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             $this->stderr( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 Console::convertEncoding(Module::translate('module', 'Fail!')), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 Console::FG_RED, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 Console::BOLD | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @throws Exception | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     public function processInit() | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         $auth = Yii::$app->authManager; | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         $this->processClear($auth); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         $roles = $this->processCreate($auth, Role::rolesArray()); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         $permissions = $this->processCreate($auth, Permission::permissionsArray(), self::TYPE_PERMISSION); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         $this->processAddPermissionToRoles($auth, $roles, $permissions); | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         $this->processAddChildRoles($auth, $roles); //Inheritance of roles - If you uncomment, the roles are inherited | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         // Assign a super administrator role to the user from id 1 | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         $role = ArrayHelper::getValue($roles, Role::ROLE_SUPER_ADMIN); | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         return $this->processAssignUserToRole($auth, $role); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * Clear | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param $auth yii\rbac\ManagerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     protected function processClear($auth) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $auth->removeAll(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * Create Roles and Permissions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @param object $auth | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @param array $array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @param string $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     protected function processCreate($auth, $array = [], $type = self::TYPE_ROLE) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         foreach ($array as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             $result[$key] = ($type === self::TYPE_ROLE) ? $auth->createRole($key) : $auth->createPermission($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             $result[$key]->description = Module::translate('module', $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             // Add rules | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             if ($key === Permission::PERMISSION_UPDATE_OWN_POST) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 $authorRule = new AuthorRule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 $auth->add($authorRule); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                 $result[$key]->ruleName = $authorRule->name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             $auth->add($result[$key]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * Add Permissions for Roles | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @param object $auth | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @param array $roles | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @param array $permissions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     protected function processAddPermissionToRoles($auth, $roles = [], $permissions = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         foreach (Permission::getGroups() as $role => $group) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             foreach ($group as $permission) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                 $auth->addChild( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                     ArrayHelper::getValue($roles, $role), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                     ArrayHelper::getValue($permissions, $permission) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * Add Child role for Roles | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * @param object $auth | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @param array $roles | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     protected function processAddChildRoles($auth, $roles = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         $array = $this->prepareArrayTree(Role::tree()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         foreach ($array as $item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             foreach ($item as $role => $child) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                 $auth->addChild( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |                     ArrayHelper::getValue($roles, $role), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |                     ArrayHelper::getValue($roles, $child) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * @param array $array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     protected function prepareArrayTree($array) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         $result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         foreach ($array as $key => $item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             if (is_array($item)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |                 foreach ($item as $k => $v) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |                     if (is_string($k)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |                         $result[][$key] = $k; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |                     } elseif (is_string($v)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |                         $result[][$key] = $v; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |                     if (is_array($v)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |                         $child = $this->prepareArrayTree([$k => $item[$k]]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |                         $result = array_merge($result, $child); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |                 $result[] = $item; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |      * Assign Role to User | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |      * @param object $auth | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |      * @param $role | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |      * @param integer $userId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |     protected function processAssignUserToRole($auth, $role, $userId = 1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |         $auth->assign($role, $userId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 188 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 189 |  |  |  |