Files
boursecu_server/app/Models/Setting.php
T

121 lines
3.4 KiB
PHP
Raw Normal View History

2026-01-24 14:03:11 +00:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Hash;
2026-06-04 14:19:54 +03:30
use Illuminate\Support\Facades\Schema;
use Rooyesh\Wallet\models\Transaction;
use Tymon\JWTAuth\Contracts\JWTSubject;
2026-01-24 14:03:11 +00:00
class Setting extends Model
{
use HasFactory,Selector;
2026-06-04 14:19:54 +03:30
private static ?bool $typeColumnExists = null;
public static function hasTypeColumn(): bool
{
if (static::$typeColumnExists !== null) {
return static::$typeColumnExists;
}
try {
static::$typeColumnExists = Schema::hasColumn((new static)->getTable(), 'type');
} catch (\Throwable) {
static::$typeColumnExists = false;
}
return static::$typeColumnExists;
}
/** @return array<int, string> */
public static function adminSelectColumns(): array
{
2026-06-04 16:01:13 +03:30
$cols = ['id', 'title_fa', 'key', 'value', 'description', 'is_active', 'can_edit'];
2026-06-04 14:19:54 +03:30
if (static::hasTypeColumn()) {
$cols[] = 'type';
}
return $cols;
}
2026-07-24 16:50:21 +03:30
/**
* کلیدهایی که هرگز نباید در پاسخ عمومی api/get_settings برگردند.
* credential درگاه، توکن‌ها، رمزها و پیکربندی داخلی پرداخت.
*/
public static function isSecretOrServerOnlyKey(string $key): bool
{
$key = strtolower(trim($key));
if ($key === '') {
return false;
}
// کل گروه پیکربندی پرداخت آنلاین — فقط سرور / ادمین
if (
str_starts_with($key, 'external_payment_')
|| $key === 'wallet_payment_provider'
|| str_starts_with($key, 'payping_')
) {
return true;
}
$exact = [
'node_client_id',
'node_shared_secret',
'montzar_password',
'montzar_token',
'montzar_mobile',
'bale_bot_token',
'telegram_bot_token',
'rubika_bot_token',
'tahesab_token',
'tahesab_host',
'tahesab_port',
'tahesab_db',
'pargar_password',
'pargar_api_token',
'pargar_username',
'zarin_api',
'zarin_token',
2026-08-05 12:56:52 +03:30
'chat_webhook_secret',
'chat_webhook_base_url',
2026-07-24 16:50:21 +03:30
];
if (in_array($key, $exact, true)) {
return true;
}
return (bool) preg_match('/(^|_)(secret|password|token|api_key|apikey|private)(_|$)/', $key);
}
/**
* فیلتر تنظیمات برای پاسخ عمومی (اپ کاربر).
*
* @param \Illuminate\Support\Collection|iterable $settings
* @return \Illuminate\Support\Collection
*/
public static function forPublicApi($settings)
{
return collect($settings)
->filter(fn ($row) => ! static::isSecretOrServerOnlyKey((string) (is_array($row) ? ($row['key'] ?? '') : ($row->key ?? ''))))
->values();
}
2026-01-24 14:03:11 +00:00
protected $fillable = [
'key',
'title_fa',
'is_active',
'description',
2026-01-24 14:03:11 +00:00
'value',
2026-06-04 14:19:54 +03:30
'value_json',
'type',
2026-08-04 18:59:38 +03:30
'can_edit',
2026-01-24 14:03:11 +00:00
];
protected $casts = [
'value_json'=>'array',
'is_active'=>'boolean',
2026-01-24 14:03:11 +00:00
];
}