rbac migration class cannot specify AuthManager and cannot execute multiple times

Suppose my application has two user tables(or two user system). one for back-end user, such as staff. another for front-end user.

They all need to have access control based on RBAC, so I decide to use one DbManager for back-end user, and use another DbManager for front-end user. such as:

//this configured in application(web.php) or front-end module(@app/modules/fontend/Module.php)
$app->setComponents([
    'authManager' => [
        'class' => yii\rbac\DbManager::class,
        'itemTable' => 'frontend_auth_item_table',
        'itemChildTable' => 'frontend_auth_item_child',
        'assignmentTable' => 'frontend_auth_assignment',
        'ruleTable' => 'frontend_auth_rule',
        'cacheKey' => 'frontendRbac'
    ]
]);
//this configured in back-end module(@app/modules/backend/Module.php)
$this->setComponents([
    'authManager' => [
        'class' => yii\rbac\DbManager::class,
        'itemTable' => 'backend_auth_item_table',
        'itemChildTable' => 'backend_auth_item_child',
        'assignmentTable' => 'backend_auth_assignment',
        'ruleTable' => 'backend_auth_rule',
        'cacheKey' => 'backendRbac'
    ]
]);

I need to create tables for them. But I can’t execute migration files in @yii/rbac/migrations twice because that migration table has same class name history.

how can I do?

  1. Because I can’t have many same class name (or migraiton name) in one migration table, so I create new migration class and inherit migration class in @yii/rbac/migrtions? I also have to overwrite getAuthManager() to get module authManager in new migration calss.
  2. Create separated migration table and execute migration files in @yii/rbac/migrations as before, but I can’t change getAuthManager() in @yii/rbac/migration file.

any suggestion?

I’d follow your first suggestion.

ok, I have taken first one.

first, add classes without namespace in @yii/rbac/migrations to Yii::$classMap.

protected function registerRbacMigrationClasses()
    {
        $migrationPath = Yii::getAlias('@yii/rbac/migrations');
        $handle = opendir($migrationPath);
        while (($file = readdir($handle)) !== false) {
            if ($file === '.' || $file === '..') {
                continue;
            }
            $path = $migrationPath . DIRECTORY_SEPARATOR . $file;
            if (preg_match('/^(m(\d{6}_?\d{6})\D.*?)\.php$/is', $file, $matches) && is_file($path)) {
                $class = $matches[1];
                Yii::$classMap[$class] = $path;
            }
        }
        closedir($handle);
    }

second, create new class and inherit old class:

class m210420_033047_rbac_init extends m140506_102106_rbac_init
{
    protected function getAuthManager()
    {
        //I don't use Yii::$app->get('authManager') because that my authManager component is configured in submodule instead of Application
        return Yii::$app->controller->module->get('authManager');
    }
}

class m210420_033111_rbac_add_index_on_auth_assignment_user_id extends m170907_052038_rbac_add_index_on_auth_assignment_user_id
{
    protected function getAuthManager()
    {
        return Yii::$app->controller->module->get('authManager');
    }
}

class m210420_033124_rbac_updates_indexes_without_prefix extends m180523_151638_rbac_updates_indexes_without_prefix
{
    protected function getAuthManager()
    {
        return Yii::$app->controller->module->get('authManager');
    }
}