Skip to content

Commit

Permalink
clear cache on settings change
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Nov 20, 2023
1 parent 5e24cd7 commit b3fbbd3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
7 changes: 4 additions & 3 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Flarum\Api\Serializer\ForumSerializer;
use Flarum\Extend;
use Flarum\Frontend\Document;
use Flarum\Settings\Event\Saving;
use Flarum\User\Event\LoggedOut;
use FoF\Extend\Events\OAuthLoginSuccessful;

Expand Down Expand Up @@ -50,8 +51,7 @@
->register(OAuthServiceProvider::class),

(new Extend\ApiSerializer(ForumSerializer::class))
->attributes(function (ForumSerializer $serializer) {
$attributes = [];
->attributes(function (ForumSerializer $serializer, $model, array $attributes): array {
if ($serializer->getActor()->isGuest()) {
$attributes['fof-oauth'] = resolve('fof-oauth.providers.forum');
}
Expand All @@ -72,7 +72,8 @@

(new Extend\Event())
->listen(OAuthLoginSuccessful::class, Listeners\UpdateEmailFromProvider::class)
->listen(LoggedOut::class, Listeners\HandleLogout::class),
->listen(LoggedOut::class, Listeners\HandleLogout::class)
->listen(Saving::class, Listeners\ClearOAuthCache::class),

(new Extend\ApiSerializer(CurrentUserSerializer::class))
->attributes(Api\CurrentUserAttributes::class),
Expand Down
28 changes: 28 additions & 0 deletions src/Listeners/ClearOAuthCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace FoF\OAuth\Listeners;

use Flarum\Settings\Event\Saving;
use Illuminate\Contracts\Cache\Store as Cache;
use Illuminate\Support\Str;

class ClearOAuthCache
{
protected $cache;

public function __construct(Cache $cache)
{
$this->cache = $cache;
}

public function handle(Saving $event)
{
foreach (array_keys($event->settings) as $key) {
if (Str::startsWith($key, 'fof-oauth')) {
$this->cache->forget('fof-oauth.providers.forum');
$this->cache->forget('fof-oauth.providers.admin');
break; // Exit the loop once the cache is cleared
}
}
}
}

0 comments on commit b3fbbd3

Please sign in to comment.