You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
3.0 KiB
90 lines
3.0 KiB
<?php
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use Illuminate\Database\Seeder;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Str;
|
|
|
|
class AdminUsersSeeder extends Seeder
|
|
{
|
|
private string $username = 'Admin';
|
|
private string $password = '12345678';
|
|
private string $roleName = '超级管理员';
|
|
|
|
/**
|
|
* Run the database seeds.
|
|
*/
|
|
public function run(): void
|
|
{
|
|
$user_id = DB::table('admin_users')->where('username', $this->username)
|
|
->value('id');
|
|
$role_id = DB::table('admin_roles')->where('name', $this->roleName)
|
|
->value('id');
|
|
if (!$user_id) {
|
|
DB::table('admin_users')->insert(
|
|
[
|
|
'username' => $this->username,
|
|
'name' => $this->username,
|
|
'email' => fake()->unique()->safeEmail(),
|
|
'password' => Hash::make($this->password), // password
|
|
'remember_token' => Str::random(10),
|
|
'packing_id' => 1,
|
|
'created_at' => date("Y-m-d H:i:s", time())
|
|
]
|
|
);
|
|
$user_id = DB::table('admin_users')->where(
|
|
'username',
|
|
$this->username
|
|
)
|
|
->value('id');
|
|
}
|
|
if (!$role_id) {
|
|
DB::table('admin_roles')->insert(
|
|
[
|
|
'name' => $this->roleName,
|
|
'remark' => "Super Administrator",
|
|
'status' => 1, // password
|
|
'level' => 1,
|
|
'created_at' => date("Y-m-d H:i:s", time()),
|
|
'updated_at' => date("Y-m-d H:i:s", time())
|
|
]
|
|
);
|
|
$role_id = DB::table('admin_roles')->where('name', $this->roleName)
|
|
->value('id');
|
|
}
|
|
$this->superAdminSettingRole($user_id, $role_id);
|
|
$this->settingRoleMenu($role_id);
|
|
}
|
|
|
|
private function superAdminSettingRole($user_id, $role_id)
|
|
{
|
|
// 超级管理员自动获取所有权限
|
|
DB::table('admin_role_users')->insert([
|
|
'role_id' => $role_id,
|
|
'user_id' => $user_id,
|
|
'created_at' => date("Y-m-d H:i:s", time()),
|
|
'updated_at' => date("Y-m-d H:i:s", time())
|
|
]);
|
|
}
|
|
|
|
private function settingRoleMenu($role_id)
|
|
{
|
|
$menu_ids = DB::table('admin_menu')->where('status', 1)->pluck('id');
|
|
$values = [];
|
|
foreach ($menu_ids as $menu_id) {
|
|
$values[] = [
|
|
'role_id' => $role_id,
|
|
'menu_id' => $menu_id,
|
|
'created_at' => date("Y-m-d H:i:s", time()),
|
|
'updated_at' => date("Y-m-d H:i:s", time())
|
|
];
|
|
}
|
|
$exists = DB::table('admin_role_menu')->where('role_id', $role_id)
|
|
->whereIn('menu_id', $menu_ids)->exists();
|
|
if ($values && !$exists) {
|
|
DB::table('admin_role_menu')->insert($values);
|
|
}
|
|
}
|
|
}
|
|
|