|
5 | 5 | namespace Hypervel\Permission\Middlewares; |
6 | 6 |
|
7 | 7 | use BackedEnum; |
| 8 | +use Hyperf\Contract\ContainerInterface; |
| 9 | +use Hypervel\Auth\AuthManager; |
8 | 10 | use Hypervel\Permission\Exceptions\PermissionException; |
9 | 11 | use Hypervel\Permission\Exceptions\UnauthorizedException; |
10 | | -use Hypervel\Support\Facades\Auth; |
| 12 | +use Hyperf\Collection\Collection; |
11 | 13 | use Psr\Http\Message\ResponseInterface; |
12 | 14 | use Psr\Http\Message\ServerRequestInterface; |
13 | 15 | use Psr\Http\Server\MiddlewareInterface; |
|
16 | 18 |
|
17 | 19 | class PermissionMiddleware implements MiddlewareInterface |
18 | 20 | { |
| 21 | + /** |
| 22 | + * Create a new middleware instance. |
| 23 | + */ |
| 24 | + public function __construct(protected ContainerInterface $container) |
| 25 | + { |
| 26 | + } |
| 27 | + |
19 | 28 | public function process( |
20 | 29 | ServerRequestInterface $request, |
21 | 30 | RequestHandlerInterface $handler, |
22 | 31 | BackedEnum|int|string|UnitEnum ...$permissions |
23 | 32 | ): ResponseInterface { |
24 | | - $user = Auth::user(); |
| 33 | + $auth = $this->container->get(AuthManager::class); |
| 34 | + $user = $auth->user(); |
25 | 35 | if (! $user) { |
26 | 36 | throw new UnauthorizedException( |
27 | 37 | 401, |
@@ -73,13 +83,10 @@ public static function using(array|BackedEnum|int|string|UnitEnum ...$permission |
73 | 83 |
|
74 | 84 | public static function parsePermissionsToString(array $permissions) |
75 | 85 | { |
76 | | - $permissions = collect($permissions) |
| 86 | + $permissions = Collection::make($permissions) |
77 | 87 | ->flatten() |
78 | 88 | ->values() |
79 | 89 | ->all(); |
80 | | - if ($permissions instanceof BackedEnum) { |
81 | | - $permissions = $permissions->value; |
82 | | - } |
83 | 90 |
|
84 | 91 | $permission = array_map(function ($permission) { |
85 | 92 | return match (true) { |
|
0 commit comments