[PLUGIN][Pinboard] Invalidate token and it's cache when actor information is changed via ActorForms

This commit is contained in:
Hugo Sales 2022-03-31 03:47:14 +01:00
parent dd70de20da
commit 94ab4ce8c4
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
2 changed files with 21 additions and 3 deletions

View File

@ -35,7 +35,10 @@ namespace Plugin\Pinboard;
use App\Core\Event;
use App\Core\Modules\Plugin;
use App\Core\Router;
use App\Entity\Actor;
use App\Entity\LocalUser;
use Plugin\Pinboard\Controller as C;
use Plugin\Pinboard\Entity\Token;
use Symfony\Component\HttpFoundation\Request;
class Pinboard extends Plugin
@ -102,4 +105,17 @@ class Pinboard extends Plugin
}
return Event::next;
}
public function onActorFormInvalidateRelated(Actor $actor, ?LocalUser $user)
{
$user ??= $actor->getLocal();
if (!$user instanceof LocalUser) {
return Event::next;
}
Cache::delete(Token::cacheKeys($user->getNickname())['user-token']);
DB::remove(DB::refetch(Token::get(nickname: null, token: null, user: $user)));
DB::flush();
// TODO notify user that their token got invalidated
return Event::next;
}
}

View File

@ -35,6 +35,7 @@ declare(strict_types = 1);
namespace App\Util\Form;
use App\Core\Cache;
use App\Core\Event;
use App\Core\Form;
use function App\Core\I18n\_m;
use App\Entity\Actor;
@ -116,10 +117,11 @@ class ActorForms
}
// Delete related cache
$cache_keys = Actor::cacheKeys($target->getId());
foreach (['id', 'nickname', 'fullname'] as $key) {
Cache::delete($cache_keys[$key]);
$cache_keys = array_intersect(Actor::cacheKeys($target->getId()), array_flip(['id', 'nickname', 'fullname']));
foreach ($cache_keys as $key) {
Cache::delete($key);
}
Event::handle('ActorFormInvalidateRelated', [$target, $local]);
};
return Form::handle(