From f9069aa5e0f9ca63d45cf406e5cb2d0f8907b0ed Mon Sep 17 00:00:00 2001 From: Chris Brown Date: Mon, 18 Sep 2023 16:01:04 -0400 Subject: [PATCH] Rename "Middlewares" namespace to "Middleware" (#2499) --- docs/basic-usage/middleware.md | 14 +++++++------- docs/basic-usage/passport.md | 2 +- docs/installation-lumen.md | 4 ++-- docs/upgrading.md | 10 +++++++--- .../PermissionMiddleware.php | 2 +- src/{Middlewares => Middleware}/RoleMiddleware.php | 2 +- .../RoleOrPermissionMiddleware.php | 2 +- tests/PermissionMiddlewareTest.php | 8 ++++---- tests/RoleMiddlewareTest.php | 8 ++++---- tests/RoleOrPermissionMiddlewareTest.php | 8 ++++---- tests/WildcardMiddlewareTest.php | 6 +++--- 11 files changed, 35 insertions(+), 31 deletions(-) rename src/{Middlewares => Middleware}/PermissionMiddleware.php (97%) rename src/{Middlewares => Middleware}/RoleMiddleware.php (97%) rename src/{Middlewares => Middleware}/RoleOrPermissionMiddleware.php (97%) diff --git a/docs/basic-usage/middleware.md b/docs/basic-usage/middleware.md index e7f2ea773..592fe8343 100644 --- a/docs/basic-usage/middleware.md +++ b/docs/basic-usage/middleware.md @@ -34,9 +34,9 @@ Note the property name difference between Laravel 10 and older versions of Larav // Laravel 10+ uses $middlewareAliases = [ protected $middlewareAliases = [ // ... - 'role' => \Spatie\Permission\Middlewares\RoleMiddleware::class, - 'permission' => \Spatie\Permission\Middlewares\PermissionMiddleware::class, - 'role_or_permission' => \Spatie\Permission\Middlewares\RoleOrPermissionMiddleware::class, + 'role' => \Spatie\Permission\Middleware\RoleMiddleware::class, + 'permission' => \Spatie\Permission\Middleware\PermissionMiddleware::class, + 'role_or_permission' => \Spatie\Permission\Middleware\RoleOrPermissionMiddleware::class, ]; ``` @@ -110,19 +110,19 @@ public function __construct() ## Use middleware static methods -All of the middlewares can also be applied by calling the static `using` method, +All of the middleware can also be applied by calling the static `using` method, which accepts either a `|`-separated string or an array as input. ```php -Route::group(['middleware' => [\Spatie\Permission\Middlewares\RoleMiddleware::using('manager')]], function () { +Route::group(['middleware' => [\Spatie\Permission\Middleware\RoleMiddleware::using('manager')]], function () { // }); -Route::group(['middleware' => [\Spatie\Permission\Middlewares\PermissionMiddleware::using('publish articles|edit articles')]], function () { +Route::group(['middleware' => [\Spatie\Permission\Middleware\PermissionMiddleware::using('publish articles|edit articles')]], function () { // }); -Route::group(['middleware' => [\Spatie\Permission\Middlewares\RoleOrPermissionMiddleware::using(['manager', 'edit articles'])]], function () { +Route::group(['middleware' => [\Spatie\Permission\Middleware\RoleOrPermissionMiddleware::using(['manager', 'edit articles'])]], function () { // }); ``` diff --git a/docs/basic-usage/passport.md b/docs/basic-usage/passport.md index a5a007009..4faa50fc3 100644 --- a/docs/basic-usage/passport.md +++ b/docs/basic-usage/passport.md @@ -39,7 +39,7 @@ The extended Client should either provide a `$guard_name` property or a `guardNa They should return a string that matches the [configured](https://laravel.com/docs/master/passport#installation) guard name for the passport driver. ## Middleware -All middlewares provided by this package work with the Client. +All middleware provided by this package work with the Client. Do make sure that you only wrap your routes in the [`client`](https://laravel.com/docs/master/passport#via-middleware) middleware and not the `auth:api` middleware as well. Wrapping routes in the `auth:api` middleware currently does not work for the Client Credentials Grant. diff --git a/docs/installation-lumen.md b/docs/installation-lumen.md index 70268977d..197ff31fe 100644 --- a/docs/installation-lumen.md +++ b/docs/installation-lumen.md @@ -34,8 +34,8 @@ Then, in `bootstrap/app.php`, uncomment the `auth` middleware, and register this ```php $app->routeMiddleware([ 'auth' => App\Http\Middleware\Authenticate::class, - 'permission' => Spatie\Permission\Middlewares\PermissionMiddleware::class, - 'role' => Spatie\Permission\Middlewares\RoleMiddleware::class, + 'permission' => Spatie\Permission\Middleware\PermissionMiddleware::class, + 'role' => Spatie\Permission\Middleware\RoleMiddleware::class, ]); ``` diff --git a/docs/upgrading.md b/docs/upgrading.md index ade6e7fd4..ab83fd790 100644 --- a/docs/upgrading.md +++ b/docs/upgrading.md @@ -40,11 +40,15 @@ eg: if you have a custom model you will need to make changes, including accessin 3. Migrations will need to be upgraded. (They have been updated to anonymous-class syntax that was introduced in Laravel 8, AND some structural coding changes in the registrar class changed the way we extracted configuration settings in the migration files.) If you had not customized it from the original then replacing the contents of the file should be straightforward. Usually the only customization is if you've switched to UUIDs or customized MySQL index name lengths. **If you get the following error, it means your migration file needs upgrading: `Error: Access to undeclared static property Spatie\Permission\PermissionRegistrar::$pivotPermission`** -4. NOTE: For consistency with `PermissionMiddleware`, the `RoleOrPermissionMiddleware` has switched from only checking permissions provided by this package to using `canAny()` to check against any abilities registered by your application. This may have the effect of granting those other abilities (such as Super Admin) when using the `RoleOrPermissionMiddleware`, which previously would have failed silently. +4. MIDDLEWARE: -5. In the unlikely event that you have customized the Wildcard Permissions feature by extending the `WildcardPermission` model, please note that the public interface has changed and you will need to update your extended model with the new method signatures. + 1. The `\Spatie\Permission\Middlewares\` namespace has been renamed to `\Spatie\Permission\Middleware\` (singular). Update your references to them in your `/app/Http/Kernel.php` and any routes that have the fully qualified path. -6. Test suites. If you have tests which manually clear the permission cache and re-register permissions, you no longer need to call `\Spatie\Permission\PermissionRegistrar::class)->registerPermissions();`. In fact, **calls to `->registerPermissions()` MUST be deleted from your tests**. + 2. NOTE: For consistency with `PermissionMiddleware`, the `RoleOrPermissionMiddleware` has switched from only checking permissions provided by this package to using `canAny()` to check against any abilities registered by your application. This may have the effect of granting those other abilities (such as Super Admin) when using the `RoleOrPermissionMiddleware`, which previously would have failed silently. + + 3. In the unlikely event that you have customized the Wildcard Permissions feature by extending the `WildcardPermission` model, please note that the public interface has changed and you will need to update your extended model with the new method signatures. + +5. Test suites. If you have tests which manually clear the permission cache and re-register permissions, you no longer need to call `\Spatie\Permission\PermissionRegistrar::class)->registerPermissions();`. In fact, **calls to `->registerPermissions()` MUST be deleted from your tests**. (Calling `app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();` after creating roles and permissions is still okay and encouraged.) diff --git a/src/Middlewares/PermissionMiddleware.php b/src/Middleware/PermissionMiddleware.php similarity index 97% rename from src/Middlewares/PermissionMiddleware.php rename to src/Middleware/PermissionMiddleware.php index 7d303261c..3e78f1d60 100644 --- a/src/Middlewares/PermissionMiddleware.php +++ b/src/Middleware/PermissionMiddleware.php @@ -1,6 +1,6 @@ assertSame( - 'Spatie\Permission\Middlewares\PermissionMiddleware:edit-articles', + 'Spatie\Permission\Middleware\PermissionMiddleware:edit-articles', PermissionMiddleware::using('edit-articles') ); $this->assertEquals( - 'Spatie\Permission\Middlewares\PermissionMiddleware:edit-articles,my-guard', + 'Spatie\Permission\Middleware\PermissionMiddleware:edit-articles,my-guard', PermissionMiddleware::using('edit-articles', 'my-guard') ); $this->assertEquals( - 'Spatie\Permission\Middlewares\PermissionMiddleware:edit-articles|edit-news', + 'Spatie\Permission\Middleware\PermissionMiddleware:edit-articles|edit-news', PermissionMiddleware::using(['edit-articles', 'edit-news']) ); } diff --git a/tests/RoleMiddlewareTest.php b/tests/RoleMiddlewareTest.php index 25f411ee1..7ffc3a5c6 100644 --- a/tests/RoleMiddlewareTest.php +++ b/tests/RoleMiddlewareTest.php @@ -9,7 +9,7 @@ use InvalidArgumentException; use Laravel\Passport\Passport; use Spatie\Permission\Exceptions\UnauthorizedException; -use Spatie\Permission\Middlewares\RoleMiddleware; +use Spatie\Permission\Middleware\RoleMiddleware; use Spatie\Permission\Tests\TestModels\UserWithoutHasRoles; class RoleMiddlewareTest extends TestCase @@ -332,15 +332,15 @@ public function user_can_access_role_with_guard_admin_while_login_using_admin_gu public function the_middleware_can_be_created_with_static_using_method() { $this->assertSame( - 'Spatie\Permission\Middlewares\RoleMiddleware:testAdminRole', + 'Spatie\Permission\Middleware\RoleMiddleware:testAdminRole', RoleMiddleware::using('testAdminRole') ); $this->assertEquals( - 'Spatie\Permission\Middlewares\RoleMiddleware:testAdminRole,my-guard', + 'Spatie\Permission\Middleware\RoleMiddleware:testAdminRole,my-guard', RoleMiddleware::using('testAdminRole', 'my-guard') ); $this->assertEquals( - 'Spatie\Permission\Middlewares\RoleMiddleware:testAdminRole|anotherRole', + 'Spatie\Permission\Middleware\RoleMiddleware:testAdminRole|anotherRole', RoleMiddleware::using(['testAdminRole', 'anotherRole']) ); } diff --git a/tests/RoleOrPermissionMiddlewareTest.php b/tests/RoleOrPermissionMiddlewareTest.php index a3de1054d..4d473aa4f 100644 --- a/tests/RoleOrPermissionMiddlewareTest.php +++ b/tests/RoleOrPermissionMiddlewareTest.php @@ -10,7 +10,7 @@ use InvalidArgumentException; use Laravel\Passport\Passport; use Spatie\Permission\Exceptions\UnauthorizedException; -use Spatie\Permission\Middlewares\RoleOrPermissionMiddleware; +use Spatie\Permission\Middleware\RoleOrPermissionMiddleware; use Spatie\Permission\Tests\TestModels\UserWithoutHasRoles; class RoleOrPermissionMiddlewareTest extends TestCase @@ -278,15 +278,15 @@ public function the_required_permissions_or_roles_can_be_displayed_in_the_except public function the_middleware_can_be_created_with_static_using_method() { $this->assertSame( - 'Spatie\Permission\Middlewares\RoleOrPermissionMiddleware:edit-articles', + 'Spatie\Permission\Middleware\RoleOrPermissionMiddleware:edit-articles', RoleOrPermissionMiddleware::using('edit-articles') ); $this->assertEquals( - 'Spatie\Permission\Middlewares\RoleOrPermissionMiddleware:edit-articles,my-guard', + 'Spatie\Permission\Middleware\RoleOrPermissionMiddleware:edit-articles,my-guard', RoleOrPermissionMiddleware::using('edit-articles', 'my-guard') ); $this->assertEquals( - 'Spatie\Permission\Middlewares\RoleOrPermissionMiddleware:edit-articles|testAdminRole', + 'Spatie\Permission\Middleware\RoleOrPermissionMiddleware:edit-articles|testAdminRole', RoleOrPermissionMiddleware::using(['edit-articles', 'testAdminRole']) ); } diff --git a/tests/WildcardMiddlewareTest.php b/tests/WildcardMiddlewareTest.php index f2d099ae8..dc359fe49 100644 --- a/tests/WildcardMiddlewareTest.php +++ b/tests/WildcardMiddlewareTest.php @@ -6,9 +6,9 @@ use Illuminate\Http\Response; use Illuminate\Support\Facades\Auth; use Spatie\Permission\Exceptions\UnauthorizedException; -use Spatie\Permission\Middlewares\PermissionMiddleware; -use Spatie\Permission\Middlewares\RoleMiddleware; -use Spatie\Permission\Middlewares\RoleOrPermissionMiddleware; +use Spatie\Permission\Middleware\PermissionMiddleware; +use Spatie\Permission\Middleware\RoleMiddleware; +use Spatie\Permission\Middleware\RoleOrPermissionMiddleware; use Spatie\Permission\Models\Permission; class WildcardMiddlewareTest extends TestCase