Files

55 lines
1.4 KiB
PHP
Raw Permalink Normal View History

2018-08-21 21:47:01 -07:00
<?php
2024-03-12 22:39:16 -04:00
namespace App\Transformers\Api\Client;
2018-08-21 21:47:01 -07:00
use App\Enums\SubuserPermission;
2024-03-12 22:39:16 -04:00
use App\Models\Database;
2025-09-24 13:34:19 +02:00
use League\Fractal\Resource\Item;
use League\Fractal\Resource\NullResource;
2018-08-21 21:47:01 -07:00
class DatabaseTransformer extends BaseClientTransformer
{
protected array $availableIncludes = ['password'];
2018-08-21 21:47:01 -07:00
public function getResourceName(): string
{
return Database::RESOURCE_NAME;
}
/**
* @param Database $model
*/
public function transform($model): array
2018-08-21 21:47:01 -07:00
{
$model->loadMissing('host');
return [
2024-05-30 00:41:44 +02:00
'id' => $model->id,
2018-08-21 21:47:01 -07:00
'host' => [
'address' => $model->getRelation('host')->host,
'port' => $model->getRelation('host')->port,
],
'name' => $model->database,
'username' => $model->username,
'connections_from' => $model->remote,
'max_connections' => $model->max_connections,
2018-08-21 21:47:01 -07:00
];
}
/**
* Include the database password in the request.
*/
public function includePassword(Database $database): Item|NullResource
2018-08-21 21:47:01 -07:00
{
if (!$this->request->user()->can(SubuserPermission::DatabaseViewPassword, $database->server)) {
return $this->null();
}
return $this->item($database, function (Database $model) {
2018-08-21 21:47:01 -07:00
return [
'password' => $model->password,
2018-08-21 21:47:01 -07:00
];
}, 'database_password');
}
}