I am writing a new console command based on the code given in the ‘Building Authorization Data’ section of the Authorization documentation for Yii2. I have two databases which I have set up in the common/main-local.php file. One is called db and the other is called db1. When I run the command yii rbac/init it populates the db database but I do not know how to get it to populate db1. I can modify the common/main-local.php file and rename db1 to db and then run yii rbac/init and this works. But is there a way to select db1 either by using a command line option (I tried yii rbac/init --db=db1, but it did’nt work) or writing some code in the RbacController to select db1. I dont mind having RbacDb1Controller and rbacDbController files.
My controller code is below
<?php
namespace console\controllers;
use Yii;
use yii\console\Controller;
use yii\base\Exception;
class RbacController extends Controller
{
public function actionInit()
{
$auth = Yii::$app->authManager;
try{
// add "admin" role and give this role the "updatePost" permission
// as well as the permissions of the "author" role
$admin = $auth->createRole('admin');
$auth->add($admin);
}
catch(Exception $e){
echo "WARNING: admin already exists.\n";
}
try{
// Assign roles to users. IDs returned by IdentityInterface::getId()
// usually implemented in your User model.
$auth->assign($admin, 1);
}
catch(Exception $e){
echo "WARNING: Cannot assign admin to user id 1.\n";
}
}
}
?>
Any help would be greatly appreciated.
Regards,
David