Boy132 b3501be6ec
Refactor api key permissions (#361)
* use RESOURCE_NAME for requests

* use RESOURCE_NAME for transformers

* add permissions field to api key

* add migration for new permissions field

* update tests

* remove debug log

* set column type to "json"

* remove default attribute to fix tests

* fix default value for permissions

* fix after merge

* fix after merge

* allow to "register" custom permissions

* add "role" to default resource names

* fix after merge

* fix phpstan

* fix migrations
2024-11-06 09:09:10 +01:00

77 lines
2.0 KiB
PHP

<?php
namespace App\Transformers\Api\Application;
use App\Models\Node;
use App\Models\Server;
use League\Fractal\Resource\Item;
use App\Models\Allocation;
use League\Fractal\Resource\NullResource;
class AllocationTransformer extends BaseTransformer
{
/**
* Relationships that can be loaded onto allocation transformations.
*/
protected array $availableIncludes = ['node', 'server'];
/**
* Return the resource name for the JSONAPI output.
*/
public function getResourceName(): string
{
return Allocation::RESOURCE_NAME;
}
/**
* Return a generic transformed allocation array.
*/
public function transform(Allocation $allocation): array
{
return [
'id' => $allocation->id,
'ip' => $allocation->ip,
'alias' => $allocation->ip_alias,
'port' => $allocation->port,
'notes' => $allocation->notes,
'assigned' => !is_null($allocation->server_id),
];
}
/**
* Load the node relationship onto a given transformation.
*
* @throws \App\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeNode(Allocation $allocation): Item|NullResource
{
if (!$this->authorize(Node::RESOURCE_NAME)) {
return $this->null();
}
return $this->item(
$allocation->node,
$this->makeTransformer(NodeTransformer::class),
Node::RESOURCE_NAME
);
}
/**
* Load the server relationship onto a given transformation.
*
* @throws \App\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeServer(Allocation $allocation): Item|NullResource
{
if (!$this->authorize(Server::RESOURCE_NAME) || !$allocation->server) {
return $this->null();
}
return $this->item(
$allocation->server,
$this->makeTransformer(ServerTransformer::class),
Server::RESOURCE_NAME
);
}
}