1
0
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:
Toby Zerner
2015-08-04 10:40:04 +09:30
parent afe031f269
commit 2e4d38b3e7
10 changed files with 81 additions and 43 deletions

View 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);
}
}

View File

@@ -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();
}
}

View 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);
}
}

View File

@@ -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));
}