[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
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
1 changed files with 24 additions and 0 deletions

View File

@ -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 [