Ekapp/skolehjem/database/seeds/RoleSeeder.php

58 lines
2.3 KiB
PHP
Raw Normal View History

2020-07-28 08:22:45 +00:00
<?php
2020-07-30 12:38:32 +00:00
use Illuminate\Database\Eloquent\Collection;
2020-07-28 08:22:45 +00:00
use Illuminate\Database\Seeder;
class RoleSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$roles = [
2020-07-29 11:28:25 +00:00
"Administrator" => "Administrator",
2020-07-30 10:28:05 +00:00
"Bruger" => "Bruger",
2020-07-28 08:22:45 +00:00
];
foreach ($roles as $key => $value) {
try {
if (\Spatie\Permission\Models\Role::findByName($key))
continue;
} catch (Exception $e) {
2020-07-30 10:28:05 +00:00
$role = new \Spatie\Permission\Models\Role();
2020-07-28 08:22:45 +00:00
2020-07-30 10:28:05 +00:00
$role->name = $key;
$role->description = $value;
2020-07-28 08:22:45 +00:00
2020-07-30 10:28:05 +00:00
$role->save();
2020-07-28 08:22:45 +00:00
}
}
2020-07-30 10:28:05 +00:00
//Give all permissions to Administrator (id: 1)
foreach (\Spatie\Permission\Models\Permission::all() as $permission) {
2020-07-30 12:38:32 +00:00
\Spatie\Permission\Models\Role::query()->find(1)->first()->givePermissionTo($permission);
2020-07-30 10:28:05 +00:00
}
2020-07-30 12:38:32 +00:00
//Bruger Permissions
$brugerPermissions = new Collection();
$brugerPermissions->add(\Spatie\Permission\Models\Permission::query()->where("name", "=", "event.list")->first());
$brugerPermissions->add(\Spatie\Permission\Models\Permission::query()->where("name", "=", "link.external.list")->first());
$brugerPermissions->add(\Spatie\Permission\Models\Permission::query()->where("name", "=", "contact.list")->first());
$brugerPermissions->add(\Spatie\Permission\Models\Permission::query()->where("name", "=", "feedback.create")->first());
$brugerPermissions->add(\Spatie\Permission\Models\Permission::query()->where("name", "=", "menuplan.list")->first());
$brugerPermissions->add(\Spatie\Permission\Models\Permission::query()->where("name", "=", "washing.machine.reservation.create")->first());
$brugerPermissions->add(\Spatie\Permission\Models\Permission::query()->where("name", "=", "washing.machine.reservation.delete")->first());
2020-07-30 12:43:14 +00:00
$brugerPermissions->add(\Spatie\Permission\Models\Permission::query()->where("name", "=", "washing.machine.reservation.list")->first());
2020-07-30 12:38:32 +00:00
2020-07-30 10:28:05 +00:00
//Give permissions to Bruger (id: 2)
2020-07-30 12:38:32 +00:00
foreach ($brugerPermissions as $permission) {
\Spatie\Permission\Models\Role::query()->find(2)->givePermissionTo($permission);
}
2020-07-28 08:22:45 +00:00
}
}