InitController.php 8.41 KB
<?php
namespace console\controllers;

use Yii;
use yii\console\Controller;
use backend\models\Admin;

/**
 * Site controller
 */
class InitController extends Controller
{

    /** 
     * 创建/修改后台用户
     */  
    public function actionAdmin()  
    {
        $this->stdout("1: Create admin account\n2: Modify admin account\n");
        $action = $this->prompt('Which:');
        
        if ($action == 1){
            echo "创建一个新用户 ...\n";                  // 提示当前操作  
            $username = $this->prompt('User Name:');        // 接收用户名  
            $email = $this->prompt('Email:');               // 接收Email  
            $password = $this->prompt('Password:');         // 接收密码  
            $model = new Admin();                            // 创建一个新用户  
            $model->username = $username;                   // 完成赋值  
            $model->email = $email;  
            $model->setPassword($password);
            $model->generateAuthKey();
            $model->generatePasswordResetToken();
        }elseif ($action == 2){
            echo "修改一个已存在用户 ...\n";                  // 提示当前操作  
            $username = $this->prompt('User Name:');        // 接收用户名  
            if (!$username){
                echo '请输入用户名';
                return 1;
            }
            // 修改一个已存在用户  
            if (!$model = Admin::findByUsername($username)){
                echo '找不到该用户';
                return 1;
            }
            
            $email = $this->prompt('Email:');               // 接收Email  
            $password = $this->prompt('Password:');         // 接收密码  
            if (!$email && !$password){
                echo '没有任何修改';
                return 0;
            }
            if ($email){
                $model->email = $email; 
            }
            if ($password){
                $model->setPassword($password);
                $model->generateAuthKey();
                $model->generatePasswordResetToken();
            }
        }else{
            $this->stderr('Not a predefined option');
            return 1;
        }
        
        
        if (!$model->save())                            // 保存新的用户  
        {  
            foreach ($model->getErrors() as $error)     // 如果保存失败,说明有错误,那就输出错误信息。  
            {  
                foreach ($error as $e)  
                {  
                    echo "$e\n";  
                }  
            }  
            return 1;                                   // 命令行返回1表示有异常  
        }  
        return 0;                                       // 返回0表示一切OK  
    }

    public function actionTest(){
        echo "init/test";
    }
    
    /**
     * 创建/移除后台权限标签(controller/action)
     */
    public function actionRbac(){
        
        $this->stdout("1: Create permission\n2: Remove permission\n3: Create role\n4: Remove role\n5: Grant permission to a role\n6: Revoke permission from a role\n7: Grant role to a user\n8: Revoke role from a a\n");
        $action = $this->prompt('Which:');
        
        $auth = Yii::createObject('yii\rbac\DbManager');
        //$auth = Yii::$app->authManager;
        if ($action == 1){
            echo "创建一个新权限标签 ...\n";                  // 提示当前操作  
            $name = $this->prompt('Permission Name:');        // 权限名称  
            $description = $this->prompt('Description(default is as name):');               // 描述  
            
            if ($name){
                $permission = $auth->createPermission($name);
                $permission->description = $description ? : ucwords(strtr($name,array('/'=>' ')));
                if ($auth->add($permission)){
                    $this->stdout('create ok');
                }
            }
        }elseif ($action == 2){
            echo "移除一个权限标签 ...\n";                  // 提示当前操作  
            $name = $this->prompt('Permission Name:');        // 权限名称  
            
            if ($name){
                if (!$permission = $auth->getPermission($name)){
                    $this->stderr('Not a predefined permission');
                    return 1;
                }
                if ($auth->remove($permission)){
                    $this->stdout('remove ok');
                }
            }
        }elseif ($action == 3){
            echo "创建一个新角色 ...\n";                  // 提示当前操作  
            $name = $this->prompt('Role Name:');        // 角色名称  
            $description = $this->prompt('Description(default is as name):');               // 描述  
            
            if ($name){
                $role = $auth->createRole($name);
                $role->description = $description ? : ucfirst($name);
                if ($auth->add($role)){
                    $this->stdout('create ok');
                }
            }
        }elseif ($action == 4){
            echo "移除一个角色 ...\n";                  // 提示当前操作  
            $name = $this->prompt('Role Name:');        // 角色名称  
            
            if ($name){
                if (!$role = $auth->getRole($name)){
                    $this->stderr('Not a predefined role');
                    return 1;
                }
                if ($auth->remove($role)){
                    $this->stdout('remove ok');
                }
            }
        }elseif ($action == 5){
            echo "向某个角色赋予权限 ...\n";                  // 提示当前操作  
            $name = $this->prompt('Role Name:');        // 接收用户名  
            if (!$role = $auth->getRole($name)){
                $this->stderr('Not a predefined role');
                return 1;
            }
            $name = $this->prompt('Permission Name:');         // 接收用户名  
            if (!$permission = $auth->getPermission($name)){
                $this->stderr('Not a predefined permission');
                return 1;
            }
            if ($auth->addChild($role,$permission)){
                $this->stdout('grant permission ok');
            }
        }elseif ($action == 6){
            echo "从某个角色剥夺权限 ...\n";                  // 提示当前操作  
            $name = $this->prompt('Role Name:');        // 接收用户名  
            if (!$role = $auth->getRole($name)){
                $this->stderr('Not a predefined role');
                return 1;
            }
            $name = $this->prompt('Permission Name:');         // 接收用户名  
            if (!$permission = $auth->getPermission($name)){
                $this->stderr('Not a predefined permission');
                return 1;
            }
            if ($auth->removeChild($role,$permission)){
                $this->stdout('revoke permission ok');
            }
        }elseif ($action == 7){
            echo "向某个用户赋予角色 ...\n";                  // 提示当前操作  
            $username = $this->prompt('User Name:');        // 接收用户名  
            if (!$user = Admin::findByUsername($username)){
                echo '找不到该用户';
                return 1;
            }
            $name = $this->prompt('Role Name:');        // 接收用户名  
            if (!$role = $auth->getRole($name)){
                $this->stderr('Not a predefined role');
                return 1;
            }
            if ($auth->assign($role,$user->getId())){
                $this->stdout('grant role ok');
            }
        }elseif ($action == 8){
            echo "从某个用户剥夺角色 ...\n";                  // 提示当前操作  
            $username = $this->prompt('User Name:');        // 接收用户名  
            if (!$user = Admin::findByUsername($username)){
                echo '找不到该用户';
                return 1;
            }
            $name = $this->prompt('Role Name:');        // 接收用户名  
            if (!$role = $auth->getRole($name)){
                $this->stderr('Not a predefined role');
                return 1;
            }
            if ($auth->revoke($role,$user->getId())){
                $this->stdout('revoke role ok');
            }
        }else{
            $this->stderr('Not a predefined option');
            return 1;
        }
        
        return 0;
    }
}