1
0
mirror of https://github.com/flarum/core.git synced 2025-08-09 09:57:06 +02:00

feat: NoJs Admin View (#3059)

Adds a nojs blade template to be able to enable/disable extensions when one of them misbehaves.
This commit is contained in:
Sami Mazouz
2021-08-31 09:08:27 +01:00
committed by GitHub
parent 55d8af44a2
commit e8153ccc79
15 changed files with 403 additions and 70 deletions

View File

@@ -0,0 +1,60 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Admin\Content;
use Flarum\Extension\ExtensionManager;
use Flarum\Foundation\Application;
use Flarum\Frontend\Document;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\View\Factory;
use Psr\Http\Message\ServerRequestInterface as Request;
class Index
{
/**
* @var Factory
*/
protected $view;
/**
* @var ExtensionManager
*/
protected $extensions;
/**
* @var SettingsRepositoryInterface
*/
protected $settings;
public function __construct(Factory $view, ExtensionManager $extensions, SettingsRepositoryInterface $settings)
{
$this->view = $view;
$this->extensions = $extensions;
$this->settings = $settings;
}
public function __invoke(Document $document, Request $request): Document
{
$extensions = $this->extensions->getExtensions();
$extensionsEnabled = json_decode($this->settings->get('extensions_enabled', '{}'), true);
$csrfToken = $request->getAttribute('session')->token();
$mysqlVersion = $document->payload['mysqlVersion'];
$phpVersion = $document->payload['phpVersion'];
$flarumVersion = Application::VERSION;
$document->content = $this->view->make(
'flarum.admin::frontend.content.admin',
compact('extensions', 'extensionsEnabled', 'csrfToken', 'flarumVersion', 'phpVersion', 'mysqlVersion')
);
return $document;
}
}

View File

@@ -0,0 +1,55 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Admin\Controller;
use Flarum\Bus\Dispatcher;
use Flarum\Extension\Command\ToggleExtension;
use Flarum\Http\RequestUtil;
use Flarum\Http\UrlGenerator;
use Illuminate\Support\Arr;
use Laminas\Diactoros\Response\RedirectResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface;
class UpdateExtensionController implements RequestHandlerInterface
{
/**
* @var UrlGenerator
*/
protected $url;
/**
* @var Dispatcher
*/
protected $bus;
public function __construct(UrlGenerator $url, Dispatcher $bus)
{
$this->url = $url;
$this->bus = $bus;
}
/**
* {@inheritdoc}
*/
public function handle(Request $request): ResponseInterface
{
$actor = RequestUtil::getActor($request);
$enabled = (bool) (int) Arr::get($request->getParsedBody(), 'enabled');
$name = Arr::get($request->getQueryParams(), 'name');
$this->bus->dispatch(
new ToggleExtension($actor, $name, $enabled)
);
return new RedirectResponse($this->url->to('admin')->base());
}
}

View File

@@ -7,6 +7,8 @@
* LICENSE file that was distributed with this source code.
*/
use Flarum\Admin\Content\Index;
use Flarum\Admin\Controller\UpdateExtensionController;
use Flarum\Http\RouteCollection;
use Flarum\Http\RouteHandlerFactory;
@@ -14,6 +16,12 @@ return function (RouteCollection $map, RouteHandlerFactory $route) {
$map->get(
'/',
'index',
$route->toAdmin()
$route->toAdmin(Index::class)
);
$map->post(
'/extensions/{name}',
'extensions.update',
$route->toController(UpdateExtensionController::class)
);
};

View File

@@ -9,7 +9,8 @@
namespace Flarum\Api\Controller;
use Flarum\Extension\ExtensionManager;
use Flarum\Bus\Dispatcher;
use Flarum\Extension\Command\ToggleExtension;
use Flarum\Http\RequestUtil;
use Illuminate\Support\Arr;
use Laminas\Diactoros\Response\EmptyResponse;
@@ -20,16 +21,13 @@ use Psr\Http\Server\RequestHandlerInterface;
class UpdateExtensionController implements RequestHandlerInterface
{
/**
* @var ExtensionManager
* @var Dispatcher
*/
protected $extensions;
protected $bus;
/**
* @param ExtensionManager $extensions
*/
public function __construct(ExtensionManager $extensions)
public function __construct(Dispatcher $bus)
{
$this->extensions = $extensions;
$this->bus = $bus;
}
/**
@@ -37,16 +35,13 @@ class UpdateExtensionController implements RequestHandlerInterface
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
RequestUtil::getActor($request)->assertAdmin();
$enabled = Arr::get($request->getParsedBody(), 'enabled');
$actor = RequestUtil::getActor($request);
$enabled = (bool) (int) Arr::get($request->getParsedBody(), 'enabled');
$name = Arr::get($request->getQueryParams(), 'name');
if ($enabled === true) {
$this->extensions->enable($name);
} elseif ($enabled === false) {
$this->extensions->disable($name);
}
$this->bus->dispatch(
new ToggleExtension($actor, $name, $enabled)
);
return new EmptyResponse;
}

View File

@@ -0,0 +1,37 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Extension\Command;
use Flarum\User\User;
class ToggleExtension
{
/**
* @var User
*/
public $actor;
/**
* @var string
*/
public $name;
/**
* @var bool
*/
public $enabled;
public function __construct(User $actor, string $name, bool $enabled)
{
$this->actor = $actor;
$this->name = $name;
$this->enabled = $enabled;
}
}

View File

@@ -0,0 +1,41 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Extension\Command;
use Flarum\Extension\ExtensionManager;
class ToggleExtensionHandler
{
/**
* @var ExtensionManager
*/
protected $extensions;
public function __construct(ExtensionManager $extensions)
{
$this->extensions = $extensions;
}
/**
* @throws \Flarum\User\Exception\PermissionDeniedException
* @throws \Flarum\Extension\Exception\MissingDependenciesException
* @throws \Flarum\Extension\Exception\DependentExtensionsException
*/
public function handle(ToggleExtension $command)
{
$command->actor->assertAdmin();
if ($command->enabled) {
$this->extensions->enable($command->name);
} else {
$this->extensions->disable($command->name);
}
}
}

View File

@@ -279,6 +279,19 @@ class Extension implements Arrayable
return $icon;
}
public function getIconStyles(): string
{
$properties = $this->getIcon();
unset($properties['name']);
return implode(';', array_map(function (string $property, string $value) {
$property = Str::kebab($property);
return "$property: $value";
}, array_keys($properties), $properties));
}
/**
* @internal
*/