Files
panel/app/Transformers/Api/Client/DatabaseTransformer.php

63 lines
1.6 KiB
PHP
Raw 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
2024-03-12 22:39:16 -04:00
use App\Models\Database;
use League\Fractal\Resource\Item;
2024-03-12 22:39:16 -04:00
use App\Models\Permission;
use League\Fractal\Resource\NullResource;
2024-03-12 22:39:16 -04:00
use App\Contracts\Extensions\HashidsInterface;
2018-08-21 21:47:01 -07:00
class DatabaseTransformer extends BaseClientTransformer
{
protected array $availableIncludes = ['password'];
2018-08-21 21:47:01 -07:00
private HashidsInterface $hashids;
2018-08-25 14:43:21 -07:00
2018-08-21 21:47:01 -07:00
/**
* Handle dependency injection.
*/
2024-03-19 05:11:41 -04:00
public function handle(HashidsInterface $hashids)
2018-08-21 21:47:01 -07:00
{
2018-08-25 14:43:21 -07:00
$this->hashids = $hashids;
2018-08-21 21:47:01 -07:00
}
public function getResourceName(): string
{
return Database::RESOURCE_NAME;
}
public function transform(Database $model): array
{
$model->loadMissing('host');
return [
2018-08-25 14:43:21 -07:00
'id' => $this->hashids->encode($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(Permission::ACTION_DATABASE_VIEW_PASSWORD, $database->server)) {
return $this->null();
}
return $this->item($database, function (Database $model) {
2018-08-21 21:47:01 -07:00
return [
2024-03-19 05:11:41 -04:00
'password' => decrypt($model->password),
2018-08-21 21:47:01 -07:00
];
}, 'database_password');
}
}