Ho creato la migrazione per inserire l’utente amministratore e il ruolo
migrations
use yii\db\Migration;
class m200124_074421_m000000_000010_add_administrator extends Migration
{
public function safeUp()
{
$auth = Yii::$app->authManager;
// create a role named "administrator"
$administratorRole = $auth->createRole('administrator');
$administratorRole->description = 'Administrator';
$auth->add($administratorRole);
// create permission for certain tasks
$permission = $auth->createPermission('user-management');
$permission->description = 'User Management';
$auth->add($permission);
// let administrators do user management
$auth->addChild($administratorRole, $auth->getPermission('user-management'));
// create user "admin" with password "verysecret"
$user = new \Da\User\Model\User([
'scenario' => 'create',
'email' => "email@example.com",
'username' => "admin",
'password' => "verysecret" // >6 characters!
]);
$user->confirmed_at = time();
$user->save();
// assign role to our admin-user
$auth->assign($administratorRole, $user->id);
}
public function safeDown()
{
$auth = Yii::$app->authManager;
// delete permission
$auth->remove($auth->getPermission('user-management'));
// delete admin-user and administrator role
$administratorRole = $auth->getRole("administrator");
$user = \Da\User\Model\User::findOne(['name'=>"admin"]);
$auth->revoke($administratorRole, $user->id);
$user->delete();
}
}
Successivamente ho modificato la configurazione
config/console.php
<?php
$params = require DIR . ‘/params.php’;
$db = require DIR . ‘/db.php’;
$config = [
‘id’ => ‘basic-console’,
‘basePath’ => dirname(DIR),
‘bootstrap’ => [‘log’],
‘controllerNamespace’ => ‘app\commands’,
‘aliases’ => [
‘@bower’ => ‘@vendor/bower-asset’,
‘@npm’ => ‘@vendor/npm-asset’,
‘@tests’ => ‘@app/tests’,
],
‘modules’ => [
‘user’ => [
‘class’ => Da\User\Module::class,
]
],
‘components’ => [
‘cache’ => [
‘class’ => ‘yii\caching\FileCache’,
],
‘log’ => [
‘targets’ => [
[
‘class’ => ‘yii\log\FileTarget’,
‘levels’ => [‘error’, ‘warning’],
],
],
],
‘db’ => $db,
‘authManager’ => [
‘class’ => ‘Da\User\Component\AuthDbManagerComponent’,
],
],
‘params’ => $params,
‘controllerMap’ => [
‘migrate’ => [
‘class’ => \yii\console\controllers\MigrateController::class,
‘migrationPath’ => [
‘@app/migrations’,
‘@yii/rbac/migrations’,
],
‘migrationNamespaces’ => [
‘Da\User\Migration’,
],
],
],
];
if (YII_ENV_DEV) {
// configuration adjustments for ‘dev’ environment
$config[‘bootstrap’][] = ‘gii’;
$config[‘modules’][‘gii’] = [
‘class’ => ‘yii\gii\Module’,
];
}
return $config;
config/web.php
$params = require DIR . ‘/params.php’;
$db = require DIR . ‘/db.php’;
$config = [
‘id’ => ‘basic’,
‘basePath’ => dirname(DIR),
‘bootstrap’ => [‘log’],
‘aliases’ => [
‘@bower’ => ‘@vendor/bower-asset’,
‘@npm’ => ‘@vendor/npm-asset’,
],
‘modules’ => [
‘user’ => [
‘class’ => Da\User\Module::class,
]
],
‘components’ => [
‘request’ => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
‘cookieValidationKey’ => ‘***********************************’,
],
‘cache’ => [
‘class’ => ‘yii\caching\FileCache’,
],
‘errorHandler’ => [
‘errorAction’ => ‘site/error’,
],
‘authManager’ => [
‘class’ => ‘Da\User\Component\AuthDbManagerComponent’,
],
‘mailer’ => [
‘class’ => ‘yii\swiftmailer\Mailer’,
// send all mails to a file by default. You have to set
// ‘useFileTransport’ to false and configure a transport
// for the mailer to send real emails.
‘useFileTransport’ => true,
],
‘log’ => [
‘traceLevel’ => YII_DEBUG ? 3 : 0,
‘targets’ => [
[
‘class’ => ‘yii\log\FileTarget’,
‘levels’ => [‘error’, ‘warning’],
],
],
],
‘db’ => $db,
/*
‘urlManager’ => [
‘enablePrettyUrl’ => true,
‘showScriptName’ => false,
‘rules’ => [
],
],
*/
],
‘params’ => $params,
];
Ho testato il login e il logout e tutto funziona correttamente, se devo accedere alla parte admin, inserendo il link user/admin invece non risulto autorizzato.
errore 403
Mi sono sicuramente perso qualcosa, ma non trovo riferimenti, quell’errore non dovrebbe darlo con l’account admin e mi sono accertato che anche sul database fossero presenti tabelle e gli inserimenti dalle migrazioni lanciate e risulta tutto…
Sono ad un punto morto…