Skip to content

Commit d35b0bd

Browse files
committed
feat: update dependencies and refactor permission handling to use BaseCollection and replace Auth Facade usage
1 parent c6ec080 commit d35b0bd

File tree

15 files changed

+110
-75
lines changed

15 files changed

+110
-75
lines changed

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) Hyperf
3+
Copyright (c) Spatie bvba [email protected]
44

55
Copyright (c) Taylor Otwell
66

src/permission/composer.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,13 @@
2727
},
2828
"require": {
2929
"php": "^8.2",
30-
"hyperf/macroable": "~3.1.0",
31-
"hypervel/support": "^0.2"
30+
"hyperf/context": "~3.1.0",
31+
"hyperf/support": "~3.1.0",
32+
"hyperf/collection": "~3.1.0",
33+
"hyperf/command": "~3.1.0",
34+
"hyperf/cache": "~3.1.0",
35+
"hyperf/database": "~3.1.0",
36+
"hyperf/db-connection": "~3.1.0"
3237
},
3338
"config": {
3439
"sort-packages": true

src/permission/src/ConfigProvider.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Hypervel\Permission;
66

7+
use Hypervel\Permission\Console\ShowCommand;
78
use Hypervel\Permission\Contracts\Factory;
89

910
class ConfigProvider
@@ -14,6 +15,17 @@ public function __invoke(): array
1415
'dependencies' => [
1516
Factory::class => PermissionManager::class,
1617
],
18+
'commands' => [
19+
ShowCommand::class,
20+
],
21+
'publish' => [
22+
[
23+
'id' => 'config',
24+
'description' => 'The config for permission.',
25+
'source' => __DIR__ . '/../publish/permission.php',
26+
'destination' => BASE_PATH . '/config/autoload/permission.php',
27+
],
28+
],
1729
];
1830
}
1931
}

src/permission/src/Contracts/Factory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
namespace Hypervel\Permission\Contracts;
66

7+
use Hypervel\Cache\Contracts\Repository;
8+
79
interface Factory
810
{
911
public function getRoleClass();
1012

1113
public function getPermissionClass();
1214

13-
public function getCache(): ?\Hypervel\Cache\Contracts\Repository;
15+
public function getCache(): ?Repository;
1416

1517
public function getOwnerRolesCacheKey(string $ownerType, int|string $ownerId): string;
1618

src/permission/src/Exceptions/PermissionException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ class PermissionException extends HttpException
1111
{
1212
public function __construct(
1313
int $statusCode,
14-
$message = '',
15-
$code = 0,
14+
string $message = '',
15+
int $code = 0,
1616
?Throwable $previous = null,
1717
protected array $headers = [],
1818
protected array $permissions = []

src/permission/src/Exceptions/RoleException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ class RoleException extends HttpException
1111
{
1212
public function __construct(
1313
int $statusCode,
14-
$message = '',
15-
$code = 0,
14+
string $message = '',
15+
int $code = 0,
1616
?Throwable $previous = null,
1717
protected array $headers = [],
1818
protected array $roles = []

src/permission/src/Middlewares/PermissionMiddleware.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
namespace Hypervel\Permission\Middlewares;
66

77
use BackedEnum;
8+
use Hyperf\Contract\ContainerInterface;
9+
use Hypervel\Auth\AuthManager;
810
use Hypervel\Permission\Exceptions\PermissionException;
911
use Hypervel\Permission\Exceptions\UnauthorizedException;
10-
use Hypervel\Support\Facades\Auth;
12+
use Hyperf\Collection\Collection;
1113
use Psr\Http\Message\ResponseInterface;
1214
use Psr\Http\Message\ServerRequestInterface;
1315
use Psr\Http\Server\MiddlewareInterface;
@@ -16,12 +18,20 @@
1618

1719
class PermissionMiddleware implements MiddlewareInterface
1820
{
21+
/**
22+
* Create a new middleware instance.
23+
*/
24+
public function __construct(protected ContainerInterface $container)
25+
{
26+
}
27+
1928
public function process(
2029
ServerRequestInterface $request,
2130
RequestHandlerInterface $handler,
2231
BackedEnum|int|string|UnitEnum ...$permissions
2332
): ResponseInterface {
24-
$user = Auth::user();
33+
$auth = $this->container->get(AuthManager::class);
34+
$user = $auth->user();
2535
if (! $user) {
2636
throw new UnauthorizedException(
2737
401,
@@ -73,13 +83,10 @@ public static function using(array|BackedEnum|int|string|UnitEnum ...$permission
7383

7484
public static function parsePermissionsToString(array $permissions)
7585
{
76-
$permissions = collect($permissions)
86+
$permissions = Collection::make($permissions)
7787
->flatten()
7888
->values()
7989
->all();
80-
if ($permissions instanceof BackedEnum) {
81-
$permissions = $permissions->value;
82-
}
8390

8491
$permission = array_map(function ($permission) {
8592
return match (true) {

src/permission/src/Middlewares/RoleMiddleware.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
namespace Hypervel\Permission\Middlewares;
66

77
use BackedEnum;
8+
use Hyperf\Collection\Collection;
9+
use Hyperf\Contract\ContainerInterface;
10+
use Hypervel\Auth\AuthManager;
811
use Hypervel\Permission\Exceptions\RoleException;
912
use Hypervel\Permission\Exceptions\UnauthorizedException;
10-
use Hypervel\Support\Facades\Auth;
1113
use Psr\Http\Message\ResponseInterface;
1214
use Psr\Http\Message\ServerRequestInterface;
1315
use Psr\Http\Server\MiddlewareInterface;
@@ -16,12 +18,20 @@
1618

1719
class RoleMiddleware implements MiddlewareInterface
1820
{
21+
/**
22+
* Create a new middleware instance.
23+
*/
24+
public function __construct(protected ContainerInterface $container)
25+
{
26+
}
27+
1928
public function process(
2029
ServerRequestInterface $request,
2130
RequestHandlerInterface $handler,
2231
BackedEnum|int|string|UnitEnum ...$roles
2332
): ResponseInterface {
24-
$user = Auth::user();
33+
$auth = $this->container->get(AuthManager::class);
34+
$user = $auth->user();
2535
if (! $user) {
2636
throw new UnauthorizedException(
2737
401,
@@ -73,13 +83,10 @@ public static function using(array|BackedEnum|int|string|UnitEnum ...$roles): st
7383

7484
public static function parseRolesToString(array $roles)
7585
{
76-
$roles = collect($roles)
86+
$roles = Collection::make($roles)
7787
->flatten()
7888
->values()
7989
->all();
80-
if ($roles instanceof BackedEnum) {
81-
$roles = $roles->value;
82-
}
8390

8491
$role = array_map(function ($role) {
8592
return match (true) {

src/permission/src/PermissionManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ protected function getCacheStoreFromConfig(): Repository
111111
}
112112

113113
// if an undefined cache store is specified, fallback to 'array' which is Laravel's closest equiv to 'none'
114-
if (! \array_key_exists($cacheDriver, config('cache.stores'))) {
114+
if (! array_key_exists($cacheDriver, config('cache.stores'))) {
115115
$cacheDriver = 'array';
116116
}
117117

0 commit comments

Comments
 (0)