Skip to content

Commit

Permalink
Rename "Middlewares" namespace to "Middleware" (#2499)
Browse files Browse the repository at this point in the history
  • Loading branch information
drbyte authored Sep 18, 2023
1 parent d5eb739 commit f9069aa
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 31 deletions.
14 changes: 7 additions & 7 deletions docs/basic-usage/middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
```

Expand Down Expand Up @@ -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 () {
//
});
```
2 changes: 1 addition & 1 deletion docs/basic-usage/passport.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions docs/installation-lumen.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
```

Expand Down
10 changes: 7 additions & 3 deletions docs/upgrading.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Spatie\Permission\Middlewares;
namespace Spatie\Permission\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Spatie\Permission\Middlewares;
namespace Spatie\Permission\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Spatie\Permission\Middlewares;
namespace Spatie\Permission\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
Expand Down
8 changes: 4 additions & 4 deletions tests/PermissionMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Laravel\Passport\Passport;
use Spatie\Permission\Contracts\Permission;
use Spatie\Permission\Exceptions\UnauthorizedException;
use Spatie\Permission\Middlewares\PermissionMiddleware;
use Spatie\Permission\Middleware\PermissionMiddleware;
use Spatie\Permission\Tests\TestModels\UserWithoutHasRoles;

class PermissionMiddlewareTest extends TestCase
Expand Down Expand Up @@ -399,15 +399,15 @@ public function user_can_access_permission_with_guard_admin_while_login_using_ad
public function the_middleware_can_be_created_with_static_using_method()
{
$this->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'])
);
}
Expand Down
8 changes: 4 additions & 4 deletions tests/RoleMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'])
);
}
Expand Down
8 changes: 4 additions & 4 deletions tests/RoleOrPermissionMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'])
);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/WildcardMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f9069aa

Please sign in to comment.