duanfen7676 2014-07-10 11:07
浏览 51

Yii2用户个人资料和Rbac

I have 3 roles with Rbac in my Yii2 based application (SuperAdmin, Admin, User). How I can allocate profile to these users.

I want to have some fields for SuperAdmin profiles and another fields for Admin profile, ...

  • 写回答

1条回答 默认 最新

  • dongpian2559 2014-12-10 17:30
    关注

    Assigning a role to a user in Yii2 rbac is best achieved initially by writing a migration script and running that. My script looks like this

    <?php
    
    use yii\db\Schema;
    use yii\db\Migration;
    use yii\base\InvalidConfigException; 
    
    use yiibac\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.
    ";
            return false;
        }
    }
    

    The critical line being

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

    Once your application grows a bit you'll have to build an admin interface that allows you to assign roles to users. ( I'm building that bit at the moment! )

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?