Skip to content

Commit

Permalink
Allow optional context for getRoles
Browse files Browse the repository at this point in the history
This allows the identity to return different roles for different contexts. A scenario would be a user having different roles for different projects and the project name is passed in as the context. Implements Solution 1 in ZF-Commons#258 (comment)
  • Loading branch information
zionsg committed Jun 5, 2015
1 parent 651176f commit 0d1a57e
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
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);
}
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

0 comments on commit 0d1a57e

Please sign in to comment.