feat(settings): add menu/show_to_user and public get_settings for client apps

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-24 15:32:35 +03:30
co-authored by Cursor
parent 5c542e2733
commit b28a98938e
9 changed files with 321 additions and 44 deletions
+71 -12
View File
@@ -14,21 +14,35 @@ class Setting extends Model
{
use HasFactory,Selector;
private static ?bool $typeColumnExists = null;
/** @var array<string, bool> */
private static array $columnExists = [];
public static function hasSettingsColumn(string $column): bool
{
if (! isset(static::$columnExists[$column])) {
try {
static::$columnExists[$column] = Schema::hasColumn((new static)->getTable(), $column);
} catch (\Throwable) {
static::$columnExists[$column] = false;
}
}
return static::$columnExists[$column];
}
public static function hasTypeColumn(): bool
{
if (static::$typeColumnExists !== null) {
return static::$typeColumnExists;
}
return static::hasSettingsColumn('type');
}
try {
static::$typeColumnExists = Schema::hasColumn((new static)->getTable(), 'type');
} catch (\Throwable) {
static::$typeColumnExists = false;
}
public static function hasMenuColumn(): bool
{
return static::hasSettingsColumn('menu');
}
return static::$typeColumnExists;
public static function hasShowToUserColumn(): bool
{
return static::hasSettingsColumn('show_to_user');
}
/** @return array<int, string> */
@@ -38,6 +52,12 @@ class Setting extends Model
if (static::hasTypeColumn()) {
$cols[] = 'type';
}
if (static::hasMenuColumn()) {
$cols[] = 'menu';
}
if (static::hasShowToUserColumn()) {
$cols[] = 'show_to_user';
}
return $cols;
}
@@ -90,16 +110,52 @@ class Setting extends Model
return (bool) preg_match('/(^|_)(secret|password|token|api_key|apikey|private)(_|$)/', $key);
}
public static function isShownToUserApp($row): bool
{
$key = (string) (is_array($row) ? ($row['key'] ?? '') : ($row->key ?? ''));
if ($key === '' || static::isSecretOrServerOnlyKey($key)) {
return false;
}
$flag = is_array($row) ? ($row['show_to_user'] ?? 0) : ($row->show_to_user ?? 0);
return $flag === true || $flag === 1 || $flag === '1';
}
/**
* فیلتر تنظیمات برای پاسخ عمومی (اپ کاربر).
* فیلتر تنظیمات برای پاسخ عمومی (اپ کاربر) — ستون show_to_user، بدون secret.
*
* @param \Illuminate\Support\Collection|iterable $settings
* @return \Illuminate\Support\Collection
*/
public static function forPublicApi($settings)
{
return static::forClientApi($settings);
}
/**
* @param \Illuminate\Support\Collection|iterable $settings
* @return \Illuminate\Support\Collection<int, array{key: string, value: mixed, is_active: mixed}>
*/
public static function forClientApi($settings)
{
return collect($settings)
->filter(fn ($row) => ! static::isSecretOrServerOnlyKey((string) (is_array($row) ? ($row['key'] ?? '') : ($row->key ?? ''))))
->filter(fn ($row) => static::isShownToUserApp($row))
->map(function ($row) {
if (is_array($row)) {
return [
'key' => (string) ($row['key'] ?? ''),
'value' => $row['value'] ?? '',
'is_active' => $row['is_active'] ?? 1,
];
}
return [
'key' => (string) ($row->key ?? ''),
'value' => $row->value ?? '',
'is_active' => $row->is_active ?? 1,
];
})
->values();
}
@@ -112,9 +168,12 @@ class Setting extends Model
'value_json',
'type',
'can_edit',
'menu',
'show_to_user',
];
protected $casts = [
'value_json'=>'array',
'is_active'=>'boolean',
'show_to_user'=>'boolean',
];
}