feat(server): overlay admin get_settings from SuperAdmin program-settings
Fetch public program-settings with 3-origin retry and no Laravel cache so local DB settings stay in sync with the sold version. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -5,6 +5,7 @@ namespace App\Http\Controllers\Admin;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Setting;
|
||||
use App\Services\PriceChannelBroadcastService;
|
||||
use App\Services\SuperAdminProgramSettingsOverlay;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class SettingsController extends Controller
|
||||
@@ -19,8 +20,6 @@ class SettingsController extends Controller
|
||||
*/
|
||||
private const PANEL_MANAGED_KEYS = [
|
||||
'tracking_systems',
|
||||
'tracking_systems_enabled',
|
||||
'banner_enabled',
|
||||
];
|
||||
|
||||
public function index(Request $request)
|
||||
@@ -30,6 +29,11 @@ class SettingsController extends Controller
|
||||
->orderBy('key');
|
||||
|
||||
if ($request->boolean('system_only')) {
|
||||
$overlay = app(SuperAdminProgramSettingsOverlay::class)->items();
|
||||
if ($overlay) {
|
||||
return response()->json($this->authorizeSettingsPayload($this->overlayToRows($overlay)));
|
||||
}
|
||||
|
||||
$rows = $query
|
||||
->where(function ($q) {
|
||||
$q->where('can_edit', 0)->orWhere('can_edit', false);
|
||||
@@ -48,26 +52,30 @@ class SettingsController extends Controller
|
||||
return response()->json($this->authorizeSettingsPayload($query->where('can_edit', 1)->get()));
|
||||
}
|
||||
|
||||
return response()->json($this->authorizeSettingsPayload($query->get()));
|
||||
return response()->json($this->authorizeSettingsPayload(
|
||||
$this->mergeOverlayIntoLocal($query->get())
|
||||
));
|
||||
}
|
||||
|
||||
public function edit(Request $request)
|
||||
{
|
||||
$isDeployAdmin = $this->isDeployAdminUser();
|
||||
$isSystemDeploy = $request->boolean('system_deploy');
|
||||
$setting = Setting::where('key', $request->key)->first();
|
||||
$isNew = !$setting;
|
||||
|
||||
if ($isNew) {
|
||||
if ($isSystemDeploy && !$isDeployAdmin) {
|
||||
return response()->json(['message' => 'Unauthorized'], 403);
|
||||
if ($request->boolean('system_deploy')) {
|
||||
return response()->json([
|
||||
'message' => 'تنظیمات پیشرفته فقط از سوپرادمین اضافه میشوند',
|
||||
], 403);
|
||||
}
|
||||
|
||||
$setting = new Setting();
|
||||
$setting->key = $request->key;
|
||||
$setting->can_edit = $isSystemDeploy ? 0 : 1;
|
||||
} elseif (!$setting->can_edit && !$isDeployAdmin && !$this->isPanelManagedKey((string) $request->key)) {
|
||||
return response()->json(['message' => 'Forbidden'], 403);
|
||||
$setting->can_edit = 1;
|
||||
} elseif (!$setting->can_edit && !$this->isPanelManagedKey((string) $request->key)) {
|
||||
return response()->json([
|
||||
'message' => 'این تنظیم از سوپرادمین کنترل میشود و در پنل خانگی قابل ویرایش نیست',
|
||||
], 403);
|
||||
}
|
||||
|
||||
$previousValue = $setting->exists ? (string) ($setting->value ?? '') : null;
|
||||
@@ -94,7 +102,7 @@ class SettingsController extends Controller
|
||||
})->afterResponse();
|
||||
}
|
||||
|
||||
if ($isNew && $isSystemDeploy) {
|
||||
if ($isNew) {
|
||||
$projectName = Setting::where('key', 'name_fa')->value('value') ?: 'پروژه';
|
||||
$settingTitle = trim($request->title_fa ?? '') ?: $request->key;
|
||||
$this->Send(
|
||||
@@ -135,6 +143,71 @@ class SettingsController extends Controller
|
||||
return in_array($key, self::PANEL_MANAGED_KEYS, true);
|
||||
}
|
||||
|
||||
private function mergeOverlayIntoLocal($rows)
|
||||
{
|
||||
$overlay = app(SuperAdminProgramSettingsOverlay::class)->items();
|
||||
if (! $overlay) {
|
||||
return $rows;
|
||||
}
|
||||
|
||||
$byKey = [];
|
||||
foreach ($rows as $row) {
|
||||
$byKey[(string) $row->key] = $row;
|
||||
}
|
||||
|
||||
foreach ($overlay as $item) {
|
||||
$key = trim((string) ($item['key'] ?? ''));
|
||||
if ($key === '' || SuperAdminProgramSettingsOverlay::isLocalOwned($key)) {
|
||||
continue;
|
||||
}
|
||||
if (isset($byKey[$key])) {
|
||||
$byKey[$key]->value = $item['value'] ?? $byKey[$key]->value;
|
||||
if (array_key_exists('title_fa', $item) && $item['title_fa'] !== null && $item['title_fa'] !== '') {
|
||||
$byKey[$key]->title_fa = $item['title_fa'];
|
||||
}
|
||||
if (array_key_exists('description', $item) && $item['description'] !== null) {
|
||||
$byKey[$key]->description = $item['description'];
|
||||
}
|
||||
if (array_key_exists('type', $item) && $item['type'] !== null && $item['type'] !== '') {
|
||||
$byKey[$key]->type = $item['type'];
|
||||
}
|
||||
if (array_key_exists('is_active', $item)) {
|
||||
$byKey[$key]->is_active = $item['is_active'];
|
||||
}
|
||||
} else {
|
||||
$byKey[$key] = $this->overlayRowToSetting($item);
|
||||
}
|
||||
}
|
||||
|
||||
return collect(array_values($byKey));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, array<string, mixed>> $overlay
|
||||
*/
|
||||
private function overlayToRows(array $overlay)
|
||||
{
|
||||
return collect($overlay)
|
||||
->filter(fn ($item) => is_array($item) && trim((string) ($item['key'] ?? '')) !== '')
|
||||
->map(fn ($item) => $this->overlayRowToSetting($item))
|
||||
->values();
|
||||
}
|
||||
|
||||
private function overlayRowToSetting(array $item): Setting
|
||||
{
|
||||
$setting = new Setting();
|
||||
$setting->id = isset($item['id']) && is_numeric($item['id']) ? (int) $item['id'] : null;
|
||||
$setting->key = (string) ($item['key'] ?? '');
|
||||
$setting->value = $item['value'] ?? '';
|
||||
$setting->title_fa = $item['title_fa'] ?? '';
|
||||
$setting->description = $item['description'] ?? '';
|
||||
$setting->type = $item['type'] ?? '';
|
||||
$setting->is_active = $item['is_active'] ?? 1;
|
||||
$setting->can_edit = 0;
|
||||
|
||||
return $setting;
|
||||
}
|
||||
|
||||
/**
|
||||
* اپ کاربر (بدون auth ادمین) هرگز secret/credential نمیبیند.
|
||||
* پنل ادمین با auth:user همه کلیدها را میبیند.
|
||||
|
||||
Reference in New Issue
Block a user