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

Commit e8161db

Browse files
committed
feat(permission): use new db schema and entities
1 parent 3ed601e commit e8161db

File tree

6 files changed

+125
-41
lines changed

6 files changed

+125
-41
lines changed

app/Entities/Permission.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ class Permission extends \Spatie\Permission\Models\Permission
1414
/**
1515
* @var array
1616
*/
17-
protected $fillable = ['name', 'uuid'];
17+
protected $fillable = ['name', 'uuid', 'guard_name'];
1818
}

app/Entities/Role.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ class Role extends \Spatie\Permission\Models\Role
1515
/**
1616
* @var array
1717
*/
18-
protected $fillable = ['name', 'uuid'];
18+
protected $fillable = ['name', 'uuid', 'guard_name'];
1919
}

app/Support/HasPermissionsUuid.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
namespace App\Support;
44

5-
use App\Entities\Permission;
5+
use Spatie\Permission\Contracts\Permission;
6+
use App\Entities\Permission as PermissionEntity;
67

78
/**
89
* Class HasPermissionsUuid.
@@ -16,14 +17,14 @@ trait HasPermissionsUuid
1617
*
1718
* @return Permission
1819
*/
19-
protected function getStoredPermission($permissions)
20+
protected function getStoredPermission($permissions): Permission
2021
{
2122
if (is_string($permissions)) {
22-
return app(Permission::class)->where('name', $permissions)->orWhere('uuid', $permissions)->first();
23+
return app(PermissionEntity::class)->where('name', $permissions)->orWhere('uuid', $permissions)->first();
2324
}
2425

2526
if (is_array($permissions)) {
26-
return app(Permission::class)->whereIn('name', $permissions)->orWhereIn('uuid', $permissions)->get();
27+
return app(PermissionEntity::class)->whereIn('name', $permissions)->orWhereIn('uuid', $permissions)->get();
2728
}
2829

2930
return $permissions;

app/Support/HasRolesUuid.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
namespace App\Support;
44

5-
use App\Entities\Role;
5+
use App\Entities\Role as RoleEntity;
6+
use Spatie\Permission\Contracts\Role;
67

78
trait HasRolesUuid
89
{
@@ -11,10 +12,10 @@ trait HasRolesUuid
1112
*
1213
* @return Role
1314
*/
14-
protected function getStoredRole($role)
15+
protected function getStoredRole($role): Role
1516
{
1617
if (is_string($role)) {
17-
return app(Role::class)->where('name', $role)->orWhere('uuid', $role)->first();
18+
return app(RoleEntity::class)->where('name', $role)->orWhere('uuid', $role)->first();
1819
}
1920

2021
return $role;

config/permission.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
return [
4+
5+
'models' => [
6+
7+
/*
8+
* When using the "HasRoles" 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' => \App\Entities\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' => \App\Entities\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 "HasRoles" 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 "HasRoles" 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+
/*
75+
* By default all permissions will be cached for 24 hours unless a permission or
76+
* role is updated. Then the cache will be flushed immediately.
77+
*/
78+
79+
'cache_expiration_time' => 60 * 24,
80+
81+
/*
82+
* By default we'll make an entry in the application log when the permissions
83+
* could not be loaded. Normally this only occurs while installing the packages.
84+
*
85+
* If for some reason you want to disable that logging, set this value to false.
86+
*/
87+
88+
'log_registration_exception' => env('APP_ENV') !== 'testing',
89+
];

database/migrations/2017_02_09_031936_create_permission_tables.php

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,68 +12,61 @@ class CreatePermissionTables extends Migration
1212
*/
1313
public function up()
1414
{
15-
$config = config('laravel-permission.table_names');
15+
$tableNames = config('permission.table_names');
16+
$foreignKeys = config('permission.foreign_keys');
1617

17-
Schema::create($config['roles'], function (Blueprint $table) {
18+
Schema::create($tableNames['permissions'], function (Blueprint $table) {
1819
$table->increments('id');
19-
$table->string('name')->unique();
20+
$table->string('name');
21+
$table->string('guard_name');
2022
$table->uuid('uuid')->index();
2123
$table->timestamps();
2224
});
2325

24-
Schema::create($config['permissions'], function (Blueprint $table) {
26+
Schema::create($tableNames['roles'], function (Blueprint $table) {
2527
$table->increments('id');
26-
$table->string('name')->unique();
28+
$table->string('name');
29+
$table->string('guard_name');
2730
$table->uuid('uuid')->index();
2831
$table->timestamps();
2932
});
3033

31-
Schema::create($config['user_has_permissions'], function (Blueprint $table) use ($config) {
32-
$table->integer('user_id')->unsigned();
34+
Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $foreignKeys) {
3335
$table->integer('permission_id')->unsigned();
34-
35-
$table->foreign('user_id')
36-
->references('id')
37-
->on($config['users'])
38-
->onDelete('cascade');
36+
$table->morphs('model');
3937

4038
$table->foreign('permission_id')
4139
->references('id')
42-
->on($config['permissions'])
40+
->on($tableNames['permissions'])
4341
->onDelete('cascade');
4442

45-
$table->primary(['user_id', 'permission_id']);
43+
$table->primary(['permission_id', 'model_id', 'model_type']);
4644
});
4745

48-
Schema::create($config['user_has_roles'], function (Blueprint $table) use ($config) {
46+
Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $foreignKeys) {
4947
$table->integer('role_id')->unsigned();
50-
$table->integer('user_id')->unsigned();
48+
$table->morphs('model');
5149

5250
$table->foreign('role_id')
5351
->references('id')
54-
->on($config['roles'])
55-
->onDelete('cascade');
56-
57-
$table->foreign('user_id')
58-
->references('id')
59-
->on($config['users'])
52+
->on($tableNames['roles'])
6053
->onDelete('cascade');
6154

62-
$table->primary(['role_id', 'user_id']);
55+
$table->primary(['role_id', 'model_id', 'model_type']);
6356
});
6457

65-
Schema::create($config['role_has_permissions'], function (Blueprint $table) use ($config) {
58+
Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) {
6659
$table->integer('permission_id')->unsigned();
6760
$table->integer('role_id')->unsigned();
6861

6962
$table->foreign('permission_id')
7063
->references('id')
71-
->on($config['permissions'])
64+
->on($tableNames['permissions'])
7265
->onDelete('cascade');
7366

7467
$table->foreign('role_id')
7568
->references('id')
76-
->on($config['roles'])
69+
->on($tableNames['roles'])
7770
->onDelete('cascade');
7871

7972
$table->primary(['permission_id', 'role_id']);
@@ -87,12 +80,12 @@ public function up()
8780
*/
8881
public function down()
8982
{
90-
$config = config('laravel-permission.table_names');
83+
$tableNames = config('permission.table_names');
9184

92-
Schema::drop($config['role_has_permissions']);
93-
Schema::drop($config['user_has_roles']);
94-
Schema::drop($config['user_has_permissions']);
95-
Schema::drop($config['roles']);
96-
Schema::drop($config['permissions']);
85+
Schema::drop($tableNames['role_has_permissions']);
86+
Schema::drop($tableNames['model_has_roles']);
87+
Schema::drop($tableNames['model_has_permissions']);
88+
Schema::drop($tableNames['roles']);
89+
Schema::drop($tableNames['permissions']);
9790
}
9891
}

0 commit comments

Comments
 (0)