<?php
    namespace common\modules\comment\commands;

    class RbacController extends \yii\console\Controller
    {

        public function actionInstall()
        {
            /**
             * @var \common\modules\comment\Module $module
             */
            $module = \Yii::$app->controller->module;
            if(!$module->useRbac) {
                throw new \yii\base\InvalidConfigException('Please set useRbac config to TRUE in your module configs');
            }
            $auth = \Yii::$app->getAuthManager();
            if(!$auth instanceof \yii\rbac\ManagerInterface) {
                throw new \yii\base\InvalidConfigException('ManagerInterface is not configured');
            }
            if(!empty($module->rbac['rules'])) {
                foreach($module->rbac['rules'] as $rule) {
                    $rule_model = new $rule();
                    echo "Creating rule: ".$rule_model->name."\n";
                    if($auth->add($rule_model)) {
                        echo "Successful\n";
                    } else {
                        echo "Failed\n";
                    }
                    unset($rule_model);
                }
            }
            if(!empty($module->rbac['permissions'])) {
                foreach($module->rbac['permissions'] as $permission) {
                    echo "Creating permission: ".$permission['name']."\n";
                    if($auth->add(new \yii\rbac\Permission($permission))) {
                        echo "Successful\n";
                    } else {
                        echo "Failed\n";
                    }
                }
            }
        }

        public function actionUninstall()
        {
            /**
             * @var \common\modules\comment\Module $module
             */
            $module = \Yii::$app->controller->module;
            if(!$module->useRbac) {
                throw new \yii\base\InvalidConfigException('Please set useRbac config to TRUE in your module configs');
            }
            $auth = \Yii::$app->getAuthManager();
            if(!$auth instanceof \yii\rbac\ManagerInterface) {
                throw new \yii\base\InvalidConfigException('ManagerInterface is not configured');
            }
            if(!empty($module->rbac['rules'])) {
                foreach($module->rbac['rules'] as $rule) {
                    $rule_model = new $rule();
                    echo "Removing rule: ".$rule_model->name."\n";
                    if($auth->remove($rule_model)) {
                        echo "Successful\n";
                    } else {
                        echo "Failed\n";
                    }
                    unset($rule_model);
                }
            }
            if(!empty($module->rbac['permissions'])) {
                foreach($module->rbac['permissions'] as $permission) {
                    echo "Removing permission: ".$permission['name']."\n";
                    if($auth->remove(new \yii\rbac\Permission($permission))) {
                        echo "Successful\n";
                    } else {
                        echo "Failed\n";
                    }
                }
            }
        }

    }