PermissionAttachmentInfo now includes a reference to the PermissionAttachmentInfo that caused its assignment (if any)

this allows plugins to easily identify which permission group caused a permission to be assigned (particularly if multiple groups might cause it).
This commit is contained in:
Dylan K. Taylor 2020-12-02 11:23:35 +00:00
parent 880635603c
commit 506e76917e
2 changed files with 17 additions and 7 deletions

View File

@ -146,13 +146,13 @@ class PermissibleBase implements Permissible{
if($perm === null){ if($perm === null){
throw new \InvalidStateException("Unregistered root permission $name"); throw new \InvalidStateException("Unregistered root permission $name");
} }
$this->permissions[$name] = new PermissionAttachmentInfo($name, null, $isGranted); $this->permissions[$name] = new PermissionAttachmentInfo($name, null, $isGranted, null);
$permManager->subscribeToPermission($name, $this); $permManager->subscribeToPermission($name, $this);
$this->calculateChildPermissions($perm->getChildren(), !$isGranted, null); $this->calculateChildPermissions($perm->getChildren(), !$isGranted, null, $this->permissions[$name]);
} }
foreach($this->attachments as $attachment){ foreach($this->attachments as $attachment){
$this->calculateChildPermissions($attachment->getPermissions(), false, $attachment); $this->calculateChildPermissions($attachment->getPermissions(), false, $attachment, null);
} }
$diff = []; $diff = [];
@ -189,16 +189,16 @@ class PermissibleBase implements Permissible{
/** /**
* @param bool[] $children * @param bool[] $children
*/ */
private function calculateChildPermissions(array $children, bool $invert, ?PermissionAttachment $attachment) : void{ private function calculateChildPermissions(array $children, bool $invert, ?PermissionAttachment $attachment, ?PermissionAttachmentInfo $parent) : void{
$permManager = PermissionManager::getInstance(); $permManager = PermissionManager::getInstance();
foreach($children as $name => $v){ foreach($children as $name => $v){
$perm = $permManager->getPermission($name); $perm = $permManager->getPermission($name);
$value = ($v xor $invert); $value = ($v xor $invert);
$this->permissions[$name] = new PermissionAttachmentInfo($name, $attachment, $value); $this->permissions[$name] = new PermissionAttachmentInfo($name, $attachment, $value, $parent);
$permManager->subscribeToPermission($name, $this); $permManager->subscribeToPermission($name, $this);
if($perm instanceof Permission){ if($perm instanceof Permission){
$this->calculateChildPermissions($perm->getChildren(), !$value, $attachment); $this->calculateChildPermissions($perm->getChildren(), !$value, $attachment, $this->permissions[$name]);
} }
} }
} }

View File

@ -33,10 +33,14 @@ class PermissionAttachmentInfo{
/** @var bool */ /** @var bool */
private $value; private $value;
public function __construct(string $permission, ?PermissionAttachment $attachment, bool $value){ /** @var PermissionAttachmentInfo|null */
private $groupPermission;
public function __construct(string $permission, ?PermissionAttachment $attachment, bool $value, ?PermissionAttachmentInfo $groupPermission){
$this->permission = $permission; $this->permission = $permission;
$this->attachment = $attachment; $this->attachment = $attachment;
$this->value = $value; $this->value = $value;
$this->groupPermission = $groupPermission;
} }
public function getPermission() : string{ public function getPermission() : string{
@ -50,4 +54,10 @@ class PermissionAttachmentInfo{
public function getValue() : bool{ public function getValue() : bool{
return $this->value; return $this->value;
} }
/**
* Returns the info of the permission group that caused this permission to be set, if any.
* If null, the permission was set explicitly, either by a permission attachment or base permission.
*/
public function getGroupPermissionInfo() : ?PermissionAttachmentInfo{ return $this->groupPermission; }
} }