Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mxm1070 committed May 15, 2024
1 parent 04ccc21 commit a0b69c0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
</COPYRIGHT>
*/

use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Migrations\Migration;
use Database\Migrations\Concerns\CanModifyPermissions;

Expand All @@ -57,7 +59,15 @@

public function up(): void
{
$this->createPermissions($this->permissions, $this->guards);
collect($this->guards)
->each(function (string $guard) {
$permissions = Arr::except($this->permissions, keys: DB::table('permissions')
->where('guard_name', $guard)
->pluck('name')
->all());

$this->createPermissions($permissions, $guard);
});
}

public function down(): void
Expand Down
25 changes: 11 additions & 14 deletions database/migrations/Concerns/CanModifyPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ trait CanModifyPermissions
/**
* @param array<string, string> $names The keys of the array should be the permission names and the values should be the name of the group they belong to.
*/
public function createPermissions(array $names, string|array $guardName): void
public function createPermissions(array $names, string $guardName): void
{
$groups = DB::table('permission_groups')
->pluck('id', 'name')
Expand Down Expand Up @@ -75,19 +75,16 @@ public function createPermissions(array $names, string|array $guardName): void
...$newGroups,
];

collect($guardName)
->each(function (string $guardName) use ($names, $groups): void {
DB::table('permissions')
->insert(array_map(function (string $name, string $groupName) use ($groups, $guardName): array {
return [
'id' => (string) Str::orderedUuid(),
'group_id' => $groups[$groupName],
'guard_name' => $guardName,
'name' => $name,
'created_at' => now(),
];
}, array_keys($names), array_values($names)));
});
DB::table('permissions')
->insert(array_map(function (string $name, string $groupName) use ($groups, $guardName): array {
return [
'id' => (string) Str::orderedUuid(),
'group_id' => $groups[$groupName],
'guard_name' => $guardName,
'name' => $name,
'created_at' => now(),
];
}, array_keys($names), array_values($names)));
}

/**
Expand Down

0 comments on commit a0b69c0

Please sign in to comment.