diff --git a/composer.json b/composer.json index b1d626e..675bed1 100644 --- a/composer.json +++ b/composer.json @@ -13,10 +13,10 @@ ], "homepage": "https://github.com/etsvthor/laravel-bifrost-bridge", "require": { - "php": "^7.4 | ~8.0.0 | ~8.1.0", - "spatie/data-transfer-object": "^2.6", + "php": "~8.0.0 | ~8.1.0", + "spatie/data-transfer-object": "^3.9", "spatie/laravel-permission": "^3.18|^4.0|^5.0", - "laravel/framework": "^6.18 | ^8.0 | ^9.0", + "laravel/framework": "^8.0 | ^9.0", "laravel/socialite": "^5.1" }, "autoload": { diff --git a/src/DataTransferObjects/Collections/BifrostRoleDataCollection.php b/src/DataTransferObjects/Collections/BifrostRoleDataCollection.php index 6e1f09e..fab47fd 100644 --- a/src/DataTransferObjects/Collections/BifrostRoleDataCollection.php +++ b/src/DataTransferObjects/Collections/BifrostRoleDataCollection.php @@ -3,17 +3,14 @@ namespace EtsvThor\BifrostBridge\DataTransferObjects\Collections; use EtsvThor\BifrostBridge\DataTransferObjects\BifrostRoleData; -use Spatie\DataTransferObject\DataTransferObjectCollection; +use EtsvThor\BifrostBridge\DataTransferObjects\Casters\BifrostRoleDataCaster; +use Spatie\DataTransferObject\Attributes\CastWith; +use Spatie\DataTransferObject\Casters\ArrayCaster; +use Spatie\DataTransferObject\DataTransferObject; -class BifrostRoleDataCollection extends DataTransferObjectCollection +class BifrostRoleDataCollection extends DataTransferObject { - public static function create(array $data): self - { - return new static(BifrostRoleData::arrayOf($data)); - } - - public function current(): BifrostRoleData - { - return parent::current(); - } + /** @var BifrostRoleData[] */ + #[CastWith(ArrayCaster::class, itemType: BifrostRoleData::class)] + public ?array $roles; } diff --git a/src/Http/Controllers/WebhookController.php b/src/Http/Controllers/WebhookController.php index 1e92aa5..e66ce7c 100644 --- a/src/Http/Controllers/WebhookController.php +++ b/src/Http/Controllers/WebhookController.php @@ -32,7 +32,7 @@ public function bifrost(Request $request): JsonResponse } ProcessWebhookBifrost::dispatch( - BifrostRoleDataCollection::create($request->get('roles')) + new BifrostRoleDataCollection(roles: $request->get('roles')) ); return response()->json(['success' => true]); diff --git a/src/Jobs/ProcessWebhookBifrost.php b/src/Jobs/ProcessWebhookBifrost.php index 8e8bf8b..08e51d8 100644 --- a/src/Jobs/ProcessWebhookBifrost.php +++ b/src/Jobs/ProcessWebhookBifrost.php @@ -36,7 +36,7 @@ public function handle(): void $oauthUserId = BifrostBridge::oauthUserIdKey(); $userClassKey = BifrostBridge::getUserClass()->getKeyName(); - foreach ($this->roles as $bifrostRole) { + foreach ($this->roles->roles as $bifrostRole) { $systemRole = $allRoles->where('name', $bifrostRole->name)->first(); // Role does not exist, by default, don't create a role @@ -83,7 +83,7 @@ public function handle(): void if (config('bifrost.auth_push_detach_on_remove') === true) { // If a role is not present on Bifrost anymore, remove all users from it. - $existingOnSystemButNotBifrost = $allRoles->whereNotIn('name', collect($this->roles)->pluck('name')); + $existingOnSystemButNotBifrost = $allRoles->whereNotIn('name', collect($this->roles->roles)->pluck('name')); foreach ($existingOnSystemButNotBifrost as $role) { $role->users()->detach(); // detach all users, but keep the role Log::info('Role ' . $role->name . ' was removed on Bifrost. Detached all users.');