A free open source IT asset/license management system
SCIM: Fixed RB-21807 (SCIM shape parsing error)
snipe3a359bf70dd9
diff --git a/app/Models/SnipeSCIMConfig.php b/app/Models/SnipeSCIMConfig.phpindex 8ac3a3090f..b805939ff4 100644--- a/app/Models/SnipeSCIMConfig.php+++ b/app/Models/SnipeSCIMConfig.php@@ -285,18 +285,53 @@ class MappedTable extends Attributepublic function add($value, Model &$object){+ $value = $this->coerceScalar($value);$object->{$this->relationship_id_field} = $value ? $this->relationship_class::firstOrCreate([$this->relationship_field => $value])->id : null;}public function replace($value, Model &$object, $path = null, $removeIfNotSet = false){+ $value = $this->coerceScalar($value);$object->{$this->relationship_id_field} = $value ? $this->relationship_class::firstOrCreate([$this->relationship_field => $value])->id : null;}public function patch($operation, $value, Model &$object, ?Path $path = null, $removeIfNotSet = false){+ $value = $this->coerceScalar($value);$object->{$this->relationship_id_field} = $value ? $this->relationship_class::firstOrCreate([$this->relationship_field => $value])->id : null;}++ // SCIM clients may send scalar-mapped attributes like `department`+ // and `location` as complex objects — {"value": "Engineering"} or+ // {"displayName": "Engineering"} — and SnipeRootComplex::replace()+ // additionally wraps sub-attribute values ({"remaining.path" => v})+ // when descending. MappedTable is a leaf that maps ONE relationship+ // field, so anything arriving as an array must be unwrapped before+ // firstOrCreate() gets it — otherwise Grammar::parameterize()+ // throws when the WHERE binding receives an array. Prefer the SCIM+ // conventional keys "value" and "displayName", then fall back to+ // any scalar leaf; return null if no usable value exists so the+ // caller nulls the relationship (matching the empty-string branch).+ private function coerceScalar($value)+ {+ if (! is_array($value)) {+ return $value;+ }++ foreach (['value', 'displayName'] as $key) {+ if (isset($value[$key]) && is_scalar($value[$key])) {+ return $value[$key];+ }+ }++ foreach ($value as $v) {+ if (is_scalar($v) && $v !== '') {+ return $v;+ }+ }++ return null;+ }}// Company is stored only in the company_user pivot, not company_id. Read from the pivotdiff --git a/database/migrations/2026_07_29_220000_make_departments_created_by_nullable.php b/database/migrations/2026_07_29_220000_make_departments_created_by_nullable.phpnew file mode 100644index 0000000000..bb4e219cb2--- /dev/null+++ b/database/migrations/2026_07_29_220000_make_departments_created_by_nullable.php@@ -0,0 +1,23 @@+<?php++use Illuminate\Database\Migrations\Migration;+use Illuminate\Database\Schema\Blueprint;+use Illuminate\Support\Facades\Schema;+++return new class extends Migration+{+ public function up(): void+ {+ Schema::table('departments', function (Blueprint $table) {+ $table->integer('created_by')->nullable()->change();+ });+ }++ public function down(): void+ {+ Schema::table('departments', function (Blueprint $table) {+ $table->integer('created_by')->nullable(false)->change();+ });+ }+};diff --git a/tests/Feature/Scim/MappedTableComplexValueTest.php b/tests/Feature/Scim/MappedTableComplexValueTest.phpnew file mode 100644index 0000000000..8f3eb9c5bd--- /dev/null+++ b/tests/Feature/Scim/MappedTableComplexValueTest.php@@ -0,0 +1,144 @@+<?php++namespace Tests\Feature\Scim;++use App\Models\Department;+use App\Models\User;+use Laravel\Passport\Passport;+use Tests\TestCase;++/**+ * Regression for a Rollbar 500 on /scim/v2/Users/{id}:+ *+ * TypeError Illuminate\Database\Grammar::parameterize():+ * Argument #1 ($values) must be of type array, string given+ * at MappedTable::replace (SnipeSCIMConfig.php:293)+ *+ * MappedTable is a leaf attribute for the User schema's `department`+ * and `location` fields — both scalar-mapped foreign relations.+ * SCIM clients (Entra, Okta in some configurations) send those as+ * complex objects: {"department": {"value": "Engineering"}} or+ * {"department": {"displayName": "Engineering"}}. The parent+ * SnipeRootComplex router also wraps sub-attribute values into an+ * array on the descent. Either way, MappedTable was receiving an+ * array in a code path that unconditionally passed it to+ * firstOrCreate() as a WHERE value — triggering the Grammar TypeError.+ *+ * The fix coerces array inputs to their SCIM-standard scalar leaf+ * (`value` / `displayName`, then any first scalar) before hitting+ * firstOrCreate.+ */+class MappedTableComplexValueTest extends TestCase+{+ public function test_put_with_department_as_complex_value_object_does_not_crash(): void+ {+ Passport::actingAs(User::factory()->superuser()->create());+ $target = User::factory()->create();++ $response = $this->putJson('/scim/v2/Users/'.$target->id, [+ 'schemas' => ['urn:ietf:params:scim:schemas:core:2.0:User'],+ 'userName' => $target->username,+ 'department' => ['value' => 'Engineering'],+ ]);++ $response->assertStatus(200);+ $this->assertDatabaseHas('departments', ['name' => 'Engineering']);+ $target->refresh();+ $this->assertSame('Engineering', $target->department->name);+ }++ public function test_put_with_department_as_display_name_object_does_not_crash(): void+ {+ Passport::actingAs(User::factory()->superuser()->create());+ $target = User::factory()->create();++ $response = $this->putJson('/scim/v2/Users/'.$target->id, [+ 'schemas' => ['urn:ietf:params:scim:schemas:core:2.0:User'],+ 'userName' => $target->username,+ 'department' => ['displayName' => 'Product'],+ ]);++ $response->assertStatus(200);+ $target->refresh();+ $this->assertSame('Product', $target->department->name);+ }++ public function test_put_with_location_as_complex_value_object_does_not_crash(): void+ {+ Passport::actingAs(User::factory()->superuser()->create());+ $target = User::factory()->create();++ $response = $this->putJson('/scim/v2/Users/'.$target->id, [+ 'schemas' => ['urn:ietf:params:scim:schemas:core:2.0:User'],+ 'userName' => $target->username,+ 'location' => ['value' => 'HQ'],+ ]);++ $response->assertStatus(200);+ $this->assertDatabaseHas('locations', ['name' => 'HQ']);+ $target->refresh();+ $this->assertSame('HQ', $target->location->name);+ }++ public function test_put_with_scalar_department_still_works(): void+ {+ // Non-regression guard: the historical scalar shape must+ // continue to work unchanged.+ Passport::actingAs(User::factory()->superuser()->create());+ $target = User::factory()->create();++ $response = $this->putJson('/scim/v2/Users/'.$target->id, [+ 'schemas' => ['urn:ietf:params:scim:schemas:core:2.0:User'],+ 'userName' => $target->username,+ 'department' => 'Support',+ ]);++ $response->assertStatus(200);+ $target->refresh();+ $this->assertSame('Support', $target->department->name);+ }++ public function test_patch_replace_with_complex_department_value_does_not_crash(): void+ {+ // PATCH shape from Entra / Okta: op=replace, no top-level+ // path, complex value under the attribute key.+ Passport::actingAs(User::factory()->superuser()->create());+ $target = User::factory()->create();++ $response = $this->patchJson('/scim/v2/Users/'.$target->id, [+ 'schemas' => ['urn:ietf:params:scim:api:messages:2.0:PatchOp'],+ 'Operations' => [+ [+ 'op' => 'replace',+ 'value' => [+ 'department' => ['value' => 'Finance'],+ ],+ ],+ ],+ ]);++ $response->assertStatus(200);+ $target->refresh();+ $this->assertSame('Finance', $target->department->name);+ }++ public function test_put_with_empty_complex_department_object_nulls_relationship(): void+ {+ // Clients sometimes send {} to clear a relationship. The+ // coercion falls through to null, which the caller uses to+ // null the foreign-key column. No crash, no orphan row.+ Passport::actingAs(User::factory()->superuser()->create());+ $existingDepartment = Department::factory()->create();+ $target = User::factory()->create(['department_id' => $existingDepartment->id]);++ $response = $this->putJson('/scim/v2/Users/'.$target->id, [+ 'schemas' => ['urn:ietf:params:scim:schemas:core:2.0:User'],+ 'userName' => $target->username,+ 'department' => new \stdClass, // empty object → [] on decode+ ]);++ $response->assertStatus(200);+ $target->refresh();+ $this->assertNull($target->department_id);+ }+}