【问题标题】:Yii2 user profile and RbacYii2 用户配置文件和 Rbac
【发布时间】:2014-07-10 11:07:07
【问题描述】:

我在基于 Yii2 的应用程序中使用 Rbac 有 3 个角色(超级管理员、管理员、用户)。我如何将配置文件分配给这些用户。

我想为超级管理员配置文件设置一些字段,为管理员配置文件设置另一个字段,...

【问题讨论】:

    标签: php yii yii-extensions yii-components yii2


    【解决方案1】:

    在 Yii2 rbac 中为用户分配角色最初最好通过编写迁移脚本并运行它来实现。我的脚本是这样的

    <?php
    
    use yii\db\Schema;
    use yii\db\Migration;
    use yii\base\InvalidConfigException; 
    
    use yii\rbac\DbManager;
    use app\models\User;
    
    class m141210_101111_user_authorize extends Migration
    {
        /**
         * @throws yii\base\InvalidConfigException
         * @return DbManager
         *
         * Get the applications Authorization manager and make sure it's a valid 
         */
        protected function getAuthManager()
        {
            $authManager = Yii::$app->getAuthManager();
            if (!$authManager instanceof DbManager) {
                throw new InvalidConfigException('You should configure "authManager" component to use database before executing this migration.');
            }
            return $authManager;
        }
    
        public function up()
        {
            $authManager = $this->getAuthManager();
            $this->db = $authManager->db;
    
        $authManager = Yii::$app->getAuthManager();
            if (!$authManager instanceof DbManager) {
                throw new InvalidConfigException('You should configure "authManager" component in console.php to use database before executing this migration.');
            }
    
        $tableOptions = null;
            if ($this->db->driverName === 'mysql') {
                // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
                $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
            }
    
        $admin = $authManager->createRole('admin');
        $admin->description = 'Administrator - can do anything';
        $authManager->add($admin);
    
        $manager=$authManager->createRole('manager');
        $manager->description = 'Manager level access';
        $authManager->add($manager);
    
        $user=$authManager->createRole('end-user');
        $user->description = 'Normal user';
        $authManager->add($user);
    
        // let user admin have admin rights.
    
        if (!User::findOne(['username' => 'admin']))
             echo ('No user named "admin" was found to assign admin rights to!');
    
        $authManager->assign($admin, User::findOne(['username' => 'admin'])->id);
    
        }
    
        public function down()
        {
            echo "m141210_101111_user_authorize cannot be reverted.\n";
            return false;
        }
    }
    

    临界线是

     $authManager->assign($admin, User::findOne(['username' => 'admin'])->id);
    

    一旦您的应用程序有所增长,您就必须构建一个管理界面,以便您为用户分配角色。 (我现在正在建造那个位!)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-26
      • 1970-01-01
      • 1970-01-01
      • 2015-08-09
      • 1970-01-01
      • 2016-06-09
      相关资源
      最近更新 更多