69 lines
1.7 KiB
PHP
69 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Settings;
|
|
|
|
use App\Utilities\UpstreamCredentialCipher;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Crypt;
|
|
|
|
class DatabaseConnection extends Model
|
|
{
|
|
protected $table = 'system_database_connections';
|
|
|
|
protected $fillable = [
|
|
'company_id',
|
|
'source_key',
|
|
'name',
|
|
'driver',
|
|
'host',
|
|
'port',
|
|
'database_name',
|
|
'username',
|
|
'password',
|
|
'odbc_driver',
|
|
'tds_version',
|
|
'connect_timeout',
|
|
'query_timeout',
|
|
'allowed_scopes',
|
|
'charset',
|
|
'enabled',
|
|
];
|
|
|
|
protected $casts = [
|
|
'allowed_scopes' => 'array',
|
|
'enabled' => 'boolean',
|
|
];
|
|
|
|
protected $hidden = ['password', 'shared_password'];
|
|
|
|
public function getPasswordAttribute(?string $value): ?string
|
|
{
|
|
$shared = (string) ($this->attributes['shared_password'] ?? '');
|
|
|
|
if ($shared !== '') {
|
|
return app(UpstreamCredentialCipher::class)->decrypt($shared);
|
|
}
|
|
|
|
return $value === null ? null : Crypt::decryptString($value);
|
|
}
|
|
|
|
public function setPasswordAttribute(?string $value): void
|
|
{
|
|
if ($value === null || $value === '') {
|
|
$this->attributes['password'] = null;
|
|
$this->attributes['shared_password'] = null;
|
|
|
|
return;
|
|
}
|
|
|
|
// 同时保留 APP_KEY 旧密文,确保旧版本和紧急回滚仍可使用。
|
|
$this->attributes['password'] = Crypt::encryptString($value);
|
|
$this->attributes['shared_password'] = app(UpstreamCredentialCipher::class)->encrypt($value);
|
|
}
|
|
|
|
public function resolvedPassword(): ?string
|
|
{
|
|
return $this->password;
|
|
}
|
|
}
|