Skip to content

Commit

Permalink
upgrade dto
Browse files Browse the repository at this point in the history
  • Loading branch information
niekbr committed Sep 30, 2022
1 parent 89ef5d2 commit 88a2ab7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
namespace EtsvThor\BifrostBridge\DataTransferObjects\Collections;

use EtsvThor\BifrostBridge\DataTransferObjects\BifrostRoleData;
use EtsvThor\BifrostBridge\DataTransferObjects\Casters\BifrostRoleDataCaster;
use Spatie\DataTransferObject\Attributes\CastWith;
use Spatie\DataTransferObject\Casters\ArrayCaster;
use Spatie\DataTransferObject\DataTransferObject;

class BifrostRoleDataCollection extends DataTransferObject
{
public static function create(array $data): self
{
return new static(BifrostRoleData::arrayOf($data));
}
/** @var BifrostRoleData[] */
#[CastWith(ArrayCaster::class, itemType: BifrostRoleData::class)]
public ?array $roles;
}
2 changes: 1 addition & 1 deletion src/Http/Controllers/WebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
4 changes: 2 additions & 2 deletions src/Jobs/ProcessWebhookBifrost.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.');
Expand Down

0 comments on commit 88a2ab7

Please sign in to comment.