Skip to content

Commit b2f41b8

Browse files
Fix for replace for extension schemas (#120)
1 parent c6d848d commit b2f41b8

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ RUN composer require arietimmerman/laravel-scim-server @dev && \
2222

2323
RUN touch ./.database.sqlite && \
2424
echo "DB_CONNECTION=sqlite" >> ./.env && \
25-
echo "DB_DATABASE=/example/.database.sqlite" >> ./.env && \
25+
echo "DB_DATABASE=/.database.sqlite" >> ./.env && \
2626
echo "APP_URL=http://localhost:18123" >> ./.env
2727

2828
RUN php artisan migrate && \

src/Attribute/Complex.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Complex extends AbstractComplex
1818
*/
1919
public function getSchemas()
2020
{
21-
return collect($this->getSchemaNodes())->map(fn ($element) => $element->name)->values()->toArray();
21+
return collect($this->getSchemaNodes())->map(fn($element) => $element->name)->values()->toArray();
2222
}
2323

2424

@@ -76,21 +76,29 @@ public function patch($operation, $value, Model &$object, Path $path = null, $re
7676
$attributeNames = $path?->getAttributePath()?->getAttributeNames() ?? [];
7777

7878
if (!empty($attributeNames)) {
79-
$attribute = $this->getSubNode($attributeNames[0]);
79+
$schema = $path->getAttributePath()?->path?->schema;
80+
// Resolve attribute either directly or via schema parent when specified
81+
$attribute = $schema
82+
? ((($parent = $this->getSubNode($schema)) instanceof Schema) ? $parent->getSubNode($attributeNames[0]) : null)
83+
: $this->getSubNode($attributeNames[0]);
84+
8085
if ($attribute != null) {
8186
$attribute->patch($operation, $value, $object, $path->shiftAttributePathAttributes());
82-
} elseif ($this->parent == null) {
83-
// this is the root node, check within the first (the default) schema node
84-
// pass the unchanged path object
87+
return; // done
88+
}
89+
90+
if ($this->parent == null) {
91+
// root node: delegate unchanged path to default schema node
8592
$this->getSchemaNode()->patch($operation, $value, $object, $path);
86-
} else {
87-
throw new SCIMException('Unknown path: ' . (string)$path . ", in object: " . $this->getFullKey());
93+
return; // done
8894
}
95+
96+
throw new SCIMException('Unknown path: ' . (string)$path . ", in object: " . $this->getFullKey());
8997
}
9098
}
9199
} else {
92100
// if there is no path, keys of value are attribute names
93-
switch($operation) {
101+
switch ($operation) {
94102
case 'replace':
95103
$this->replace($value, $object, $path, false);
96104
break;
@@ -300,6 +308,6 @@ public function applyComparison(Builder &$query, Path $path, $parentAttribute =
300308
*/
301309
public function getDefaultSchema()
302310
{
303-
return collect($this->subAttributes)->first(fn ($element) => $element instanceof Schema)->name;
311+
return collect($this->subAttributes)->first(fn($element) => $element instanceof Schema)->name;
304312
}
305313
}

tests/CustomSchemaTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,28 @@ public function testPost()
7979
$this->assertEquals('Dr. Marie Jo', $json['urn:ietf:params:scim:schemas:core:2.0:User']['userName']);
8080
$this->assertEquals('123', $json['urn:ietf:params:scim:schemas:extension:enterprise:2.0:User']['employeeNumber']);
8181
}
82+
83+
public function testPatchEnterpriseSchema()
84+
{
85+
$response = $this->patch('/scim/v2/Users/2', [
86+
"schemas" => [
87+
"urn:ietf:params:scim:api:messages:2.0:PatchOp",
88+
],
89+
"Operations" => [
90+
[
91+
"op" => "replace",
92+
"path" => "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:employeeNumber",
93+
"value" => "12345"
94+
]
95+
]
96+
]);
97+
98+
99+
$response->assertStatus(200);
100+
101+
$json = $response->json();
102+
103+
$this->assertArrayHasKey('urn:ietf:params:scim:schemas:extension:enterprise:2.0:User', $json);
104+
$this->assertEquals('12345', $json['urn:ietf:params:scim:schemas:extension:enterprise:2.0:User']['employeeNumber']);
105+
}
82106
}

0 commit comments

Comments
 (0)