| @@ 31-102 (lines=72) @@ | ||
| 28 | use Pterodactyl\Models\Location; |
|
| 29 | use League\Fractal\TransformerAbstract; |
|
| 30 | ||
| 31 | class LocationTransformer extends TransformerAbstract |
|
| 32 | { |
|
| 33 | /** |
|
| 34 | * List of resources that can be included. |
|
| 35 | * |
|
| 36 | * @var array |
|
| 37 | */ |
|
| 38 | protected $availableIncludes = [ |
|
| 39 | 'nodes', |
|
| 40 | 'servers', |
|
| 41 | ]; |
|
| 42 | ||
| 43 | /** |
|
| 44 | * The Illuminate Request object if provided. |
|
| 45 | * |
|
| 46 | * @var \Illuminate\Http\Request|bool |
|
| 47 | */ |
|
| 48 | protected $request; |
|
| 49 | ||
| 50 | /** |
|
| 51 | * Setup request object for transformer. |
|
| 52 | * |
|
| 53 | * @param \Illuminate\Http\Request|bool $request |
|
| 54 | * @return void |
|
| 55 | */ |
|
| 56 | public function __construct($request = false) |
|
| 57 | { |
|
| 58 | if (! $request instanceof Request && $request !== false) { |
|
| 59 | throw new DisplayException('Request passed to constructor must be of type Request or false.'); |
|
| 60 | } |
|
| 61 | ||
| 62 | $this->request = $request; |
|
| 63 | } |
|
| 64 | ||
| 65 | /** |
|
| 66 | * Return a generic transformed pack array. |
|
| 67 | * |
|
| 68 | * @return array |
|
| 69 | */ |
|
| 70 | public function transform(Location $location) |
|
| 71 | { |
|
| 72 | return $location->toArray(); |
|
| 73 | } |
|
| 74 | ||
| 75 | /** |
|
| 76 | * Return the nodes associated with this location. |
|
| 77 | * |
|
| 78 | * @return \Leauge\Fractal\Resource\Collection |
|
| 79 | */ |
|
| 80 | public function includeServers(Location $location) |
|
| 81 | { |
|
| 82 | if ($this->request && ! $this->request->apiKeyHasPermission('server-list')) { |
|
| 83 | return; |
|
| 84 | } |
|
| 85 | ||
| 86 | return $this->collection($location->servers, new ServerTransformer($this->request), 'server'); |
|
| 87 | } |
|
| 88 | ||
| 89 | /** |
|
| 90 | * Return the nodes associated with this location. |
|
| 91 | * |
|
| 92 | * @return \Leauge\Fractal\Resource\Collection |
|
| 93 | */ |
|
| 94 | public function includeNodes(Location $location) |
|
| 95 | { |
|
| 96 | if ($this->request && ! $this->request->apiKeyHasPermission('node-list')) { |
|
| 97 | return; |
|
| 98 | } |
|
| 99 | ||
| 100 | return $this->collection($location->nodes, new NodeTransformer($this->request), 'node'); |
|
| 101 | } |
|
| 102 | } |
|
| 103 | ||
| @@ 31-102 (lines=72) @@ | ||
| 28 | use Pterodactyl\Models\User; |
|
| 29 | use League\Fractal\TransformerAbstract; |
|
| 30 | ||
| 31 | class UserTransformer extends TransformerAbstract |
|
| 32 | { |
|
| 33 | /** |
|
| 34 | * List of resources that can be included. |
|
| 35 | * |
|
| 36 | * @var array |
|
| 37 | */ |
|
| 38 | protected $availableIncludes = [ |
|
| 39 | 'access', |
|
| 40 | 'servers', |
|
| 41 | ]; |
|
| 42 | ||
| 43 | /** |
|
| 44 | * The Illuminate Request object if provided. |
|
| 45 | * |
|
| 46 | * @var \Illuminate\Http\Request|bool |
|
| 47 | */ |
|
| 48 | protected $request; |
|
| 49 | ||
| 50 | /** |
|
| 51 | * Setup request object for transformer. |
|
| 52 | * |
|
| 53 | * @param \Illuminate\Http\Request|bool $request |
|
| 54 | * @return void |
|
| 55 | */ |
|
| 56 | public function __construct($request = false) |
|
| 57 | { |
|
| 58 | if (! $request instanceof Request && $request !== false) { |
|
| 59 | throw new DisplayException('Request passed to constructor must be of type Request or false.'); |
|
| 60 | } |
|
| 61 | ||
| 62 | $this->request = $request; |
|
| 63 | } |
|
| 64 | ||
| 65 | /** |
|
| 66 | * Return a generic transformed subuser array. |
|
| 67 | * |
|
| 68 | * @return array |
|
| 69 | */ |
|
| 70 | public function transform(User $user) |
|
| 71 | { |
|
| 72 | return $user->toArray(); |
|
| 73 | } |
|
| 74 | ||
| 75 | /** |
|
| 76 | * Return the servers associated with this user. |
|
| 77 | * |
|
| 78 | * @return \Leauge\Fractal\Resource\Collection |
|
| 79 | */ |
|
| 80 | public function includeServers(User $user) |
|
| 81 | { |
|
| 82 | if ($this->request && ! $this->request->apiKeyHasPermission('server-list')) { |
|
| 83 | return; |
|
| 84 | } |
|
| 85 | ||
| 86 | return $this->collection($user->servers, new ServerTransformer($this->request), 'server'); |
|
| 87 | } |
|
| 88 | ||
| 89 | /** |
|
| 90 | * Return the servers that this user can access. |
|
| 91 | * |
|
| 92 | * @return \Leauge\Fractal\Resource\Collection |
|
| 93 | */ |
|
| 94 | public function includeAccess(User $user) |
|
| 95 | { |
|
| 96 | if ($this->request && ! $this->request->apiKeyHasPermission('server-list')) { |
|
| 97 | return; |
|
| 98 | } |
|
| 99 | ||
| 100 | return $this->collection($user->access()->get(), new ServerTransformer($this->request), 'server'); |
|
| 101 | } |
|
| 102 | } |
|
| 103 | ||