Skip to content

Commit 03a7db2

Browse files
authored
Roles and permissions (#6)
* Install spatie/laravel-permission via Composer * Add spatie/laravel-permission config * Add seeder for roles and permissions * Assign roles to admin user when seeding * Add logout link to home component * Add "allPermissions" to User model for simple array of permissions * Remove "data" item in resources * Add Vue composable for checking user permissions * Update files paths to components to match directory case
1 parent 1540437 commit 03a7db2

File tree

16 files changed

+479
-15
lines changed

16 files changed

+479
-15
lines changed

app/Http/Resources/UserResource.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ class UserResource extends JsonResource
99
public function toArray($request)
1010
{
1111
return [
12-
'id' => $this->id,
13-
'email' => $this->email,
14-
'name' => $this->name,
12+
'id' => $this->id,
13+
'email' => $this->email,
14+
'first_name' => $this->first_name,
15+
'last_name' => $this->last_name,
16+
'can' => $this->allPermissions,
1517
];
1618
}
1719
}

app/Models/User.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,20 @@
1010
use Illuminate\Notifications\Notifiable;
1111
use Illuminate\Support\Facades\Hash;
1212
use Laravel\Sanctum\HasApiTokens;
13+
use Spatie\Permission\Traits\HasRoles;
1314

1415
class User extends Authenticatable
1516
{
1617
use HasApiTokens;
1718
use HasFactory;
1819
use Notifiable;
20+
use HasRoles;
1921

2022
protected $fillable = [
2123
'first_name',
2224
'last_name',
2325
'email',
26+
'current_organisation_id',
2427
];
2528

2629
protected $hidden = [
@@ -39,6 +42,13 @@ protected function password(): Attribute
3942
);
4043
}
4144

45+
protected function allPermissions(): Attribute
46+
{
47+
return Attribute::make(
48+
get: fn () => $this->getAllPermissions()->pluck('name')
49+
);
50+
}
51+
4252
public function organisations()
4353
{
4454
return $this->hasMany(Organisation::class);

app/Providers/AppServiceProvider.php

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

33
namespace App\Providers;
44

5+
use Illuminate\Http\Resources\Json\JsonResource;
56
use Illuminate\Support\ServiceProvider;
67

78
class AppServiceProvider extends ServiceProvider
@@ -23,6 +24,6 @@ public function register()
2324
*/
2425
public function boot()
2526
{
26-
//
27+
JsonResource::withoutWrapping();
2728
}
2829
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"laravel/framework": "^9.43",
1111
"laravel/sanctum": "^3.0.1",
1212
"laravel/tinker": "^2.7.3",
13+
"spatie/laravel-permission": "^5.7",
1314
"tightenco/ziggy": "^1.5"
1415
},
1516
"require-dev": {

composer.lock

Lines changed: 83 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/permission.php

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?php
2+
3+
return [
4+
5+
'models' => [
6+
7+
/*
8+
* When using the "HasPermissions" trait from this package, we need to know which
9+
* Eloquent model should be used to retrieve your permissions. Of course, it
10+
* is often just the "Permission" model but you may use whatever you like.
11+
*
12+
* The model you want to use as a Permission model needs to implement the
13+
* `Spatie\Permission\Contracts\Permission` contract.
14+
*/
15+
16+
'permission' => Spatie\Permission\Models\Permission::class,
17+
18+
/*
19+
* When using the "HasRoles" trait from this package, we need to know which
20+
* Eloquent model should be used to retrieve your roles. Of course, it
21+
* is often just the "Role" model but you may use whatever you like.
22+
*
23+
* The model you want to use as a Role model needs to implement the
24+
* `Spatie\Permission\Contracts\Role` contract.
25+
*/
26+
27+
'role' => Spatie\Permission\Models\Role::class,
28+
29+
],
30+
31+
'table_names' => [
32+
33+
/*
34+
* When using the "HasRoles" trait from this package, we need to know which
35+
* table should be used to retrieve your roles. We have chosen a basic
36+
* default value but you may easily change it to any table you like.
37+
*/
38+
39+
'roles' => 'roles',
40+
41+
/*
42+
* When using the "HasPermissions" trait from this package, we need to know which
43+
* table should be used to retrieve your permissions. We have chosen a basic
44+
* default value but you may easily change it to any table you like.
45+
*/
46+
47+
'permissions' => 'permissions',
48+
49+
/*
50+
* When using the "HasPermissions" trait from this package, we need to know which
51+
* table should be used to retrieve your models permissions. We have chosen a
52+
* basic default value but you may easily change it to any table you like.
53+
*/
54+
55+
'model_has_permissions' => 'model_has_permissions',
56+
57+
/*
58+
* When using the "HasRoles" trait from this package, we need to know which
59+
* table should be used to retrieve your models roles. We have chosen a
60+
* basic default value but you may easily change it to any table you like.
61+
*/
62+
63+
'model_has_roles' => 'model_has_roles',
64+
65+
/*
66+
* When using the "HasRoles" trait from this package, we need to know which
67+
* table should be used to retrieve your roles permissions. We have chosen a
68+
* basic default value but you may easily change it to any table you like.
69+
*/
70+
71+
'role_has_permissions' => 'role_has_permissions',
72+
],
73+
74+
'column_names' => [
75+
/*
76+
* Change this if you want to name the related pivots other than defaults
77+
*/
78+
'role_pivot_key' => null, //default 'role_id',
79+
'permission_pivot_key' => null, //default 'permission_id',
80+
81+
/*
82+
* Change this if you want to name the related model primary key other than
83+
* `model_id`.
84+
*
85+
* For example, this would be nice if your primary keys are all UUIDs. In
86+
* that case, name this `model_uuid`.
87+
*/
88+
89+
'model_morph_key' => 'model_id',
90+
91+
/*
92+
* Change this if you want to use the teams feature and your related model's
93+
* foreign key is other than `team_id`.
94+
*/
95+
96+
'team_foreign_key' => 'team_id',
97+
],
98+
99+
/*
100+
* When set to true, the method for checking permissions will be registered on the gate.
101+
* Set this to false, if you want to implement custom logic for checking permissions.
102+
*/
103+
104+
'register_permission_check_method' => true,
105+
106+
/*
107+
* When set to true the package implements teams using the 'team_foreign_key'. If you want
108+
* the migrations to register the 'team_foreign_key', you must set this to true
109+
* before doing the migration. If you already did the migration then you must make a new
110+
* migration to also add 'team_foreign_key' to 'roles', 'model_has_roles', and
111+
* 'model_has_permissions'(view the latest version of package's migration file)
112+
*/
113+
114+
'teams' => false,
115+
116+
/*
117+
* When set to true, the required permission names are added to the exception
118+
* message. This could be considered an information leak in some contexts, so
119+
* the default setting is false here for optimum safety.
120+
*/
121+
122+
'display_permission_in_exception' => false,
123+
124+
/*
125+
* When set to true, the required role names are added to the exception
126+
* message. This could be considered an information leak in some contexts, so
127+
* the default setting is false here for optimum safety.
128+
*/
129+
130+
'display_role_in_exception' => false,
131+
132+
/*
133+
* By default wildcard permission lookups are disabled.
134+
*/
135+
136+
'enable_wildcard_permission' => false,
137+
138+
'cache' => [
139+
140+
/*
141+
* By default all permissions are cached for 24 hours to speed up performance.
142+
* When permissions or roles are updated the cache is flushed automatically.
143+
*/
144+
145+
'expiration_time' => \DateInterval::createFromDateString('24 hours'),
146+
147+
/*
148+
* The cache key used to store all permissions.
149+
*/
150+
151+
'key' => 'spatie.permission.cache',
152+
153+
/*
154+
* You may optionally indicate a specific cache driver to use for permission and
155+
* role caching using any of the `store` drivers listed in the cache.php config
156+
* file. Using 'default' here means to use the `default` set in cache.php.
157+
*/
158+
159+
'store' => 'default',
160+
],
161+
];

0 commit comments

Comments
 (0)