Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/Attribute/MutableCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,28 @@ public function replace($value, Model &$object, ?Path $path = null)

// Check if objects exist
$existingObjects = $object
->{$this->attribute}()
->getRelated()
::findMany($values)
->{$this->attribute}()
->getRelated()
::findMany($values);
$existingObjectIds = $existingObjects
->map(fn ($o) => $o->getKey());

if (($diff = collect($values)->diff($existingObjects))->count() > 0) {
if (($diff = collect($values)->diff($existingObjectIds))->count() > 0) {
throw new SCIMException(
sprintf('One or more %s are unknown: %s', $this->attribute, implode(',', $diff->all())),
500
);
}

$object->{$this->attribute}()->sync($existingObjects->all());
// Act like the relation is already saved. This allows running validations, if needed.
$object->setRelation($this->attribute, $existingObjects);

$object->saved(function (Model $model) use ($object, $existingObjectIds) {
// Save relationships only after the model is saved. Essential if the model is new.
// Intentionlly `$object` is used instead of `$model`, to avoid accidentially updating the wrong model.
$object->{$this->attribute}()->sync($existingObjectIds->all());
});

}

public function patch($operation, $value, Model &$object, ?Path $path = null)
Expand Down
32 changes: 32 additions & 0 deletions tests/GroupsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ArieTimmerman\Laravel\SCIMServer\Tests;

use ArieTimmerman\Laravel\SCIMServer\Tests\Model\Group;
use ArieTimmerman\Laravel\SCIMServer\Tests\Model\User;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

Expand Down Expand Up @@ -69,6 +70,37 @@ public function testCreate(){

}

public function testCreateWithMembers(){
$response = $this->post('/scim/v2/Groups', [
'schemas' => ['urn:ietf:params:scim:schemas:core:2.0:Group'], // Required
'urn:ietf:params:scim:schemas:core:2.0:Group' => [
'displayName' => 'TestGroup',
'members' => [
[
'value' => User::first()->id,
]
]
]
]);

$response->assertJsonStructure([
'id',
'urn:ietf:params:scim:schemas:core:2.0:Group' => [
'displayName',
'members' => [
0 => [
'value',
'display'
]
]
]
]);

$this->assertEquals(User::first()->id, $response->json(['urn:ietf:params:scim:schemas:core:2.0:Group'])['members'][0]['value']);
$this->assertNotNull(Group::find($response->json('id')));
$this->assertNotNull(Group::where('displayName', 'TestGroup')->first());
}

public function testBulk(){
$response = $this->post('/scim/v2/Bulk', [
'schemas' => ['urn:ietf:params:scim:api:messages:2.0:BulkRequest'], // Required
Expand Down
Loading