Skip to content

Commit d189927

Browse files
committed
- Upgraded to Laravel Permissions 7.2
1 parent c486d2f commit d189927

9 files changed

Lines changed: 73 additions & 87 deletions

File tree

.github/workflows/run-tests.yml

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,16 @@ jobs:
1616
fail-fast: true
1717
matrix:
1818
os: [ ubuntu-latest ]
19-
php: [ 8.4, 8.3, 8.2, 8.1 ]
20-
laravel: [ 12.*, 11.*, 10.*, 9.* ]
19+
php: [ 8.4, 8.3 ]
20+
laravel: [ 13.*, 12.* ]
2121
stability: [ prefer-lowest, prefer-stable ]
2222
include:
23+
- laravel: 13.*
24+
testbench: 11.*
25+
carbon: ^3.8.4
2326
- laravel: 12.*
2427
testbench: 10.*
2528
carbon: ^3.8.4
26-
- laravel: 11.*
27-
testbench: 9.*
28-
carbon: ^2.63
29-
- laravel: 10.*
30-
testbench: 8.*
31-
carbon: ^2.63
32-
- laravel: 9.*
33-
testbench: 7.*
34-
carbon: ^2.63
35-
exclude:
36-
- laravel: 12.*
37-
php: 8.1
38-
- laravel: 11.*
39-
php: 8.1
4029

4130
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}
4231

composer.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
}
1111
],
1212
"require": {
13-
"php": "^8.1",
14-
"illuminate/support": "^9.0 || ^10.0 || ^11.0 || ^12.0",
15-
"spatie/laravel-permission": "^6.3",
16-
"javaabu/helpers": "^1.59",
17-
"javaabu/activitylog": "^1.4"
13+
"php": "^8.3",
14+
"illuminate/support": "^12.0 || ^13.0",
15+
"spatie/laravel-permission": "^7.2",
16+
"javaabu/helpers": "^1.70",
17+
"javaabu/activitylog": "^1.5"
1818
},
1919
"require-dev": {
20-
"orchestra/testbench": "^7.0 || ^8.0 || ^9.0 || ^10.0",
21-
"phpunit/phpunit": "^9.5 || ^10.5 || ^11.5.3"
20+
"orchestra/testbench": "^10.0 || ^11.0",
21+
"phpunit/phpunit": "^12.5.8 || ^13.0.3"
2222
},
2323
"autoload": {
2424
"psr-4": {
@@ -34,7 +34,7 @@
3434
},
3535
"extra": {
3636
"branch-alias": {
37-
"dev-main": "1.0-dev"
37+
"dev-main": "2.0-dev"
3838
},
3939
"laravel": {
4040
"providers": [

config/permission.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@
7575
/*
7676
* Change this if you want to name the related pivots other than defaults
7777
*/
78-
'role_pivot_key' => null, //default 'role_id',
79-
'permission_pivot_key' => null, //default 'permission_id',
78+
'role_pivot_key' => null, // default 'role_id',
79+
'permission_pivot_key' => null, // default 'permission_id',
8080

8181
/*
8282
* Change this if you want to name the related model primary key other than
@@ -172,7 +172,7 @@
172172
* The class to use for interpreting wildcard permissions.
173173
* If you need to modify delimiters, override the class and specify its name here.
174174
*/
175-
// 'permission.wildcard_permission' => Spatie\Permission\WildcardPermission::class,
175+
// 'wildcard_permission' => Spatie\Permission\WildcardPermission::class,
176176

177177
/* Cache-specific settings */
178178

database/migrations/2024_03_06_181813_create_permission_tables.php

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
use Illuminate\Support\Facades\Schema;
4-
use Illuminate\Database\Schema\Blueprint;
53
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
66

77
return new class extends Migration
88
{
@@ -17,17 +17,16 @@ public function up(): void
1717
$pivotRole = $columnNames['role_pivot_key'] ?? 'role_id';
1818
$pivotPermission = $columnNames['permission_pivot_key'] ?? 'permission_id';
1919

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->bigIncrements('id'); // permission id
29-
$table->string('name'); // For MySQL 8.0 use string('name', 125);
30-
$table->string('guard_name'); // For MySQL 8.0 use string('guard_name', 125);
20+
throw_if(empty($tableNames), 'Error: config/permission.php not loaded. Run [php artisan config:clear] and try again.');
21+
throw_if($teams && empty($columnNames['team_foreign_key'] ?? null), 'Error: team_foreign_key on config/permission.php not loaded. Run [php artisan config:clear] and try again.');
22+
23+
/**
24+
* See `docs/prerequisites.md` for suggested lengths on 'name' and 'guard_name' if "1071 Specified key was too long" errors are encountered.
25+
*/
26+
Schema::create($tableNames['permissions'], static function (Blueprint $table) {
27+
$table->id(); // permission id
28+
$table->string('name');
29+
$table->string('guard_name');
3130
$table->timestamps();
3231

3332
$table->unique(['name', 'guard_name']);
@@ -37,14 +36,17 @@ public function up(): void
3736
$table->string('model')->index()->nullable();
3837
});
3938

40-
Schema::create($tableNames['roles'], function (Blueprint $table) use ($teams, $columnNames) {
41-
$table->bigIncrements('id'); // role id
39+
/**
40+
* See `docs/prerequisites.md` for suggested lengths on 'name' and 'guard_name' if "1071 Specified key was too long" errors are encountered.
41+
*/
42+
Schema::create($tableNames['roles'], static function (Blueprint $table) use ($teams, $columnNames) {
43+
$table->id(); // role id
4244
if ($teams || config('permission.testing')) { // permission.testing is a fix for sqlite testing
4345
$table->unsignedBigInteger($columnNames['team_foreign_key'])->nullable();
4446
$table->index($columnNames['team_foreign_key'], 'roles_team_foreign_key_index');
4547
}
46-
$table->string('name'); // For MySQL 8.0 use string('name', 125);
47-
$table->string('guard_name'); // For MySQL 8.0 use string('guard_name', 125);
48+
$table->string('name');
49+
$table->string('guard_name');
4850
$table->timestamps();
4951
if ($teams || config('permission.testing')) {
5052
$table->unique([$columnNames['team_foreign_key'], 'name', 'guard_name']);
@@ -56,7 +58,7 @@ public function up(): void
5658
$table->string('description')->nullable();
5759
});
5860

59-
Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames, $pivotPermission, $teams) {
61+
Schema::create($tableNames['model_has_permissions'], static function (Blueprint $table) use ($tableNames, $columnNames, $pivotPermission, $teams) {
6062
$table->unsignedBigInteger($pivotPermission);
6163

6264
$table->string('model_type');
@@ -66,7 +68,7 @@ public function up(): void
6668
$table->foreign($pivotPermission)
6769
->references('id') // permission id
6870
->on($tableNames['permissions'])
69-
->onDelete('cascade');
71+
->cascadeOnDelete();
7072
if ($teams) {
7173
$table->unsignedBigInteger($columnNames['team_foreign_key']);
7274
$table->index($columnNames['team_foreign_key'], 'model_has_permissions_team_foreign_key_index');
@@ -77,10 +79,9 @@ public function up(): void
7779
$table->primary([$pivotPermission, $columnNames['model_morph_key'], 'model_type'],
7880
'model_has_permissions_permission_model_type_primary');
7981
}
80-
8182
});
8283

83-
Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames, $pivotRole, $teams) {
84+
Schema::create($tableNames['model_has_roles'], static function (Blueprint $table) use ($tableNames, $columnNames, $pivotRole, $teams) {
8485
$table->unsignedBigInteger($pivotRole);
8586

8687
$table->string('model_type');
@@ -90,7 +91,7 @@ public function up(): void
9091
$table->foreign($pivotRole)
9192
->references('id') // role id
9293
->on($tableNames['roles'])
93-
->onDelete('cascade');
94+
->cascadeOnDelete();
9495
if ($teams) {
9596
$table->unsignedBigInteger($columnNames['team_foreign_key']);
9697
$table->index($columnNames['team_foreign_key'], 'model_has_roles_team_foreign_key_index');
@@ -103,19 +104,19 @@ public function up(): void
103104
}
104105
});
105106

106-
Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames, $pivotRole, $pivotPermission) {
107+
Schema::create($tableNames['role_has_permissions'], static function (Blueprint $table) use ($tableNames, $pivotRole, $pivotPermission) {
107108
$table->unsignedBigInteger($pivotPermission);
108109
$table->unsignedBigInteger($pivotRole);
109110

110111
$table->foreign($pivotPermission)
111112
->references('id') // permission id
112113
->on($tableNames['permissions'])
113-
->onDelete('cascade');
114+
->cascadeOnDelete();
114115

115116
$table->foreign($pivotRole)
116117
->references('id') // role id
117118
->on($tableNames['roles'])
118-
->onDelete('cascade');
119+
->cascadeOnDelete();
119120

120121
$table->primary([$pivotPermission, $pivotRole], 'role_has_permissions_permission_id_role_id_primary');
121122
});
@@ -132,14 +133,12 @@ public function down(): void
132133
{
133134
$tableNames = config('permission.table_names');
134135

135-
if (empty($tableNames)) {
136-
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.');
137-
}
136+
throw_if(empty($tableNames), 'Error: config/permission.php not found and defaults could not be merged. Please publish the package configuration before proceeding, or drop the tables manually.');
138137

139-
Schema::drop($tableNames['role_has_permissions']);
140-
Schema::drop($tableNames['model_has_roles']);
141-
Schema::drop($tableNames['model_has_permissions']);
142-
Schema::drop($tableNames['roles']);
143-
Schema::drop($tableNames['permissions']);
138+
Schema::dropIfExists($tableNames['role_has_permissions']);
139+
Schema::dropIfExists($tableNames['model_has_roles']);
140+
Schema::dropIfExists($tableNames['model_has_permissions']);
141+
Schema::dropIfExists($tableNames['roles']);
142+
Schema::dropIfExists($tableNames['permissions']);
144143
}
145144
};

database/migrations/2024_03_06_181814_add_teams_fields.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
35
use Illuminate\Support\Facades\DB;
46
use Illuminate\Support\Facades\Schema;
5-
use Illuminate\Database\Schema\Blueprint;
6-
use Illuminate\Database\Migrations\Migration;
77

88
return new class extends Migration
99
{
@@ -21,12 +21,9 @@ public function up(): void
2121
if (! $teams) {
2222
return;
2323
}
24-
if (empty($tableNames)) {
25-
throw new \Exception('Error: config/permission.php not loaded. Run [php artisan config:clear] and try again.');
26-
}
27-
if (empty($columnNames['team_foreign_key'] ?? null)) {
28-
throw new \Exception('Error: team_foreign_key on config/permission.php not loaded. Run [php artisan config:clear] and try again.');
29-
}
24+
25+
throw_if(empty($tableNames), 'Error: config/permission.php not loaded. Run [php artisan config:clear] and try again.');
26+
throw_if(empty($columnNames['team_foreign_key'] ?? null), 'Error: team_foreign_key on config/permission.php not loaded. Run [php artisan config:clear] and try again.');
3027

3128
if (! Schema::hasColumn($tableNames['roles'], $columnNames['team_foreign_key'])) {
3229
Schema::table($tableNames['roles'], function (Blueprint $table) use ($columnNames) {
@@ -52,7 +49,9 @@ public function up(): void
5249
'model_has_permissions_permission_model_type_primary');
5350
if (DB::getDriverName() !== 'sqlite') {
5451
$table->foreign($pivotPermission)
55-
->references('id')->on($tableNames['permissions'])->onDelete('cascade');
52+
->references('id')
53+
->on($tableNames['permissions'])
54+
->cascadeOnDelete();
5655
}
5756
});
5857
}
@@ -71,7 +70,9 @@ public function up(): void
7170
'model_has_roles_role_model_type_primary');
7271
if (DB::getDriverName() !== 'sqlite') {
7372
$table->foreign($pivotRole)
74-
->references('id')->on($tableNames['roles'])->onDelete('cascade');
73+
->references('id')
74+
->on($tableNames['roles'])
75+
->cascadeOnDelete();
7576
}
7677
});
7778
}
@@ -84,8 +85,5 @@ public function up(): void
8485
/**
8586
* Reverse the migrations.
8687
*/
87-
public function down(): void
88-
{
89-
90-
}
88+
public function down(): void {}
9189
};

docs/metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"name": "Permissions",
33
"description": "Modifications brought upon spatie/laravel-permissions",
4-
"version": "1"
4+
"version": "2"
55
}

tests/Feature/HasRolesTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Javaabu\Permissions\Tests\Models\User;
1212
use Javaabu\Permissions\Tests\Policies\UserPolicy;
1313
use Javaabu\Permissions\Tests\TestCase;
14+
use PHPUnit\Framework\Attributes\Test;
1415

1516
class HasRolesTest extends TestCase
1617
{
@@ -23,7 +24,7 @@ public function setUp(): void
2324
$this->runMigrations();
2425
}
2526

26-
/** @test */
27+
#[Test]
2728
public function it_skips_fake_roles_when_setting_user_roles(): void
2829
{
2930
Gate::policy(User::class, UserPolicy::class);
@@ -41,7 +42,7 @@ public function it_skips_fake_roles_when_setting_user_roles(): void
4142
]);
4243
}
4344

44-
/** @test */
45+
#[Test]
4546
public function it_can_assign_a_single_role(): void
4647
{
4748
Gate::policy(User::class, UserPolicy::class);
@@ -68,7 +69,7 @@ public function it_can_assign_a_single_role(): void
6869
]);
6970
}
7071

71-
/** @test */
72+
#[Test]
7273
public function it_can_assign_multiple_roles(): void
7374
{
7475
Gate::policy(User::class, UserPolicy::class);
@@ -111,7 +112,7 @@ public function it_can_assign_multiple_roles(): void
111112
]);
112113
}
113114

114-
/** @test */
115+
#[Test]
115116
function an_event_is_emitted_when_the_user_role_is_updated()
116117
{
117118
Event::fake();
@@ -144,7 +145,7 @@ function an_event_is_emitted_when_the_user_role_is_updated()
144145
});
145146
}
146147

147-
/** @test */
148+
#[Test]
148149
function it_logs_the_new_and_old_roles_when_a_user_role_is_updated()
149150
{
150151
Gate::policy(User::class, UserPolicy::class);

tests/Feature/PermissionsTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Javaabu\Permissions\Models\Role;
88
use Javaabu\Permissions\Tests\Models\User;
99
use Javaabu\Permissions\Tests\TestCase;
10+
use PHPUnit\Framework\Attributes\Test;
1011

1112
class PermissionsTest extends TestCase
1213
{
@@ -19,7 +20,7 @@ public function setUp(): void
1920
$this->runMigrations();
2021
}
2122

22-
/** @test */
23+
#[Test]
2324
public function it_can_check_if_a_user_has_a_permission(): void
2425
{
2526
$user = new User(['name' => 'Test User', 'email' => 'test@example.com']);

0 commit comments

Comments
 (0)