yii2-netdisk/migrations/m240305_042554_init_rbac.php

64 lines
1.2 KiB
PHP
Raw Normal View History

2024-03-05 13:32:12 +08:00
<?php
use yii\db\Migration;
use yii\db\Query;
2024-03-05 13:32:12 +08:00
/**
* Class m240305_042554_init_rbac
*/
class m240305_042554_init_rbac extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp(): void
{
$auth = Yii::$app->authManager;
$user = $auth->createRole('user');
$admin = $auth->createRole('admin');
$auth->add($user);
$auth->add($admin);
// 获取所有用户
$users = (new Query())
2024-03-05 13:32:12 +08:00
->select(['id', 'role'])
->from('user')
->all();
// 为每个用户分配角色
foreach ($users as $user) {
$role = $auth->getRole($user['role']);
if ($role) {
$auth->assign($role, $user['id']);
}
}
}
/**
* {@inheritdoc}
*/
2024-03-21 14:00:58 +08:00
public function safeDown(): void
2024-03-05 13:32:12 +08:00
{
$auth = Yii::$app->authManager;
// 删除角色和权限
$auth->removeAll();
}
/*
// Use up()/down() to run migration code without a transaction.
public function up()
{
}
public function down()
{
echo "m240305_042554_init_rbac cannot be reverted.\n";
return false;
}
*/
}