mirror of
https://github.com/flarum/core.git
synced 2025-10-18 10:16:09 +02:00
Move config/permission actions to API; clean up cache flushing
This commit is contained in:
55
src/Api/Actions/ConfigAction.php
Normal file
55
src/Api/Actions/ConfigAction.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php namespace Flarum\Api\Actions;
|
||||
|
||||
use Flarum\Api\Request;
|
||||
use Flarum\Core\Settings\SettingsRepository;
|
||||
use Flarum\Core\Groups\Permission;
|
||||
use Flarum\Core\Exceptions\PermissionDeniedException;
|
||||
use Zend\Diactoros\Response\EmptyResponse;
|
||||
use Exception;
|
||||
|
||||
class ConfigAction implements Action
|
||||
{
|
||||
/**
|
||||
* @var SettingsRepository
|
||||
*/
|
||||
protected $settings;
|
||||
|
||||
/**
|
||||
* @param SettingsRepository $settings
|
||||
*/
|
||||
public function __construct(SettingsRepository $settings)
|
||||
{
|
||||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function handle(Request $request, array $routeParams = [])
|
||||
{
|
||||
if (! $request->actor->isAdmin()) {
|
||||
throw new PermissionDeniedException;
|
||||
}
|
||||
|
||||
$config = $request->get('config', []);
|
||||
|
||||
// TODO: throw HTTP status 400 or 422
|
||||
if (! is_array($config)) {
|
||||
throw new Exception;
|
||||
}
|
||||
|
||||
foreach ($config as $k => $v) {
|
||||
$this->settings->set($k, $v);
|
||||
|
||||
if (strpos($k, 'theme_') === 0) {
|
||||
$forum = app('Flarum\Forum\Actions\ClientAction');
|
||||
$forum->flushAssets();
|
||||
|
||||
$admin = app('Flarum\Admin\Actions\ClientAction');
|
||||
$admin->flushAssets();
|
||||
}
|
||||
}
|
||||
|
||||
return new EmptyResponse(204);
|
||||
}
|
||||
}
|
@@ -32,12 +32,10 @@ class UpdateAction extends JsonApiAction
|
||||
|
||||
app('flarum.formatter')->flush();
|
||||
|
||||
$assetPath = public_path('assets');
|
||||
$manifest = file_get_contents($assetPath . '/rev-manifest.json');
|
||||
$revisions = json_decode($manifest, true);
|
||||
$forum = app('Flarum\Forum\Actions\ClientAction');
|
||||
$forum->flushAssets();
|
||||
|
||||
foreach ($revisions as $file => $revision) {
|
||||
@unlink($assetPath . '/' . substr_replace($file, '-' . $revision, strrpos($file, '.'), 0));
|
||||
}
|
||||
$admin = app('Flarum\Admin\Actions\ClientAction');
|
||||
$admin->flushAssets();
|
||||
}
|
||||
}
|
||||
|
33
src/Api/Actions/PermissionAction.php
Normal file
33
src/Api/Actions/PermissionAction.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php namespace Flarum\Api\Actions;
|
||||
|
||||
use Flarum\Api\Request;
|
||||
use Flarum\Core\Groups\Permission;
|
||||
use Flarum\Core\Exceptions\PermissionDeniedException;
|
||||
use Zend\Diactoros\Response\EmptyResponse;
|
||||
|
||||
class PermissionAction implements Action
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function handle(Request $request, array $routeParams = [])
|
||||
{
|
||||
if (! $request->actor->isAdmin()) {
|
||||
throw new PermissionDeniedException;
|
||||
}
|
||||
|
||||
$permission = $request->get('permission');
|
||||
$groupIds = $request->get('groupIds');
|
||||
|
||||
Permission::where('permission', $permission)->delete();
|
||||
|
||||
Permission::insert(array_map(function ($groupId) use ($permission) {
|
||||
return [
|
||||
'permission' => $permission,
|
||||
'group_id' => $groupId
|
||||
];
|
||||
}, $groupIds));
|
||||
|
||||
return new EmptyResponse(204);
|
||||
}
|
||||
}
|
@@ -315,7 +315,7 @@ class ApiServiceProvider extends ServiceProvider
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Extensions
|
||||
| Administration
|
||||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
@@ -326,6 +326,20 @@ class ApiServiceProvider extends ServiceProvider
|
||||
$this->action('Flarum\Api\Actions\Extensions\UpdateAction')
|
||||
);
|
||||
|
||||
// Update config settings
|
||||
$routes->post(
|
||||
'/config',
|
||||
'flarum.api.config',
|
||||
$this->action('Flarum\Api\Actions\ConfigAction')
|
||||
);
|
||||
|
||||
// Update a permission
|
||||
$routes->post(
|
||||
'/permission',
|
||||
'flarum.api.permission',
|
||||
$this->action('Flarum\Api\Actions\PermissionAction')
|
||||
);
|
||||
|
||||
event(new RegisterApiRoutes($routes));
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user