Node and user API routes implemented.

More attempts at the logic for API permissions, most likely will need
continued tweaking in the future, but base is there.
This commit is contained in:
Dane Everitt 2017-04-09 15:31:10 -04:00
parent f24b238e30
commit 820d2bf172
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
15 changed files with 447 additions and 26 deletions

View file

@ -79,7 +79,11 @@ class LocationTransformer extends TransformerAbstract
*/
public function includeServers(Location $location)
{
return $this->collection($location->servers, new ServerTransformer, 'server');
if ($this->request && ! $this->request->apiKeyHasPermission('server-list')) {
return;
}
return $this->collection($location->servers, new ServerTransformer($this->request), 'server');
}
/**
@ -89,6 +93,10 @@ class LocationTransformer extends TransformerAbstract
*/
public function includeNodes(Location $location)
{
return $this->collection($location->nodes, new NodeTransformer, 'node');
if ($this->request && ! $this->request->apiKeyHasPermission('location-list')) {
return;
}
return $this->collection($location->nodes, new NodeTransformer($this->request), 'node');
}
}

View file

@ -84,7 +84,7 @@ class NodeTransformer extends TransformerAbstract
return;
}
return $this->collection($node->allocations, new AllocationTransformer, 'allocation');
return $this->collection($node->allocations, new AllocationTransformer($this->request), 'allocation');
}
/**
@ -98,7 +98,7 @@ class NodeTransformer extends TransformerAbstract
return;
}
return $this->item($node->location, new LocationTransformer, 'location');
return $this->item($node->location, new LocationTransformer($this->request), 'location');
}
/**
@ -112,6 +112,6 @@ class NodeTransformer extends TransformerAbstract
return;
}
return $this->collection($node->servers, new ServerTransformer, 'server');
return $this->collection($node->servers, new ServerTransformer($this->request), 'server');
}
}

View file

@ -81,7 +81,11 @@ class OptionTransformer extends TransformerAbstract
*/
public function includeService(ServiceOption $option)
{
return $this->item($option->service, new ServiceTransformer, 'service');
if ($this->request && ! $this->request->apiKeyHasPermission('option-view')) {
return;
}
return $this->item($option->service, new ServiceTransformer($this->request), 'service');
}
/**
@ -91,7 +95,11 @@ class OptionTransformer extends TransformerAbstract
*/
public function includePacks(ServiceOption $option)
{
return $this->collection($option->packs, new PackTransformer, 'pack');
if ($this->request && ! $this->request->apiKeyHasPermission('option-view')) {
return;
}
return $this->collection($option->packs, new PackTransformer($this->request), 'pack');
}
/**
@ -101,7 +109,11 @@ class OptionTransformer extends TransformerAbstract
*/
public function includeServers(ServiceOption $option)
{
return $this->collection($option->servers, new ServerTransformer, 'server');
if ($this->request && ! $this->request->apiKeyHasPermission('option-view')) {
return;
}
return $this->collection($option->servers, new ServerTransformer($this->request), 'server');
}
/**
@ -111,6 +123,10 @@ class OptionTransformer extends TransformerAbstract
*/
public function includeVariables(ServiceOption $option)
{
return $this->collection($option->variables, new ServiceVariableTransformer, 'variable');
if ($this->request && ! $this->request->apiKeyHasPermission('option-view')) {
return;
}
return $this->collection($option->variables, new ServiceVariableTransformer($this->request), 'variable');
}
}

View file

@ -83,7 +83,11 @@ class PackTransformer extends TransformerAbstract
*/
public function includeOption(Pack $pack)
{
return $this->item($pack->option, new OptionTransformer, 'option');
if ($this->request && ! $this->request->apiKeyHasPermission('pack-view')) {
return;
}
return $this->item($pack->option, new OptionTransformer($this->request), 'option');
}
/**
@ -93,6 +97,10 @@ class PackTransformer extends TransformerAbstract
*/
public function includeServers(Pack $pack)
{
return $this->collection($pack->servers, new ServerTransformer, 'server');
if ($this->request && ! $this->request->apiKeyHasPermission('pack-view')) {
return;
}
return $this->collection($pack->servers, new ServerTransformer($this->request), 'server');
}
}

View file

@ -86,6 +86,10 @@ class ServerTransformer extends TransformerAbstract
*/
public function includeAllocations(Server $server)
{
if ($this->request && ! $this->request->apiKeyHasPermission('server-view')) {
return;
}
return $this->collection($server->allocations, new AllocationTransformer($this->request, 'server'), 'allocation');
}
@ -96,6 +100,10 @@ class ServerTransformer extends TransformerAbstract
*/
public function includeSubusers(Server $server)
{
if ($this->request && ! $this->request->apiKeyHasPermission('server-view')) {
return;
}
return $this->collection($server->subusers, new SubuserTransformer($this->request), 'subuser');
}
@ -106,6 +114,10 @@ class ServerTransformer extends TransformerAbstract
*/
public function includeUser(Server $server)
{
if ($this->request && ! $this->request->apiKeyHasPermission('server-view')) {
return;
}
return $this->item($server->user, new UserTransformer($this->request), 'user');
}
@ -116,6 +128,10 @@ class ServerTransformer extends TransformerAbstract
*/
public function includePack(Server $server)
{
if ($this->request && ! $this->request->apiKeyHasPermission('server-view')) {
return;
}
return $this->item($server->pack, new PackTransformer($this->request), 'pack');
}
@ -126,6 +142,10 @@ class ServerTransformer extends TransformerAbstract
*/
public function includeService(Server $server)
{
if ($this->request && ! $this->request->apiKeyHasPermission('server-view')) {
return;
}
return $this->item($server->service, new ServiceTransformer($this->request), 'service');
}
@ -136,6 +156,10 @@ class ServerTransformer extends TransformerAbstract
*/
public function includeOption(Server $server)
{
if ($this->request && ! $this->request->apiKeyHasPermission('server-view')) {
return;
}
return $this->item($server->option, new OptionTransformer($this->request), 'option');
}
@ -146,6 +170,10 @@ class ServerTransformer extends TransformerAbstract
*/
public function includeVariables(Server $server)
{
if ($this->request && ! $this->request->apiKeyHasPermission('server-view')) {
return;
}
return $this->collection($server->variables, new ServerVariableTransformer($this->request), 'server_variable');
}
@ -156,6 +184,10 @@ class ServerTransformer extends TransformerAbstract
*/
public function includeLocation(Server $server)
{
if ($this->request && ! $this->request->apiKeyHasPermission('server-view')) {
return;
}
return $this->item($server->location, new LocationTransformer($this->request), 'location');
}
@ -166,6 +198,10 @@ class ServerTransformer extends TransformerAbstract
*/
public function includeNode(Server $server)
{
if ($this->request && ! $this->request->apiKeyHasPermission('server-view')) {
return;
}
return $this->item($server->node, new NodeTransformer($this->request), 'node');
}
}

View file

@ -76,6 +76,10 @@ class ServerVariableTransformer extends TransformerAbstract
*/
public function includeParent(ServerVariable $variable)
{
return $this->item($variable->variable, new ServiceVariableTransformer, 'variable');
if ($this->request && ! $this->request->apiKeyHasPermission('server-view')) {
return;
}
return $this->item($variable->variable, new ServiceVariableTransformer($this->request), 'variable');
}
}

View file

@ -80,7 +80,11 @@ class ServiceTransformer extends TransformerAbstract
*/
public function includeOptions(Service $service)
{
return $this->collection($service->options, new OptionTransformer, 'option');
if ($this->request && ! $this->request->apiKeyHasPermission('service-view')) {
return;
}
return $this->collection($service->options, new OptionTransformer($this->request), 'option');
}
/**
@ -90,7 +94,11 @@ class ServiceTransformer extends TransformerAbstract
*/
public function includeServers(Service $service)
{
return $this->collection($service->servers, new ServerTransformer, 'server');
if ($this->request && ! $this->request->apiKeyHasPermission('service-view')) {
return;
}
return $this->collection($service->servers, new ServerTransformer($this->request), 'server');
}
/**
@ -100,6 +108,10 @@ class ServiceTransformer extends TransformerAbstract
*/
public function includePacks(Service $service)
{
return $this->collection($service->packs, new PackTransformer, 'pack');
if ($this->request && ! $this->request->apiKeyHasPermission('service-view')) {
return;
}
return $this->collection($service->packs, new PackTransformer($this->request), 'pack');
}
}

View file

@ -76,6 +76,10 @@ class ServiceVariableTransformer extends TransformerAbstract
*/
public function includeVariables(ServiceVariable $variable)
{
return $this->collection($variable->serverVariable, new ServerVariableTransformer, 'server_variable');
if ($this->request && ! $this->request->apiKeyHasPermission('option-view')) {
return;
}
return $this->collection($variable->serverVariable, new ServerVariableTransformer($this->request), 'server_variable');
}
}

View file

@ -60,6 +60,10 @@ class SubuserTransformer extends TransformerAbstract
*/
public function transform(Subuser $subuser)
{
if ($this->request && ! $this->request->apiKeyHasPermission('server-view')) {
return;
}
return [
'id' => $subuser->id,
'username' => $subuser->user->username,

View file

@ -30,6 +30,16 @@ use League\Fractal\TransformerAbstract;
class UserTransformer extends TransformerAbstract
{
/**
* List of resources that can be included.
*
* @var array
*/
protected $availableIncludes = [
'access',
'servers',
];
/**
* The Illuminate Request object if provided.
*
@ -61,4 +71,32 @@ class UserTransformer extends TransformerAbstract
{
return $user->toArray();
}
/**
* Return the servers associated with this user.
*
* @return \Leauge\Fractal\Resource\Collection
*/
public function includeServers(User $user)
{
if ($this->request && ! $this->request->apiKeyHasPermission('user-view')) {
return;
}
return $this->collection($user->servers, new ServerTransformer($this->request), 'server');
}
/**
* Return the servers that this user can access.
*
* @return \Leauge\Fractal\Resource\Collection
*/
public function includeAccess(User $user)
{
if ($this->request && ! $this->request->apiKeyHasPermission('user-view')) {
return;
}
return $this->collection($user->access()->get(), new ServerTransformer($this->request), 'server');
}
}