Skip to content

Commit

Permalink
Merge pull request #18 from etsvThor/upgrade/dto
Browse files Browse the repository at this point in the history
Upgrade/dto
  • Loading branch information
niekbr authored Sep 30, 2022
2 parents aadb09f + 88a2ab7 commit 5c956ce
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 17 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
19 changes: 8 additions & 11 deletions src/DataTransferObjects/Collections/BifrostRoleDataCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
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 5c956ce

Please sign in to comment.