129 lines
3.6 KiB
PHP
129 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Libraries\Encrypter;
|
|
use CodeIgniter\Model;
|
|
|
|
class DataSourceModel extends Model
|
|
{
|
|
protected $table = 'data_sources';
|
|
protected $primaryKey = 'id';
|
|
protected $useAutoIncrement = true;
|
|
protected $returnType = 'array';
|
|
protected $useSoftDeletes = true;
|
|
protected $protectFields = true;
|
|
|
|
protected $allowedFields = [
|
|
'workspace_id',
|
|
'name',
|
|
'type',
|
|
'host',
|
|
'port',
|
|
'database_name',
|
|
'username',
|
|
'password',
|
|
'connection_uri',
|
|
'ssl_enabled',
|
|
'ssl_ca',
|
|
'api_base_url',
|
|
'api_method',
|
|
'api_auth_type',
|
|
'api_auth_value',
|
|
'api_headers',
|
|
'csv_file_path',
|
|
'csv_delimiter',
|
|
'last_tested_at',
|
|
'status',
|
|
'error_message',
|
|
'created_by',
|
|
];
|
|
|
|
protected bool $allowEmptyInserts = false;
|
|
protected bool $updateOnlyChanged = true;
|
|
|
|
protected $useTimestamps = true;
|
|
protected $dateFormat = 'datetime';
|
|
protected $createdField = 'created_at';
|
|
protected $updatedField = 'updated_at';
|
|
protected $deletedField = 'deleted_at';
|
|
|
|
protected $validationRules = [
|
|
'workspace_id' => 'required|integer',
|
|
'name' => 'required|min_length[3]|max_length[150]',
|
|
'type' => 'required|in_list[mysql,postgresql,mongodb,rest_api,csv]',
|
|
'status' => 'permit_empty|in_list[untested,connected,failed]',
|
|
'created_by' => 'required|integer',
|
|
];
|
|
|
|
protected $allowCallbacks = true;
|
|
protected $beforeInsert = ['encryptSensitiveFields'];
|
|
protected $beforeUpdate = ['encryptSensitiveFields'];
|
|
protected $afterFind = ['decryptSensitiveFields'];
|
|
|
|
public function forWorkspace(int $workspaceId): array
|
|
{
|
|
return $this->where('workspace_id', $workspaceId)
|
|
->orderBy('id', 'DESC')
|
|
->findAll();
|
|
}
|
|
|
|
protected function encryptSensitiveFields(array $data): array
|
|
{
|
|
if (! isset($data['data']) || ! is_array($data['data'])) {
|
|
return $data;
|
|
}
|
|
|
|
$encrypter = new Encrypter();
|
|
foreach (['password', 'api_auth_value'] as $field) {
|
|
if (! array_key_exists($field, $data['data'])) {
|
|
continue;
|
|
}
|
|
|
|
$value = trim((string) $data['data'][$field]);
|
|
if ($value === '') {
|
|
continue;
|
|
}
|
|
|
|
$data['data'][$field] = $encrypter->encrypt($value);
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
protected function decryptSensitiveFields(array $data): array
|
|
{
|
|
if (! isset($data['data'])) {
|
|
return $data;
|
|
}
|
|
|
|
$decryptRecord = static function (array $row): array {
|
|
$encrypter = new Encrypter();
|
|
foreach (['password', 'api_auth_value'] as $field) {
|
|
if (! isset($row[$field]) || $row[$field] === null || $row[$field] === '') {
|
|
continue;
|
|
}
|
|
|
|
$row[$field] = $encrypter->decrypt((string) $row[$field]);
|
|
}
|
|
|
|
return $row;
|
|
};
|
|
|
|
if (($data['singleton'] ?? false) === true) {
|
|
$data['data'] = is_array($data['data']) ? $decryptRecord($data['data']) : $data['data'];
|
|
return $data;
|
|
}
|
|
|
|
if (is_array($data['data'])) {
|
|
foreach ($data['data'] as $key => $row) {
|
|
if (is_array($row)) {
|
|
$data['data'][$key] = $decryptRecord($row);
|
|
}
|
|
}
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
}
|