diff --git a/app/Http/Controllers/API/Admin/ServerController.php b/app/Http/Controllers/API/Admin/ServerController.php index 651f70787..716d39dca 100644 --- a/app/Http/Controllers/API/Admin/ServerController.php +++ b/app/Http/Controllers/API/Admin/ServerController.php @@ -64,10 +64,7 @@ class ServerController extends Controller $fractal = Fractal::create()->item($server); if ($request->input('include')) { - $fractal->parseIncludes(collect(explode(',', $request->input('include')))->intersect([ - 'allocations', 'subusers', 'user', - 'pack', 'service', 'option', - ])->toArray()); + $fractal->parseIncludes(explode(',', $request->input('include'))); } return $fractal->transformWith(new ServerTransformer) diff --git a/app/Http/Controllers/API/User/ServerController.php b/app/Http/Controllers/API/User/ServerController.php index 6db58cb4b..36fdc0fac 100644 --- a/app/Http/Controllers/API/User/ServerController.php +++ b/app/Http/Controllers/API/User/ServerController.php @@ -47,9 +47,7 @@ class ServerController extends Controller $fractal = Fractal::create()->item($server); if ($request->input('include')) { - $fractal->parseIncludes(collect(explode(',', $request->input('include')))->intersect([ - 'allocations', 'subusers', 'stats', - ])->toArray()); + $fractal->parseIncludes(explode(',', $request->input('include'))); } return $fractal->transformWith(new ServerTransformer) diff --git a/app/Models/Server.php b/app/Models/Server.php index 633c1a542..ee2e1a89e 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -313,4 +313,14 @@ class Server extends Model { return $this->hasMany(Database::class); } + + /** + * Gets the location of the server. + * + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + */ + public function location() + { + return $this->node->location(); + } } diff --git a/app/Transformers/Admin/AllocationTransformer.php b/app/Transformers/Admin/AllocationTransformer.php index d59d29a25..c7fa85724 100644 --- a/app/Transformers/Admin/AllocationTransformer.php +++ b/app/Transformers/Admin/AllocationTransformer.php @@ -30,12 +30,55 @@ use League\Fractal\TransformerAbstract; class AllocationTransformer extends TransformerAbstract { /** - * Return a generic transformed server array. + * The filter to be applied to this transformer. + * + * @var bool|string + */ + protected $filter; + + /** + * Transformer constructor. + * + * @param bool|string $filter + * @return void + */ + public function __construct($filter = false) + { + $this->filter = $filter; + } + + /** + * Return a generic transformed allocation array. * * @return array */ public function transform(Allocation $allocation) { + return $this->transformWithFilter($allocation); + } + + /** + * Determine which transformer filter to apply. + * + * @return array + */ + protected function transformWithFilter(Allocation $allocation) + { + if ($this->filter === 'server') { + return $this->transformForServer($allocation); + } + return $allocation->toArray(); } + + /** + * Transform the allocation to only return information not duplicated + * in the server response (discard node_id and server_id). + * + * @return array + */ + protected function transformForServer(Allocation $allocation) + { + return collect($allocation)->only('id', 'ip', 'ip_alias', 'port')->toArray(); + } } diff --git a/app/Transformers/Admin/LocationTransformer.php b/app/Transformers/Admin/LocationTransformer.php new file mode 100644 index 000000000..f41ead289 --- /dev/null +++ b/app/Transformers/Admin/LocationTransformer.php @@ -0,0 +1,71 @@ +. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +namespace Pterodactyl\Transformers\Admin; + +use Pterodactyl\Models\Location; +use League\Fractal\TransformerAbstract; + +class LocationTransformer extends TransformerAbstract +{ + /** + * List of resources that can be included. + * + * @var array + */ + protected $availableIncludes = [ + 'nodes', + 'servers', + ]; + + /** + * Return a generic transformed pack array. + * + * @return array + */ + public function transform(Location $location) + { + return $location->toArray(); + } + + /** + * Return the nodes associated with this location. + * + * @return \Leauge\Fractal\Resource\Collection + */ + public function includeServers(Location $location) + { + return $this->collection($location->servers, new ServerTransformer, 'server'); + } + + /** + * Return the nodes associated with this location. + * + * @return \Leauge\Fractal\Resource\Collection + */ + public function includeNodes(Location $location) + { + return $this->collection($location->nodes, new NodeTransformer, 'node'); + } +} diff --git a/app/Transformers/Admin/NodeTransformer.php b/app/Transformers/Admin/NodeTransformer.php new file mode 100644 index 000000000..779f96059 --- /dev/null +++ b/app/Transformers/Admin/NodeTransformer.php @@ -0,0 +1,82 @@ +. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +namespace Pterodactyl\Transformers\Admin; + +use Pterodactyl\Models\Node; +use League\Fractal\TransformerAbstract; + +class NodeTransformer extends TransformerAbstract +{ + /** + * List of resources that can be included. + * + * @var array + */ + protected $availableIncludes = [ + 'allocations', + 'location', + 'servers', + ]; + + /** + * Return a generic transformed pack array. + * + * @return array + */ + public function transform(Node $node) + { + return $node->toArray(); + } + + /** + * Return the nodes associated with this location. + * + * @return \Leauge\Fractal\Resource\Collection + */ + public function includeAllocations(Node $node) + { + return $this->collection($node->allocations, new AllocationTransformer, 'allocation'); + } + + /** + * Return the nodes associated with this location. + * + * @return \Leauge\Fractal\Resource\Item + */ + public function includeLocation(Node $node) + { + return $this->item($node->location, new LocationTransformer, 'location'); + } + + /** + * Return the nodes associated with this location. + * + * @return \Leauge\Fractal\Resource\Collection + */ + public function includeServers(Node $node) + { + return $this->collection($node->servers, new ServerTransformer, 'server'); + } +} diff --git a/app/Transformers/Admin/OptionTransformer.php b/app/Transformers/Admin/OptionTransformer.php new file mode 100644 index 000000000..bdc0b1baf --- /dev/null +++ b/app/Transformers/Admin/OptionTransformer.php @@ -0,0 +1,93 @@ +. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +namespace Pterodactyl\Transformers\Admin; + +use Pterodactyl\Models\ServiceOption; +use League\Fractal\TransformerAbstract; + +class OptionTransformer extends TransformerAbstract +{ + /** + * List of resources that can be included. + * + * @var array + */ + protected $availableIncludes = [ + 'service', + 'packs', + 'servers', + 'variables', + ]; + + /** + * Return a generic transformed service option array. + * + * @return array + */ + public function transform(ServiceOption $option) + { + return $option->toArray(); + } + + /** + * Return the parent service for this service option. + * + * @return \Leauge\Fractal\Resource\Collection + */ + public function includeService(ServiceOption $option) + { + return $this->item($option->service, new ServiceTransformer, 'service'); + } + + /** + * Return the packs associated with this service option. + * + * @return \Leauge\Fractal\Resource\Collection + */ + public function includePacks(ServiceOption $option) + { + return $this->collection($option->packs, new PackTransformer, 'pack'); + } + + /** + * Return the servers associated with this service option. + * + * @return \Leauge\Fractal\Resource\Collection + */ + public function includeServers(ServiceOption $option) + { + return $this->collection($option->servers, new ServerTransformer, 'server'); + } + + /** + * Return the variables for this service option. + * + * @return \Leauge\Fractal\Resource\Collection + */ + public function includeVariables(ServiceOption $option) + { + return $this->collection($option->variables, new ServiceVariableTransformer, 'variable'); + } +} diff --git a/app/Transformers/Admin/PackTransformer.php b/app/Transformers/Admin/PackTransformer.php new file mode 100644 index 000000000..309280fd1 --- /dev/null +++ b/app/Transformers/Admin/PackTransformer.php @@ -0,0 +1,75 @@ +. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +namespace Pterodactyl\Transformers\Admin; + +use Pterodactyl\Models\Pack; +use League\Fractal\TransformerAbstract; + +class PackTransformer extends TransformerAbstract +{ + /** + * List of resources that can be included. + * + * @var array + */ + protected $availableIncludes = [ + 'option', + 'servers', + ]; + + /** + * Return a generic transformed pack array. + * + * @return array + */ + public function transform($pack) + { + if (! $pack instanceof Pack) { + return ['id' => null]; + } + + return $pack->toArray(); + } + + /** + * Return the packs associated with this service. + * + * @return \Leauge\Fractal\Resource\Item + */ + public function includeOption(Pack $pack) + { + return $this->item($pack->option, new OptionTransformer, 'option'); + } + + /** + * Return the packs associated with this service. + * + * @return \Leauge\Fractal\Resource\Collection + */ + public function includeServers(Pack $pack) + { + return $this->collection($pack->servers, new ServerTransformer, 'server'); + } +} diff --git a/app/Transformers/Admin/ServerTransformer.php b/app/Transformers/Admin/ServerTransformer.php index 875e2261c..e17ba8ae5 100644 --- a/app/Transformers/Admin/ServerTransformer.php +++ b/app/Transformers/Admin/ServerTransformer.php @@ -38,6 +38,12 @@ class ServerTransformer extends TransformerAbstract 'allocations', 'user', 'subusers', + 'pack', + 'service', + 'option', + 'variables', + 'location', + 'node', ]; /** @@ -57,7 +63,7 @@ class ServerTransformer extends TransformerAbstract */ public function includeAllocations(Server $server) { - return $this->collection($server->allocations, new AllocationTransformer, 'allocation'); + return $this->collection($server->allocations, new AllocationTransformer('server'), 'allocation'); } /** @@ -79,4 +85,64 @@ class ServerTransformer extends TransformerAbstract { return $this->item($server->user, new UserTransformer, 'user'); } + + /** + * Return a generic array with pack information for this server. + * + * @return \Leauge\Fractal\Resource\Item + */ + public function includePack(Server $server) + { + return $this->item($server->pack, new PackTransformer, 'pack'); + } + + /** + * Return a generic array with service information for this server. + * + * @return \Leauge\Fractal\Resource\Item + */ + public function includeService(Server $server) + { + return $this->item($server->service, new ServiceTransformer, 'service'); + } + + /** + * Return a generic array with service option information for this server. + * + * @return \Leauge\Fractal\Resource\Item + */ + public function includeOption(Server $server) + { + return $this->item($server->option, new OptionTransformer, 'option'); + } + + /** + * Return a generic array of data about subusers for this server. + * + * @return \Leauge\Fractal\Resource\Collection + */ + public function includeVariables(Server $server) + { + return $this->collection($server->variables, new ServerVariableTransformer, 'server_variable'); + } + + /** + * Return a generic array with pack information for this server. + * + * @return \Leauge\Fractal\Resource\Item + */ + public function includeLocation(Server $server) + { + return $this->item($server->location, new LocationTransformer, 'location'); + } + + /** + * Return a generic array with pack information for this server. + * + * @return \Leauge\Fractal\Resource\Item + */ + public function includeNode(Server $server) + { + return $this->item($server->node, new NodeTransformer, 'node'); + } } diff --git a/app/Transformers/Admin/ServerVariableTransformer.php b/app/Transformers/Admin/ServerVariableTransformer.php new file mode 100644 index 000000000..9d2247418 --- /dev/null +++ b/app/Transformers/Admin/ServerVariableTransformer.php @@ -0,0 +1,58 @@ +. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +namespace Pterodactyl\Transformers\Admin; + +use Pterodactyl\Models\ServerVariable; +use League\Fractal\TransformerAbstract; + +class ServerVariableTransformer extends TransformerAbstract +{ + /** + * List of resources that can be included. + * + * @var array + */ + protected $availableIncludes = ['parent']; + + /** + * Return a generic transformed server variable array. + * + * @return array + */ + public function transform(ServerVariable $variable) + { + return $variable->toArray(); + } + + /** + * Return the parent service variable data. + * + * @return \Leauge\Fractal\Resource\Item + */ + public function includeParent(ServerVariable $variable) + { + return $this->item($variable->variable, new ServiceVariableTransformer, 'variable'); + } +} diff --git a/app/Transformers/Admin/ServiceTransformer.php b/app/Transformers/Admin/ServiceTransformer.php new file mode 100644 index 000000000..eb1fb2ab1 --- /dev/null +++ b/app/Transformers/Admin/ServiceTransformer.php @@ -0,0 +1,82 @@ +. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +namespace Pterodactyl\Transformers\Admin; + +use Pterodactyl\Models\Service; +use League\Fractal\TransformerAbstract; + +class ServiceTransformer extends TransformerAbstract +{ + /** + * List of resources that can be included. + * + * @var array + */ + protected $availableIncludes = [ + 'options', + 'servers', + 'packs', + ]; + + /** + * Return a generic transformed service array. + * + * @return array + */ + public function transform(Service $service) + { + return $service->toArray(); + } + + /** + * Return the the service options. + * + * @return \Leauge\Fractal\Resource\Collection + */ + public function includeOptions(Service $service) + { + return $this->collection($service->options, new OptionTransformer, 'option'); + } + + /** + * Return the servers associated with this service. + * + * @return \Leauge\Fractal\Resource\Collection + */ + public function includeServers(Service $service) + { + return $this->collection($service->servers, new ServerTransformer, 'server'); + } + + /** + * Return the packs associated with this service. + * + * @return \Leauge\Fractal\Resource\Collection + */ + public function includePacks(Service $service) + { + return $this->collection($service->packs, new PackTransformer, 'pack'); + } +} diff --git a/app/Transformers/Admin/ServiceVariableTransformer.php b/app/Transformers/Admin/ServiceVariableTransformer.php new file mode 100644 index 000000000..1a15c7ee0 --- /dev/null +++ b/app/Transformers/Admin/ServiceVariableTransformer.php @@ -0,0 +1,58 @@ +. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +namespace Pterodactyl\Transformers\Admin; + +use Pterodactyl\Models\ServiceVariable; +use League\Fractal\TransformerAbstract; + +class ServiceVariableTransformer extends TransformerAbstract +{ + /** + * List of resources that can be included. + * + * @var array + */ + protected $availableIncludes = ['variables']; + + /** + * Return a generic transformed server variable array. + * + * @return array + */ + public function transform(ServiceVariable $variable) + { + return $variable->toArray(); + } + + /** + * Return the server variables associated with this variable. + * + * @return \Leauge\Fractal\Resource\Collection + */ + public function includeVariables(ServiceVariable $variable) + { + return $this->collection($variable->serverVariable, new ServerVariableTransformer, 'server_variable'); + } +}