Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Ensure getPermissions returns Collection #54

Merged
merged 6 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
"illuminate/database": "^11.0"
},
"require-dev": {
"larastan/larastan": "^2.9.1",
"laravel/pint": "^1.14",
"rector/rector": "^1.0",
"orchestra/testbench": "^9.0"
"larastan/larastan": "^2.9.8",
"laravel/pint": "^1.17.1",
"rector/rector": "^1.2.2",
"orchestra/testbench": "^9.2"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 0 additions & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,3 @@ parameters:
ignoreErrors:

excludePaths:

checkMissingIterableValueType: true
4 changes: 1 addition & 3 deletions src/Directives/DirectiveAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,5 @@ abstract class DirectiveAbstract
/**
* IsRoleDirective constructor.
*/
public function __construct(protected Guard $auth)
{
}
public function __construct(protected Guard $auth) {}
}
35 changes: 13 additions & 22 deletions src/GateRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,22 @@
use Illuminate\Contracts\Auth\Access\Gate as GateContract;
use Illuminate\Contracts\Cache\Repository;
use Illuminate\Foundation\Auth\User;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Yajra\Acl\Models\Permission;

class GateRegistrar
{
public function __construct(public GateContract $gate, public Repository $cache)
{
}
public function __construct(public GateContract $gate, public Repository $cache) {}

public function register(): void
{
collect($this->getPermissions())->each(function ($data) {
$permission = new Permission($data);

$this->getPermissions()->each(function (Permission $permission) {
$ability = $permission->slug;
$policy = function (User $user) use ($permission) {
if (method_exists($user, 'getPermissions')) {
// @phpstan-ignore-next-line
$permissions = collect($user->getPermissions());

return $permissions->contains($permission->slug);
return collect($user->getPermissions())->contains($permission->slug);
}

return false;
Expand All @@ -43,36 +38,32 @@ public function register(): void
/**
* Get all permissions.
*
* @return array<array<string, mixed>>
* @return \Illuminate\Support\Collection<array-key, Permission>
*/
protected function getPermissions(): array
protected function getPermissions(): Collection
{
/** @var string $key */
$key = config('acl.cache.key', 'permissions.policies');

try {
if (config('acl.cache.enabled', true)) {
return $this->cache->rememberForever($key, fn () => $this->getPermissionsFromQuery());
} else {
return $this->getPermissionsFromQuery();
}
return config('acl.cache.enabled', true)
? $this->cache->rememberForever($key, fn () => $this->getPermissionsFromQuery())
: $this->getPermissionsFromQuery();
} catch (\Throwable) {
$this->cache->forget($key);

return [];
return collect();
}
}

/**
* @return array<array<string, mixed>>
* @return \Illuminate\Support\Collection<array-key, Permission>
*/
public function getPermissionsFromQuery(): array
public function getPermissionsFromQuery(): Collection
{
// @phpstan-ignore-next-line
return $this->getPermissionClass()
->with('roles')
->get()
->toArray();
->get();
}

protected function getPermissionClass(): Permission
Expand Down
1 change: 1 addition & 0 deletions src/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public function users(): BelongsToMany
/** @var class-string<\Illuminate\Foundation\Auth\User> $model */
$model = config('acl.user', config('auth.providers.users.model'));

// @phpstan-ignore-next-line
return $this->belongsToMany($model)->withTimestamps();
}
}
1 change: 1 addition & 0 deletions src/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function users(): BelongsToMany
/** @var class-string<\Illuminate\Foundation\Auth\User> $model */
$model = config('acl.user', config('auth.providers.users.model'));

// @phpstan-ignore-next-line
return $this->belongsToMany($model)->withTimestamps();
}
}
3 changes: 2 additions & 1 deletion src/Traits/InteractsWithPermission.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ public function grantPermission(mixed $ids, array $attributes = [], bool $touch
*/
public function permissions(): BelongsToMany
{
/** @var class-string $model */
/** @var class-string<Permission> $model */
$model = config('acl.permission', Permission::class);

// @phpstan-ignore-next-line
return $this->belongsToMany($model)->withTimestamps();
}

Expand Down
3 changes: 2 additions & 1 deletion src/Traits/InteractsWithRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ public function attachRole(mixed $role, array $attributes = [], bool $touch = tr
*/
public function roles(): BelongsToMany
{
/** @var class-string $model */
/** @var class-string<Role> $model */
$model = config('acl.role', Role::class);

// @phpstan-ignore-next-line
return $this->belongsToMany($model)->withTimestamps();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/CanAtLeastMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function it_can_access_permission_protected_routes()

$middleware = new CanAtLeastMiddleware;

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

$this->assertEquals('Pass', $response);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/HasRoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ public function it_can_query_with_having_roles_scopes()

$supportUser->attachRole($support);

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

$roles = (new User())->havingRolesBySlugs([$manager->slug, $user->slug])->get();
$roles = (new User)->havingRolesBySlugs([$manager->slug, $user->slug])->get();
$this->assertCount(1, $roles);
}
}
2 changes: 1 addition & 1 deletion tests/Feature/PermissionMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function it_can_access_permission_protected_routes()

$middleware = new PermissionMiddleware;

$response = $middleware->handle(new Request(), fn () => 'Pass', 'create-article');
$response = $middleware->handle(new Request, fn () => 'Pass', 'create-article');

$this->assertEquals('Pass', $response);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ protected function getEnvironmentSetUp($app): void

$app['config']->set('view.paths', [__DIR__.'/resources/views']);
$app['config']->set('auth.providers.users.model', User::class);
$app['log']->getLogger()->pushHandler(new TestHandler());
$app['log']->getLogger()->pushHandler(new TestHandler);
}

protected function getPackageProviders($app): array
Expand Down
Loading