yii2-netdisk/migrations/m240305_042554_init_rbac.php

69 lines
1.4 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);
$access_home = $auth->createPermission('accessHome');
$access_home->description = '访问文件管理';
$auth->add($access_home);
$auth->addChild($user,$access_home);
// 获取所有用户
$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}
*/
public function safeDown()
{
$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;
}
*/
}