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

Error getPermissions method in GateRegistrar.php #52

Open
yanbrasiliano opened this issue Aug 1, 2024 · 2 comments
Open

Error getPermissions method in GateRegistrar.php #52

yanbrasiliano opened this issue Aug 1, 2024 · 2 comments

Comments

@yanbrasiliano
Copy link
Contributor

yanbrasiliano commented Aug 1, 2024

Summary of problem or feature request

The getPermissions() method in GateRegistrar.php returns an array instead of an Illuminate\Support\Collection as expected, causing a TypeError in Laravel 11.

Code snippet of problem

The error occurs in the vendor/yajra/laravel-acl/src/GateRegistrar.php file:

protected function getPermissions()
{
    try {
        $key = config('acl.cache.key', 'acl.permissions');

        if (config('acl.cache.enabled', true)) {
            // @phpstan-ignore-next-line
            return $this->cache->rememberForever($key, function () {
                return $this->getPermissionClass()->with('roles')->get();
            });
        } else {
            return $this->getPermissionClass()->with('roles')->get();
        }
    } catch (Exception $exception) {
        return [];
    }
}

This method returns an array in the catch block, which causes a TypeError when a Collection is expected.

The error in my CLI:

TypeError 

  Yajra\Acl\GateRegistrar::getPermissions(): Return value must be of type Illuminate\Support\Collection, array returned

  at vendor/yajra/laravel-acl/src/GateRegistrar.php:61
     57▕             if (config('acl.cache.enabled', true)) {
     58▕                 // @phpstan-ignore-next-line
     59▕                 return $this->cache->rememberForever($key, function () {
     60▕                     return $this->getPermissionClass()->with('roles')->get();
  ➜  61▕                 });
     62▕             } else {
     63▕                 return $this->getPermissionClass()->with('roles')->get();
     64▕             }
     65▕         } catch (Exception $exception) {

      �[2m+9 vendor frames �[22m

  10  [internal]:0
      Illuminate\Foundation\Application::Illuminate\Foundation\{closure}(Object(Yajra\Acl\AclServiceProvider))
      �[2m+5 vendor frames �[22m

  16  artisan:35
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1

System details

  • Operating System: Debian GNU/Linux 12 (bookworm)
  • PHP Version: 8.2
  • Laravel Version: 11.0
  • Laravel-ACL Version: 11.1

Possible Solution

To resolve this issue, I downgraded to the more stable version of Laravel 10 and modified the getPermissions method to ensure it always returns a collection of Permission instances. The function has been simplified to avoid using else and uses a ternary operator for cleaner and more concise code.
If you're following the package's code standards, I can open a PR to implement the solution. Waiting for feedback.

protected function getPermissions(): Collection
{
    /** @var string $key */
    $key = config('acl.cache.key', 'permissions.policies');

    try {
        $permissions = config('acl.cache.enabled', true)
            ? $this->cache->rememberForever($key, function () {
                return $this->getPermissionClass()->with('roles')->get()->toArray();
            })
            : $this->getPermissionClass()->with('roles')->get()->toArray();

        return collect($permissions)->map(function ($permission) {
            return new \Yajra\Acl\Models\Permission($permission);
        });
    } catch (Exception $exception) {
        $this->cache->forget($key);

        return new Collection;
    }
}
@yajra
Copy link
Owner

yajra commented Aug 2, 2024

Yes please, a PR would be much appreciated. Thanks!

@yanbrasiliano
Copy link
Contributor Author

yanbrasiliano commented Aug 2, 2024

Yes please, a PR would be much appreciated. Thanks!

@yajra, i opened the PR #53
Check that everything is OK; thanks for you feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants