Skip to content

Commit cf711cb

Browse files
author
Curtis Delicata
committed
Update UserSeeder and RoleSeeder
1 parent 31cab39 commit cf711cb

2 files changed

Lines changed: 26 additions & 26 deletions

File tree

database/seeders/RolesSeeder.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Database\Seeders;
44

5+
use App\Models\Team;
56
use Illuminate\Database\Seeder;
67
use Spatie\Permission\Models\Permission;
78
use Spatie\Permission\Models\Role;
9+
use BezhanSalleh\FilamentShield\Support\Utils;
810

911
class RolesSeeder extends Seeder
1012
{
@@ -13,12 +15,19 @@ class RolesSeeder extends Seeder
1315
*/
1416
public function run(): void
1517
{
16-
$adminRole = Role::firstOrCreate(['name' => 'admin']);
18+
$roleData = [
19+
'name' => 'super_admin',
20+
'guard_name' => 'web',
21+
];
22+
23+
if (Utils::isTenancyEnabled()) {
24+
$team = Team::firstOrFail();
25+
$roleData["team_id"] = $team->id;
26+
}
27+
28+
$adminRole = Role::firstOrCreate($roleData);
29+
1730
$permissions = Permission::where('guard_name', 'web')->pluck('id')->toArray();
1831
$adminRole->syncPermissions($permissions);
19-
20-
$staffRole = Role::firstOrCreate(['name' => 'staff']);
21-
$staffPermissions = Permission::where('guard_name', 'web')->pluck('id')->toArray();
22-
$staffRole->syncPermissions($staffPermissions);
2332
}
2433
}

database/seeders/UserSeeder.php

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace Database\Seeders;
44

5+
use App\Models\Role;
56
use App\Models\Team;
67
use App\Models\User;
8+
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
79
use Illuminate\Database\Seeder;
810
use Illuminate\Support\Facades\Hash;
11+
use Illuminate\Support\Str;
912

1013
class UserSeeder extends Seeder
1114
{
@@ -14,34 +17,22 @@ class UserSeeder extends Seeder
1417
*/
1518
public function run(): void
1619
{
17-
setPermissionsTeamId(Team::first()->id);
20+
21+
$adminPassword = Str::random(12);
1822
$adminUser = User::create([
1923
'name' => 'Admin User',
2024
'email' => 'admin@example.com',
21-
'password' => Hash::make('password'),
22-
'email_verified_at' => now(),
23-
]);
24-
$adminUser->assignRole('admin');
25-
26-
$staffUser = User::create([
27-
'name' => 'Staff User',
28-
'email' => 'staff@example.com',
29-
'password' => Hash::make('password'),
25+
'password' => Hash::make($adminPassword),
3026
'email_verified_at' => now(),
3127
]);
32-
$staffUser->assignRole('staff');
3328

34-
// Create teams for admin and staff users
35-
$this->createTeamForUser($adminUser);
36-
$this->createTeamForUser($staffUser);
37-
}
29+
$team = Team::firstOrFail();
30+
$adminUser->teams()->syncWithoutDetaching([$team->id]);
3831

39-
private function createTeamForUser($user)
40-
{
41-
$team = Team::first();
42-
$team->users()->attach($user);
32+
$role = Role::where('name', 'super_admin')->firstOrFail();
33+
$adminUser->assignRole($role);
4334

44-
$user->current_team_id = 1;
45-
$user->save();
35+
// Print passwords to console
36+
echo "Admin password: {$adminPassword}\n";
4637
}
4738
}

0 commit comments

Comments
 (0)