From cf280e39b4220b4caad22e02d6090529b0fb18bc Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Tue, 4 Aug 2015 10:40:04 +0930 Subject: [PATCH] Move config/permission actions to API; clean up cache flushing --- .../src/components/PermissionDropdown.js | 2 +- .../core/js/admin/src/utils/saveConfig.js | 2 +- .../core/src/Admin/AdminServiceProvider.php | 12 ------- .../Actions/ConfigAction.php} | 33 +++++++++++-------- .../Api/Actions/Extensions/UpdateAction.php | 10 +++--- .../Actions/PermissionAction.php} | 20 ++++++----- framework/core/src/Api/ApiServiceProvider.php | 16 ++++++++- framework/core/src/Assets/AssetManager.php | 6 ++++ .../core/src/Assets/RevisionCompiler.php | 12 +++++++ framework/core/src/Support/ClientAction.php | 11 +++++++ 10 files changed, 81 insertions(+), 43 deletions(-) rename framework/core/src/{Admin/Actions/UpdateConfigAction.php => Api/Actions/ConfigAction.php} (51%) rename framework/core/src/{Admin/Actions/UpdatePermissionAction.php => Api/Actions/PermissionAction.php} (50%) diff --git a/framework/core/js/admin/src/components/PermissionDropdown.js b/framework/core/js/admin/src/components/PermissionDropdown.js index 84f005b7c..866a07a45 100644 --- a/framework/core/js/admin/src/components/PermissionDropdown.js +++ b/framework/core/js/admin/src/components/PermissionDropdown.js @@ -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} }); } diff --git a/framework/core/js/admin/src/utils/saveConfig.js b/framework/core/js/admin/src/utils/saveConfig.js index b17787ab1..84b4604ee 100644 --- a/framework/core/js/admin/src/utils/saveConfig.js +++ b/framework/core/js/admin/src/utils/saveConfig.js @@ -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; diff --git a/framework/core/src/Admin/AdminServiceProvider.php b/framework/core/src/Admin/AdminServiceProvider.php index a1fd767fb..be60a09d3 100644 --- a/framework/core/src/Admin/AdminServiceProvider.php +++ b/framework/core/src/Admin/AdminServiceProvider.php @@ -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) diff --git a/framework/core/src/Admin/Actions/UpdateConfigAction.php b/framework/core/src/Api/Actions/ConfigAction.php similarity index 51% rename from framework/core/src/Admin/Actions/UpdateConfigAction.php rename to framework/core/src/Api/Actions/ConfigAction.php index aedede1dc..b9023b28c 100644 --- a/framework/core/src/Admin/Actions/UpdateConfigAction.php +++ b/framework/core/src/Api/Actions/ConfigAction.php @@ -1,12 +1,13 @@ -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); } } diff --git a/framework/core/src/Api/Actions/Extensions/UpdateAction.php b/framework/core/src/Api/Actions/Extensions/UpdateAction.php index 8d6260eb5..c110eda12 100644 --- a/framework/core/src/Api/Actions/Extensions/UpdateAction.php +++ b/framework/core/src/Api/Actions/Extensions/UpdateAction.php @@ -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(); } } diff --git a/framework/core/src/Admin/Actions/UpdatePermissionAction.php b/framework/core/src/Api/Actions/PermissionAction.php similarity index 50% rename from framework/core/src/Admin/Actions/UpdatePermissionAction.php rename to framework/core/src/Api/Actions/PermissionAction.php index 80379a19a..806b9e13e 100644 --- a/framework/core/src/Admin/Actions/UpdatePermissionAction.php +++ b/framework/core/src/Api/Actions/PermissionAction.php @@ -1,19 +1,23 @@ -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); } } diff --git a/framework/core/src/Api/ApiServiceProvider.php b/framework/core/src/Api/ApiServiceProvider.php index 55325dcad..4002bdd89 100644 --- a/framework/core/src/Api/ApiServiceProvider.php +++ b/framework/core/src/Api/ApiServiceProvider.php @@ -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)); } diff --git a/framework/core/src/Assets/AssetManager.php b/framework/core/src/Assets/AssetManager.php index 3c9e45da3..52871eeb0 100644 --- a/framework/core/src/Assets/AssetManager.php +++ b/framework/core/src/Assets/AssetManager.php @@ -57,4 +57,10 @@ class AssetManager { return $this->js->getFile(); } + + public function flush() + { + $this->less->flush(); + $this->js->flush(); + } } diff --git a/framework/core/src/Assets/RevisionCompiler.php b/framework/core/src/Assets/RevisionCompiler.php index 148f2901c..486230d4a 100644 --- a/framework/core/src/Assets/RevisionCompiler.php +++ b/framework/core/src/Assets/RevisionCompiler.php @@ -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); + } } diff --git a/framework/core/src/Support/ClientAction.php b/framework/core/src/Support/ClientAction.php index a1302a901..b02748d56 100644 --- a/framework/core/src/Support/ClientAction.php +++ b/framework/core/src/Support/ClientAction.php @@ -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,