|
|
@@ -6,59 +6,59 @@
|
|
|
use App\Models\UserTypePermission;
|
|
|
use Illuminate\Database\Seeder;
|
|
|
use App\Enums\UserTypeEnum;
|
|
|
-use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
class UserTypePermissionSeeder extends Seeder
|
|
|
{
|
|
|
- /**
|
|
|
- * Seed the application's database.
|
|
|
- */
|
|
|
public function run(): void
|
|
|
{
|
|
|
+ $allPermissions = Permission::all()->keyBy('scope');
|
|
|
foreach (UserTypeEnum::cases() as $userType) {
|
|
|
+ $dataToSync = [];
|
|
|
switch ($userType) {
|
|
|
case UserTypeEnum::ADMIN:
|
|
|
- $permissions = Permission::get()->map(function ($permission) {
|
|
|
- return [
|
|
|
- 'scope' => $permission->scope,
|
|
|
- 'bits' => $permission->bits,
|
|
|
+ foreach ($allPermissions as $scope => $perm) {
|
|
|
+ $dataToSync[] = [
|
|
|
+ 'scope' => $scope,
|
|
|
+ 'bits' => $perm->bits
|
|
|
];
|
|
|
- })->toArray();
|
|
|
- $this->seedUserTypePermissions($permissions, UserTypeEnum::ADMIN->value);
|
|
|
+ }
|
|
|
break;
|
|
|
-
|
|
|
case UserTypeEnum::USER:
|
|
|
- $userPermissions = [
|
|
|
- ['scope' => 'dashboard', 'bits' => 1],
|
|
|
- ['scope' => 'config.user', 'bits' => 5],
|
|
|
- ['scope' => 'config.city', 'bits' => 1],
|
|
|
- ['scope' => 'config.country', 'bits' => 1],
|
|
|
- ['scope' => 'config.state', 'bits' => 1],
|
|
|
+ $dataToSync = [
|
|
|
+ ['scope' => 'dashboard', 'bits' => Permission::VIEW],
|
|
|
+ ['scope' => 'config.user', 'bits' => Permission::VIEW | Permission::EDIT],
|
|
|
+ ['scope' => 'config.city', 'bits' => Permission::VIEW],
|
|
|
+ ['scope' => 'config.country', 'bits' => Permission::VIEW],
|
|
|
+ ['scope' => 'config.state', 'bits' => Permission::VIEW],
|
|
|
];
|
|
|
- $this->seedUserTypePermissions($userPermissions, UserTypeEnum::USER->value);
|
|
|
break;
|
|
|
-
|
|
|
case UserTypeEnum::GUEST:
|
|
|
- $guestPermissions = [
|
|
|
- ['scope' => 'config.user', 'bits' => 1],
|
|
|
+ $dataToSync = [
|
|
|
+ ['scope' => 'config.user', 'bits' => Permission::VIEW],
|
|
|
];
|
|
|
- $this->seedUserTypePermissions($guestPermissions, UserTypeEnum::GUEST->value);
|
|
|
break;
|
|
|
}
|
|
|
+ if (!empty($dataToSync)) {
|
|
|
+ $this->seedUserTypePermissions($dataToSync, $userType->value, $allPermissions);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private function seedUserTypePermissions(array $permissions, string $userType): void
|
|
|
+ private function seedUserTypePermissions(array $permissionDataList, string $userType, $allPermissions): void
|
|
|
{
|
|
|
- foreach ($permissions as $permissionData) {
|
|
|
- $permission = Permission::where('scope', $permissionData['scope'])->first();
|
|
|
- if ($permission) {
|
|
|
- $userTypePermission = UserTypePermission::firstOrNew([
|
|
|
- 'user_type' => $userType,
|
|
|
- 'permission_id' => $permission->id,
|
|
|
- 'bits' => $permissionData['bits'],
|
|
|
- ]);
|
|
|
- $userTypePermission->save();
|
|
|
+ foreach ($permissionDataList as $data) {
|
|
|
+ $permissionModel = $allPermissions->get($data['scope']);
|
|
|
+
|
|
|
+ if ($permissionModel) {
|
|
|
+ UserTypePermission::updateOrCreate(
|
|
|
+ [
|
|
|
+ 'user_type' => $userType,
|
|
|
+ 'permission_id' => $permissionModel->id,
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ 'bits' => $data['bits']
|
|
|
+ ]
|
|
|
+ );
|
|
|
}
|
|
|
}
|
|
|
}
|