Skip to content
This repository has been archived by the owner on Sep 28, 2023. It is now read-only.

Commit

Permalink
Define roles & permissions for all access user
Browse files Browse the repository at this point in the history
  • Loading branch information
kkamara committed Aug 21, 2023
1 parent c90174b commit 1064a5b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
30 changes: 25 additions & 5 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use \App\Models\User;
use App\Models\User;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;

class DatabaseSeeder extends Seeder
{
Expand All @@ -13,15 +15,33 @@ class DatabaseSeeder extends Seeder
*/
public function run(): void
{
$role = Role::create(['name' => 'all access']);
$permissions = json_decode(
file_get_contents(__DIR__.'/permissions.json'),
false
);
foreach($permissions->data as $key => $permission)
{
$p = Permission::create(['name' => $permission]);
$role->givePermissionTo($p);
}


$email = 'admin@example.com';
$user = User::where(
'email', $email
)->first();
if (
null !== User::where(
'email', $email
)->first()
null !== $user
) {
echo 'Not creating a user because user with admin@example.com already exists'.
PHP_EOL;
$user->assignRole('all access');
return;
}
User::factory()->count(1)
echo 'Creating user with email admin@example.com'.PHP_EOL;
$user = User::factory()->count(1)
->create(compact('email'));
$user[0]->assignRole('all access');
}
}
16 changes: 16 additions & 0 deletions database/seeders/permissions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"data": [
"create clients",
"edit clients",
"delete clients",
"view clients",
"create logs",
"edit logs",
"delete logs",
"view logs",
"create users",
"edit users",
"delete users",
"view users"
]
}

0 comments on commit 1064a5b

Please sign in to comment.