Merge pull request #31 from itsvic-dev/vic/route-eggs

feat: add "route eggs" setting to default extension config controller
This commit is contained in:
purple 2024-04-08 14:27:21 +02:00 committed by GitHub
commit 708dfcea32
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 246 additions and 118 deletions

9
.prettierrc.json Normal file
View file

@ -0,0 +1,9 @@
{
"printWidth": 120,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"jsxSingleQuote": true,
"endOfLine": "lf"
}

View file

@ -20,7 +20,23 @@ class ExtensionConfigurationController extends Controller
*/
public function update(ExtensionConfigurationRequest $request): RedirectResponse
{
foreach ($request->normalize() as $key => $value) { $this->settings->set('blueprint::extensionconfig_' . $key, $value); }
// set extension eggs to be -1 (Show all), then overwrite if needed
$this->settings->set('blueprint::extensionconfig_' . $request->input('_identifier', 'blueprint') . '_eggs', '["-1"]');
foreach ($request->normalize() as $key => $value) {
if (str_ends_with($key, '_eggs')) {
// if there are other eggs set, remove the -1 'egg'
$eggs = (array)$value['*'];
if (count($eggs) > 1 && in_array('-1', $eggs)) {
$eggs = array_diff($eggs, ['-1']);
}
$value = json_encode(array_values($eggs));
}
$this->settings->set('blueprint::extensionconfig_' . $key, $value);
}
return redirect()->route('admin.extensions.'.$request->input('_identifier', 'blueprint').'.index');
}
}
@ -31,6 +47,8 @@ class ExtensionConfigurationRequest extends AdminFormRequest
return [
$this->input('_identifier', 'blueprint').'_adminlayouts' => 'boolean',
$this->input('_identifier', 'blueprint').'_dashboardwrapper' => 'boolean',
$this->input('_identifier', 'blueprint').'_eggs' => 'array',
$this->input('_identifier', 'blueprint').'_eggs.*' => 'numeric',
];
}

View file

@ -0,0 +1,30 @@
<?php
namespace Pterodactyl\BlueprintFramework\Controllers;
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
use Pterodactyl\Contracts\Repository\SettingsRepositoryInterface;
class ExtensionRouteController extends ClientApiController
{
public function __construct(
private SettingsRepositoryInterface $settings,
) {
parent::__construct();
}
public function eggs(GetRouteEggsRequest $request): array
{
$id = $request->input('id', 'blueprint');
$eggs = $this->settings->get('blueprint::extensionconfig_' . $id . '_eggs');
return json_decode($eggs ?: '["-1"]');
}
}
class GetRouteEggsRequest extends ClientApiRequest {
public function authorize(): bool
{
return true;
}
}

View file

@ -2,9 +2,9 @@
namespace Pterodactyl\Http\Controllers\Admin\Extensions\Blueprint;
use Artisan;
use Illuminate\View\View;
use Illuminate\View\Factory as ViewFactory;
use Artisan;
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\BlueprintFramework\Services\PlaceholderService\BlueprintPlaceholderService;
use Pterodactyl\BlueprintFramework\Services\ConfigService\BlueprintConfigService;

View file

@ -3,6 +3,7 @@
namespace Pterodactyl\Http\Controllers\Admin\Extensions\[id];
use Illuminate\View\View;
use Pterodactyl\Models\Egg;
use Illuminate\View\Factory as ViewFactory;
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Services\Helpers\SoftwareVersionService;
@ -29,6 +30,7 @@ class [id]ExtensionController extends Controller
return $this->view->make('admin.extensions.[id].index', [
'blueprint' => $this->blueprint,
'version' => $this->version,
'eggs' => Egg::all(),
'root' => $rootPath
]);
}

View file

@ -75,12 +75,15 @@ export const rawDataToServerObject = ({ attributes: data }: FractalResponseData)
rawDataToServerAllocation
),
BlueprintFramework: { ...data.BlueprintFramework }
BlueprintFramework: {
eggId: data.BlueprintFramework.egg_id,
},
});
export default (uuid: string): Promise<[Server, string[]]> => {
return new Promise((resolve, reject) => {
http.get(`/api/client/servers/${uuid}`)
http
.get(`/api/client/servers/${uuid}`)
.then(({ data }) =>
resolve([
rawDataToServerObject(data),

View file

@ -1,4 +1,4 @@
import React from 'react';
import React, { useState, useEffect } from 'react';
import { NavLink, Route, Switch, useRouteMatch } from 'react-router-dom';
import TransitionRouter from '@/TransitionRouter';
import PermissionRoute from '@/components/elements/PermissionRoute';
@ -12,6 +12,30 @@ import { ServerContext } from '@/state/server';
import routes from '@/routers/routes';
import blueprintRoutes from './routes';
const blueprintExtensions = [...new Set(blueprintRoutes.server.map((route) => route.identifier))];
/**
* Get the route egg IDs for each extension with server routes.
*/
const useExtensionEggs = () => {
const [extensionEggs, setExtensionEggs] = useState<{ [x: string]: string[] }>(
blueprintExtensions.reduce((prev, current) => ({ ...prev, [current]: ['-1'] }), {})
);
useEffect(() => {
(async () => {
const newEggs: { [x: string]: string[] } = {};
for (const id of blueprintExtensions) {
const resp = await fetch(`/api/client/extensions/blueprint/eggs?${new URLSearchParams({ id })}`);
newEggs[id] = (await resp.json()) as string[];
}
setExtensionEggs(newEggs);
})();
}, []);
return extensionEggs;
};
export const NavigationLinks = () => {
const rootAdmin = useStoreState((state) => state.user.data!.rootAdmin);
const serverEgg = ServerContext.useStoreState((state) => state.server.data?.BlueprintFramework.eggId);
@ -22,10 +46,11 @@ export const NavigationLinks = () => {
}
return `${(url ? match.url : match.path).replace(/\/*$/, '')}/${value.replace(/^\/+/, '')}`;
};
const extensionEggs = useExtensionEggs();
console.log(serverEgg);
return (
<>
{/* Pterodactyl routes */}
{routes.server
.filter((route) => !!route.name)
@ -41,14 +66,18 @@ export const NavigationLinks = () => {
{route.name}
</NavLink>
)
)
}
)}
{/* Blueprint routes */}
{blueprintRoutes.server.length > 0 && blueprintRoutes.server
{blueprintRoutes.server.length > 0 &&
blueprintRoutes.server
.filter((route) => !!route.name)
.filter((route) => route.adminOnly ? rootAdmin : true)
.filter((route) => route.eggs && serverEgg ? route.eggs.includes(serverEgg) : true )
.filter((route) => (route.adminOnly ? rootAdmin : true))
.filter((route) =>
extensionEggs[route.identifier].includes('-1')
? true
: extensionEggs[route.identifier].find((id) => id === serverEgg?.toString())
)
.map((route) =>
route.permission ? (
<Can key={route.path} action={route.permission} matchAny>
@ -61,9 +90,7 @@ export const NavigationLinks = () => {
{route.name}
</NavLink>
)
)
}
)}
</>
);
};
@ -78,13 +105,13 @@ export const NavigationRouter = () => {
}
return `${(url ? match.url : match.path).replace(/\/*$/, '')}/${value.replace(/^\/+/, '')}`;
};
const extensionEggs = useExtensionEggs();
const location = useLocation();
return (
<>
<TransitionRouter>
<Switch location={location}>
{/* Pterodactyl routes */}
{routes.server.map(({ path, permission, component: Component }) => (
<PermissionRoute key={path} permission={permission} path={to(path)} exact>
@ -95,17 +122,21 @@ export const NavigationRouter = () => {
))}
{/* Blueprint routes */}
{blueprintRoutes.server.length > 0 && blueprintRoutes.server
.filter((route) => route.adminOnly ? rootAdmin : true)
.filter((route) => route.eggs && serverEgg ? route.eggs.includes(serverEgg) : true )
{blueprintRoutes.server.length > 0 &&
blueprintRoutes.server
.filter((route) => (route.adminOnly ? rootAdmin : true))
.filter((route) =>
extensionEggs[route.identifier].includes('-1')
? true
: extensionEggs[route.identifier].find((id) => id === serverEgg?.toString())
)
.map(({ path, permission, component: Component }) => (
<PermissionRoute key={path} permission={permission} path={to(path)} exact>
<Spinner.Suspense>
<Component />
</Spinner.Suspense>
</PermissionRoute>
))
}
))}
<Route path={'*'} component={NotFound} />
</Switch>

View file

@ -12,7 +12,6 @@ interface RouteDefinition {
}
interface ServerRouteDefinition extends RouteDefinition {
permission: string | string[] | null;
eggs?: number[];
}
interface Routes {
account: RouteDefinition[];

View file

@ -55,6 +55,19 @@
<p class="text-muted small">Allow this extension to extend the dashboard's blade wrapper.</p>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<label class="control-label">Route eggs</label>
<select multiple class="pOptions form-control" name="{{ $EXTENSION_ID }}_eggs[]">
<option value="-1" @if(in_array('-1', json_decode($blueprint->dbGet('blueprint', 'extensionconfig_'.$EXTENSION_ID.'_eggs') ?: '["-1"]'))) selected @endif>Show on all eggs</option>
@foreach ($eggs as $egg)
<option value="{{ $egg->id }}" @if(in_array(strval($egg->id), json_decode($blueprint->dbGet('blueprint', 'extensionconfig_'.$EXTENSION_ID.'_eggs') ?: '["-1"]'))) selected @endif>{{ $egg->name }}</option>
@endforeach
</select>
<p class="text-muted small">Guess</p>
</div>
</div>
</div>
<div class="modal-footer" style="border-color:transparent; border-radius:7px">
@ -75,3 +88,19 @@
</div>
</div>
@endsection
@section('footer-scripts')
@parent
<script>
$('.pOptions').select2();
</script>
<style>
.select2-selection {
border-radius: 6px !important;
}
.select2-container--open .select2-selection {
border-bottom-left-radius: 0px !important;
border-bottom-right-radius: 0px !important;
}
</style>
@endsection

View file

@ -1,5 +1,7 @@
<?php
use Pterodactyl\BlueprintFramework\Controllers\ExtensionRouteController;
foreach (File::allFiles(__DIR__ . '/client') as $partial) {
if ($partial->getExtension() == 'php') {
Route::prefix('/'.basename($partial->getFilename(), '.php'))
@ -7,3 +9,8 @@ foreach (File::allFiles(__DIR__ . '/client') as $partial) {
);
}
}
/* Routes internally used by Blueprint. */
Route::prefix('/blueprint')->group(function () {
Route::get('/eggs', [ExtensionRouteController::class, 'eggs']);
});