Skip to content

Commit

Permalink
* add PSR-2 compatibility #1
Browse files Browse the repository at this point in the history
  • Loading branch information
dantodev committed May 20, 2016
1 parent 4172f4e commit 1421952
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 221 deletions.
206 changes: 103 additions & 103 deletions src/Judge.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,117 +5,117 @@
class Judge
{

private $_config;
private $_user;

/**
* @param array|Config $config
* @param UserAccessInterface|null $user
*/
public function __construct($config, UserAccessInterface $user)
{
$this->_config = $config instanceof Config ? $config : new Config($config);
$this->_user = $user;
}

/**
* @param string|string[] $rights
* @param ObjectInterface|null $object
* @param UserAccessInterface|null $user
* @return bool
*/
public function hasRight($rights, ObjectInterface $object = null, UserAccessInterface $user = null)
{
$rights = (array) $rights;
$user = $user ?: $this->_user;
$global_roles = $user->getGlobalRoles();

// check global rights
foreach ($global_roles as $role) {
if ($this->_config->has("global.$role.rights")) {
if ($this->checkRightsInConfig($rights, $this->_config, "global.$role.rights")) {
return true;
}
}
private $_config;
private $_user;

/**
* @param array|Config $config
* @param UserAccessInterface|null $user
*/
public function __construct($config, UserAccessInterface $user)
{
$this->_config = $config instanceof Config ? $config : new Config($config);
$this->_user = $user;
}

if ($object !== null) {
$object_config = $this->getObjectConfig($object);
$object_identifier = $object_config->get("identifier");

// check global rights
foreach ($global_roles as $role) {
if ($this->_config->has("global.$role.related_rights.$object_identifier")) {
if ($this->checkRightsInConfig($rights, $this->_config, "global.$role.related_rights.$object_identifier")) {
return true;
}
/**
* @param string|string[] $rights
* @param ObjectInterface|null $object
* @param UserAccessInterface|null $user
* @return bool
*/
public function hasRight($rights, ObjectInterface $object = null, UserAccessInterface $user = null)
{
$rights = (array)$rights;
$user = $user ?: $this->_user;
$global_roles = $user->getGlobalRoles();

// check global rights
foreach ($global_roles as $role) {
if ($this->_config->has("global.$role.rights")) {
if ($this->checkRightsInConfig($rights, $this->_config, "global.$role.rights")) {
return true;
}
}
}
}

// check object rights
foreach ($object->getObjectRoles($user) as $role) {
if ($this->checkRightsInConfig($rights, $object_config, "roles.$role.rights")) {
return true;
if ($object !== null) {
$object_config = $this->getObjectConfig($object);
$object_identifier = $object_config->get("identifier");

// check global rights
foreach ($global_roles as $role) {
if ($this->_config->has("global.$role.related_rights.$object_identifier")) {
if ($this->checkRightsInConfig($rights, $this->_config, "global.$role.related_rights.$object_identifier")) {
return true;
}
}
}

// check object rights
foreach ($object->getObjectRoles($user) as $role) {
if ($this->checkRightsInConfig($rights, $object_config, "roles.$role.rights")) {
return true;
}
}

// check object related rights
foreach ($object->getRelatedObjects() as $related) {
$related_config = $this->getObjectConfig($related);
foreach ($related->getObjectRoles($user) as $role) {
if ($this->checkRightsInConfig($rights, $related_config, "roles.$role.related_rights.$object_identifier")) {
return true;
}
}
}
}
}

// check object related rights
foreach ($object->getRelatedObjects() as $related) {
$related_config = $this->getObjectConfig($related);
foreach ($related->getObjectRoles($user) as $role) {
if ($this->checkRightsInConfig($rights, $related_config, "roles.$role.related_rights.$object_identifier")) {
return true;
}

return false;
}

/**
* @param string $role
* @param ObjectInterface|null $object
* @param UserAccessInterface|null $user
* @return bool
*/
public function hasRole($role, ObjectInterface $object = null, UserAccessInterface $user = null)
{
$user = $user ?: $this->_user;
return in_array($role, $object == null ? $user->getGlobalRoles() : $object->getObjectRoles($user));
}

/**
* @param ObjectInterface $object
* @throws \RuntimeException
* @return Config
*/
private function getObjectConfig(ObjectInterface $object)
{
$object_class = get_class($object);
if ($this->_config->has("objects.$object_class")) {
return new Config($this->_config->get("objects.$object_class"));
}
}
throw new \RuntimeException("Given object is not configured for judge.");
}

/**
* @param array $rights
* @param Config $config
* @param $path
* @return bool
*/
private function checkRightsInConfig(array $rights, Config $config, $path)
{
return $config->has($path) && empty(array_diff($rights, $config->get($path)));
}

return false;
}

/**
* @param string $role
* @param ObjectInterface|null $object
* @param UserAccessInterface|null $user
* @return bool
*/
public function hasRole($role, ObjectInterface $object = null, UserAccessInterface $user = null)
{
$user = $user ?: $this->_user;
return in_array($role, $object == null ? $user->getGlobalRoles() : $object->getObjectRoles($user));
}

/**
* @param ObjectInterface $object
* @throws \RuntimeException
* @return Config
*/
private function getObjectConfig(ObjectInterface $object)
{
$object_class = get_class($object);
if ($this->_config->has("objects.$object_class")) {
return new Config($this->_config->get("objects.$object_class"));
/**
* @param UserAccessInterface $user
*/
public function setUser($user)
{
$this->_user = $user;
}
throw new \RuntimeException("Given object is not configured for judge.");
}

/**
* @param array $rights
* @param Config $config
* @param $path
* @return bool
*/
private function checkRightsInConfig(array $rights, Config $config, $path)
{
return $config->has($path) && empty(array_diff($rights, $config->get($path)));
}

/**
* @param UserAccessInterface $user
*/
public function setUser($user)
{
$this->_user = $user;
}

}
28 changes: 14 additions & 14 deletions src/ObjectInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
interface ObjectInterface
{

/**
* Returns an array of roles related through this object instance to given user instance.
* Keep in mind that there are no recursive checks by AccessControl. You have to do this on your own in this method.
*
* @param UserAccessInterface $user
* @return string[]
*/
public function getObjectRoles(UserAccessInterface $user);
/**
* Returns an array of roles related through this object instance to given user instance.
* Keep in mind that there are no recursive checks by AccessControl. You have to do this on your own in this method.
*
* @param UserAccessInterface $user
* @return string[]
*/
public function getObjectRoles(UserAccessInterface $user);

/**
* Returns an array of relevant objects related to this object instance.
*
* @return ObjectInterface[]
*/
public function getRelatedObjects();
/**
* Returns an array of relevant objects related to this object instance.
*
* @return ObjectInterface[]
*/
public function getRelatedObjects();

}
12 changes: 6 additions & 6 deletions src/UserAccessInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
interface UserAccessInterface
{

/**
* Returns an array of roles in connection with this user
*
* @return string[]
*/
public function getGlobalRoles();
/**
* Returns an array of roles in connection with this user
*
* @return string[]
*/
public function getGlobalRoles();

}
Loading

0 comments on commit 1421952

Please sign in to comment.