Skip to content

Commit 9c15c54

Browse files
authored
Role and permission enums (#41)
* Create Role.php * Create Permission.php * Use enums in seeder * Use enums
1 parent 37948be commit 9c15c54

File tree

6 files changed

+55
-16
lines changed

6 files changed

+55
-16
lines changed

app/Enums/Permission.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\Enums;
4+
5+
enum Permission: string
6+
{
7+
case EDIT_ORGANISATION = 'edit-organisation';
8+
case UPDATE_ORGANISATION = 'update-organisation';
9+
10+
case CREATE_POSTS = 'create-posts';
11+
case VIEW_POSTS = 'view-posts';
12+
case EDIT_POSTS = 'edit-posts';
13+
case UPDATE_POSTS = 'update-posts';
14+
case DELETE_POSTS = 'delete-posts';
15+
16+
public static function values(): array
17+
{
18+
return \collect(self::cases())->map(fn ($case): string => $case->value)->toArray();
19+
}
20+
}

app/Enums/Role.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace App\Enums;
4+
5+
enum Role: string
6+
{
7+
case ADMIN = 'admin';
8+
case USER = 'user';
9+
10+
public static function values(): array
11+
{
12+
return \collect(self::cases())->map(fn ($case): string => $case->value)->toArray();
13+
}
14+
}

app/Http/Requests/Organisation/OrganisationEdit.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
namespace App\Http\Requests\Organisation;
44

5+
use App\Enums\Permission;
56
use Illuminate\Foundation\Http\FormRequest;
67

78
class OrganisationEdit extends FormRequest
89
{
910
public function authorize()
1011
{
11-
return $this->user()->can('manage-organisation');
12+
return $this->user()->can(Permission::EDIT_ORGANISATION->value);
1213
}
1314
}

app/Http/Requests/Organisation/OrganisationUpdate.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
namespace App\Http\Requests\Organisation;
44

5+
use App\Enums\Permission;
56
use Illuminate\Foundation\Http\FormRequest;
67

78
class OrganisationUpdate extends FormRequest
89
{
910
public function authorize()
1011
{
11-
return $this->user()->can('manage-organisation');
12+
return $this->user()->can(Permission::UPDATE_ORGANISATION->value);
1213
}
1314

1415
public function rules()

database/seeders/RolesAndPermissionsSeeder.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace Database\Seeders;
44

5+
use App\Enums\Permission;
6+
use App\Enums\Role;
57
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
68
use Illuminate\Database\Seeder;
79
use Illuminate\Support\Collection;
8-
use Spatie\Permission\Models\Permission;
9-
use Spatie\Permission\Models\Role;
10+
use Spatie\Permission\Models\Permission as SpatiePermission;
11+
use Spatie\Permission\Models\Role as SpatieRole;
1012

1113
class RolesAndPermissionsSeeder extends Seeder
1214
{
@@ -28,31 +30,32 @@ public function run()
2830
protected function createRoles()
2931
{
3032
$this->roles = \collect([
31-
'admin' => Role::create(['name' => 'admin']),
32-
'user' => Role::create(['name' => 'user']),
33+
Role::ADMIN->value => SpatieRole::create(['name' => Role::ADMIN]),
34+
Role::USER->value => SpatieRole::create(['name' => Role::USER]),
3335
]);
3436
}
3537

3638
protected function createPermissions()
3739
{
3840
$this->orgnaisationPermissions = \collect([
39-
Permission::create(['name' => 'manage-organisation']),
41+
SpatiePermission::create(['name' => Permission::EDIT_ORGANISATION]),
42+
SpatiePermission::create(['name' => Permission::UPDATE_ORGANISATION]),
4043
]);
4144

4245
$this->postPermissions = \collect([
43-
Permission::create(['name' => 'create-posts']),
44-
Permission::create(['name' => 'view-posts']),
45-
Permission::create(['name' => 'edit-posts']),
46-
Permission::create(['name' => 'update-posts']),
47-
Permission::create(['name' => 'delete-posts']),
46+
SpatiePermission::create(['name' => Permission::CREATE_POSTS]),
47+
SpatiePermission::create(['name' => Permission::VIEW_POSTS]),
48+
SpatiePermission::create(['name' => Permission::EDIT_POSTS]),
49+
SpatiePermission::create(['name' => Permission::UPDATE_POSTS]),
50+
SpatiePermission::create(['name' => Permission::DELETE_POSTS]),
4851
]);
4952
}
5053

5154
protected function assignPermissionsToRoles()
5255
{
53-
$this->roles->get('admin')->givePermissionTo($this->postPermissions);
54-
$this->roles->get('admin')->givePermissionTo($this->orgnaisationPermissions);
56+
$this->roles->get(Role::ADMIN->value)->givePermissionTo($this->postPermissions);
57+
$this->roles->get(Role::ADMIN->value)->givePermissionTo($this->orgnaisationPermissions);
5558

56-
$this->roles->get('user')->givePermissionTo($this->postPermissions);
59+
$this->roles->get(Role::USER->value)->givePermissionTo($this->postPermissions);
5760
}
5861
}

resources/js/Layouts/App.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
{
6666
label: "Organisation",
6767
route: "organisation.edit",
68-
condition: this.userCan(this.$page.props, 'manage-organisation'),
68+
condition: this.userCan(this.$page.props, 'edit-organisation'),
6969
components: ['Organisation/Edit'],
7070
},
7171
{

0 commit comments

Comments
 (0)