Skip to content

Commit

Permalink
Show info on added roles
Browse files Browse the repository at this point in the history
  • Loading branch information
v-stamenova committed Oct 31, 2024
1 parent cc3bd25 commit 4356eca
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions app/Console/Commands/SyncPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use App\Actions\Permissions\ReadPermissionConfig;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use Mockery\Exception;
use Spatie\Permission\Exceptions\PermissionDoesNotExist;
use Spatie\Permission\Exceptions\RoleDoesNotExist;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
Expand All @@ -27,6 +29,9 @@ class SyncPermissions extends Command
protected $description = 'Synchronizes the permissions';
protected ReadPermissionConfig $readPermissionConfig;

private $newRoles = [];
private $newPermissions = [];

/**
* Execute the console command.
*/
Expand All @@ -41,6 +46,14 @@ public function handle()

$this->syncRoles($config['roles']);
$this->syncPermissions($config['permissions']);

if(count($this->newRoles) > 0) {

Check failure on line 50 in app/Console/Commands/SyncPermissions.php

View workflow job for this annotation

GitHub Actions / phpcs

Expected 1 space after IF keyword; 0 found
$this->info('The added roles: ' . implode(', ', $this->newRoles));
}
if(count($this->newPermissions) > 0) {

Check failure on line 53 in app/Console/Commands/SyncPermissions.php

View workflow job for this annotation

GitHub Actions / phpcs

Expected 1 space after IF keyword; 0 found
$this->info('The added permissions: ' . implode(', ', $this->newPermissions));
}

return 0;
}

Expand All @@ -61,6 +74,12 @@ private function syncRoles(array|string $input)
];
}
foreach ($role_data as $role) {
try {
Role::findByName($role['name']);
} catch (RoleDoesNotExist) {
$this->newRoles[] = $role['name'];
}

Role::updateOrCreate(
['name' => $role['name']],
$role
Expand Down Expand Up @@ -103,6 +122,13 @@ private function syncPermissions(array $permissions)
foreach ($permissions as $item) {
$roles = $item['roles'];
unset($item['roles']);

try {
Permission::findByName($item['name']);
} catch (PermissionDoesNotExist) {
$this->newPermissions[] = $item['name'];
}

$permission = Permission::updateOrCreate(
['name' => $item['name']],
$item
Expand Down

0 comments on commit 4356eca

Please sign in to comment.