Skip to content

Commit 46d2a24

Browse files
authored
Merge pull request #54 from yajra/yanbrasiliano-fix-gateregistrar-permissions
fix: Ensure getPermissions returns Collection
2 parents 37127b9 + 26090c3 commit 46d2a24

File tree

12 files changed

+29
-38
lines changed

12 files changed

+29
-38
lines changed

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
"illuminate/database": "^11.0"
2525
},
2626
"require-dev": {
27-
"larastan/larastan": "^2.9.1",
28-
"laravel/pint": "^1.14",
29-
"rector/rector": "^1.0",
30-
"orchestra/testbench": "^9.0"
27+
"larastan/larastan": "^2.9.8",
28+
"laravel/pint": "^1.17.1",
29+
"rector/rector": "^1.2.2",
30+
"orchestra/testbench": "^9.2"
3131
},
3232
"autoload": {
3333
"psr-4": {

phpstan.neon.dist

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,3 @@ parameters:
1111
ignoreErrors:
1212

1313
excludePaths:
14-
15-
checkMissingIterableValueType: true

src/Directives/DirectiveAbstract.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,5 @@ abstract class DirectiveAbstract
99
/**
1010
* IsRoleDirective constructor.
1111
*/
12-
public function __construct(protected Guard $auth)
13-
{
14-
}
12+
public function __construct(protected Guard $auth) {}
1513
}

src/GateRegistrar.php

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,22 @@
55
use Illuminate\Contracts\Auth\Access\Gate as GateContract;
66
use Illuminate\Contracts\Cache\Repository;
77
use Illuminate\Foundation\Auth\User;
8+
use Illuminate\Support\Collection;
89
use Illuminate\Support\Str;
910
use Yajra\Acl\Models\Permission;
1011

1112
class GateRegistrar
1213
{
13-
public function __construct(public GateContract $gate, public Repository $cache)
14-
{
15-
}
14+
public function __construct(public GateContract $gate, public Repository $cache) {}
1615

1716
public function register(): void
1817
{
19-
collect($this->getPermissions())->each(function ($data) {
20-
$permission = new Permission($data);
21-
18+
$this->getPermissions()->each(function (Permission $permission) {
2219
$ability = $permission->slug;
2320
$policy = function (User $user) use ($permission) {
2421
if (method_exists($user, 'getPermissions')) {
2522
// @phpstan-ignore-next-line
26-
$permissions = collect($user->getPermissions());
27-
28-
return $permissions->contains($permission->slug);
23+
return collect($user->getPermissions())->contains($permission->slug);
2924
}
3025

3126
return false;
@@ -43,36 +38,32 @@ public function register(): void
4338
/**
4439
* Get all permissions.
4540
*
46-
* @return array<array<string, mixed>>
41+
* @return \Illuminate\Support\Collection<array-key, Permission>
4742
*/
48-
protected function getPermissions(): array
43+
protected function getPermissions(): Collection
4944
{
5045
/** @var string $key */
5146
$key = config('acl.cache.key', 'permissions.policies');
5247

5348
try {
54-
if (config('acl.cache.enabled', true)) {
55-
return $this->cache->rememberForever($key, fn () => $this->getPermissionsFromQuery());
56-
} else {
57-
return $this->getPermissionsFromQuery();
58-
}
49+
return config('acl.cache.enabled', true)
50+
? $this->cache->rememberForever($key, fn () => $this->getPermissionsFromQuery())
51+
: $this->getPermissionsFromQuery();
5952
} catch (\Throwable) {
6053
$this->cache->forget($key);
6154

62-
return [];
55+
return collect();
6356
}
6457
}
6558

6659
/**
67-
* @return array<array<string, mixed>>
60+
* @return \Illuminate\Support\Collection<array-key, Permission>
6861
*/
69-
public function getPermissionsFromQuery(): array
62+
public function getPermissionsFromQuery(): Collection
7063
{
71-
// @phpstan-ignore-next-line
7264
return $this->getPermissionClass()
7365
->with('roles')
74-
->get()
75-
->toArray();
66+
->get();
7667
}
7768

7869
protected function getPermissionClass(): Permission

src/Models/Permission.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public function users(): BelongsToMany
105105
/** @var class-string<\Illuminate\Foundation\Auth\User> $model */
106106
$model = config('acl.user', config('auth.providers.users.model'));
107107

108+
// @phpstan-ignore-next-line
108109
return $this->belongsToMany($model)->withTimestamps();
109110
}
110111
}

src/Models/Role.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function users(): BelongsToMany
4848
/** @var class-string<\Illuminate\Foundation\Auth\User> $model */
4949
$model = config('acl.user', config('auth.providers.users.model'));
5050

51+
// @phpstan-ignore-next-line
5152
return $this->belongsToMany($model)->withTimestamps();
5253
}
5354
}

src/Traits/InteractsWithPermission.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@ public function grantPermission(mixed $ids, array $attributes = [], bool $touch
6969
*/
7070
public function permissions(): BelongsToMany
7171
{
72-
/** @var class-string $model */
72+
/** @var class-string<Permission> $model */
7373
$model = config('acl.permission', Permission::class);
7474

75+
// @phpstan-ignore-next-line
7576
return $this->belongsToMany($model)->withTimestamps();
7677
}
7778

src/Traits/InteractsWithRole.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,10 @@ public function attachRole(mixed $role, array $attributes = [], bool $touch = tr
7979
*/
8080
public function roles(): BelongsToMany
8181
{
82-
/** @var class-string $model */
82+
/** @var class-string<Role> $model */
8383
$model = config('acl.role', Role::class);
8484

85+
// @phpstan-ignore-next-line
8586
return $this->belongsToMany($model)->withTimestamps();
8687
}
8788

tests/Feature/CanAtLeastMiddlewareTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function it_can_access_permission_protected_routes()
2020

2121
$middleware = new CanAtLeastMiddleware;
2222

23-
$response = $middleware->handle(new Request(), fn () => 'Pass', 'create-article,non-existing-permission');
23+
$response = $middleware->handle(new Request, fn () => 'Pass', 'create-article,non-existing-permission');
2424

2525
$this->assertEquals('Pass', $response);
2626
}

tests/Feature/HasRoleTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,10 @@ public function it_can_query_with_having_roles_scopes()
181181

182182
$supportUser->attachRole($support);
183183

184-
$roles = (new User())->havingRoles([$manager->getKey(), $support->getKey()])->get();
184+
$roles = (new User)->havingRoles([$manager->getKey(), $support->getKey()])->get();
185185
$this->assertCount(2, $roles);
186186

187-
$roles = (new User())->havingRolesBySlugs([$manager->slug, $user->slug])->get();
187+
$roles = (new User)->havingRolesBySlugs([$manager->slug, $user->slug])->get();
188188
$this->assertCount(1, $roles);
189189
}
190190
}

0 commit comments

Comments
 (0)