misc_pterodactyl-panel/app/Transformers/Admin/UserTransformer.php

60 lines
1.4 KiB
PHP
Raw Normal View History

<?php
namespace Pterodactyl\Transformers\Admin;
use Illuminate\Http\Request;
use Pterodactyl\Models\User;
2017-11-19 22:30:00 +00:00
use Pterodactyl\Transformers\ApiTransformer;
2017-11-19 22:30:00 +00:00
class UserTransformer extends ApiTransformer
{
/**
* List of resources that can be included.
*
* @var array
*/
2017-11-19 22:30:00 +00:00
protected $availableIncludes = ['servers'];
/**
* Setup request object for transformer.
*
2017-11-19 22:30:00 +00:00
* @param \Illuminate\Http\Request $request
*/
2017-11-19 22:30:00 +00:00
public function __construct(Request $request)
{
$this->request = $request;
}
/**
* Return a generic transformed subuser array.
*
2017-11-19 22:30:00 +00:00
* @param \Pterodactyl\Models\User $user
* @return array
*/
2017-11-19 22:30:00 +00:00
public function transform(User $user): array
{
return $user->toArray();
}
/**
* Return the servers associated with this user.
*
2017-11-19 22:30:00 +00:00
* @param \Pterodactyl\Models\User $user
* @return bool|\League\Fractal\Resource\Collection
*
* @throws \Pterodactyl\Exceptions\PterodactylException
*/
public function includeServers(User $user)
{
2017-11-19 22:30:00 +00:00
if ($this->authorize('server-list')) {
return false;
}
2017-11-19 22:30:00 +00:00
if (! $user->relationLoaded('servers')) {
$user->load('servers');
}
2017-11-19 22:30:00 +00:00
return $this->collection($user->getRelation('servers'), new ServerTransformer($this->request), 'server');
}
}