Skip to content
This repository has been archived by the owner on Jul 3, 2020. It is now read-only.

Allow optional context for getRoles #299

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/ZfcRbac/Identity/IdentityInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ interface IdentityInterface
/**
* Get the list of roles of this identity
*
* @param string $context Optional context for determining roles returned, eg. project name as context
* @return string[]|\Rbac\Role\RoleInterface[]
*/
public function getRoles();
public function getRoles($context = null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep optional arguments out of the contract.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not? It at least enforces that getRoles can receive an optional context.

}
2 changes: 1 addition & 1 deletion src/ZfcRbac/Service/AuthorizationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function getIdentity()
*/
public function isGranted($permission, $context = null)
{
$roles = $this->roleService->getIdentityRoles();
$roles = $this->roleService->getIdentityRoles($context);

if (empty($roles)) {
return false;
Expand Down
5 changes: 3 additions & 2 deletions src/ZfcRbac/Service/RoleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,11 @@ public function getIdentity()
/**
* Get the identity roles from the current identity, applying some more logic
*
* @param string $context Optional context for determining roles returned, eg. project name as context
* @return RoleInterface[]
* @throws Exception\RuntimeException
*/
public function getIdentityRoles()
public function getIdentityRoles($context = null)
{
if (!$identity = $this->getIdentity()) {
return $this->convertRoles([$this->guestRole]);
Expand All @@ -123,7 +124,7 @@ public function getIdentityRoles()
));
}

return $this->convertRoles($identity->getRoles());
return $this->convertRoles($identity->getRoles($context));
}

/**
Expand Down