[ENTITY][Actor] Add helper function for checking if the current actor can admin another

This commit is contained in:
Hugo Sales 2021-12-21 19:09:26 +00:00 committed by Diogo Peralta Cordeiro
parent 63679426b6
commit 80ebd6fb7b
No known key found for this signature in database
GPG Key ID: 18D2D35001FBFAB0

View File

@ -261,6 +261,7 @@ class Actor extends Entity
'subscriber' => "subscriber-{$actor_id}", 'subscriber' => "subscriber-{$actor_id}",
'subscribed' => "subscribed-{$actor_id}", 'subscribed' => "subscribed-{$actor_id}",
'relative-nickname' => "actor-{$actor_id}-relative-nickname-{$other}", // $other is $nickname '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 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 public static function schemaDef(): array
{ {
return [ return [