From 80ebd6fb7b36d1f5d41bcd142b3d16c1cbfeb9e5 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Tue, 21 Dec 2021 19:09:26 +0000 Subject: [PATCH] [ENTITY][Actor] Add helper function for checking if the current actor can admin another --- src/Entity/Actor.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/Entity/Actor.php b/src/Entity/Actor.php index 5ccfa3e07f..b3d94f11ae 100644 --- a/src/Entity/Actor.php +++ b/src/Entity/Actor.php @@ -261,6 +261,7 @@ class Actor extends Entity 'subscriber' => "subscriber-{$actor_id}", 'subscribed' => "subscribed-{$actor_id}", 'relative-nickname' => "actor-{$actor_id}-relative-nickname-{$other}", // $other is $nickname + 'can-admin' => "actor-{$actor_id}-can-admin-{$other}", // $other is an actor id ]; } @@ -595,6 +596,29 @@ class Actor extends Entity return true; // TODO } + /** + * Check whether $this has permission for performing actions on behalf of $other + */ + public function canAdmin(self $other): bool + { + switch ($other->getType()) { + case self::GROUP: + return Cache::get( + self::cacheKeys($this->getId(), $other->getId())['can-admin'], + function () use ($other) { + try { + return DB::findOneBy('group_member', ['group_id' => $other->getId(), 'actor_id' => $this->getId()])->getIsAdmin(); + } catch (NotFoundException) { + return false; + } + }, + ); + break; + default: + return false; + } + } + public static function schemaDef(): array { return [