Skip to content

Commit

Permalink
Include default seeders in the installer.
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-cintra committed Feb 19, 2024
1 parent d6f8c87 commit 4cb419b
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/Console/InstallerTraits/CoreModules.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ protected function migrateDatabase(): void

protected function seedDatabase(): void
{
(new Process([$this->phpBinary(), 'artisan', 'db:seed', '--class=Modules\User\Database\Seeders\UserSeeder'], base_path()))
copy(__DIR__.'/../../../stubs/database/seeders/DatabaseSeeder.php', base_path('database/seeders/DatabaseSeeder.php'));

(new Process([$this->phpBinary(), 'artisan', 'db:seed'], base_path()))
->setTimeout(null)
->run(function ($type, $output) {
$this->output->write($output);
Expand Down
20 changes: 20 additions & 0 deletions stubs/database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

use Illuminate\Database\Seeder;
use Modules\Acl\Database\Seeders\AclModelHasRolesSeeder;
use Modules\Acl\Database\Seeders\AclPermissionSeeder;
use Modules\Acl\Database\Seeders\AclRoleSeeder;
use Modules\User\Database\Seeders\UserSeeder;

class DatabaseSeeder extends Seeder
{
public function run(): void
{
$this->call([
UserSeeder::class,
AclRoleSeeder::class,
AclPermissionSeeder::class,
AclModelHasRolesSeeder::class,
]);
}
}
21 changes: 21 additions & 0 deletions stubs/modules/Acl/Database/Seeders/AclModelHasRolesSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Modules\Acl\Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class AclModelHasRolesSeeder extends Seeder
{
public function run(): void
{
DB::table('model_has_roles')->truncate();

//root
DB::table('model_has_roles')->insert([
'role_id' => 1,
'model_type' => 'user',
'model_id' => 1,
]);
}
}
58 changes: 58 additions & 0 deletions stubs/modules/Acl/Database/Seeders/AclPermissionSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Modules\Acl\Database\Seeders;

use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Permission;

class AclPermissionSeeder extends Seeder
{
public function run(): void
{
Permission::truncate();

$permissions = $this->getPermissions();

foreach ($permissions as $permission) {
Permission::create([
'name' => $permission,
'guard_name' => 'user',
]);
}
}

private function getPermissions(): array
{
return [
//Main Menu
'Dashboard',

//Acl: Access Control List
'Acl',
'Acl: User - List',
'Acl: Permission - List',
'Acl: Role - List',

//User/UserIndex.vue
'Acl: User - Manage Roles',
'Acl: User - Manage Permissions',
'Acl: User - Create',
'Acl: User - Edit',
'Acl: User - Delete',

//AclPermission/PermissionIndex.vue
'Acl: Permission - Create',
'Acl: Permission - Edit',
'Acl: Permission - Delete',

//AclRole/RoleIndex.vue
'Acl: Role - Create',
'Acl: Role - Edit',
'Acl: Role - Delete',

//AclRolePermission/RolePermissionForm.vue
'Acl: Role - Manage Permissions',
];

}
}
19 changes: 19 additions & 0 deletions stubs/modules/Acl/Database/Seeders/AclRoleSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Modules\Acl\Database\Seeders;

use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Role;

class AclRoleSeeder extends Seeder
{
public function run(): void
{
Role::truncate();

Role::create([
'name' => 'root',
'guard_name' => 'user',
]);
}
}
4 changes: 1 addition & 3 deletions stubs/modules/User/Database/Seeders/UserSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Schema;
use Modules\User\Models\User;
use Spatie\Permission\Models\Role;

class UserSeeder extends Seeder
{
Expand All @@ -18,15 +17,14 @@ public function run()
{
Schema::disableForeignKeyConstraints();

//this will be a root user...
$testUser = User::factory()->create([
'name' => 'Example User',
'email' => '[email protected]',
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
]);

Role::create(['name' => 'root']);

$testUser->assignRole('root');

Schema::enableForeignKeyConstraints();
Expand Down

0 comments on commit 4cb419b

Please sign in to comment.