A free open source IT asset/license management system
Better handle flash message coloring
snipe3801db4915b6
diff --git a/app/Http/Controllers/SettingsController.php b/app/Http/Controllers/SettingsController.phpindex 73d534dfec..448bf7825f 100644--- a/app/Http/Controllers/SettingsController.php+++ b/app/Http/Controllers/SettingsController.php@@ -1141,8 +1141,23 @@ class SettingsController extends Controller$instance->last_sync_result = $result;$instance->save();+ // at least one seen, no errors -> success (green)+ // at least one seen, some errors -> warning (orange, admin should check log)+ // nothing seen at all, at least one error -> error (red)+ // nothing seen AND no errors -> warning: vendor returned no rows,+ // which may be legitimate (empty inventory) but is also the+ // canary for a misconfigured pull that silently returned empty.+ // Warning flags it for the admin to notice without being a+ // hard error, so a genuinely empty vendor doesn't scream red.+ $flashType = match (true) {+ $seen > 0 && $errors === 0 => 'success',+ $seen > 0 && $errors > 0 => 'warning',+ $seen === 0 && $errors > 0 => 'error',+ default => 'warning',+ };+return redirect()->route('settings.adapters.index', ['adapter' => $instance->slug])- ->with('success', $result);+ ->with($flashType, $result);}/**@@ -1172,16 +1187,27 @@ class SettingsController extends Controller->with('error', trans('admin/settings/sync_adapters.not_configured'));}+ // Nothing to push means the admin has no fields directed 'push'+ // AND no composed-notes template + target configured. Iterating+ // asset rows in that state would just silent-no-op through+ // every one and flash a misleading "Pushed N assets" success.+ // Fail fast with a message that names the actual gap.+ if (! $adapter->hasPushConfiguration()) {+ return redirect()->route('settings.adapters.index', ['adapter' => $instance->slug])+ ->with('error', trans('admin/settings/sync_adapters.push_nothing_configured'));+ }+set_time_limit(0);$pushed = 0;+ $skipped = 0;$errors = 0;try {\App\Models\AssetExternalSource::query()->where('source', $instance->slug)->with('asset')- ->chunkById(200, function ($rows) use ($adapter, &$pushed, &$errors) {+ ->chunkById(200, function ($rows) use ($adapter, &$pushed, &$skipped, &$errors) {foreach ($rows as $row) {$asset = $row->asset;if ($asset === null) {@@ -1189,8 +1215,11 @@ class SettingsController extends Controller}try {- $adapter->push($asset);- $pushed++;+ if ($adapter->push($asset)) {+ $pushed++;+ } else {+ $skipped++;+ }} catch (\Throwable $e) {$errors++;// Log the sanitized summary (short) plus,@@ -1229,19 +1258,25 @@ class SettingsController extends Controller]));}- // all succeeded -> success (green)- // partial (some failed) -> warning (orange, admin should check log)- // all failed -> error (red)- // Message stays the same either way so admins see the count breakdown.+ // at least one push, no failures -> success (green)+ // at least one push, some failures -> warning (orange, admin should check log)+ // no pushes, at least one failure -> error (red)+ // no pushes AND no failures (every row skipped) -> error too, since the+ // button did nothing meaningful and the admin should know+ // nothing to iterate at all (no matching external_sources) -> warning+ // Message includes skipped count so admins see the full breakdown.$flashType = match (true) {$pushed > 0 && $errors === 0 => 'success',+ $pushed > 0 && $errors > 0 => 'warning',$pushed === 0 && $errors > 0 => 'error',+ $pushed === 0 && $errors === 0 && $skipped > 0 => 'error',default => 'warning',};return redirect()->route('settings.adapters.index', ['adapter' => $instance->slug])->with($flashType, trans('admin/settings/sync_adapters.push_complete', ['count' => $pushed,+ 'skipped' => $skipped,'errors' => $errors,]));}diff --git a/app/SyncAdapters/CustomHttp/CustomHttpAdapter.php b/app/SyncAdapters/CustomHttp/CustomHttpAdapter.phpindex b5bd408b86..0c4fa53c55 100644--- a/app/SyncAdapters/CustomHttp/CustomHttpAdapter.php+++ b/app/SyncAdapters/CustomHttp/CustomHttpAdapter.php@@ -428,16 +428,16 @@ class CustomHttpAdapter extends SyncAdapter implements PushableAdapter** @param array<int, string> $changedFields*/- public function push(Asset $asset, array $changedFields = []): void+ public function push(Asset $asset, array $changedFields = []): bool{$externalSource = $this->pushPrologue($asset, $changedFields);if ($externalSource === null || ! $this->canPush()) {- return;+ return false;}[$payload, $touched] = $this->buildPushPayload($asset);if ($payload === []) {- return;+ return false;}// Blank push_path is allowed. Combined with a non-blank base@@ -456,10 +456,12 @@ class CustomHttpAdapter extends SyncAdapter implements PushableAdapterjson_encode($payload, JSON_UNESCAPED_SLASHES),));- return;+ return true;}$this->dispatchPushRequest($method, $endpoint, $payload, $touched);++ return true;}/**diff --git a/app/SyncAdapters/Intune/IntuneAdapter.php b/app/SyncAdapters/Intune/IntuneAdapter.phpindex cd86eee3ae..9e2410b956 100644--- a/app/SyncAdapters/Intune/IntuneAdapter.php+++ b/app/SyncAdapters/Intune/IntuneAdapter.php@@ -186,16 +186,16 @@ class IntuneAdapter extends SyncAdapter implements PushableAdapter** @param array<int, string> $changedFields*/- public function push(Asset $asset, array $changedFields = []): void+ public function push(Asset $asset, array $changedFields = []): bool{$externalSource = $this->pushPrologue($asset, $changedFields);if ($externalSource === null) {- return;+ return false;}$composedNotes = $this->composeNotesForPush($asset);if ($composedNotes === null) {- return;+ return false;}$payload = [$composedNotes['target'] => $composedNotes['value']];@@ -208,7 +208,7 @@ class IntuneAdapter extends SyncAdapter implements PushableAdapterjson_encode($payload, JSON_UNESCAPED_SLASHES),));- return;+ return true;}$graphBaseUrl = $this->url();@@ -227,5 +227,7 @@ class IntuneAdapter extends SyncAdapter implements PushableAdapter$externalSource->external_id,$composedNotes['target'],));++ return true;}}diff --git a/app/SyncAdapters/Jamf/JamfAdapter.php b/app/SyncAdapters/Jamf/JamfAdapter.phpindex 05ef220e4a..2e3182d3b7 100644--- a/app/SyncAdapters/Jamf/JamfAdapter.php+++ b/app/SyncAdapters/Jamf/JamfAdapter.php@@ -166,11 +166,11 @@ class JamfAdapter extends SyncAdapter implements PushableAdapter** @param array<int, string> $changedFields*/- public function push(Asset $asset, array $changedFields = []): void+ public function push(Asset $asset, array $changedFields = []): bool{$externalSource = $this->pushPrologue($asset, $changedFields);if ($externalSource === null) {- return;+ return false;}$payload = [];@@ -193,7 +193,7 @@ class JamfAdapter extends SyncAdapter implements PushableAdapter$this->applyComposedNotesToPayload($asset, $payload, $touched);if ($payload === []) {- return;+ return false;}if ($this->isPushDryRun()) {@@ -204,7 +204,7 @@ class JamfAdapter extends SyncAdapter implements PushableAdapterjson_encode($payload, JSON_UNESCAPED_SLASHES),));- return;+ return true;}$client = new JamfClient(baseUrl: $this->url(), token: $this->credential('token'));@@ -216,6 +216,8 @@ class JamfAdapter extends SyncAdapter implements PushableAdapter$externalSource->external_id,implode(', ', $touched),));++ return true;}/**diff --git a/app/SyncAdapters/Kandji/KandjiAdapter.php b/app/SyncAdapters/Kandji/KandjiAdapter.phpindex edfe7ceada..b9de97c7c0 100644--- a/app/SyncAdapters/Kandji/KandjiAdapter.php+++ b/app/SyncAdapters/Kandji/KandjiAdapter.php@@ -173,11 +173,11 @@ class KandjiAdapter extends SyncAdapter implements PushableAdapter** @param array<int, string> $changedFields*/- public function push(Asset $asset, array $changedFields = []): void+ public function push(Asset $asset, array $changedFields = []): bool{$externalSource = $this->pushPrologue($asset, $changedFields);if ($externalSource === null) {- return;+ return false;}$payload = [];@@ -201,13 +201,15 @@ class KandjiAdapter extends SyncAdapter implements PushableAdapter$this->applyComposedNotesToPayload($asset, $payload);if ($payload === []) {- return;+ return false;}// Dry-run: log the payload we WOULD send and return without// hitting Kandji. Lets admins verify their config end-to-end// (mapping resolution, direction, credential wiring, external_id- // lookup) without a real API call.+ // lookup) without a real API call. Dry-run counts as a push+ // attempt from the controller's POV so the flash reflects+ // that admins actually did the thing they clicked.if ($this->isPushDryRun()) {Log::channel('sync-adapters')->info(sprintf('%s push [dry-run]: would PATCH Kandji device %s with %s',@@ -216,7 +218,7 @@ class KandjiAdapter extends SyncAdapter implements PushableAdapterjson_encode($payload, JSON_UNESCAPED_SLASHES),));- return;+ return true;}$client = new KandjiClient(baseUrl: $this->url(), token: $this->credential('token'));@@ -228,6 +230,8 @@ class KandjiAdapter extends SyncAdapter implements PushableAdapter$externalSource->external_id,implode(', ', array_keys($payload)),));++ return true;}/**diff --git a/app/SyncAdapters/Mosyle/MosyleAdapter.php b/app/SyncAdapters/Mosyle/MosyleAdapter.phpindex 3748292f0e..cce9e520a8 100644--- a/app/SyncAdapters/Mosyle/MosyleAdapter.php+++ b/app/SyncAdapters/Mosyle/MosyleAdapter.php@@ -164,11 +164,11 @@ class MosyleAdapter extends SyncAdapter implements PushableAdapter** @param array<int, string> $changedFields*/- public function push(Asset $asset, array $changedFields = []): void+ public function push(Asset $asset, array $changedFields = []): bool{$externalSource = $this->pushPrologue($asset, $changedFields);if ($externalSource === null) {- return;+ return false;}$serial = $asset->serial;@@ -186,8 +186,16 @@ class MosyleAdapter extends SyncAdapter implements PushableAdapter$this->pushComposedNotesField($asset, $serial, $client),);- if ($pushedFields === [] || $this->isPushDryRun()) {- return;+ if ($pushedFields === []) {+ return false;+ }++ if ($this->isPushDryRun()) {+ // Sub-helpers already logged their per-field dry-run lines.+ // Skip the summary line but count as attempted so the+ // controller flash reflects that admins actually did the+ // thing they clicked.+ return true;}Log::channel('sync-adapters')->info(sprintf(@@ -196,6 +204,8 @@ class MosyleAdapter extends SyncAdapter implements PushableAdapter$serial,implode(', ', $pushedFields),));++ return true;}/**diff --git a/app/SyncAdapters/NinjaOne/NinjaOneAdapter.php b/app/SyncAdapters/NinjaOne/NinjaOneAdapter.phpindex b2d6bc3e33..cb1b940a77 100644--- a/app/SyncAdapters/NinjaOne/NinjaOneAdapter.php+++ b/app/SyncAdapters/NinjaOne/NinjaOneAdapter.php@@ -183,11 +183,11 @@ class NinjaOneAdapter extends SyncAdapter implements PushableAdapter** @param array<int, string> $changedFields*/- public function push(Asset $asset, array $changedFields = []): void+ public function push(Asset $asset, array $changedFields = []): bool{$externalSource = $this->pushPrologue($asset, $changedFields);if ($externalSource === null) {- return;+ return false;}$customFieldName = $this->credentialOrNull('asset_tag_custom_field');@@ -198,12 +198,12 @@ class NinjaOneAdapter extends SyncAdapter implements PushableAdapter$asset->id,));- return;+ return false;}$payload = $this->buildCustomFieldPayload($asset, $customFieldName);if ($payload === []) {- return;+ return false;}if ($this->isPushDryRun()) {@@ -214,7 +214,7 @@ class NinjaOneAdapter extends SyncAdapter implements PushableAdapterjson_encode($payload, JSON_UNESCAPED_SLASHES),));- return;+ return true;}$client = new NinjaOneClient(@@ -230,6 +230,8 @@ class NinjaOneAdapter extends SyncAdapter implements PushableAdapter$externalSource->external_id,$customFieldName,));++ return true;}/**diff --git a/app/SyncAdapters/PushableAdapter.php b/app/SyncAdapters/PushableAdapter.phpindex d00a16fd68..e1a589d1c0 100644--- a/app/SyncAdapters/PushableAdapter.php+++ b/app/SyncAdapters/PushableAdapter.php@@ -57,6 +57,14 @@ interface PushableAdapter* row for this instance (never synced from here, so we have no* vendor-side id to write against).*+ * Return true when the adapter actually reached out to the vendor+ * (or logged a dry-run payload). Return false when the call was+ * skipped for any reason (nothing to push for this asset, no+ * external-source row, empty payload, etc.). The controller's+ * batch runner uses the return value to distinguish "successfully+ * pushed" from "skipped" so the flash message reflects reality+ * instead of counting every silent no-op as a success.+ ** @param array<int, string> $changedFields* Optional list of source-field names (hostname, asset_tag,* etc.) that changed and prompted this push. Implementations@@ -64,5 +72,5 @@ interface PushableAdapter* is in the changed set. When empty, treat as "push everything* marked push-direction".*/- public function push(Asset $asset, array $changedFields = []): void;+ public function push(Asset $asset, array $changedFields = []): bool;}diff --git a/app/SyncAdapters/SyncAdapter.php b/app/SyncAdapters/SyncAdapter.phpindex dfa22a4486..112ca916bb 100644--- a/app/SyncAdapters/SyncAdapter.php+++ b/app/SyncAdapters/SyncAdapter.php@@ -989,6 +989,20 @@ abstract class SyncAdapter];}+ /**+ * True when this instance has ANY push configuration in place+ * (at least one field directed 'push' OR a composed-notes+ * template + target). Used by the controller's Push Now handler+ * to abort with a clear error before iterating asset rows when+ * the admin hit the button on a fresh or incorrectly-configured+ * adapter, instead of iterating N rows that each silent-no-op+ * and reporting an inaccurate success.+ */+ public function hasPushConfiguration(): bool+ {+ return $this->pushDirectedFields() !== [] || $this->hasComposedNotesConfigured();+ }+/*** Cheap "should we bother trying to push composed notes?" check* used by pushPrologue to gate the vendor call without payingdiff --git a/app/SyncAdapters/WorkspaceOne/WorkspaceOneAdapter.php b/app/SyncAdapters/WorkspaceOne/WorkspaceOneAdapter.phpindex 02492e38c8..6ad058b10f 100644--- a/app/SyncAdapters/WorkspaceOne/WorkspaceOneAdapter.php+++ b/app/SyncAdapters/WorkspaceOne/WorkspaceOneAdapter.php@@ -194,23 +194,25 @@ class WorkspaceOneAdapter extends SyncAdapter implements PushableAdapter** @param array<int, string> $changedFields*/- public function push(Asset $asset, array $changedFields = []): void+ public function push(Asset $asset, array $changedFields = []): bool{$externalSource = $this->pushPrologue($asset, $changedFields);if ($externalSource === null) {- return;+ return false;}$payload = $this->buildDevicePayload($asset);$composedNotes = $this->composeNotesForPush($asset);if ($payload === [] && $composedNotes === null) {- return;+ return false;}$client = $this->isPushDryRun() ? null : $this->buildClient();$this->pushDeviceFields($externalSource->external_id, $payload, $client);$this->pushCustomAttribute($externalSource->external_id, $composedNotes, $client);++ return true;}/**diff --git a/resources/lang/en-US/admin/settings/sync_adapters.php b/resources/lang/en-US/admin/settings/sync_adapters.phpindex 5135fd938c..5d919b61cf 100644--- a/resources/lang/en-US/admin/settings/sync_adapters.php+++ b/resources/lang/en-US/admin/settings/sync_adapters.php@@ -8,9 +8,10 @@ return ['pull_now' => 'Pull Now','push_now' => 'Push Now','unsaved_changes_before_sync' => 'Save your configuration changes before syncing. Pull Now and Push Now use the last-saved settings, so unsaved edits would not take effect.',- 'push_complete' => 'Push complete. Pushed :count asset(s), :errors error(s).',+ 'push_complete' => 'Push complete. Pushed :count asset(s), skipped :skipped, :errors error(s).','push_failed' => 'Push failed: :summary. See storage/logs/sync-adapters.log for full details.','push_not_supported' => 'This adapter does not support pushing data to the vendor.',+ 'push_nothing_configured' => 'Push aborted: no fields on the mapping table are directed "Push to vendor" and no composed-notes template is configured. Set at least one before hitting Push Now.','not_configured' => 'This adapter has not been configured yet.','sync_complete' => 'Sync complete. Synced :count host(s), :errors error(s).','sync_failed' => 'Sync failed: :summary. See storage/logs/sync-adapters.log for full details.',