Skip to content

Commit 1fdd477

Browse files
author
Mariusz
committed
feat: config access by roles
1 parent 6bec619 commit 1fdd477

File tree

3 files changed

+351
-0
lines changed

3 files changed

+351
-0
lines changed

config/permission.php

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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, Laravel\Octane\Events\OperationTerminated event listener will be registered
108+
* this will refresh permissions on every TickTerminated, TaskTerminated and RequestTerminated
109+
* NOTE: This should not be needed in most cases, but an Octane/Vapor combination benefited from it.
110+
*/
111+
'register_octane_reset_listener' => false,
112+
113+
/*
114+
* Teams Feature.
115+
* When set to true the package implements teams using the 'team_foreign_key'.
116+
* If you want the migrations to register the 'team_foreign_key', you must
117+
* set this to true before doing the migration.
118+
* If you already did the migration then you must make a new migration to also
119+
* add 'team_foreign_key' to 'roles', 'model_has_roles', and 'model_has_permissions'
120+
* (view the latest version of this package's migration file)
121+
*/
122+
123+
'teams' => false,
124+
125+
/*
126+
* Passport Client Credentials Grant
127+
* When set to true the package will use Passports Client to check permissions
128+
*/
129+
130+
'use_passport_client_credentials' => false,
131+
132+
/*
133+
* When set to true, the required permission names are added to exception messages.
134+
* This could be considered an information leak in some contexts, so the default
135+
* setting is false here for optimum safety.
136+
*/
137+
138+
'display_permission_in_exception' => false,
139+
140+
/*
141+
* When set to true, the required role names are added to exception messages.
142+
* This could be considered an information leak in some contexts, so the default
143+
* setting is false here for optimum safety.
144+
*/
145+
146+
'display_role_in_exception' => false,
147+
148+
/*
149+
* By default wildcard permission lookups are disabled.
150+
* See documentation to understand supported syntax.
151+
*/
152+
153+
'enable_wildcard_permission' => false,
154+
155+
/*
156+
* The class to use for interpreting wildcard permissions.
157+
* If you need to modify delimiters, override the class and specify its name here.
158+
*/
159+
// 'permission.wildcard_permission' => Spatie\Permission\WildcardPermission::class,
160+
161+
/* Cache-specific settings */
162+
163+
'cache' => [
164+
165+
/*
166+
* By default all permissions are cached for 24 hours to speed up performance.
167+
* When permissions or roles are updated the cache is flushed automatically.
168+
*/
169+
170+
'expiration_time' => \DateInterval::createFromDateString('24 hours'),
171+
172+
/*
173+
* The cache key used to store all permissions.
174+
*/
175+
176+
'key' => 'spatie.permission.cache',
177+
178+
/*
179+
* You may optionally indicate a specific cache driver to use for permission and
180+
* role caching using any of the `store` drivers listed in the cache.php config
181+
* file. Using 'default' here means to use the `default` set in cache.php.
182+
*/
183+
184+
'store' => 'default',
185+
],
186+
];
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
$teams = config('permission.teams');
15+
$tableNames = config('permission.table_names');
16+
$columnNames = config('permission.column_names');
17+
$pivotRole = $columnNames['role_pivot_key'] ?? 'role_id';
18+
$pivotPermission = $columnNames['permission_pivot_key'] ?? 'permission_id';
19+
20+
if (empty($tableNames)) {
21+
throw new \Exception('Error: config/permission.php not loaded. Run [php artisan config:clear] and try again.');
22+
}
23+
if ($teams && empty($columnNames['team_foreign_key'] ?? null)) {
24+
throw new \Exception('Error: team_foreign_key on config/permission.php not loaded. Run [php artisan config:clear] and try again.');
25+
}
26+
27+
Schema::create($tableNames['permissions'], function (Blueprint $table) {
28+
//$table->engine('InnoDB');
29+
$table->bigIncrements('id'); // permission id
30+
$table->string('name'); // For MyISAM use string('name', 225); // (or 166 for InnoDB with Redundant/Compact row format)
31+
$table->string('guard_name'); // For MyISAM use string('guard_name', 25);
32+
$table->timestamps();
33+
34+
$table->unique(['name', 'guard_name']);
35+
});
36+
37+
Schema::create($tableNames['roles'], function (Blueprint $table) use ($teams, $columnNames) {
38+
//$table->engine('InnoDB');
39+
$table->bigIncrements('id'); // role id
40+
if ($teams || config('permission.testing')) { // permission.testing is a fix for sqlite testing
41+
$table->unsignedBigInteger($columnNames['team_foreign_key'])->nullable();
42+
$table->index($columnNames['team_foreign_key'], 'roles_team_foreign_key_index');
43+
}
44+
$table->string('name'); // For MyISAM use string('name', 225); // (or 166 for InnoDB with Redundant/Compact row format)
45+
$table->string('guard_name'); // For MyISAM use string('guard_name', 25);
46+
$table->timestamps();
47+
if ($teams || config('permission.testing')) {
48+
$table->unique([$columnNames['team_foreign_key'], 'name', 'guard_name']);
49+
} else {
50+
$table->unique(['name', 'guard_name']);
51+
}
52+
});
53+
54+
Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames, $pivotPermission, $teams) {
55+
$table->unsignedBigInteger($pivotPermission);
56+
57+
$table->string('model_type');
58+
$table->unsignedBigInteger($columnNames['model_morph_key']);
59+
$table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_permissions_model_id_model_type_index');
60+
61+
$table->foreign($pivotPermission)
62+
->references('id') // permission id
63+
->on($tableNames['permissions'])
64+
->onDelete('cascade');
65+
if ($teams) {
66+
$table->unsignedBigInteger($columnNames['team_foreign_key']);
67+
$table->index($columnNames['team_foreign_key'], 'model_has_permissions_team_foreign_key_index');
68+
69+
$table->primary([$columnNames['team_foreign_key'], $pivotPermission, $columnNames['model_morph_key'], 'model_type'],
70+
'model_has_permissions_permission_model_type_primary');
71+
} else {
72+
$table->primary([$pivotPermission, $columnNames['model_morph_key'], 'model_type'],
73+
'model_has_permissions_permission_model_type_primary');
74+
}
75+
76+
});
77+
78+
Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames, $pivotRole, $teams) {
79+
$table->unsignedBigInteger($pivotRole);
80+
81+
$table->string('model_type');
82+
$table->unsignedBigInteger($columnNames['model_morph_key']);
83+
$table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_roles_model_id_model_type_index');
84+
85+
$table->foreign($pivotRole)
86+
->references('id') // role id
87+
->on($tableNames['roles'])
88+
->onDelete('cascade');
89+
if ($teams) {
90+
$table->unsignedBigInteger($columnNames['team_foreign_key']);
91+
$table->index($columnNames['team_foreign_key'], 'model_has_roles_team_foreign_key_index');
92+
93+
$table->primary([$columnNames['team_foreign_key'], $pivotRole, $columnNames['model_morph_key'], 'model_type'],
94+
'model_has_roles_role_model_type_primary');
95+
} else {
96+
$table->primary([$pivotRole, $columnNames['model_morph_key'], 'model_type'],
97+
'model_has_roles_role_model_type_primary');
98+
}
99+
});
100+
101+
Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames, $pivotRole, $pivotPermission) {
102+
$table->unsignedBigInteger($pivotPermission);
103+
$table->unsignedBigInteger($pivotRole);
104+
105+
$table->foreign($pivotPermission)
106+
->references('id') // permission id
107+
->on($tableNames['permissions'])
108+
->onDelete('cascade');
109+
110+
$table->foreign($pivotRole)
111+
->references('id') // role id
112+
->on($tableNames['roles'])
113+
->onDelete('cascade');
114+
115+
$table->primary([$pivotPermission, $pivotRole], 'role_has_permissions_permission_id_role_id_primary');
116+
});
117+
118+
app('cache')
119+
->store(config('permission.cache.store') != 'default' ? config('permission.cache.store') : null)
120+
->forget(config('permission.cache.key'));
121+
}
122+
123+
/**
124+
* Reverse the migrations.
125+
*/
126+
public function down(): void
127+
{
128+
$tableNames = config('permission.table_names');
129+
130+
if (empty($tableNames)) {
131+
throw new \Exception('Error: config/permission.php not found and defaults could not be merged. Please publish the package configuration before proceeding, or drop the tables manually.');
132+
}
133+
134+
Schema::drop($tableNames['role_has_permissions']);
135+
Schema::drop($tableNames['model_has_roles']);
136+
Schema::drop($tableNames['model_has_permissions']);
137+
Schema::drop($tableNames['roles']);
138+
Schema::drop($tableNames['permissions']);
139+
}
140+
};

database/seeders/RoleSeeder.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Database\Seeders;
4+
5+
use App\Enums\RolesEnum;
6+
use App\Models\User;
7+
use Illuminate\Database\Seeder;
8+
use Spatie\Permission\Models\Role;
9+
10+
class RoleSeeder extends Seeder
11+
{
12+
/**
13+
* Run the database seeds.
14+
*/
15+
public function run(): void
16+
{
17+
foreach (RolesEnum::cases() as $case) {
18+
Role::updateOrCreate(['name' => $case->value]);
19+
}
20+
21+
//pass your email to assign roles
22+
$user = User::firstWhere('email', 'mario@mail.com');
23+
$user->assignRole([RolesEnum::OWNER->value, RolesEnum::INSTRUCTOR->value, RolesEnum::DRIVER->value]);
24+
}
25+
}

0 commit comments

Comments
 (0)