Skip to content

A free open source IT asset/license management system

Got multicompany support working with Entra SCIM

Brady Wetheringtonaf1d0dfca1f0
diff --git a/app/Models/SnipeSCIMConfig.php b/app/Models/SnipeSCIMConfig.php
index b805939ff4..bb90d2c481 100644
--- a/app/Models/SnipeSCIMConfig.php
+++ b/app/Models/SnipeSCIMConfig.php
@@ -336,7 +336,7 @@ class MappedTable extends Attribute
// Company is stored only in the company_user pivot, not company_id. Read from the pivot
// and sync it on write. For new users (not yet saved) defer the sync via a saved() callback.
-class SCIMCompanyAttribute extends MappedTable
+class SCIMCompanyAttribute extends Attribute
{
protected function doRead(&$object, $attributes = [])
{
@@ -378,6 +378,58 @@ class SCIMCompanyAttribute extends MappedTable
}
}
+class SCIMMultiCompanyArray extends Attribute
+{
+ protected function doRead(&$object, $attributes = [])
+ {
+ return $object->companies()->pluck('name')->toArray();
+ }
+
+ private function applyCompanies(array $company_names, Model &$object): void
+ {
+ $names = [];
+ foreach ($company_names as $company) {
+ if (is_array($company) && isset($company['value'])) {
+ // this is how Entra ID does it
+ $names[] = $company['value'];
+ } elseif (is_string($company)) {
+ // This seems to be how Okta does it?
+ $names[] = $company;
+ } else {
+ throw new SCIMException("Unknown 'companies' value: '" . print_r($company, true) . "' of type: " . get_debug_type($company), 400);
+ }
+ }
+
+ $ids = [];
+ foreach ($names as $company_name) {
+ $ids[] = Company::firstOrCreate(['name' => $company_name])->id;
+ }
+ if ($object->exists) {
+ $object->companies()->sync($ids);
+ } else {
+ $object->saved(fn() => $object->companies()->sync($ids));
+ }
+ }
+
+ public function add($value, Model &$object)
+ {
+ \Log::debug("MC ADD VALUE IS: " . print_r($value, true));
+ $this->applyCompanies($value, $object);
+ }
+
+ public function replace($value, Model &$object, $path = null, $removeIfNotSet = false)
+ {
+ \Log::debug("MC REPLACE VALUE IS: " . print_r($value, true));
+ $this->applyCompanies($value, $object);
+ }
+
+ public function patch($operation, $value, Model &$object, ?Path $path = null, $removeIfNotSet = false)
+ {
+ \Log::debug("MC PATCH VALUE IS: " . print_r($value, true));
+ $this->applyCompanies($value, $object);
+ }
+}
+
class EloquentWithRemove extends Eloquent
{
public function remove($value, Model &$object, ?Path $path = null)
@@ -752,7 +804,8 @@ class SnipeSCIMConfig
),
(new AttributeSchema(self::GROKABILITY, false))->withSubAttributes(
new MappedTable('location', 'location', Location::class, 'location_id', 'name'),
- new SCIMCompanyAttribute('company', 'company', Company::class, 'company_id', 'name'),
+ new SCIMCompanyAttribute('company'),
+ (new SCIMMultiCompanyArray('companies'))->ensure('array')->setMultiValued(true),
)
),
];