A free open source IT asset/license management system
Fixed #19962 - check for address subnode in SCIM
snipe576d57eb4c9e
diff --git a/app/Models/SnipeSCIMConfig.php b/app/Models/SnipeSCIMConfig.phpindex 0bdf9b9088..99315835ea 100644--- a/app/Models/SnipeSCIMConfig.php+++ b/app/Models/SnipeSCIMConfig.php@@ -225,39 +225,39 @@ class SnipeRootComplex extends Complex}}- // Complex::patch's value-path branch (branch A: path like+ // Complex::patch's value-path branch (path like// `employeeNumber[value eq "123"]`) calls getSubNode($attr) on root,// falls back to getSchemaNode() (always the FIRST schema, core) when// that misses, and throws "Unknown path" because employeeNumber// lives on the enterprise schema. Entra ID sends exactly this shape// when it de-provisions a user whose employeeNumber also changed:// one PATCH with ops for `active=false` and `employeeNumber[value eq- // "..."]`. See #19347 and $19819.+ // "..."]`. See #19347 and #19819.//- // Before deferring to the vendor, scan every AttributeSchema- // sub-node for the attribute by name. If any schema has it, dispatch- // the operation directly (add/replace/clear-via-null) on that- // attribute. Dropping the leftover `[value eq "X"]` filter is- // deliberate: Entra attaches it as "the one whose current value is- // X" context, but for a single-valued attribute like employeeNumber- // there's nothing to filter against and the Eloquent attribute's- // patch throws when it sees any non-empty path anyway.+ // Before deferring to the vendor, check whether getSubNode() misses+ // the attribute on root.+ // Dropping the leftover `[value eq "X"]` filter is deliberate:+ // Entra attaches it as "the one whose current value is X" context,+ // but for a single-valued attribute like employeeNumber there's nothing to filter+ // against and the Eloquent attribute's patch throws when it sees+ // any non-empty path anyway.+ //+ // When getSubNode DOES resolve the attribute (ie, the attribute is+ // on the first/core schema), defer to the vendor.public function patch($operation, $value, Model &$object, ?Path $path = null, $removeIfNotSet = false){- if ($path !== null && $path->isNotEmpty()) {+ if ($path !== null && $path->isNotEmpty() && $this->parent === null) {$valuePathAttributes = $path->getValuePathAttributes();- if (!empty($valuePathAttributes) && $this->parent === null) {+ if (!empty($valuePathAttributes) && $this->getSubNode($valuePathAttributes[0]) === null) {$attrName = $valuePathAttributes[0];- $attribute = $this->getSubNode($attrName);-- if ($attribute === null) {- foreach ($this->subAttributes as $sub) {- if ($sub instanceof AttributeSchema) {- $candidate = $sub->getSubNode($attrName);- if ($candidate !== null) {- $attribute = $candidate;- break;- }+ $attribute = null;++ foreach ($this->subAttributes as $sub) {+ if ($sub instanceof AttributeSchema) {+ $candidate = $sub->getSubNode($attrName);+ if ($candidate !== null) {+ $attribute = $candidate;+ break;}}}diff --git a/tests/Feature/Scim/PatchAddressSubAttributeTest.php b/tests/Feature/Scim/PatchAddressSubAttributeTest.phpnew file mode 100644index 0000000000..f4975adfdb--- /dev/null+++ b/tests/Feature/Scim/PatchAddressSubAttributeTest.php@@ -0,0 +1,90 @@+<?php++namespace Tests\Feature\Scim;++use App\Models\User;+use Laravel\Passport\Passport;+use Tests\TestCase;+++class PatchAddressSubAttributeTest extends TestCase+{+ public function test_patch_updates_work_address_street_via_sub_attribute_path(): void+ {+ Passport::actingAs(User::factory()->superuser()->create());+ $target = User::factory()->create([+ 'address' => '123 Old Street',+ 'city' => 'Portland',+ ]);++ $response = $this->patchJson('/scim/v2/Users/'.$target->id, [+ 'schemas' => ['urn:ietf:params:scim:api:messages:2.0:PatchOp'],+ 'Operations' => [+ [+ 'op' => 'replace',+ 'path' => 'addresses[type eq "work"].streetAddress',+ 'value' => '456 New Avenue',+ ],+ ],+ ]);++ $response->assertStatus(200);+ $target->refresh();+ $this->assertSame('456 New Avenue', $target->address);+ $this->assertSame('Portland', $target->city, 'Only streetAddress was in the op; city must stay unchanged.');+ }++ public function test_patch_updates_every_work_address_sub_attribute_in_a_single_request(): void+ {+ // Reported case: Entra ID sends one PATCH with a separate op+ // per sub-attribute.+ Passport::actingAs(User::factory()->superuser()->create());+ $target = User::factory()->create([+ 'address' => null,+ 'city' => null,+ 'state' => null,+ 'zip' => null,+ 'country' => null,+ ]);++ $response = $this->patchJson('/scim/v2/Users/'.$target->id, [+ 'schemas' => ['urn:ietf:params:scim:api:messages:2.0:PatchOp'],+ 'Operations' => [+ ['op' => 'replace', 'path' => 'addresses[type eq "work"].streetAddress', 'value' => '1 Infinite Loop'],+ ['op' => 'replace', 'path' => 'addresses[type eq "work"].locality', 'value' => 'Cupertino'],+ ['op' => 'replace', 'path' => 'addresses[type eq "work"].region', 'value' => 'CA'],+ ['op' => 'replace', 'path' => 'addresses[type eq "work"].postalCode', 'value' => '95014'],+ ['op' => 'replace', 'path' => 'addresses[type eq "work"].country', 'value' => 'US'],+ ],+ ]);++ $response->assertStatus(200);+ $target->refresh();+ $this->assertSame('1 Infinite Loop', $target->address);+ $this->assertSame('Cupertino', $target->city);+ $this->assertSame('CA', $target->state);+ $this->assertSame('95014', $target->zip);+ $this->assertSame('US', $target->country);+ }++ public function test_patch_add_work_address_sub_attribute_sets_a_previously_null_field(): void+ {+ Passport::actingAs(User::factory()->superuser()->create());+ $target = User::factory()->create(['city' => null]);++ $response = $this->patchJson('/scim/v2/Users/'.$target->id, [+ 'schemas' => ['urn:ietf:params:scim:api:messages:2.0:PatchOp'],+ 'Operations' => [+ [+ 'op' => 'add',+ 'path' => 'addresses[type eq "work"].locality',+ 'value' => 'Boston',+ ],+ ],+ ]);++ $response->assertStatus(200);+ $target->refresh();+ $this->assertSame('Boston', $target->city);+ }+}