8000 add user roles · cba85/laravel8-saas@b73288f · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Jul 6, 2023. It is now read-only.

Commit b73288f

Browse files
committed
add user roles
1 parent 0e9b3a4 commit b73288f

File tree

10 files changed

+362
-5
lines changed

10 files changed

+362
-5
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ $ php artisan db:seed
4141

4242
Manually add your Stripe products in `plans` table, including Stripe Id.
4343

44-
### 5. File storage
44+
### 5. Create a super admin user
45+
46+
### 6. File storage
4547

4648
OPTIONAL: If you prefer to use Laravel public disk storage instead Cloudinary:
4749

app/Models/User.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
use Illuminate\Foundation\Auth\User as Authenticatable;
88
use Illuminate\Notifications\Notifiable;
99
use Laravel\Cashier\Billable;
10+
use Spatie\Permission\Traits\HasRoles;
1011

1112
class User extends Authenticatable
1213
{
13-
use HasFactory, Notifiable, Billable;
14+
use HasFactory, Notifiable, Billable, HasRoles;
1415

1516
protected $fillable = [
1617
'name',

app/Providers/AuthServiceProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public function boot()
1515
{
1616
$this->registerPolicies();
1717

18-
//
18+
Gate::before(function ($user, $ability) {
19+
return $user->hasRole('Super Admin') ? true : null;
20+
});
1921
}
2022
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"laravel/cashier": "^12.14",
1414
"laravel/framework": "^8.40",
1515
"laravel/tinker": "^2.5",
16-
"laravel/ui": "^3.3"
16+
"laravel/ui": "^3.3",
17+
"spatie/laravel-permission": "^4.2"
1718
},
1819
"require-dev": {
1920
"facade/ignition": "^2.5",

composer.lock

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

config/app.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
App\Providers\EventServiceProvider::class,
4242
App\Providers\RouteServiceProvider::class,
4343
CloudinaryLabs\CloudinaryLaravel\CloudinaryServiceProvider::class,
44+
Spatie\Permission\PermissionServiceProvider::class,
4445
],
4546
'aliases' => [
4647
'App' => Illuminate\Support\Facades\App::class,

config/permission.php

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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+
/*
77+
* Change this if you want to name the related model primary key other than
78+
* `model_id`.
79+
*
80+
* For example, this would be nice if your primary keys are all UUIDs. In
81+
* that case, name this `model_uuid`.
82+
*/
83+
84+
'model_morph_key' => 'model_id',
85+
],
86+
87+
/*
88+
* When set to true, the required permission names are added to the exception
89+
* message. This could be considered an information leak in some contexts, so
90+
* the default setting is false here for optimum safety.
91+
*/
92+
93+
'display_permission_in_exception' => false,
94+
95+
/*
96+
* When set to true, the required role names are added to the exception
97+
* message. This could be considered an information leak in some contexts, so
98+
* the default setting is false here for optimum safety.
99+
*/
100+
101+
'display_role_in_exception' => false,
102+
103+
/*
104+
* By default wildcard permission lookups are disabled.
105+
*/
106+
107+
'enable_wildcard_permission' => false,
108+
109+
'cache' => [
110+
111+
/*
112+
* By default all permissions are cached for 24 hours to speed up performance.
113+
* When permissions or roles are updated the cache is flushed automatically.
114+
*/
115+
116+
'expiration_time' => \DateInterval::createFromDateString('24 hours'),
117+
118+
/*
119+
* The cache key used to store all permissions.
120+
*/
121+
122+
'key' => 'spatie.permission.cache',
123+
124+
/*
125+
* When checking for a permission against a model by passing a Permission
126+
* instance to the check, this key determines what attribute on the
127+
* Permissions model is used to cache against.
128+
*
129+
* Ideally, this should match your preferred way of checking permissions, eg:
130+
* `$user->can('view-posts')` would be 'name'.
131+
*/
132+
133+
'model_key' => 'name',
134+
135+
/*
136+
* You may optionally indicate a specific cache driver to use for permission and
137+
* role caching using any of the `store` drivers listed in the cache.php config
138+
* file. Using 'default' here means to use the `default` set in cache.php.
139+
*/
140+
141+
'store' => 'default',
142+
],
143+
];

0 commit comments

Comments
 (0)
0