Skip to content

A free open source IT asset/license management system

Removed built-in from shape

snipebd07d0ca69c2
diff --git a/app/Models/SyncAdapterInstance.php b/app/Models/SyncAdapterInstance.php
index bfc3e8c1bd..ebcc8a56ab 100644
--- a/app/Models/SyncAdapterInstance.php
+++ b/app/Models/SyncAdapterInstance.php
@@ -18,11 +18,10 @@ use Illuminate\Support\Str;
*/
class SyncAdapterInstance extends Model
{
- protected $fillable = ['adapter_type', 'label', 'slug', 'active', 'built_in', 'company_id'];
+ protected $fillable = ['adapter_type', 'label', 'slug', 'active', 'company_id'];
protected $casts = [
'active' => 'boolean',
- 'built_in' => 'boolean',
'last_synced_at' => 'datetime',
];
diff --git a/app/SyncAdapters/SyncAdapter.php b/app/SyncAdapters/SyncAdapter.php
index 2dfbade33e..00baf99780 100644
--- a/app/SyncAdapters/SyncAdapter.php
+++ b/app/SyncAdapters/SyncAdapter.php
@@ -245,14 +245,14 @@ abstract class SyncAdapter
return $this->instance->label;
}
- public function isActive(): bool
+ public function companyId(): ?int
{
- return $this->instance->active;
+ return $this->instance->company_id;
}
- public function isBuiltIn(): bool
+ public function isActive(): bool
{
- return $this->instance->built_in;
+ return $this->instance->active;
}
public function isEnabled(): bool
diff --git a/resources/views/blade/sync-adapter/form.blade.php b/resources/views/blade/sync-adapter/form.blade.php
index b175f42738..1431eab61e 100644
--- a/resources/views/blade/sync-adapter/form.blade.php
+++ b/resources/views/blade/sync-adapter/form.blade.php
@@ -14,6 +14,12 @@
$urlField = $slug.'_url';
$activeField = $slug.'_active';
$locked = config('app.lock_passwords') === true;
+ // Memoized per-request via Laravel's Str/once so N adapter tab-panes
+ // don't each fire their own companies-count query. The pickers below
+ // (both the per-instance company scope and the per-vendor-group
+ // mapping picker further down) are pure noise on a single-tenant
+ // install with zero company rows.
+ $hasCompanies = once(fn () => \App\Models\Company::query()->exists());
@endphp
<x-form
:id="'adapter-form-' . $slug"
@@ -47,6 +53,26 @@
</x-slot:input>
</x-form.row>
+ {{-- Company scope. Set at Add-adapter time in the modal. This row
+ surfaces it in the per-adapter settings so admins can see and
+ change what company the instance is scoped to without having
+ to delete and recreate. Blank means shared across every
+ company (available to all). The id is slug-scoped because
+ every adapter tab-pane renders this same form, and duplicate
+ DOM ids silently break select2's init on all but the first
+ one. Only rendered when at least one company exists, since
+ on a single-tenant install the picker would only ever show
+ "no selection" and confuses more than it helps. --}}
+ @if ($hasCompanies)
+ <x-input.company-select
+ name="company_id"
+ :id="$slug . '_company_id_select'"
+ :label="trans('admin/settings/sync_adapters.company_scope_label')"
+ :selected="old('company_id', $adapter->companyId())"
+ :help_text="trans('admin/settings/sync_adapters.company_scope_help')"
+ />
+ @endif
+
<x-form.checkbox-row
:name="$activeField"
:checked="$adapter->isActive()"
@@ -303,7 +329,7 @@
/>
@endif
- @if ($adapter->supportsGroupScoping() && $adapter->isEnabled())
+ @if ($adapter->supportsGroupScoping() && $adapter->isEnabled() && $hasCompanies)
@php
$cachedGroups = $adapter->cachedGroups();
$groupMappings = $adapter->groupMappings();
diff --git a/tests/Feature/SyncAdapters/BuiltInDeleteProtectionTest.php b/tests/Feature/SyncAdapters/BuiltInDeleteProtectionTest.php
deleted file mode 100644
index 07b1371e70..0000000000
--- a/tests/Feature/SyncAdapters/BuiltInDeleteProtectionTest.php
+++ /dev/null
@@ -1,142 +0,0 @@
-<?php
-
-namespace Tests\Feature\SyncAdapters;
-
-use App\Models\SyncAdapterInstance;
-use App\Models\User;
-use Tests\TestCase;
-
-/**
- * Built-in adapters (Fleet, Kandji, Intune, and the 8 others seeded by
- * the migration) must never be deletable. If a user doesn't want to
- * use one, they leave it inactive. the record has to persist because
- * asset_external_sources.source may still reference it after past syncs.
- *
- * Protection lives in three places: the settings-page delete button is
- * hidden by @if(! isBuiltIn()) in the blade, the controller refuses
- * built_in=1 rows in deleteAdapterInstance, and the seed migration
- * sets built_in=1 on every shipped adapter. This test exercises all
- * three so a future refactor can't silently drop the guard.
- */
-class BuiltInDeleteProtectionTest extends TestCase
-{
- public function test_delete_button_is_hidden_when_a_built_in_adapter_is_selected()
- {
- $fleet = SyncAdapterInstance::where('slug', 'fleet')->firstOrFail();
-
- $html = $this->actingAs(User::factory()->superuser()->create())
- ->get(route('settings.adapters.index', ['adapter' => $fleet->slug]))
- ->assertOk()
- ->getContent();
-
- // Trigger is always rendered so the tab-switch JS can flip its
- // visibility as the admin moves between adapters. Server-side
- // initial state must hide it when a built-in is the selected
- // adapter (matches what the JS also enforces).
- $this->assertMatchesRegularExpression(
- '/id="adapter-delete-trigger"[^>]*style="[^"]*display:\s*none/i',
- $html,
- );
- }
-
- public function test_delete_button_is_visible_when_a_user_created_adapter_is_selected()
- {
- $userMade = SyncAdapterInstance::create([
- 'adapter_type' => 'fleet',
- 'label' => 'Second Fleet',
- 'built_in' => false,
- ]);
-
- $html = $this->actingAs(User::factory()->superuser()->create())
- ->get(route('settings.adapters.index', ['adapter' => $userMade->slug]))
- ->assertOk()
- ->getContent();
-
- // Visible = style="" (empty) when initially selected. The regex
- // is generous to survive attribute-order shuffles from Blade
- // formatting changes.
- $this->assertMatchesRegularExpression(
- '/id="adapter-delete-trigger"[^>]*style=""/i',
- $html,
- );
-
- // data-href points at the user-created adapter, not any of the
- // built-ins that share the tab list. The shared .delete-asset
- // handler in snipeit.js reads data-href when the button is
- // clicked and swaps it into #dataConfirmModal's <form action>.
- $this->assertStringContainsString(
- 'data-href="'.route('settings.adapters.destroy', $userMade->slug).'"',
- $html,
- );
- }
-
- public function test_tab_list_carries_data_attrs_for_the_js_toggle()
- {
- // Every tab link gets data-adapter-built-in + data-adapter-destroy-url
- // so the shown.bs.tab handler can flip the delete form's
- // visibility and action URL on client-side tab switches.
- SyncAdapterInstance::create([
- 'adapter_type' => 'fleet',
- 'label' => 'Second Fleet',
- 'built_in' => false,
- ]);
-
- $html = $this->actingAs(User::factory()->superuser()->create())
- ->get(route('settings.adapters.index'))
- ->assertOk()
- ->getContent();
-
- // Built-in flag on the Fleet tab
- $this->assertMatchesRegularExpression(
- '/data-adapter-name="fleet"[^>]*data-adapter-built-in="1"/',
- $html,
- );
- // User-created gets the 0 flag
- $this->assertMatchesRegularExpression(
- '/data-adapter-name="second-fleet"[^>]*data-adapter-built-in="0"/',
- $html,
- );
- }
-
- public function test_backend_refuses_to_delete_a_built_in_adapter()
- {
- $fleet = SyncAdapterInstance::where('slug', 'fleet')->firstOrFail();
-
- $this->actingAs(User::factory()->superuser()->create())
- ->delete(route('settings.adapters.destroy', $fleet->slug))
- ->assertRedirect(route('settings.adapters.index', ['adapter' => $fleet->slug]))
- ->assertSessionHas('error');
-
- // Row still exists. Belt-and-suspenders: even if the blade
- // gate broke and someone crafted a raw DELETE, the built-in
- // stays put.
- $this->assertDatabaseHas('sync_adapter_instances', [
- 'id' => $fleet->id,
- 'slug' => 'fleet',
- ]);
- }
-
- public function test_unknown_adapter_slug_redirects_to_index_with_error()
- {
- $this->actingAs(User::factory()->superuser()->create())
- ->get(route('settings.adapters.index', ['adapter' => 'never-existed']))
- ->assertRedirect(route('settings.adapters.index'))
- ->assertSessionHas('error');
- }
-
- public function test_backend_deletes_a_user_created_adapter()
- {
- $userMade = SyncAdapterInstance::create([
- 'adapter_type' => 'fleet',
- 'label' => 'Second Fleet',
- 'built_in' => false,
- ]);
-
- $this->actingAs(User::factory()->superuser()->create())
- ->delete(route('settings.adapters.destroy', $userMade->slug))
- ->assertRedirect(route('settings.adapters.index'))
- ->assertSessionHas('success');
-
- $this->assertDatabaseMissing('sync_adapter_instances', ['id' => $userMade->id]);
- }
-}
diff --git a/tests/Feature/SyncAdapters/CustomHttp/CustomHttpAdapterTest.php b/tests/Feature/SyncAdapters/CustomHttp/CustomHttpAdapterTest.php
index 32eb9e450f..22d48455b7 100644
--- a/tests/Feature/SyncAdapters/CustomHttp/CustomHttpAdapterTest.php
+++ b/tests/Feature/SyncAdapters/CustomHttp/CustomHttpAdapterTest.php
@@ -696,7 +696,6 @@ class CustomHttpAdapterTest extends TestCase
'adapter_type' => 'custom_http',
'label' => 'Test Custom '.uniqid('', true),
'active' => true,
- 'built_in' => false,
]);
SyncAdapterConfig::put($instance->id, 'url', 'https://vendor.example/api');