Skip to content

Commit 75f7ecf

Browse files
committed
Added Spatie Laravel-permission packages
1 parent b567295 commit 75f7ecf

File tree

3 files changed

+303
-0
lines changed

3 files changed

+303
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"laravel/framework": "^9.19",
1212
"laravel/sanctum": "^2.8",
1313
"laravel/tinker": "^2.7",
14+
"spatie/laravel-permission": "^5.5",
1415
"tightenco/ziggy": "^1.0"
1516
},
1617
"require-dev": {

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+
];
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
use Spatie\Permission\PermissionRegistrar;
7+
8+
class CreatePermissionTables extends Migration
9+
{
10+
/**
11+
* Run the migrations.
12+
*
13+
* @return void
14+
*/
15+
public function up()
16+
{
17+
$tableNames = config('permission.table_names');
18+
$columnNames = config('permission.column_names');
19+
$teams = config('permission.teams');
20+
21+
if (empty($tableNames)) {
22+
throw new \Exception('Error: config/permission.php not loaded. Run [php artisan config:clear] and try again.');
23+
}
24+
if ($teams && empty($columnNames['team_foreign_key'] ?? null)) {
25+
throw new \Exception('Error: team_foreign_key on config/permission.php not loaded. Run [php artisan config:clear] and try again.');
26+
}
27+
28+
Schema::create($tableNames['permissions'], function (Blueprint $table) {
29+
$table->bigIncrements('id'); // permission id
30+
$table->string('name'); // For MySQL 8.0 use string('name', 125);
31+
$table->string('guard_name'); // For MySQL 8.0 use string('guard_name', 125);
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->bigIncrements('id'); // role id
39+
if ($teams || config('permission.testing')) { // permission.testing is a fix for sqlite testing
40+
$table->unsignedBigInteger($columnNames['team_foreign_key'])->nullable();
41+
$table->index($columnNames['team_foreign_key'], 'roles_team_foreign_key_index');
42+
}
43+
$table->string('name'); // For MySQL 8.0 use string('name', 125);
44+
$table->string('guard_name'); // For MySQL 8.0 use string('guard_name', 125);
45+
$table->timestamps();
46+
if ($teams || config('permission.testing')) {
47+
$table->unique([$columnNames['team_foreign_key'], 'name', 'guard_name']);
48+
} else {
49+
$table->unique(['name', 'guard_name']);
50+
}
51+
});
52+
53+
Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames, $teams) {
54+
$table->unsignedBigInteger(PermissionRegistrar::$pivotPermission);
55+
56+
$table->string('model_type');
57+
$table->unsignedBigInteger($columnNames['model_morph_key']);
58+
$table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_permissions_model_id_model_type_index');
59+
60+
$table->foreign(PermissionRegistrar::$pivotPermission)
61+
->references('id') // permission id
62+
->on($tableNames['permissions'])
63+
->onDelete('cascade');
64+
if ($teams) {
65+
$table->unsignedBigInteger($columnNames['team_foreign_key']);
66+
$table->index($columnNames['team_foreign_key'], 'model_has_permissions_team_foreign_key_index');
67+
68+
$table->primary([$columnNames['team_foreign_key'], PermissionRegistrar::$pivotPermission, $columnNames['model_morph_key'], 'model_type'],
69+
'model_has_permissions_permission_model_type_primary');
70+
} else {
71+
$table->primary([PermissionRegistrar::$pivotPermission, $columnNames['model_morph_key'], 'model_type'],
72+
'model_has_permissions_permission_model_type_primary');
73+
}
74+
75+
});
76+
77+
Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames, $teams) {
78+
$table->unsignedBigInteger(PermissionRegistrar::$pivotRole);
79+
80+
$table->string('model_type');
81+
$table->unsignedBigInteger($columnNames['model_morph_key']);
82+
$table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_roles_model_id_model_type_index');
83+
84+
$table->foreign(PermissionRegistrar::$pivotRole)
85+
->references('id') // role id
86+
->on($tableNames['roles'])
87+
->onDelete('cascade');
88+
if ($teams) {
89+
$table->unsignedBigInteger($columnNames['team_foreign_key']);
90+
$table->index($columnNames['team_foreign_key'], 'model_has_roles_team_foreign_key_index');
91+
92+
$table->primary([$columnNames['team_foreign_key'], PermissionRegistrar::$pivotRole, $columnNames['model_morph_key'], 'model_type'],
93+
'model_has_roles_role_model_type_primary');
94+
} else {
95+
$table->primary([PermissionRegistrar::$pivotRole, $columnNames['model_morph_key'], 'model_type'],
96+
'model_has_roles_role_model_type_primary');
97+
}
98+
});
99+
100+
Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) {
101+
$table->unsignedBigInteger(PermissionRegistrar::$pivotPermission);
102+
$table->unsignedBigInteger(PermissionRegistrar::$pivotRole);
103+
104+
$table->foreign(PermissionRegistrar::$pivotPermission)
105+
->references('id') // permission id
106+
->on($tableNames['permissions'])
107+
->onDelete('cascade');
108+
109+
$table->foreign(PermissionRegistrar::$pivotRole)
110+
->references('id') // role id
111+
->on($tableNames['roles'])
112+
->onDelete('cascade');
113+
114+
$table->primary([PermissionRegistrar::$pivotPermission, PermissionRegistrar::$pivotRole], 'role_has_permissions_permission_id_role_id_primary');
115+
});
116+
117+
app('cache')
118+
->store(config('permission.cache.store') != 'default' ? config('permission.cache.store') : null)
119+
->forget(config('permission.cache.key'));
120+
}
121+
122+
/**
123+
* Reverse the migrations.
124+
*
125+
* @return void
126+
*/
127+
public function down()
128+
{
129+
$tableNames = config('permission.table_names');
130+
131+
if (empty($tableNames)) {
132+
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.');
133+
}
134+
135+
Schema::drop($tableNames['role_has_permissions']);
136+
Schema::drop($tableNames['model_has_roles']);
137+
Schema::drop($tableNames['model_has_permissions']);
138+
Schema::drop($tableNames['roles']);
139+
Schema::drop($tableNames['permissions']);
140+
}
141+
}

0 commit comments

Comments
 (0)