Skip to content

Commit

Permalink
Create PermissionSeeder.php
Browse files Browse the repository at this point in the history
  • Loading branch information
flxcn committed Jul 27, 2022
1 parent afd1576 commit 25f7212
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions database/seeders/PermissionSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use Spatie\Permission\PermissionRegistrar;

class PermissionSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
// Reset cached roles and permissions
app()[PermissionRegistrar::class]->forgetCachedPermissions();

// create permissions
Permission::create(['name' => 'create clients']);
Permission::create(['name' => 'edit clients']);
Permission::create(['name' => 'delete clients']);

Permission::create(['name' => 'create contacts']);
Permission::create(['name' => 'edit contacts']);
Permission::create(['name' => 'delete contacts']);

Permission::create(['name' => 'manage users']);



// create roles and assign existing permissions
$role1 = Role::create(['name' => 'comper']);
$role1->givePermissionTo('create clients');
$role1->givePermissionTo('edit clients');
$role1->givePermissionTo('create contacts');
$role1->givePermissionTo('edit contacts');

$role2 = Role::create(['name' => 'volunteer']);
$role2->givePermissionTo('create clients');
$role2->givePermissionTo('edit clients');
$role2->givePermissionTo('delete clients');
$role2->givePermissionTo('create contacts');
$role2->givePermissionTo('edit contacts');
$role2->givePermissionTo('delect contacts');

$role3 = Role::create(['name' => 'director']);
$role3->givePermissionTo('create clients');
$role3->givePermissionTo('edit clients');
$role3->givePermissionTo('delete clients');
$role3->givePermissionTo('create contacts');
$role3->givePermissionTo('edit contacts');
$role3->givePermissionTo('delete contacts');
$role3->givePermissionTo('manage users');

$role4 = Role::create(['name' => 'Super-Admin']);
// gets all permissions via Gate::before rule; see AuthServiceProvider

// create demo users
$user = \App\Models\User::factory()->create([
'name' => 'Example User',
'email' => '[email protected]',
]);
$user->assignRole($role1);

$user = \App\Models\User::factory()->create([
'name' => 'Example Admin User',
'email' => '[email protected]',
]);
$user->assignRole($role2);

$user = \App\Models\User::factory()->create([
'name' => 'Example Super-Admin User',
'email' => '[email protected]',
]);
$user->assignRole($role3);
}
}

0 comments on commit 25f7212

Please sign in to comment.