diff --git a/app/Http/Controllers/Admin/SettingsController.php b/app/Http/Controllers/Admin/SettingsController.php index 054088e..507b11f 100644 --- a/app/Http/Controllers/Admin/SettingsController.php +++ b/app/Http/Controllers/Admin/SettingsController.php @@ -37,10 +37,10 @@ class SettingsController extends Controller } if ($request->boolean('editable_only')) { + $local = $query->get(); + return response()->json($this->authorizeSettingsPayload( - $query->where(function ($q) { - $q->where('can_edit', 1)->orWhere('can_edit', true)->orWhere('can_edit', '1'); - })->get() + $this->composeEditableSettings($local) )); } @@ -171,6 +171,12 @@ class SettingsController extends Controller if (! is_array($item)) { continue; } + if ( + SuperAdminProgramSettingsOverlay::isRuntime((string) $key) + || SuperAdminProgramSettingsOverlay::isLocalOwned((string) $key) + ) { + continue; + } $byKey[(string) $key] = $this->overlayRowToSetting($item); } } else { @@ -187,13 +193,31 @@ class SettingsController extends Controller SuperAdminProgramSettingsOverlay::isLocalOwned($key) || SuperAdminProgramSettingsOverlay::isRuntime($key) ) { - $byKey[$key] = $row; + $byKey[$key] = $this->normalizeTenantOwnedRow($row); } } return collect(array_values($byKey))->sortBy(fn ($row) => (string) ($row->key ?? ''))->values(); } + /** + * تنظیمات عادی پنل — can_edit=1 در DB + کلیدهای runtime (کد tenant می‌نویسد). + */ + private function composeEditableSettings($localRows) + { + $rows = collect($localRows)->filter(function ($row) { + $key = (string) ($row->key ?? ''); + + return $this->isEditableCanEdit($row->can_edit ?? null) + || SuperAdminProgramSettingsOverlay::isRuntime($key); + }); + + return $rows + ->map(fn ($row) => $this->normalizeTenantOwnedRow($row)) + ->sortBy(fn ($row) => (string) ($row->key ?? '')) + ->values(); + } + /** * get_settings: کلیدهای سوپرادمین (can_edit=0) + ردیف‌های جدول لوکال با can_edit=1. * اگر سوپرادمین در دسترس نباشد کل جدول لوکال برمی‌گردد. @@ -211,7 +235,7 @@ class SettingsController extends Controller foreach ($localRows as $row) { if ($this->isEditableCanEdit($row->can_edit ?? null)) { - $byKey[(string) $row->key] = $row; + $byKey[(string) $row->key] = $this->normalizeTenantOwnedRow($row); } } @@ -219,6 +243,12 @@ class SettingsController extends Controller if (! is_array($item)) { continue; } + if ( + SuperAdminProgramSettingsOverlay::isRuntime((string) $key) + || SuperAdminProgramSettingsOverlay::isLocalOwned((string) $key) + ) { + continue; + } $byKey[(string) $key] = $this->overlayRowToSetting($item); } @@ -228,7 +258,7 @@ class SettingsController extends Controller SuperAdminProgramSettingsOverlay::isLocalOwned($key) || SuperAdminProgramSettingsOverlay::isRuntime($key) ) { - $byKey[$key] = $row; + $byKey[$key] = $this->normalizeTenantOwnedRow($row); } } @@ -271,6 +301,20 @@ class SettingsController extends Controller return $setting; } + /** کلیدهای tenant-only — can_edit=1 در پاسخ API حتی اگر DB قدیمی 0 باشد */ + private function normalizeTenantOwnedRow(Setting $row): Setting + { + $key = (string) ($row->key ?? ''); + if ( + SuperAdminProgramSettingsOverlay::isRuntime($key) + || SuperAdminProgramSettingsOverlay::isLocalOwned($key) + ) { + $row->can_edit = 1; + } + + return $row; + } + /** * اپ کاربر (بدون auth ادمین) هرگز secret/credential نمی‌بیند. * پنل ادمین با auth:user همه کلیدها را می‌بیند. diff --git a/app/Services/ConfigService.php b/app/Services/ConfigService.php index 029a434..bca9a1a 100644 --- a/app/Services/ConfigService.php +++ b/app/Services/ConfigService.php @@ -444,9 +444,14 @@ class ConfigService public function UpdateConfig($name, $value) { + $payload = ['value' => $value]; + if (SuperAdminProgramSettingsOverlay::isRuntime((string) $name)) { + $payload['can_edit'] = 1; + } + return Setting::updateOrCreate( ['key' => $name], - ['value' => $value] + $payload ); } diff --git a/app/Services/MontzarService.php b/app/Services/MontzarService.php index 127d233..2d3e3cd 100644 --- a/app/Services/MontzarService.php +++ b/app/Services/MontzarService.php @@ -71,7 +71,7 @@ class MontzarService Setting::updateOrCreate( ['key' => 'montzar_token'], - ['value' => $token, 'is_active' => 1] + ['value' => $token, 'is_active' => 1, 'can_edit' => 1] ); return $token; diff --git a/database/sql/deploy_settings_const.sql b/database/sql/deploy_settings_const.sql index 2830664..96d807b 100644 --- a/database/sql/deploy_settings_const.sql +++ b/database/sql/deploy_settings_const.sql @@ -2159,6 +2159,15 @@ WHERE `key` = 'accounting_provider' AND TRIM(COALESCE(`value`, '')) IN ('', 'none') AND EXISTS (SELECT 1 FROM (SELECT `value` FROM `settings` WHERE `key` = 'send_to_tahesab') AS s WHERE s.`value` IN ('true', '1', 'yes')); +-- آخرین override: کلیدهای runtime همیشه can_edit=1 (tenant-only) +UPDATE `settings` SET `can_edit` = 1 WHERE `key` IN ( + 'montzar_last_product_log', 'montzar_order_response', + 'montzar_live_price', 'montzar_mazaneh', 'montzar_diff', + 'montzar_truly_buy_mazaneh', 'montzar_truly_sell_mazaneh', + 'montzar_token', 'montzar_closed', + 'up_price', 'down_price', 'price_extreme_date', 'do_send_liman' +); + COMMIT; SELECT 'deploy_settings_const.sql finished' AS status;