fix(settings): force can_edit=1 for runtime keys in API and DB writes

editable_only now includes RUNTIME_KEYS; normalize can_edit in responses.
UpdateConfig and montzar login persist can_edit=1. SQL override moved before COMMIT.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-19 20:15:28 +03:30
co-authored by Cursor
parent 027170a33f
commit b7eed369c6
4 changed files with 66 additions and 8 deletions
@@ -37,10 +37,10 @@ class SettingsController extends Controller
} }
if ($request->boolean('editable_only')) { if ($request->boolean('editable_only')) {
$local = $query->get();
return response()->json($this->authorizeSettingsPayload( return response()->json($this->authorizeSettingsPayload(
$query->where(function ($q) { $this->composeEditableSettings($local)
$q->where('can_edit', 1)->orWhere('can_edit', true)->orWhere('can_edit', '1');
})->get()
)); ));
} }
@@ -171,6 +171,12 @@ class SettingsController extends Controller
if (! is_array($item)) { if (! is_array($item)) {
continue; continue;
} }
if (
SuperAdminProgramSettingsOverlay::isRuntime((string) $key)
|| SuperAdminProgramSettingsOverlay::isLocalOwned((string) $key)
) {
continue;
}
$byKey[(string) $key] = $this->overlayRowToSetting($item); $byKey[(string) $key] = $this->overlayRowToSetting($item);
} }
} else { } else {
@@ -187,13 +193,31 @@ class SettingsController extends Controller
SuperAdminProgramSettingsOverlay::isLocalOwned($key) SuperAdminProgramSettingsOverlay::isLocalOwned($key)
|| SuperAdminProgramSettingsOverlay::isRuntime($key) || SuperAdminProgramSettingsOverlay::isRuntime($key)
) { ) {
$byKey[$key] = $row; $byKey[$key] = $this->normalizeTenantOwnedRow($row);
} }
} }
return collect(array_values($byKey))->sortBy(fn ($row) => (string) ($row->key ?? ''))->values(); 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. * get_settings: کلیدهای سوپرادمین (can_edit=0) + ردیف‌های جدول لوکال با can_edit=1.
* اگر سوپرادمین در دسترس نباشد کل جدول لوکال برمی‌گردد. * اگر سوپرادمین در دسترس نباشد کل جدول لوکال برمی‌گردد.
@@ -211,7 +235,7 @@ class SettingsController extends Controller
foreach ($localRows as $row) { foreach ($localRows as $row) {
if ($this->isEditableCanEdit($row->can_edit ?? null)) { 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)) { if (! is_array($item)) {
continue; continue;
} }
if (
SuperAdminProgramSettingsOverlay::isRuntime((string) $key)
|| SuperAdminProgramSettingsOverlay::isLocalOwned((string) $key)
) {
continue;
}
$byKey[(string) $key] = $this->overlayRowToSetting($item); $byKey[(string) $key] = $this->overlayRowToSetting($item);
} }
@@ -228,7 +258,7 @@ class SettingsController extends Controller
SuperAdminProgramSettingsOverlay::isLocalOwned($key) SuperAdminProgramSettingsOverlay::isLocalOwned($key)
|| SuperAdminProgramSettingsOverlay::isRuntime($key) || SuperAdminProgramSettingsOverlay::isRuntime($key)
) { ) {
$byKey[$key] = $row; $byKey[$key] = $this->normalizeTenantOwnedRow($row);
} }
} }
@@ -271,6 +301,20 @@ class SettingsController extends Controller
return $setting; 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 ادمین) هرگز secret/credential نمی‌بیند.
* پنل ادمین با auth:user همه کلیدها را می‌بیند. * پنل ادمین با auth:user همه کلیدها را می‌بیند.
+6 -1
View File
@@ -444,9 +444,14 @@ class ConfigService
public function UpdateConfig($name, $value) public function UpdateConfig($name, $value)
{ {
$payload = ['value' => $value];
if (SuperAdminProgramSettingsOverlay::isRuntime((string) $name)) {
$payload['can_edit'] = 1;
}
return Setting::updateOrCreate( return Setting::updateOrCreate(
['key' => $name], ['key' => $name],
['value' => $value] $payload
); );
} }
+1 -1
View File
@@ -71,7 +71,7 @@ class MontzarService
Setting::updateOrCreate( Setting::updateOrCreate(
['key' => 'montzar_token'], ['key' => 'montzar_token'],
['value' => $token, 'is_active' => 1] ['value' => $token, 'is_active' => 1, 'can_edit' => 1]
); );
return $token; return $token;
+9
View File
@@ -2159,6 +2159,15 @@ WHERE `key` = 'accounting_provider'
AND TRIM(COALESCE(`value`, '')) IN ('', 'none') 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')); 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; COMMIT;
SELECT 'deploy_settings_const.sql finished' AS status; SELECT 'deploy_settings_const.sql finished' AS status;