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 95e45e8c7b
commit cf280e39b4
10 changed files with 81 additions and 43 deletions

View File

@ -91,7 +91,7 @@ export default class PermissionDropdown extends Dropdown {
app.request({
method: 'POST',
url: app.forum.attribute('adminUrl') + '/permission',
url: app.forum.attribute('apiUrl') + '/permission',
data: {permission, groupIds}
});
}

View File

@ -5,7 +5,7 @@ export default function saveConfig(config) {
return app.request({
method: 'POST',
url: app.forum.attribute('adminUrl') + '/config',
url: app.forum.attribute('apiUrl') + '/config',
data: {config}
}).catch(error => {
app.config = oldConfig;

View File

@ -50,18 +50,6 @@ class AdminServiceProvider extends ServiceProvider
'flarum.admin.index',
$this->action('Flarum\Admin\Actions\ClientAction')
);
$routes->post(
'/config',
'flarum.admin.updateConfig',
$this->action('Flarum\Admin\Actions\UpdateConfigAction')
);
$routes->post(
'/permission',
'flarum.admin.updatePermission',
$this->action('Flarum\Admin\Actions\UpdatePermissionAction')
);
}
protected function action($class)

View File

@ -1,12 +1,13 @@
<?php namespace Flarum\Admin\Actions;
<?php namespace Flarum\Api\Actions;
use Psr\Http\Message\ServerRequestInterface as Request;
use Flarum\Api\Request;
use Flarum\Core\Settings\SettingsRepository;
use Flarum\Support\Action;
use Flarum\Core\Groups\Permission;
use Flarum\Core\Exceptions\PermissionDeniedException;
use Zend\Diactoros\Response\EmptyResponse;
use Exception;
class UpdateConfigAction extends Action
class ConfigAction implements Action
{
/**
* @var SettingsRepository
@ -26,7 +27,11 @@ class UpdateConfigAction extends Action
*/
public function handle(Request $request, array $routeParams = [])
{
$config = array_get($request->getAttributes(), 'config', []);
if (! $request->actor->isAdmin()) {
throw new PermissionDeniedException;
}
$config = $request->get('config', []);
// TODO: throw HTTP status 400 or 422
if (! is_array($config)) {
@ -35,16 +40,16 @@ class UpdateConfigAction extends Action
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();
}
}
$assetPath = public_path('assets');
$manifest = file_get_contents($assetPath . '/rev-manifest.json');
$revisions = json_decode($manifest, true);
foreach ($revisions as $file => $revision) {
@unlink($assetPath . '/' . substr_replace($file, '-' . $revision, strrpos($file, '.'), 0));
}
return $this->success();
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

@ -1,19 +1,23 @@
<?php namespace Flarum\Admin\Actions;
<?php namespace Flarum\Api\Actions;
use Psr\Http\Message\ServerRequestInterface as Request;
use Flarum\Support\Action;
use Flarum\Api\Request;
use Flarum\Core\Groups\Permission;
use Flarum\Core\Exceptions\PermissionDeniedException;
use Zend\Diactoros\Response\EmptyResponse;
class UpdatePermissionAction extends Action
class PermissionAction implements Action
{
/**
* {@inheritdoc}
*/
public function handle(Request $request, array $routeParams = [])
{
$input = $request->getAttributes();
$permission = array_get($input, 'permission');
$groupIds = array_get($input, 'groupIds');
if (! $request->actor->isAdmin()) {
throw new PermissionDeniedException;
}
$permission = $request->get('permission');
$groupIds = $request->get('groupIds');
Permission::where('permission', $permission)->delete();
@ -24,6 +28,6 @@ class UpdatePermissionAction extends Action
];
}, $groupIds));
return $this->success();
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));
}

View File

@ -57,4 +57,10 @@ class AssetManager
{
return $this->js->getFile();
}
public function flush()
{
$this->less->flush();
$this->js->flush();
}
}

View File

@ -79,6 +79,7 @@ class RevisionCompiler implements Compiler
{
if (file_exists($file = $this->getRevisionFile())) {
$manifest = json_decode(file_get_contents($file), true);
return array_get($manifest, $this->filename);
}
}
@ -95,4 +96,15 @@ class RevisionCompiler implements Compiler
return file_put_contents($this->getRevisionFile(), json_encode($manifest));
}
public function flush()
{
$revision = $this->getRevision();
$ext = pathinfo($this->filename, PATHINFO_EXTENSION);
$file = $this->path . '/' . substr_replace($this->filename, '-' . $revision, -strlen($ext) - 1, 0);
@unlink($file);
}
}

View File

@ -118,6 +118,17 @@ abstract class ClientAction extends HtmlAction
return $view;
}
/**
* Flush the client's assets so that they will be regenerated from scratch
* on the next render.
*
* @return void
*/
public function flushAssets()
{
$this->getAssets()->flush();
}
/**
* Set up the asset manager, preloaded with a JavaScript compiler and a LESS
* compiler. Automatically add the files necessary to boot a Flarum client,