A free open source IT asset/license management system
SCIM: Fixed #19819 - disabled failing when employee ID is included
snipe1d9f6a05e475
diff --git a/app/Models/SnipeSCIMConfig.php b/app/Models/SnipeSCIMConfig.phpindex 0d98ff1a4d..b227067003 100644--- a/app/Models/SnipeSCIMConfig.php+++ b/app/Models/SnipeSCIMConfig.php@@ -225,6 +225,69 @@ class SnipeRootComplex extends Complex}}+ // Complex::patch's value-path branch (branch A: 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.+ //+ // 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.+ public function patch($operation, $value, Model &$object, ?Path $path = null, $removeIfNotSet = false)+ {+ if ($path !== null && $path->isNotEmpty()) {+ $valuePathAttributes = $path->getValuePathAttributes();+ if (!empty($valuePathAttributes) && $this->parent === 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;+ }+ }+ }+ }++ if ($attribute !== null) {+ switch ($operation) {+ case 'add':+ $attribute->add($value, $object);++ return;+ case 'replace':+ $attribute->replace($value, $object);++ return;+ case 'remove':+ // Eloquent::patch treats remove as "set to+ // null". No public remove() on the Eloquent+ // attribute, so emulate it via replace(null)+ // which hits the same underlying write.+ $attribute->replace(null, $object);++ return;+ }+ }+ }+ }++ parent::patch($operation, $value, $object, $path, $removeIfNotSet);+ }+// #19347: Complex::applyComparison ignores the schema URN on the// filter path and always falls back to the FIRST schema node (core).// A filter like:@@ -236,7 +299,7 @@ class SnipeRootComplex extends Complex// Same shape of bug as the URN-blind add()/replace() routing above.// If the path carries a schema URN and we have a schema node for it,// dispatch the whole path there so its own applyComparison finds the- // attribute; otherwise defer to the library.+ // attribute. Otherwise defer to the library.public function applyComparison(Builder &$query, Path $path, $parentAttribute = null){$schemaUrn = $path->getAttributePath()?->path?->schema;diff --git a/tests/Feature/Scim/PatchEmployeeNumberValueFilterTest.php b/tests/Feature/Scim/PatchEmployeeNumberValueFilterTest.phpnew file mode 100644index 0000000000..1b1eb81370--- /dev/null+++ b/tests/Feature/Scim/PatchEmployeeNumberValueFilterTest.php@@ -0,0 +1,109 @@+<?php++namespace Tests\Feature\Scim;++use App\Models\User;+use Laravel\Passport\Passport;+use Tests\TestCase;++/**+ * Regression for #19819. When Entra ID de-provisions a user whose+ * employeeNumber has also changed, it sends one PATCH to+ * /scim/v2/Users/{id} with two operations: one that flips `active` to+ * false and one that touches `employeeNumber` using a value-path+ * filter like `employeeNumber[value eq "<previous value>"]`. The whole+ * PATCH was failing with:+ *+ * Unknown path: employeeNumber[value eq "123"], in object:+ * urn:ietf:params:scim:schemas:core:2.0:User+ *+ * because the vendor Complex::patch's value-path branch calls+ * getSubNode('employeeNumber') on root and (missing it there) falls+ * back to the FIRST schema node, which is core. employeeNumber lives+ * on the enterprise schema, so the fallback never resolves it. The+ * user never flipped to inactive even though the active=false op was+ * well-formed, because PATCH is all-or-nothing.+ *+ * SnipeRootComplex::patch now scans every AttributeSchema sub-node+ * for the attribute by name before deferring to the vendor, mirroring+ * the schema-aware routing already in add() / replace() /+ * applyComparison() from #19347.+ */+class PatchEmployeeNumberValueFilterTest extends TestCase+{+ public function test_patch_with_value_filter_on_employee_number_disables_user_and_updates_employee_number(): void+ {+ Passport::actingAs(User::factory()->superuser()->create());+ $target = User::factory()->create([+ 'activated' => true,+ 'employee_num' => 'OLD-123',+ ]);++ $response = $this->patchJson('/scim/v2/Users/' . $target->id, [+ 'schemas' => ['urn:ietf:params:scim:api:messages:2.0:PatchOp'],+ 'Operations' => [+ [+ 'op' => 'replace',+ 'path' => 'active',+ 'value' => false,+ ],+ [+ 'op' => 'replace',+ 'path' => 'employeeNumber[value eq "OLD-123"]',+ 'value' => 'NEW-456',+ ],+ ],+ ]);++ $response->assertStatus(200);++ $target->refresh();+ $this->assertFalse((bool) $target->activated, 'active=false op must land even though employeeNumber op shares the request.');+ $this->assertSame('NEW-456', $target->employee_num);+ }++ public function test_patch_remove_employee_number_via_value_filter_clears_the_field(): void+ {+ Passport::actingAs(User::factory()->superuser()->create());+ $target = User::factory()->create(['employee_num' => 'KEEP-999']);++ $response = $this->patchJson('/scim/v2/Users/' . $target->id, [+ 'schemas' => ['urn:ietf:params:scim:api:messages:2.0:PatchOp'],+ 'Operations' => [+ [+ 'op' => 'remove',+ 'path' => 'employeeNumber[value eq "KEEP-999"]',+ ],+ ],+ ]);++ $response->assertStatus(200);+ $target->refresh();+ $this->assertNull($target->employee_num);+ }++ public function test_patch_replace_without_filter_still_works(): void+ {+ // Belt-and-suspenders: the bare `employeeNumber` path (no value+ // filter) uses the attribute-path branch rather than the value-+ // path branch my fix overrides. Pinning it so the override+ // doesn't accidentally regress the already-working case.+ Passport::actingAs(User::factory()->superuser()->create());+ $target = User::factory()->create(['employee_num' => null]);++ $response = $this->patchJson('/scim/v2/Users/' . $target->id, [+ 'schemas' => ['urn:ietf:params:scim:api:messages:2.0:PatchOp'],+ 'Operations' => [+ [+ 'op' => 'replace',+ 'path' => 'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:employeeNumber',+ 'value' => 'SET-FIRST-TIME',+ ],+ ],+ ]);++ $response->assertStatus(200);+ $target->refresh();+ $this->assertSame('SET-FIRST-TIME', $target->employee_num);+ }+}