spatie/laravel-permission

On creating 4 roles and trying to assign about 10 permissions to the first role I get Maximum execution time of 60 seconds exceeded

ajGulati05 opened this issue · 3 comments

Describe the bug
Timeout after trying to assign the new user and location default roles. From the below code it creates all 4 roles and after assigning the first 4 permissions to the role it fails with 'Maximum execution time of 60 seconds exceeded' tried using syncPermissions and names of permission.

Versions
You can use composer show to get the version numbers of:

  • spatie/laravel-permission package version: 5.5
  • illuminate/framework package 8.75 laravel version

PHP version: 7.4

Database version: 5.7

To Reproduce
Steps to reproduce the behavior:

Here is my example code and/or tests showing the problem in my app:

$adminRole=Role::create(['name' => 'admin', 'location_id' => $salonlocation_id>id]); $managerRole=Role::create(['name' => 'manager', 'location_id' => $location_id->id]); $staffRole=Role::create(['name' => 'staff', 'location_id' => $location_id->id]); $securityRole=Role::create(['name' => 'security', 'location_id' => $location_id->id]); //link permissions to the above roles

  `  $adminRole->givePermissionTo(1);
    $adminRole->givePermissionTo(2);
    $adminRole->givePermissionTo(3);
    $adminRole->givePermissionTo(4);
    $adminRole->givePermissionTo(5);
    $adminRole->givePermissionTo(6);
    $adminRole->givePermissionTo(7);
    $adminRole->givePermissionTo(8);
    $adminRole->givePermissionTo(9);
      
    $managerRole->givePermissionTo(1);
    $managerRole->givePermissionTo(2);
    $managerRole->givePermissionTo(3);
    $managerRole->givePermissionTo(4);
    $managerRole->givePermissionTo(5);
    $managerRole->givePermissionTo(6);
    $managerRole->givePermissionTo(7);
    $managerRole->givePermissionTo(8);
 
    $securityRole->givePermissionTo(5;
    $securityRole->givePermissionTo('6);
    $securityRole->giverPermissionTo(7);

    $staffRole->givePermissionTo(8);
    //Add the above user as owner

    $user->givePermissionTo('owner');`

Expected behavior
It should link the permissions to the roles correctly.

Additional context
I thought it was a caching issue as I was playing with data on the db. I rebuilt the container for my docker image tried with a new user and location as well.

Environment (please complete the following information, because it helps us investigate better):

  • OS: macOS
  • Version 12.2.1
  • Laravel Sail for Laravel 8

If you had 1,000 permissions, would you write 1,000 lines?

* @param string|int|array|\Spatie\Permission\Contracts\Permission|\Illuminate\Support\Collection $permissions
*
* @return $this
*/
public function givePermissionTo(...$permissions)

@erikn69 that solved the problem. I usually rely on documentation and I might have missed that. I assume with all the caching taking place its taking longer but passing an array made it work but still very slow.

but passing an array made it work but still very slow.

You could make the sync process manually, and use a single cache reload in the end

$managerRole->permissions()->sync([1, 2 ,3, 4, 5, 6], false);
$securityRole->permissions()->sync([5, 6, 7], false);

$user->forgetCachedPermissions();

https://spatie.be/docs/laravel-permission/v5/best-practices/performance