Skip to content

Commit

Permalink
feat: add permission and LSCache purge button for discussions
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaucau committed Dec 28, 2024
1 parent bcadb24 commit 8f8a6a9
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 22 deletions.
8 changes: 8 additions & 0 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace ACPL\FlarumLSCache;

use Flarum\Api\Serializer\UserSerializer;
use ACPL\FlarumLSCache\Api\Controller\{LSCacheCsrfResponseController, PurgeLSCacheController};
use ACPL\FlarumLSCache\Command\LSCachePurgeCommand;
use ACPL\FlarumLSCache\Compatibility\{
Expand Down Expand Up @@ -57,6 +58,13 @@
->default('acpl-lscache.status_codes_cache', "404 3600\n403 3600\n500 120"),
(new Extend\Event())->listen(Saved::class, Listener\UpdateSettingsListener::class),

// Permissions
(new Extend\ApiSerializer(UserSerializer::class))
->attribute(
'canPurgeLSCache',
fn (UserSerializer $serializer) => $serializer->getActor()->can('lscache.purge'),
),

// Vary cookie
(new Extend\Middleware('forum'))->insertAfter(CheckCsrfToken::class, VaryCookieMiddleware::class),
(new Extend\Middleware('admin'))->insertAfter(CheckCsrfToken::class, VaryCookieMiddleware::class),
Expand Down
7 changes: 7 additions & 0 deletions js/src/@types/shims.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'flarum/common/models/User';

declare module 'flarum/common/models/User' {
export default interface User {
canPurgeLSCache(): boolean;
}
}
18 changes: 3 additions & 15 deletions js/src/admin/addPurgeLSCacheButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,10 @@ import app from 'flarum/admin/app';
import { extend } from 'flarum/common/extend';
import StatusWidget from 'flarum/admin/components/StatusWidget';
import Button from 'flarum/common/components/Button';
import type ItemList from 'flarum/common/utils/ItemList';
import type { Children } from 'mithril';

function handleClearLSCache() {
app
.request({
url: `${app.forum.attribute('apiUrl')}/lscache-purge`,
method: 'GET',
})
.then(() => {
app.alerts.show({ type: 'success' }, app.translator.trans('acpl-lscache.admin.purge_all_success'));
});
}
import purgeLSCache from '../common/purgeLSCache';

export default () => {
extend(StatusWidget.prototype, 'toolsItems', (items: ItemList<Children>) => {
items.add('clearLSCache', <Button onclick={handleClearLSCache}>{app.translator.trans('acpl-lscache.admin.purge_all')}</Button>);
extend(StatusWidget.prototype, 'toolsItems', (items) => {
items.add('clearLSCache', <Button onclick={() => purgeLSCache()}>{app.translator.trans('acpl-lscache.admin.purge_all')}</Button>);
});
};
14 changes: 11 additions & 3 deletions js/src/admin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import Link from 'flarum/common/components/Link';
import addPurgeLSCacheButton from './addPurgeLSCacheButton';

app.initializers.add('acpl-lscache', () => {
addPurgeLSCacheButton();

app.extensionData
.for('acpl-lscache')
.registerSetting({
Expand Down Expand Up @@ -79,7 +81,13 @@ app.initializers.add('acpl-lscache', () => {
label: app.translator.trans('acpl-lscache.admin.status_codes_cache_label'),
help: app.translator.trans('acpl-lscache.admin.status_codes_cache_help'),
type: 'textarea',
});

addPurgeLSCacheButton();
})
.registerPermission(
{
icon: 'fas fa-broom',
label: app.translator.trans('acpl-lscache.admin.permissions.purge'),
permission: 'lscache.purge',
},
'moderate'
);
});
24 changes: 24 additions & 0 deletions js/src/common/purgeLSCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import app from 'flarum/common/app';

export default async function purgeLSCache(tags?: string[], paths?: string[]) {
const queryParams: Record<string, string[]> = {};

if (tags?.length) {
queryParams.tags = tags;
}

if (paths?.length) {
queryParams.paths = paths;
}

await app.request({
url: `${app.forum.attribute('apiUrl')}/lscache-purge`,
method: 'GET',
params: queryParams,
});

app.alerts.show(
{ type: 'success' },
app.translator.trans(!tags?.length && !paths?.length ? 'acpl-lscache.lib.purge_all_success' : 'acpl-lscache.lib.purge_success')
);
}
4 changes: 4 additions & 0 deletions js/src/forum/extend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Extend from 'flarum/common/extenders';
import User from 'flarum/common/models/User';

export default [new Extend.Model(User).attribute<boolean>('canPurgeLSCache')];
23 changes: 23 additions & 0 deletions js/src/forum/extendDiscussionControls.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import app from 'flarum/forum/app';
import { extend } from 'flarum/common/extend';
import DiscussionControls from 'flarum/forum/utils/DiscussionControls';
import Button from 'flarum/common/components/Button';
import purgeLSCache from '../common/purgeLSCache';

export default function extendDiscussionControls() {
extend(DiscussionControls, 'moderationControls', (items, discussion) => {
const discussionId = discussion.id();
const { user } = app.session;

if (!discussionId || !user || !user.canPurgeLSCache()) {
return;
}

items.add(
'acpl-lscache-purge',
<Button icon="fas fa-broom" onclick={() => purgeLSCache([`discussion_${discussionId}`])}>
{app.translator.trans('acpl-lscache.forum.purge.discussion')}
</Button>
);
});
}
4 changes: 4 additions & 0 deletions js/src/forum/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ import app from 'flarum/forum/app';
import { extend } from 'flarum/common/extend';
import Modal from 'flarum/common/components/Modal';
import updateCSRF from './utils/updateCSRF';
import extendDiscussionControls from './extendDiscussionControls';

export { default as extend } from './extend';

app.initializers.add('acpl-lscache', () => {
// We extend each modal to also include those added by external extensions
extend(Modal.prototype, 'oninit', updateCSRF);
extendDiscussionControls();
});
17 changes: 15 additions & 2 deletions locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,18 @@ acpl-lscache:
status_codes_cache_label: "Default HTTP Status Code Page TTL"
status_codes_cache_help: "Specify an HTTP status code and the number of seconds to cache that page, separated by a space. One per line."

purge_all: "Purge All LSCache"
purge_all_success: "LiteSpeed Web Server has been notified to purge all LSCache entries"
purge_all: => acpl-lscache.ref.purge_cache

permissions:
purge: => acpl-lscache.ref.purge_cache

forum:
purge:
discussion: => acpl-lscache.ref.purge_cache

lib:
purge_success: "Notified LiteSpeed Server to purge LSCache entries"
purge_all_success: "Notified LiteSpeed Server to purge all LSCache entries"

ref:
purge_cache: "Purge LSCache"
4 changes: 2 additions & 2 deletions src/Api/Controller/PurgeLSCacheController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public function handle(ServerRequestInterface $request): ResponseInterface
{
$canPurge = false;

if (RequestUtil::getActor($request)->isAdmin()) {
if (RequestUtil::getActor($request)->can('lscache.purge')) {
$canPurge = true;
}

//If a command is used, use the temporary key because the user is not logged in
// If a command is used, use the temporary key because the user is not logged in
if (! $canPurge) {
$key = $this->settings->get('acpl-lscache.purgeKey');
$reqKey = $request->getHeaderLine('LSCachePurgeKey');
Expand Down

0 comments on commit 8f8a6a9

Please sign in to comment.