From 80f4be6a53b1ccb730f60ea0187defa519992627 Mon Sep 17 00:00:00 2001 From: Axel VENET Date: Thu, 2 Feb 2017 14:14:21 +0100 Subject: [PATCH] fix code style with PHp CS fixer --- example.php | 16 +- example/Country.php | 17 +- example/User.php | 94 +++-- example/Vehicle.php | 61 ++-- example/Visa.php | 31 +- src/Abac.php | 323 +++++++++--------- src/Cache/Exception/ExpiredCacheException.php | 4 +- src/Cache/Item/MemoryCacheItem.php | 32 +- src/Cache/Item/TextCacheItem.php | 32 +- src/Cache/Pool/MemoryCacheItemPool.php | 48 +-- src/Cache/Pool/TextCacheItemPool.php | 61 ++-- src/Comparison/AbstractComparison.php | 8 +- src/Comparison/ArrayComparison.php | 11 +- src/Comparison/BooleanComparison.php | 3 +- src/Comparison/DatetimeComparison.php | 2 +- src/Comparison/NumericComparison.php | 4 +- src/Comparison/ObjectComparison.php | 3 +- src/Comparison/UserComparison.php | 3 +- src/Loader/AbacLoader.php | 84 ++--- src/Loader/JsonAbacLoader.php | 14 +- src/Loader/YamlAbacLoader.php | 12 +- src/Manager/AttributeManager.php | 62 ++-- src/Manager/CacheManager.php | 19 +- src/Manager/ComparisonManager.php | 34 +- src/Manager/ConfigurationManager.php | 213 ++++++------ src/Manager/PolicyRuleManager.php | 2 +- src/Model/AbstractAttribute.php | 1 - src/Model/Attribute.php | 6 +- src/Model/PolicyRuleAttribute.php | 54 +-- tests/AbacTest.php | 81 +++-- tests/Cache/Item/MemoryCacheItemTest.php | 29 +- tests/Cache/Item/TextCacheItemTest.php | 27 +- tests/Cache/Pool/MemoryCacheItemPoolTest.php | 38 ++- tests/Cache/Pool/TextCacheItemPoolTest.php | 39 ++- tests/Comparison/ArrayComparisonTest.php | 7 +- tests/Comparison/BooleanComparisonTest.php | 6 +- tests/Comparison/DatetimeComparisonTest.php | 6 +- tests/Comparison/ObjectComparisonTest.php | 3 +- tests/Comparison/UserComparisonTest.php | 3 +- tests/Loader/JsonAbacLoaderTest.php | 50 +-- tests/Loader/YamlAbacLoaderTest.php | 50 +-- tests/Manager/AttributeManagerTest.php | 12 +- tests/Manager/CacheManagerTest.php | 15 +- tests/Manager/ComparisonManagerTest.php | 29 +- tests/Manager/ConfigurationManagerTest.php | 11 +- tests/fixtures/users.php | 2 +- tests/fixtures/visas.php | 2 +- 47 files changed, 934 insertions(+), 730 deletions(-) diff --git a/example.php b/example.php index fa42617..eaaf809 100644 --- a/example.php +++ b/example.php @@ -3,7 +3,7 @@ require_once('vendor/autoload.php'); use PhpAbac\Abac; - + $countries = include('tests/fixtures/countries.php'); $visas = include('tests/fixtures/visas.php'); $users = include('tests/fixtures/users.php'); @@ -35,7 +35,7 @@ $user1Vehicle = $abac->enforce('vehicle-homologation', $users[0], $vehicles[0], [ 'dynamic_attributes' => ['proprietaire' => 1] ]); - if($user1Vehicle === true) { + if ($user1Vehicle === true) { echo("GRANTED : The vehicle 1 is able to be approved for the user 1\n"); } else { echo("FAIL : The system didn't grant access\n"); @@ -43,7 +43,7 @@ $user3Vehicle = $abac->enforce('vehicle-homologation', $users[2], $vehicles[1], [ 'dynamic_attributes' => ['proprietaire' => 3] ]); - if(!$user3Vehicle !== true) { + if (!$user3Vehicle !== true) { echo("DENIED : The vehicle 2 is not approved for the user 3 because its last technical review is too old\n"); } else { echo("FAIL : The system didn't deny access\n"); @@ -51,7 +51,7 @@ $user4Vehicle = $abac->enforce('vehicle-homologation', $users[3], $vehicles[3], [ 'dynamic_attributes' => ['proprietaire' => 4] ]); - if($user4Vehicle !== true) { + if ($user4Vehicle !== true) { echo("DENIED : The vehicle 4 is not able to be approved for the user 4 because he has no driving license\n"); } else { echo("FAIL : The system didn't deny access\n"); @@ -59,7 +59,7 @@ $user5Vehicle = $abac->enforce('vehicle-homologation', $users[3], $vehicles[3], [ 'dynamic_attributes' => ['proprietaire' => 1] ]); - if($user5Vehicle !== true) { + if ($user5Vehicle !== true) { echo("DENIED : The vehicle 4 is not able to be approved for the user 2 because he doesn't own the vehicle\n"); } else { echo("FAIL : The system didn't deny access\n"); @@ -69,7 +69,7 @@ 'code-pays' => 'US' ] ]); - if($userTravel1 !== true) { + if ($userTravel1 !== true) { echo("DENIED: The user 1 is not allowed to travel to the USA because he doesn't have an US visa\n"); } else { echo('FAIL: The system didn\'t deny access'); @@ -79,8 +79,8 @@ 'code-pays' => 'US' ] ]); - if($userTravel2 === true) { + if ($userTravel2 === true) { echo("GRANTED: The user 2 is allowed to travel to the USA\n"); } else { echo('FAIL: The system didn\'t grant access'); - } \ No newline at end of file + } diff --git a/example/Country.php b/example/Country.php index 7c33487..0f614e3 100644 --- a/example/Country.php +++ b/example/Country.php @@ -2,7 +2,8 @@ namespace PhpAbac\Example; -class Country { +class Country +{ /** @var string **/ protected $name; /** @var string **/ @@ -12,7 +13,8 @@ class Country { * @param string $name * @return \PhpAbac\Example\Country */ - public function setName($name) { + public function setName($name) + { $this->name = $name; return $this; @@ -21,7 +23,8 @@ public function setName($name) { /** * @return string */ - public function getName() { + public function getName() + { return $this->name; } @@ -29,7 +32,8 @@ public function getName() { * @param string $code * @return \PhpAbac\Example\Country */ - public function setCode($code) { + public function setCode($code) + { $this->code = $code; return $this; @@ -38,7 +42,8 @@ public function setCode($code) { /** * @return string */ - public function getCode() { + public function getCode() + { return $this->code; } -} \ No newline at end of file +} diff --git a/example/User.php b/example/User.php index b134fe0..c1f71fb 100644 --- a/example/User.php +++ b/example/User.php @@ -2,7 +2,8 @@ namespace PhpAbac\Example; -class User { +class User +{ /** @var int **/ private $id; /** @var string **/ @@ -24,7 +25,8 @@ class User { * @param int $id * @return \PhpAbac\Example\User */ - public function setId($id) { + public function setId($id) + { $this->id = $id; return $this; @@ -33,7 +35,8 @@ public function setId($id) { /** * @return int */ - public function getId() { + public function getId() + { return $this->id; } @@ -41,7 +44,8 @@ public function getId() { * @param string $name * @return \PhpAbac\Example\User */ - public function setName($name) { + public function setName($name) + { $this->name = $name; return $this; @@ -50,7 +54,8 @@ public function setName($name) { /** * @return string */ - public function getName() { + public function getName() + { return $this->name; } @@ -58,7 +63,8 @@ public function getName() { * @param int $age * @return \PhpAbac\Example\User */ - public function setAge($age) { + public function setAge($age) + { $this->age = $age; return $this; @@ -67,7 +73,8 @@ public function setAge($age) { /** * @return int */ - public function getAge() { + public function getAge() + { return $this->age; } @@ -75,7 +82,8 @@ public function getAge() { * @param string $parentNationality * @return \PhpAbac\Example\User */ - public function setParentNationality($parentNationality) { + public function setParentNationality($parentNationality) + { $this->parentNationality = $parentNationality; return $this; @@ -84,7 +92,8 @@ public function setParentNationality($parentNationality) { /** * @return bool */ - public function getParentNationality() { + public function getParentNationality() + { return $this->parentNationality; } @@ -92,7 +101,8 @@ public function getParentNationality() { * @param \PhpAbac\Example\Visa $visa * @return \PhpAbac\Example\User */ - public function addVisa(Visa $visa) { + public function addVisa(Visa $visa) + { $this->visas[$visa->getId()] = $visa; return $this; @@ -102,8 +112,9 @@ public function addVisa(Visa $visa) { * @param \PhpAbac\Example\Visa $visa * @return \PhpAbac\Example\User */ - public function removeVisa(Visa $visa) { - if(isset($this->visas[$visa->getId()])) { + public function removeVisa(Visa $visa) + { + if (isset($this->visas[$visa->getId()])) { unset($this->visas[$visa->getId()]); } return $this; @@ -112,32 +123,36 @@ public function removeVisa(Visa $visa) { /** * @return array */ - public function getVisas() { + public function getVisas() + { return $this->visas; } - - /** - * Return a specific visa - * - * @param Visa $visa - * - * @return mixed|null - */ - public function getVisa($country_code) { - /** @var Visa $visa */ - $visas = []; - foreach($this->visas as $visa) { - if ($visa->getCountry()->getCode() == $country_code) - $visas[] = $visa; - } - return $visas; - } + + /** + * Return a specific visa + * + * @param Visa $visa + * + * @return mixed|null + */ + public function getVisa($country_code) + { + /** @var Visa $visa */ + $visas = []; + foreach ($this->visas as $visa) { + if ($visa->getCountry()->getCode() == $country_code) { + $visas[] = $visa; + } + } + return $visas; + } /** * @param bool $hasDoneJapd * @return \PhpAbac\Example\User */ - public function setHasDoneJapd($hasDoneJapd) { + public function setHasDoneJapd($hasDoneJapd) + { $this->hasDoneJapd = $hasDoneJapd; return $this; @@ -146,7 +161,8 @@ public function setHasDoneJapd($hasDoneJapd) { /** * @return bool */ - public function getHasDoneJapd() { + public function getHasDoneJapd() + { return $this->hasDoneJapd; } @@ -154,7 +170,8 @@ public function getHasDoneJapd() { * @param bool $hasDrivingLicense * @return \PhpAbac\Example\User */ - public function setHasDrivingLicense($hasDrivingLicense) { + public function setHasDrivingLicense($hasDrivingLicense) + { $this->hasDrivingLicense = $hasDrivingLicense; return $this; @@ -163,7 +180,8 @@ public function setHasDrivingLicense($hasDrivingLicense) { /** * @return bool */ - public function getHasDrivingLicense() { + public function getHasDrivingLicense() + { return $this->hasDrivingLicense; } @@ -173,7 +191,8 @@ public function getHasDrivingLicense() { * * @param $country */ - public function setCountry($country) { + public function setCountry($country) + { $this->country = $country; return $this; @@ -182,7 +201,8 @@ public function setCountry($country) { /** * @return string Iso code of the user country */ - public function getCountry() { + public function getCountry() + { return $this->country; } -} \ No newline at end of file +} diff --git a/example/Vehicle.php b/example/Vehicle.php index 1a9cbf7..c2c4755 100644 --- a/example/Vehicle.php +++ b/example/Vehicle.php @@ -2,7 +2,8 @@ namespace PhpAbac\Example; -class Vehicle { +class Vehicle +{ /** @var int **/ private $id; /** @var \PhpAbac\Example\User **/ @@ -26,7 +27,8 @@ class Vehicle { * @param int $id * @return \PhpAbac\Example\Vehicle */ - public function setId($id) { + public function setId($id) + { $this->id = $id; return $this; @@ -35,7 +37,8 @@ public function setId($id) { /** * @return int */ - public function getId() { + public function getId() + { return $this->id; } @@ -43,7 +46,8 @@ public function getId() { * @param \PhpAbac\Example\User $owner * @return \PhpAbac\Example\Vehicle */ - public function setOwner(User $owner) { + public function setOwner(User $owner) + { $this->owner = $owner; return $this; @@ -52,7 +56,8 @@ public function setOwner(User $owner) { /** * @return \PhpAbac\Example\User */ - public function getOwner() { + public function getOwner() + { return $this->owner; } @@ -60,7 +65,8 @@ public function getOwner() { * @param string $brand * @return \PhpAbac\Example\Vehicle */ - public function setBrand($brand) { + public function setBrand($brand) + { $this->brand = $brand; return $this; @@ -69,16 +75,18 @@ public function setBrand($brand) { /** * @return string */ - public function getBrand() { + public function getBrand() + { return $this->brand; } /** - * + * * @param string $model * @return \PhpAbac\Example\Vehicle */ - public function setModel($model) { + public function setModel($model) + { $this->model = $model; return $this; @@ -87,7 +95,8 @@ public function setModel($model) { /** * @return string */ - public function getModel() { + public function getModel() + { return $this->model; } @@ -95,7 +104,8 @@ public function getModel() { * @param \DateTime $lastTechnicalReviewDate * @return \PhpAbac\Example\Vehicle */ - public function setLastTechnicalReviewDate(\DateTime $lastTechnicalReviewDate) { + public function setLastTechnicalReviewDate(\DateTime $lastTechnicalReviewDate) + { $this->lastTechnicalReviewDate = $lastTechnicalReviewDate; return $this; @@ -104,7 +114,8 @@ public function setLastTechnicalReviewDate(\DateTime $lastTechnicalReviewDate) { /** * @return \DateTime */ - public function getLastTechnicalReviewDate() { + public function getLastTechnicalReviewDate() + { return $this->lastTechnicalReviewDate; } @@ -112,43 +123,51 @@ public function getLastTechnicalReviewDate() { * @param \DateTime $manufactureDate * @return \PhpAbac\Example\Vehicle */ - public function setManufactureDate(\DateTime $manufactureDate) { + public function setManufactureDate(\DateTime $manufactureDate) + { $this->manufactureDate = $manufactureDate; return $this; } - public function getManufactureDate() { + public function getManufactureDate() + { return $this->manufactureDate; } - public function setOrigin($origin) { + public function setOrigin($origin) + { $this->origin = $origin; return $this; } - public function getOrigin() { + public function getOrigin() + { return $this->origin; } - public function setEngineType($engineType) { + public function setEngineType($engineType) + { $this->engineType = $engineType; return $this; } - public function getEngineType() { + public function getEngineType() + { return $this->engineType; } - public function setEcoClass($ecoClass) { + public function setEcoClass($ecoClass) + { $this->ecoClass = $ecoClass; return $this; } - public function getEcoClass() { + public function getEcoClass() + { return $this->ecoClass; } -} \ No newline at end of file +} diff --git a/example/Visa.php b/example/Visa.php index ceca28d..3cfb692 100644 --- a/example/Visa.php +++ b/example/Visa.php @@ -2,7 +2,8 @@ namespace PhpAbac\Example; -class Visa { +class Visa +{ /** @var int **/ protected $id; /** @var Country **/ @@ -16,7 +17,8 @@ class Visa { * @param int $id * @return \PhpAbac\Example\Visa */ - public function setId($id) { + public function setId($id) + { $this->id = $id; return $this; @@ -25,15 +27,17 @@ public function setId($id) { /** * @return int */ - public function getId() { + public function getId() + { return $this->id; - } + } /** * @param Country $country * @return \PhpAbac\Example\Visa */ - public function setCountry(Country $country) { + public function setCountry(Country $country) + { $this->country = $country; return $this; @@ -42,7 +46,8 @@ public function setCountry(Country $country) { /** * @return Country */ - public function getCountry() { + public function getCountry() + { return $this->country; } @@ -50,7 +55,8 @@ public function getCountry() { * @param \DateTime $createdAt * @return \PhpAbac\Example\Visa */ - public function setCreatedAt(\DateTime $createdAt) { + public function setCreatedAt(\DateTime $createdAt) + { $this->createdAt = $createdAt; return $this; @@ -59,7 +65,8 @@ public function setCreatedAt(\DateTime $createdAt) { /** * @return \DateTime */ - public function getCreatedAt() { + public function getCreatedAt() + { return $this->createdAt; } @@ -67,7 +74,8 @@ public function getCreatedAt() { * @param \DateTime $lastRenewal * @return \PhpAbac\Example\Visa */ - public function setLastRenewal(\DateTime $lastRenewal) { + public function setLastRenewal(\DateTime $lastRenewal) + { $this->lastRenewal = $lastRenewal; return $this; @@ -76,7 +84,8 @@ public function setLastRenewal(\DateTime $lastRenewal) { /** * @return \DateTime */ - public function getLastRenewal() { + public function getLastRenewal() + { return $this->lastRenewal; } -} \ No newline at end of file +} diff --git a/src/Abac.php b/src/Abac.php index 718e687..8b7140e 100644 --- a/src/Abac.php +++ b/src/Abac.php @@ -12,166 +12,173 @@ use PhpAbac\Model\PolicyRuleAttribute; -class Abac { - /** @var \PhpAbac\Manager\ConfigurationManager * */ - private $configuration; - /** @var \PhpAbac\Manager\PolicyRuleManager * */ - private $policyRuleManager; - /** @var \PhpAbac\Manager\AttributeManager * */ - private $attributeManager; - /** @var \PhpAbac\Manager\CacheManager * */ - private $cacheManager; - /** @var \PhpAbac\Manager\ComparisonManager * */ - private $comparisonManager; - - /** - * @param array $configPaths - * @param array $cacheOptions Option for cache - * @param string $configPaths_root The origin folder to find $configPaths - * @param array $options - */ - public function __construct( $configPaths, $cacheOptions = [], $configPaths_root = null, $options = [] ) { - $this->configure( $configPaths, $configPaths_root ); - $this->attributeManager = new AttributeManager( $this->configuration->getAttributes(), $options ); - $this->policyRuleManager = new PolicyRuleManager( $this->attributeManager, $this->configuration->getRules() ); - $this->cacheManager = new CacheManager( $cacheOptions ); - $this->comparisonManager = new ComparisonManager( $this->attributeManager ); - } - - /** - * @param array $configPaths - * @param string $configPaths_root The origin folder to find $configPaths - */ - public function configure( $configPaths, $configPaths_root = null ) { -// foreach ( $configPaths as &$configPath ) { +class Abac +{ + /** @var \PhpAbac\Manager\ConfigurationManager * */ + private $configuration; + /** @var \PhpAbac\Manager\PolicyRuleManager * */ + private $policyRuleManager; + /** @var \PhpAbac\Manager\AttributeManager * */ + private $attributeManager; + /** @var \PhpAbac\Manager\CacheManager * */ + private $cacheManager; + /** @var \PhpAbac\Manager\ComparisonManager * */ + private $comparisonManager; + + /** + * @param array $configPaths + * @param array $cacheOptions Option for cache + * @param string $configPaths_root The origin folder to find $configPaths + * @param array $options + */ + public function __construct($configPaths, $cacheOptions = [], $configPaths_root = null, $options = []) + { + $this->configure($configPaths, $configPaths_root); + $this->attributeManager = new AttributeManager($this->configuration->getAttributes(), $options); + $this->policyRuleManager = new PolicyRuleManager($this->attributeManager, $this->configuration->getRules()); + $this->cacheManager = new CacheManager($cacheOptions); + $this->comparisonManager = new ComparisonManager($this->attributeManager); + } + + /** + * @param array $configPaths + * @param string $configPaths_root The origin folder to find $configPaths + */ + public function configure($configPaths, $configPaths_root = null) + { + // foreach ( $configPaths as &$configPath ) { // $configPath = $configPaths_root . $configPath; // } - $locator = new FileLocator( $configPaths_root ); - $this->configuration = new ConfigurationManager( $locator ); - $this->configuration->setConfigPathRoot( $configPaths_root ); - $this->configuration->parseConfigurationFile( $configPaths ); - } - - /** - * Return true if both user and object respects all the rules conditions - * If the objectId is null, policy rules about its attributes will be ignored - * In case of mismatch between attributes and expected values, - * an array with the concerned attributes slugs will be returned. - * - * Available options are : - * * dynamic_attributes: array - * * cache_result: boolean - * * cache_ttl: integer - * * cache_driver: string - * - * Available cache drivers are : - * * memory - * - * @param string $ruleName - * @param object $user - * @param object $resource - * @param array $options - * - * @return boolean|array - */ - public function enforce( $ruleName, $user, $resource = null, $options = [] ) { - // If there is dynamic attributes, we pass them to the comparison manager - // When a comparison will be performed, the passed values will be retrieved and used - if ( isset( $options[ 'dynamic_attributes' ] ) ) { - $this->comparisonManager->setDynamicAttributes( $options[ 'dynamic_attributes' ] ); - } - // Retrieve cache value for the current rule and values if cache item is valid - if ( ( $cacheResult = isset( $options[ 'cache_result' ] ) && $options[ 'cache_result' ] === true ) === true ) { - $cacheItem = $this->cacheManager->getItem( "$ruleName-{$user->getId()}-" . ( ( $resource !== null ) ? $resource->getId() : '' ), ( isset( $options[ 'cache_driver' ] ) ) ? $options[ 'cache_driver' ] : null, ( isset( $options[ 'cache_ttl' ] ) ) ? $options[ 'cache_ttl' ] : null ); - // We check if the cache value s valid before returning it - if ( ( $cacheValue = $cacheItem->get() ) !== null ) { - return $cacheValue; - } - } - $policyRule_a = $this->policyRuleManager->getRule( $ruleName, $user, $resource ); - - foreach ( $policyRule_a as $policyRule ) { - // For each policy rule attribute, we retrieve the attribute value and proceed configured extra data - foreach ( $policyRule->getPolicyRuleAttributes() as $pra ) { - /** @var PolicyRuleAttribute $pra */ - $attribute = $pra->getAttribute(); - - $getter_params = $this->prepareGetterParams($pra->getGetterParams(), $user, $resource); + $locator = new FileLocator($configPaths_root); + $this->configuration = new ConfigurationManager($locator); + $this->configuration->setConfigPathRoot($configPaths_root); + $this->configuration->parseConfigurationFile($configPaths); + } + + /** + * Return true if both user and object respects all the rules conditions + * If the objectId is null, policy rules about its attributes will be ignored + * In case of mismatch between attributes and expected values, + * an array with the concerned attributes slugs will be returned. + * + * Available options are : + * * dynamic_attributes: array + * * cache_result: boolean + * * cache_ttl: integer + * * cache_driver: string + * + * Available cache drivers are : + * * memory + * + * @param string $ruleName + * @param object $user + * @param object $resource + * @param array $options + * + * @return boolean|array + */ + public function enforce($ruleName, $user, $resource = null, $options = []) + { + // If there is dynamic attributes, we pass them to the comparison manager + // When a comparison will be performed, the passed values will be retrieved and used + if (isset($options[ 'dynamic_attributes' ])) { + $this->comparisonManager->setDynamicAttributes($options[ 'dynamic_attributes' ]); + } + // Retrieve cache value for the current rule and values if cache item is valid + if (($cacheResult = isset($options[ 'cache_result' ]) && $options[ 'cache_result' ] === true) === true) { + $cacheItem = $this->cacheManager->getItem("$ruleName-{$user->getId()}-" . (($resource !== null) ? $resource->getId() : ''), (isset($options[ 'cache_driver' ])) ? $options[ 'cache_driver' ] : null, (isset($options[ 'cache_ttl' ])) ? $options[ 'cache_ttl' ] : null); + // We check if the cache value s valid before returning it + if (($cacheValue = $cacheItem->get()) !== null) { + return $cacheValue; + } + } + $policyRule_a = $this->policyRuleManager->getRule($ruleName, $user, $resource); + + foreach ($policyRule_a as $policyRule) { + // For each policy rule attribute, we retrieve the attribute value and proceed configured extra data + foreach ($policyRule->getPolicyRuleAttributes() as $pra) { + /** @var PolicyRuleAttribute $pra */ + $attribute = $pra->getAttribute(); + + $getter_params = $this->prepareGetterParams($pra->getGetterParams(), $user, $resource); // var_dump($pra->getGetterParams()); // var_dump($getter_params); - $attribute->setValue( $this->attributeManager->retrieveAttribute( $attribute, $user, $resource, $getter_params ) ); - if ( count( $pra->getExtraData() ) > 0 ) { - $this->processExtraData( $pra, $user, $resource ); - } - $this->comparisonManager->compare( $pra ); - } - // The given result could be an array of rejected attributes or true - // True means that the rule is correctly enforced for the given user and resource - $result = $this->comparisonManager->getResult(); - if ( true === $result ) { - break; - } - } - if ( $cacheResult ) { - $cacheItem->set( $result ); - $this->cacheManager->save( $cacheItem ); - } - - return $result; - } - - /** - * Function to prepare Getter Params when getter require parameters ( this parameters must be specified in configuration file) - * - * @param $getter_params - * @param $user - * @param $resource - * - * @return array - */ - private function prepareGetterParams($getter_params, $user, $resource) { - if (empty($getter_params)) return []; - $values = []; - foreach($getter_params as $getter_name=>$params) { - foreach($params as $param) { - if ( '@' !== $param[ 'param_name' ][ 0 ] ) { - $values[$getter_name][] = $param[ 'param_value' ]; - } - else { - $values[$getter_name][] = $this->attributeManager->retrieveAttribute( $this->attributeManager->getAttribute( $param[ 'param_value' ] ) , $user, $resource ); - } - } - } - return $values; - } - - /** - * @param \PhpAbac\Model\PolicyRuleAttribute $pra - * @param object $user - * @param object $resource - */ - public function processExtraData( PolicyRuleAttribute $pra, $user, $resource ) { - foreach ( $pra->getExtraData() as $key => $data ) { - switch ( $key ) { - case 'with': - // This data has to be removed for it will be stored elsewhere - // in the policy rule attribute - $pra->removeExtraData( 'with' ); - // The "with" extra data is an array of attributes, which are objects - // Once we process it as policy rule attributes, we set it as the main policy rule attribute value - $subPolicyRuleAttributes = []; - $extraData = []; - - foreach ( $this->policyRuleManager->processRuleAttributes( $data, $user, $resource ) as $subPolicyRuleAttribute ) { - $subPolicyRuleAttributes[] = $subPolicyRuleAttribute; - } - $pra->setValue( $subPolicyRuleAttributes ); - // This data can be used in complex comparisons - $pra->addExtraData( 'attribute', $pra->getAttribute() ); - $pra->addExtraData( 'user', $user ); - $pra->addExtraData( 'resource', $resource ); - break; - } - } - } + $attribute->setValue($this->attributeManager->retrieveAttribute($attribute, $user, $resource, $getter_params)); + if (count($pra->getExtraData()) > 0) { + $this->processExtraData($pra, $user, $resource); + } + $this->comparisonManager->compare($pra); + } + // The given result could be an array of rejected attributes or true + // True means that the rule is correctly enforced for the given user and resource + $result = $this->comparisonManager->getResult(); + if (true === $result) { + break; + } + } + if ($cacheResult) { + $cacheItem->set($result); + $this->cacheManager->save($cacheItem); + } + + return $result; + } + + /** + * Function to prepare Getter Params when getter require parameters ( this parameters must be specified in configuration file) + * + * @param $getter_params + * @param $user + * @param $resource + * + * @return array + */ + private function prepareGetterParams($getter_params, $user, $resource) + { + if (empty($getter_params)) { + return []; + } + $values = []; + foreach ($getter_params as $getter_name=>$params) { + foreach ($params as $param) { + if ('@' !== $param[ 'param_name' ][ 0 ]) { + $values[$getter_name][] = $param[ 'param_value' ]; + } else { + $values[$getter_name][] = $this->attributeManager->retrieveAttribute($this->attributeManager->getAttribute($param[ 'param_value' ]), $user, $resource); + } + } + } + return $values; + } + + /** + * @param \PhpAbac\Model\PolicyRuleAttribute $pra + * @param object $user + * @param object $resource + */ + public function processExtraData(PolicyRuleAttribute $pra, $user, $resource) + { + foreach ($pra->getExtraData() as $key => $data) { + switch ($key) { + case 'with': + // This data has to be removed for it will be stored elsewhere + // in the policy rule attribute + $pra->removeExtraData('with'); + // The "with" extra data is an array of attributes, which are objects + // Once we process it as policy rule attributes, we set it as the main policy rule attribute value + $subPolicyRuleAttributes = []; + $extraData = []; + + foreach ($this->policyRuleManager->processRuleAttributes($data, $user, $resource) as $subPolicyRuleAttribute) { + $subPolicyRuleAttributes[] = $subPolicyRuleAttribute; + } + $pra->setValue($subPolicyRuleAttributes); + // This data can be used in complex comparisons + $pra->addExtraData('attribute', $pra->getAttribute()); + $pra->addExtraData('user', $user); + $pra->addExtraData('resource', $resource); + break; + } + } + } } diff --git a/src/Cache/Exception/ExpiredCacheException.php b/src/Cache/Exception/ExpiredCacheException.php index ae9fcab..49a2ee1 100644 --- a/src/Cache/Exception/ExpiredCacheException.php +++ b/src/Cache/Exception/ExpiredCacheException.php @@ -4,6 +4,6 @@ use \Psr\Cache\CacheException; -class ExpiredCacheException extends \Exception implements CacheException { - +class ExpiredCacheException extends \Exception implements CacheException +{ } diff --git a/src/Cache/Item/MemoryCacheItem.php b/src/Cache/Item/MemoryCacheItem.php index fab5054..02bf193 100644 --- a/src/Cache/Item/MemoryCacheItem.php +++ b/src/Cache/Item/MemoryCacheItem.php @@ -6,7 +6,8 @@ use PhpAbac\Cache\Exception\ExpiredCacheException; -class MemoryCacheItem implements CacheItemInterface { +class MemoryCacheItem implements CacheItemInterface +{ /** @var string **/ protected $key; /** @var mixed **/ @@ -22,7 +23,8 @@ class MemoryCacheItem implements CacheItemInterface { * @param string $key * @param int $ttl */ - public function __construct($key, $ttl = null) { + public function __construct($key, $ttl = null) + { $this->key = $key; $this->expiresAfter($ttl); } @@ -30,7 +32,8 @@ public function __construct($key, $ttl = null) { /** * {@inheritdoc} */ - public function set($value) { + public function set($value) + { $this->value = $value; return $this; @@ -39,22 +42,25 @@ public function set($value) { /** * {@inheritdoc} */ - public function isHit() { + public function isHit() + { return $this->expiresAt >= new \DateTime(); } /** * {@inheritdoc} */ - public function getKey() { + public function getKey() + { return $this->key; } /** * {@inheritdoc} */ - public function get() { - if(!$this->isHit()) { + public function get() + { + if (!$this->isHit()) { throw new ExpiredCacheException('Cache item is expired'); } return $this->value; @@ -63,7 +69,8 @@ public function get() { /** * {@inheritdoc} */ - public function expiresAfter($time) { + public function expiresAfter($time) + { $lifetime = ($time !== null) ? $time : $this->defaultLifetime; $this->expiresAt = (new \DateTime())->setTimestamp(time() + $lifetime); @@ -74,7 +81,8 @@ public function expiresAfter($time) { /** * {@inheritdoc} */ - public function expiresAt($expiration) { + public function expiresAt($expiration) + { $this->expiresAt = ($expiration === null) ? (new \DateTime())->setTimestamp(time() + $this->defaultLifetime) @@ -86,14 +94,16 @@ public function expiresAt($expiration) { /** * @return \DateTime */ - public function getExpirationDate() { + public function getExpirationDate() + { return $this->expiresAt; } /** * @return string */ - public function getDriver() { + public function getDriver() + { return $this->driver; } } diff --git a/src/Cache/Item/TextCacheItem.php b/src/Cache/Item/TextCacheItem.php index 127bfd3..af61a12 100644 --- a/src/Cache/Item/TextCacheItem.php +++ b/src/Cache/Item/TextCacheItem.php @@ -6,7 +6,8 @@ use PhpAbac\Cache\Exception\ExpiredCacheException; -class TextCacheItem implements CacheItemInterface { +class TextCacheItem implements CacheItemInterface +{ /** @var string **/ protected $key; /** @var mixed **/ @@ -22,7 +23,8 @@ class TextCacheItem implements CacheItemInterface { * @param string $key * @param int $ttl */ - public function __construct($key, $ttl = null) { + public function __construct($key, $ttl = null) + { $this->key = $key; $this->expiresAfter($ttl); } @@ -30,7 +32,8 @@ public function __construct($key, $ttl = null) { /** * {@inheritdoc} */ - public function set($value) { + public function set($value) + { $this->value = $value; return $this; @@ -39,22 +42,25 @@ public function set($value) { /** * {@inheritdoc} */ - public function isHit() { + public function isHit() + { return $this->expiresAt >= new \DateTime(); } /** * {@inheritdoc} */ - public function getKey() { + public function getKey() + { return $this->key; } /** * {@inheritdoc} */ - public function get() { - if(!$this->isHit()) { + public function get() + { + if (!$this->isHit()) { throw new ExpiredCacheException('Cache item is expired'); } return $this->value; @@ -63,7 +69,8 @@ public function get() { /** * {@inheritdoc} */ - public function expiresAfter($time) { + public function expiresAfter($time) + { $lifetime = ($time !== null) ? $time : $this->defaultLifetime; $this->expiresAt = (new \DateTime())->setTimestamp(time() + $lifetime); @@ -74,7 +81,8 @@ public function expiresAfter($time) { /** * {@inheritdoc} */ - public function expiresAt($expiration) { + public function expiresAt($expiration) + { $this->expiresAt = ($expiration === null) ? (new \DateTime())->setTimestamp(time() + $this->defaultLifetime) @@ -86,14 +94,16 @@ public function expiresAt($expiration) { /** * @return \DateTime */ - public function getExpirationDate() { + public function getExpirationDate() + { return $this->expiresAt; } /** * @return string */ - public function getDriver() { + public function getDriver() + { return $this->driver; } } diff --git a/src/Cache/Pool/MemoryCacheItemPool.php b/src/Cache/Pool/MemoryCacheItemPool.php index 09a0278..f2e1427 100644 --- a/src/Cache/Pool/MemoryCacheItemPool.php +++ b/src/Cache/Pool/MemoryCacheItemPool.php @@ -7,7 +7,8 @@ use PhpAbac\Cache\Item\MemoryCacheItem; -class MemoryCacheItemPool implements CacheItemPoolInterface { +class MemoryCacheItemPool implements CacheItemPoolInterface +{ /** @var array **/ protected $items; /** @var array **/ @@ -15,11 +16,12 @@ class MemoryCacheItemPool implements CacheItemPoolInterface { /** * {@inheritdoc} */ - public function deleteItem($key) { - if(isset($this->items[$key])) { + public function deleteItem($key) + { + if (isset($this->items[$key])) { unset($this->items[$key]); } - if(isset($this->deferredItems[$key])) { + if (isset($this->deferredItems[$key])) { unset($this->deferredItems[$key]); } return true; @@ -28,9 +30,10 @@ public function deleteItem($key) { /** * {@inheritdoc} */ - public function deleteItems(array $keys) { - foreach($keys as $key) { - if(!$this->deleteItem($key)) { + public function deleteItems(array $keys) + { + foreach ($keys as $key) { + if (!$this->deleteItem($key)) { return false; } } @@ -40,7 +43,8 @@ public function deleteItems(array $keys) { /** * {@inheritdoc} */ - public function save(CacheItemInterface $item) { + public function save(CacheItemInterface $item) + { $this->items[$item->getKey()] = $item; return true; @@ -49,7 +53,8 @@ public function save(CacheItemInterface $item) { /** * {@inheritdoc} */ - public function saveDeferred(CacheItemInterface $item) { + public function saveDeferred(CacheItemInterface $item) + { $this->deferredItems[$item->getKey()] = $item; return true; @@ -58,8 +63,9 @@ public function saveDeferred(CacheItemInterface $item) { /** * {@inheritdoc} */ - public function commit() { - foreach($this->deferredItems as $key => $item) { + public function commit() + { + foreach ($this->deferredItems as $key => $item) { $this->items[$key] = $item; unset($this->deferredItems[$key]); } @@ -69,15 +75,17 @@ public function commit() { /** * {@inheritdoc} */ - public function hasItem($key) { + public function hasItem($key) + { return isset($this->items[$key]); } /** * {@inheritdoc} */ - public function getItem($key) { - if(!$this->hasItem($key)) { + public function getItem($key) + { + if (!$this->hasItem($key)) { return new MemoryCacheItem($key); } return $this->items[$key]; @@ -86,10 +94,11 @@ public function getItem($key) { /** * {@inheritdoc} */ - public function getItems(array $keys = array()) { + public function getItems(array $keys = array()) + { $items = []; - foreach($keys as $key) { - if($this->hasItem($key)) { + foreach ($keys as $key) { + if ($this->hasItem($key)) { $items[$key] = $this->getItem($key); } } @@ -99,9 +108,10 @@ public function getItems(array $keys = array()) { /** * {@inheritdoc} */ - public function clear() { + public function clear() + { $this->items = []; $this->deferredItems = []; return true; } -} \ No newline at end of file +} diff --git a/src/Cache/Pool/TextCacheItemPool.php b/src/Cache/Pool/TextCacheItemPool.php index 65b044e..3d48aa8 100644 --- a/src/Cache/Pool/TextCacheItemPool.php +++ b/src/Cache/Pool/TextCacheItemPool.php @@ -7,7 +7,8 @@ use PhpAbac\Cache\Item\TextCacheItem; -class TextCacheItemPool implements CacheItemPoolInterface { +class TextCacheItemPool implements CacheItemPoolInterface +{ /** @var array **/ protected $deferredItems; /** @var string **/ @@ -16,14 +17,16 @@ class TextCacheItemPool implements CacheItemPoolInterface { /** * @param array $options */ - public function __construct($options = []) { + public function __construct($options = []) + { $this->configure($options); } /** * @param array $options */ - protected function configure($options) { + protected function configure($options) + { $this->cacheFolder = (isset($options['cache_folder'])) ? "{$options['cache_folder']}/text" @@ -34,11 +37,12 @@ protected function configure($options) { /** * {@inheritdoc} */ - public function deleteItem($key) { - if(is_file("{$this->cacheFolder}/$key.txt")) { + public function deleteItem($key) + { + if (is_file("{$this->cacheFolder}/$key.txt")) { unlink("{$this->cacheFolder}/$key.txt"); } - if(isset($this->deferredItems[$key])) { + if (isset($this->deferredItems[$key])) { unset($this->deferredItems[$key]); } return true; @@ -47,9 +51,10 @@ public function deleteItem($key) { /** * {@inheritdoc} */ - public function deleteItems(array $keys) { - foreach($keys as $key) { - if(!$this->deleteItem($key)) { + public function deleteItems(array $keys) + { + foreach ($keys as $key) { + if (!$this->deleteItem($key)) { return false; } } @@ -59,7 +64,8 @@ public function deleteItems(array $keys) { /** * {@inheritdoc} */ - public function save(CacheItemInterface $item) { + public function save(CacheItemInterface $item) + { $data = "{$item->get()};{$item->getExpirationDate()->format('Y-m-d H:i:s')}"; file_put_contents("{$this->cacheFolder}/{$item->getKey()}.txt", $data); @@ -70,7 +76,8 @@ public function save(CacheItemInterface $item) { /** * {@inheritdoc} */ - public function saveDeferred(CacheItemInterface $item) { + public function saveDeferred(CacheItemInterface $item) + { $this->deferredItems[$item->getKey()] = $item; return true; @@ -79,8 +86,9 @@ public function saveDeferred(CacheItemInterface $item) { /** * {@inheritdoc} */ - public function commit() { - foreach($this->deferredItems as $key => $item) { + public function commit() + { + foreach ($this->deferredItems as $key => $item) { $this->save($item); unset($this->deferredItems[$key]); } @@ -90,19 +98,21 @@ public function commit() { /** * {@inheritdoc} */ - public function hasItem($key) { + public function hasItem($key) + { return is_file("{$this->cacheFolder}/{$key}.txt"); } /** * {@inheritdoc} */ - public function getItem($key) { + public function getItem($key) + { $item = new TextCacheItem($key); - if(!$this->hasItem($key)) { + if (!$this->hasItem($key)) { return $item; } - $data = explode(';',file_get_contents("{$this->cacheFolder}/{$key}.txt")); + $data = explode(';', file_get_contents("{$this->cacheFolder}/{$key}.txt")); return $item ->set($data[0]) ->expiresAt((new \DateTime($data[1]))) @@ -112,10 +122,11 @@ public function getItem($key) { /** * {@inheritdoc} */ - public function getItems(array $keys = array()) { + public function getItems(array $keys = array()) + { $items = []; - foreach($keys as $key) { - if($this->hasItem($key)) { + foreach ($keys as $key) { + if ($this->hasItem($key)) { $items[$key] = $this->getItem($key); } } @@ -125,11 +136,13 @@ public function getItems(array $keys = array()) { /** * {@inheritdoc} */ - public function clear() { + public function clear() + { $items = glob("{$this->cacheFolder}/*.txt"); // get all file names - foreach($items as $item){ // iterate files - if(is_file($item)) - unlink($item); // delete file + foreach ($items as $item) { // iterate files + if (is_file($item)) { + unlink($item); + } // delete file } $this->deferredItems = []; return true; diff --git a/src/Comparison/AbstractComparison.php b/src/Comparison/AbstractComparison.php index 8c424a5..7a78bb4 100644 --- a/src/Comparison/AbstractComparison.php +++ b/src/Comparison/AbstractComparison.php @@ -4,14 +4,16 @@ use PhpAbac\Manager\ComparisonManager; -abstract class AbstractComparison { +abstract class AbstractComparison +{ /** @var \PhpAbac\Manager\ComparisonManager **/ protected $comparisonManager; /** * @param \PhpAbac\Manager\ComparisonManager $comparisonManager */ - public function __construct(ComparisonManager $comparisonManager) { + public function __construct(ComparisonManager $comparisonManager) + { $this->comparisonManager = $comparisonManager; } -} \ No newline at end of file +} diff --git a/src/Comparison/ArrayComparison.php b/src/Comparison/ArrayComparison.php index 2a6af88..2671f6f 100644 --- a/src/Comparison/ArrayComparison.php +++ b/src/Comparison/ArrayComparison.php @@ -54,24 +54,25 @@ public function doNotIntersect($array1, $array2) * @param array $extraData * @return boolean */ - public function contains($policyRuleAttributes, $attributes, $extraData = []) { - foreach($extraData['attribute']->getValue() as $attribute) { + public function contains($policyRuleAttributes, $attributes, $extraData = []) + { + foreach ($extraData['attribute']->getValue() as $attribute) { $result = true; // For each attribute, we check the whole rules set - foreach($policyRuleAttributes as $pra) { + foreach ($policyRuleAttributes as $pra) { $attributeData = $pra->getAttribute(); $attributeData->setValue( $this->comparisonManager->getAttributeManager()->retrieveAttribute($attributeData, $extraData['user'], $attribute) ); // If one field is not matched, the whole attribute is rejected - if(!$this->comparisonManager->compare($pra, true)) { + if (!$this->comparisonManager->compare($pra, true)) { //var_dump($attributeData->getName(), $attributeData->getValue(), $pra->getValue()); $result = false; break; } } // If the result is still true at the end of the attribute check, the rule is enforced - if($result === true) { + if ($result === true) { return true; } } diff --git a/src/Comparison/BooleanComparison.php b/src/Comparison/BooleanComparison.php index 3897a23..50919a7 100644 --- a/src/Comparison/BooleanComparison.php +++ b/src/Comparison/BooleanComparison.php @@ -39,7 +39,8 @@ public function isNull($expected, $value) * @param mixed $expected * @param mixed $value */ - public function isNotNull($expected, $value) { + public function isNotNull($expected, $value) + { return $value !== null; } } diff --git a/src/Comparison/DatetimeComparison.php b/src/Comparison/DatetimeComparison.php index 26857ba..bb4f99e 100644 --- a/src/Comparison/DatetimeComparison.php +++ b/src/Comparison/DatetimeComparison.php @@ -6,7 +6,7 @@ class DatetimeComparison extends AbstractComparison { /** * Return true if the given formatted datetime is between two other datetimes. - * + * * @param \DateTime $start * @param \DateTime $end * @param \DateTime $datetime diff --git a/src/Comparison/NumericComparison.php b/src/Comparison/NumericComparison.php index d0b8b02..1501ca8 100644 --- a/src/Comparison/NumericComparison.php +++ b/src/Comparison/NumericComparison.php @@ -17,7 +17,7 @@ public function isEqual($expected, $value) /** * If strict is set to false, equal values will return true. - * + * * @param int $expected * @param int $value * @param bool $strict @@ -35,7 +35,7 @@ public function isLesserThan($expected, $value, $strict = true) /** * If strict is set to false, equal values will return true. - * + * * @param int $expected * @param int $value * @param bool $strict diff --git a/src/Comparison/ObjectComparison.php b/src/Comparison/ObjectComparison.php index 321e23c..8df43a0 100644 --- a/src/Comparison/ObjectComparison.php +++ b/src/Comparison/ObjectComparison.php @@ -12,7 +12,8 @@ class ObjectComparison extends AbstractComparison * @param array $extraData * @return boolean */ - public function isFieldEqual($attributeId, $value, $extraData = []) { + public function isFieldEqual($attributeId, $value, $extraData = []) + { $attributeManager = $this->comparisonManager->getAttributeManager(); // Create an attribute out of the extra data we have and compare its retrieved value to the expected one return $attributeManager->retrieveAttribute( diff --git a/src/Comparison/UserComparison.php b/src/Comparison/UserComparison.php index 2762a16..ca76a56 100644 --- a/src/Comparison/UserComparison.php +++ b/src/Comparison/UserComparison.php @@ -12,7 +12,8 @@ class UserComparison extends AbstractComparison * @param array $extraData * @return boolean */ - public function isFieldEqual($attributeId, $value, $extraData = []) { + public function isFieldEqual($attributeId, $value, $extraData = []) + { $attributeManager = $this->comparisonManager->getAttributeManager(); // Create an attribute out of the extra data we have and compare its retrieved value to the expected one return $attributeManager->retrieveAttribute( diff --git a/src/Loader/AbacLoader.php b/src/Loader/AbacLoader.php index 772f412..8e852c4 100644 --- a/src/Loader/AbacLoader.php +++ b/src/Loader/AbacLoader.php @@ -12,29 +12,31 @@ use Symfony\Component\Config\FileLocatorInterface; use Symfony\Component\Config\Loader\FileLoader; -abstract class AbacLoader extends FileLoader { - /** - * Must be overrided to contains an array of allowed extension - */ - protected static $_EXTENSION_ALLOWED_A = []; - - /** @var ConfigurationManager The configuration manage instanciator and user of this AbacLoader Instance */ - protected $configurationManger; - - public function __construct( FileLocatorInterface $locator ) { - parent::__construct( $locator ); - } - - /** - * Method to load a resource and return an array that contains decoded data of the resource - * - * @param string $resource The path of the resource to load - * @param null $type ?? - * - * @return string - */ - abstract public function load( $resource, $type = null ); - +abstract class AbacLoader extends FileLoader +{ + /** + * Must be overrided to contains an array of allowed extension + */ + protected static $_EXTENSION_ALLOWED_A = []; + + /** @var ConfigurationManager The configuration manage instanciator and user of this AbacLoader Instance */ + protected $configurationManger; + + public function __construct(FileLocatorInterface $locator) + { + parent::__construct($locator); + } + + /** + * Method to load a resource and return an array that contains decoded data of the resource + * + * @param string $resource The path of the resource to load + * @param null $type ?? + * + * @return string + */ + abstract public function load($resource, $type = null); + // /** // * Method to check if a resource ( by his path ) is supported by the loader // * @@ -44,26 +46,26 @@ abstract public function load( $resource, $type = null ); // * @return boolean Return true if the resource is supported by the loader // */ // abstract public function supports( $resource, $type = null ); - - /** - * Method to check if a resource is supported by the loader - * This method check only extension file - * - * @param $resource - * - * @return boolean Return true if the extension of the ressource is supported by the loader - */ - public static final function supportsExtension( $resource ) { - return in_array( pathinfo( $resource, PATHINFO_EXTENSION ), self::getExtensionAllowed() ); - } + + /** + * Method to check if a resource is supported by the loader + * This method check only extension file + * + * @param $resource + * + * @return boolean Return true if the extension of the ressource is supported by the loader + */ + final public static function supportsExtension($resource) + { + return in_array(pathinfo($resource, PATHINFO_EXTENSION), self::getExtensionAllowed()); + } /** - * Method to return allowed extension for file to load with the loader + * Method to return allowed extension for file to load with the loader * @return mixed */ - private static final function getExtensionAllowed() { - return static::$_EXTENSION_ALLOWED_A; - } + final private static function getExtensionAllowed() + { + return static::$_EXTENSION_ALLOWED_A; + } } - -?> diff --git a/src/Loader/JsonAbacLoader.php b/src/Loader/JsonAbacLoader.php index 2936a74..d94906d 100644 --- a/src/Loader/JsonAbacLoader.php +++ b/src/Loader/JsonAbacLoader.php @@ -7,18 +7,18 @@ class JsonAbacLoader extends AbacLoader { - protected static $_EXTENSION_ALLOWED_A = ['json']; - + protected static $_EXTENSION_ALLOWED_A = ['json']; + public function load($resource, $type = null) { -// $path_to_load = $this->locator->locate($resource); - $path_to_load = $resource; - - return (new JsonDecode(true))->decode(file_get_contents($path_to_load),JsonEncoder::FORMAT,[ 'json_decode_associative' => true ] ) + ['path' => $path_to_load]; + // $path_to_load = $this->locator->locate($resource); + $path_to_load = $resource; + + return (new JsonDecode(true))->decode(file_get_contents($path_to_load), JsonEncoder::FORMAT, [ 'json_decode_associative' => true ]) + ['path' => $path_to_load]; } public function supports($resource, $type = null) { return is_string($resource) && self::supportsExtension($resource); } -} \ No newline at end of file +} diff --git a/src/Loader/YamlAbacLoader.php b/src/Loader/YamlAbacLoader.php index d2a0f5a..299189e 100644 --- a/src/Loader/YamlAbacLoader.php +++ b/src/Loader/YamlAbacLoader.php @@ -6,13 +6,13 @@ class YamlAbacLoader extends AbacLoader { - protected static $_EXTENSION_ALLOWED_A = ['yml','yaml']; - + protected static $_EXTENSION_ALLOWED_A = ['yml','yaml']; + public function load($resource, $type = null) { - // $path_to_load = $this->locator->locate($resource); - $path_to_load = $resource; - + // $path_to_load = $this->locator->locate($resource); + $path_to_load = $resource; + return Yaml::parse(file_get_contents($path_to_load)) + ['path' => $path_to_load]; } @@ -20,4 +20,4 @@ public function supports($resource, $type = null) { return is_string($resource) && self::supportsExtension($resource); } -} \ No newline at end of file +} diff --git a/src/Manager/AttributeManager.php b/src/Manager/AttributeManager.php index b22ed2f..fc46e0c 100644 --- a/src/Manager/AttributeManager.php +++ b/src/Manager/AttributeManager.php @@ -19,30 +19,31 @@ class AttributeManager /** * @param array $attributes - * @param array $options A List of option to configure This Abac Instance - * Options list : - * 'getter_prefix' => Prefix to add before getter name (default)'get' - * 'getter_name_transformation_function' => Function to apply on the getter name ( before adding prefix ) (default)'ucfirst' + * @param array $options A List of option to configure This Abac Instance + * Options list : + * 'getter_prefix' => Prefix to add before getter name (default)'get' + * 'getter_name_transformation_function' => Function to apply on the getter name ( before adding prefix ) (default)'ucfirst' */ public function __construct($attributes, $options = []) { $this->attributes = $attributes; - - $options = array_intersect_key( $options, array_flip( [ - 'getter_prefix', - 'getter_name_transformation_function', - ] ) ); - - foreach($options as $name => $value) { - $this->$name = $value; - } + + $options = array_intersect_key($options, array_flip([ + 'getter_prefix', + 'getter_name_transformation_function', + ])); + + foreach ($options as $name => $value) { + $this->$name = $value; + } } /** * @param string $attributeId * @return \PhpAbac\Model\AbstractAttribute */ - public function getAttribute($attributeId) { + public function getAttribute($attributeId) + { $attributeKeys = explode('.', $attributeId); // The first element will be the attribute ID, then the field ID $attributeId = array_shift($attributeKeys); @@ -61,7 +62,8 @@ public function getAttribute($attributeId) { * @param string $property * @return \PhpAbac\Model\Attribute */ - private function getClassicAttribute($attributeData, $property) { + private function getClassicAttribute($attributeData, $property) + { return (new Attribute()) ->setName($attributeData['fields'][$property]['name']) @@ -76,7 +78,8 @@ private function getClassicAttribute($attributeData, $property) { * @param string $key * @return \PhpAbac\Model\EnvironmentAttribute */ - private function getEnvironmentAttribute($attributeData, $key) { + private function getEnvironmentAttribute($attributeData, $key) + { return (new EnvironmentAttribute()) ->setName($attributeData[$key]['name']) @@ -95,7 +98,7 @@ private function getEnvironmentAttribute($attributeData, $key) { */ public function retrieveAttribute(AbstractAttribute $attribute, $user = null, $object = null, $getter_params = []) { - switch($attribute->getType()) { + switch ($attribute->getType()) { case 'user': return $this->retrieveClassicAttribute($attribute, $user, $getter_params); case 'resource': @@ -114,21 +117,19 @@ private function retrieveClassicAttribute(Attribute $attribute, $object, $getter { $propertyPath = explode('.', $attribute->getProperty()); $propertyValue = $object; - foreach($propertyPath as $property) { - - - $getter = $this->getter_prefix.call_user_func($this->getter_name_transformation_function,$property); + foreach ($propertyPath as $property) { + $getter = $this->getter_prefix.call_user_func($this->getter_name_transformation_function, $property); // Use is_callable, instead of method_exists, to deal with __call magic method - if(!is_callable([$propertyValue,$getter])) { + if (!is_callable([$propertyValue,$getter])) { throw new \InvalidArgumentException('There is no getter for the "'.$attribute->getProperty().'" attribute for object "'.get_class($propertyValue).'" with getter "'.$getter.'"'); } - if ( ( $propertyValue = call_user_func_array( [ - $propertyValue, - $getter, - ], isset( $getter_params[ $property ] ) ? $getter_params[ $property ] : [] ) ) === null - ) { - return null; - } + if (($propertyValue = call_user_func_array([ + $propertyValue, + $getter, + ], isset($getter_params[ $property ]) ? $getter_params[ $property ] : [])) === null + ) { + return null; + } } return $propertyValue; } @@ -138,7 +139,8 @@ private function retrieveClassicAttribute(Attribute $attribute, $object, $getter * @param \PhpAbac\Model\EnvironmentAttribute $attribute * @return mixed */ - private function retrieveEnvironmentAttribute(EnvironmentAttribute $attribute) { + private function retrieveEnvironmentAttribute(EnvironmentAttribute $attribute) + { return getenv($attribute->getVariableName()); } diff --git a/src/Manager/CacheManager.php b/src/Manager/CacheManager.php index bcc121a..d545ae4 100644 --- a/src/Manager/CacheManager.php +++ b/src/Manager/CacheManager.php @@ -4,7 +4,8 @@ use Psr\Cache\CacheItemInterface; -class CacheManager { +class CacheManager +{ /** @var string **/ protected $defaultDriver = 'memory'; /** @var array **/ @@ -15,14 +16,16 @@ class CacheManager { /** * @param array $options */ - public function __construct($options = []) { + public function __construct($options = []) + { $this->options = $options; } /** * @param \Psr\Cache\CacheItemInterface $item */ - public function save(CacheItemInterface $item) { + public function save(CacheItemInterface $item) + { $this->getItemPool($item->getDriver())->save($item); } @@ -32,14 +35,15 @@ public function save(CacheItemInterface $item) { * @param int $ttl * @return \Psr\Cache\CacheItemInterface */ - public function getItem($key, $driver = null, $ttl = null) { + public function getItem($key, $driver = null, $ttl = null) + { $finalDriver = ($driver !== null) ? $driver : $this->defaultDriver; $pool = $this->getItemPool($finalDriver); $item = $pool->getItem($key); // In this case, the pool returned a new CacheItem - if($item->get() === null) { + if ($item->get() === null) { $item->expiresAfter($ttl); } return $item; @@ -50,8 +54,9 @@ public function getItem($key, $driver = null, $ttl = null) { * @param string $driver * @return Psr\Cache\CacheItemPoolInterface */ - public function getItemPool($driver) { - if(!isset($this->pools[$driver])) { + public function getItemPool($driver) + { + if (!isset($this->pools[$driver])) { $poolClass = 'PhpAbac\\Cache\\Pool\\' . ucfirst($driver) . 'CacheItemPool'; $this->pools[$driver] = new $poolClass($this->options); } diff --git a/src/Manager/ComparisonManager.php b/src/Manager/ComparisonManager.php index 5f78939..0b5218a 100644 --- a/src/Manager/ComparisonManager.php +++ b/src/Manager/ComparisonManager.php @@ -12,7 +12,8 @@ use PhpAbac\Comparison\UserComparison; use PhpAbac\Comparison\StringComparison; -class ComparisonManager { +class ComparisonManager +{ /** @var \PhpAbac\Manager\AttributeManager **/ protected $attributeManager; /** @var array **/ @@ -31,7 +32,8 @@ class ComparisonManager { /** * @param \PhpAbac\Manager\AttributeManager $manager */ - public function __construct(AttributeManager $manager) { + public function __construct(AttributeManager $manager) + { $this->attributeManager = $manager; } @@ -50,7 +52,8 @@ public function __construct(AttributeManager $manager) { * @param boolean $subComparing * @return bool */ - public function compare(PolicyRuleAttribute $pra, $subComparing = false) { + public function compare(PolicyRuleAttribute $pra, $subComparing = false) + { $attribute = $pra->getAttribute(); // The expected value can be set in the configuration as dynamic // In this case, we retrieve the expected value in the passed options @@ -60,22 +63,22 @@ public function compare(PolicyRuleAttribute $pra, $subComparing = false) { : $pra->getValue() ; // Checking that the configured comparison type is available - if(!isset($this->comparisons[$pra->getComparisonType()])) { + if (!isset($this->comparisons[$pra->getComparisonType()])) { throw new \InvalidArgumentException('The requested comparison class does not exist'); } // The comparison class will perform the attribute check with the configured method // For more complex comparisons, the comparison manager is injected $comparison = new $this->comparisons[$pra->getComparisonType()]($this); - if(!method_exists($comparison, $pra->getComparison())) { + if (!method_exists($comparison, $pra->getComparison())) { throw new \InvalidArgumentException('The requested comparison method does not exist'); } // Then the comparison is performed with needed $result = $comparison->{$pra->getComparison()}($praValue, $attribute->getValue(), $pra->getExtraData()); // If the checked attribute is not valid, the attribute slug is marked as rejected // The rejected attributes will be returned instead of the expected true boolean - if($result !== true) { + if ($result !== true) { // In case of sub comparing, the error reporting is disabled - if(!in_array($attribute->getSlug(), $this->rejectedAttributes) && $subComparing === false) { + if (!in_array($attribute->getSlug(), $this->rejectedAttributes) && $subComparing === false) { $this->rejectedAttributes[] = $attribute->getSlug(); } return false; @@ -86,7 +89,8 @@ public function compare(PolicyRuleAttribute $pra, $subComparing = false) { /** * @param array $dynamicAttributes */ - public function setDynamicAttributes($dynamicAttributes) { + public function setDynamicAttributes($dynamicAttributes) + { $this->dynamicAttributes = $dynamicAttributes; } @@ -101,8 +105,9 @@ public function setDynamicAttributes($dynamicAttributes) { * @return mixed * @throws \InvalidArgumentException */ - public function getDynamicAttribute($attributeSlug) { - if(!isset($this->dynamicAttributes[$attributeSlug])) { + public function getDynamicAttribute($attributeSlug) + { + if (!isset($this->dynamicAttributes[$attributeSlug])) { throw new \InvalidArgumentException("The dynamic value for attribute $attributeSlug was not given"); } return $this->dynamicAttributes[$attributeSlug]; @@ -112,14 +117,16 @@ public function getDynamicAttribute($attributeSlug) { * @param string $type * @param string $class */ - public function addComparison($type, $class) { + public function addComparison($type, $class) + { $this->comparisons[$type] = $class; } /** * @return \PhpAbac\Manager\AttributeManager */ - public function getAttributeManager() { + public function getAttributeManager() + { return $this->attributeManager; } @@ -130,7 +137,8 @@ public function getAttributeManager() { * * @return array|bool */ - public function getResult() { + public function getResult() + { $result = (count($this->rejectedAttributes) > 0) ? $this->rejectedAttributes diff --git a/src/Manager/ConfigurationManager.php b/src/Manager/ConfigurationManager.php index fa374f9..53cca3c 100644 --- a/src/Manager/ConfigurationManager.php +++ b/src/Manager/ConfigurationManager.php @@ -8,108 +8,113 @@ use Symfony\Component\Config\FileLocatorInterface; -class ConfigurationManager { - /** @var FileLocatorInterface * */ - protected $locator; - /** @var AbacLoader[] * */ - protected $loaders; - /** @var array * */ - protected $rules; - /** @var array * */ - protected $attributes; - +class ConfigurationManager +{ + /** @var FileLocatorInterface * */ + protected $locator; + /** @var AbacLoader[] * */ + protected $loaders; + /** @var array * */ + protected $rules; + /** @var array * */ + protected $attributes; + // protected $config_path_route; - - /** @var array List of File Already Loader */ - protected $config_files_loaded; - - /** - * @param FileLocatorInterface $locator - * @param string|array $format A format or an array of format - */ - public function __construct( FileLocatorInterface $locator, $format = [ - 'yaml', - 'json', - ] ) { - $this->locator = $locator; - $this->attributes = []; - $this->rules = []; - $this->config_files_loaded = []; - if ( in_array( 'yaml', $format ) ) { - $this->loaders[ 'yaml' ] = new YamlAbacLoader( $locator, $this ); - } - if ( in_array( 'json', $format ) ) { - $this->loaders[ 'json' ] = new JsonAbacLoader( $locator, $this ); - } - - } - - public function setConfigPathRoot($configPaths_root = null) { -// $this->config_path_route = $configPaths_root; - foreach($this->loaders as $loader) { - $loader->setCurrentDir($configPaths_root); - } - } - - /** - * @param array $configurationFiles - */ - public function parseConfigurationFile( $configurationFiles ) { - foreach ( $configurationFiles as $configurationFile ) { - $config = $this->getLoader( $configurationFile )->import( $configurationFile, pathinfo( $configurationFile, PATHINFO_EXTENSION ) ); - - if (in_array($config['path'],$this->config_files_loaded)) { - continue; - } - - $this->config_files_loaded[] = $config['path']; - - if (isset($config['@import'])) { - $this->parseConfigurationFile($config['@import']); - unset($config['@import']); - } - - if ( isset( $config[ 'attributes' ] ) ) { - $this->attributes = array_merge( $this->attributes, $config[ 'attributes' ] ); - } - if ( isset( $config[ 'rules' ] ) ) { - $this->rules = array_merge( $this->rules, $config[ 'rules' ] ); - } - } - } - - - - /** - * Function to retrieve the good loader for the configuration file - * - * @param $configurationFile - * - * @return AbacLoader - * - * @throws \Exception - */ - private function getLoader( $configurationFile ) { - - foreach ( $this->loaders as $AbacLoader ) { - if ( $AbacLoader::supportsExtension( $configurationFile ) ) { - return $AbacLoader; - } - } - throw new \Exception( 'Loader not found for the file ' . $configurationFile ); - } - - /** - * @return array - */ - public function getAttributes() { - return $this->attributes; - } - - /** - * @return array - */ - public function getRules() { - return $this->rules; - } -} \ No newline at end of file + + /** @var array List of File Already Loader */ + protected $config_files_loaded; + + /** + * @param FileLocatorInterface $locator + * @param string|array $format A format or an array of format + */ + public function __construct(FileLocatorInterface $locator, $format = [ + 'yaml', + 'json', + ]) + { + $this->locator = $locator; + $this->attributes = []; + $this->rules = []; + $this->config_files_loaded = []; + if (in_array('yaml', $format)) { + $this->loaders[ 'yaml' ] = new YamlAbacLoader($locator, $this); + } + if (in_array('json', $format)) { + $this->loaders[ 'json' ] = new JsonAbacLoader($locator, $this); + } + } + + public function setConfigPathRoot($configPaths_root = null) + { + // $this->config_path_route = $configPaths_root; + foreach ($this->loaders as $loader) { + $loader->setCurrentDir($configPaths_root); + } + } + + /** + * @param array $configurationFiles + */ + public function parseConfigurationFile($configurationFiles) + { + foreach ($configurationFiles as $configurationFile) { + $config = $this->getLoader($configurationFile)->import($configurationFile, pathinfo($configurationFile, PATHINFO_EXTENSION)); + + if (in_array($config['path'], $this->config_files_loaded)) { + continue; + } + + $this->config_files_loaded[] = $config['path']; + + if (isset($config['@import'])) { + $this->parseConfigurationFile($config['@import']); + unset($config['@import']); + } + + if (isset($config[ 'attributes' ])) { + $this->attributes = array_merge($this->attributes, $config[ 'attributes' ]); + } + if (isset($config[ 'rules' ])) { + $this->rules = array_merge($this->rules, $config[ 'rules' ]); + } + } + } + + + + /** + * Function to retrieve the good loader for the configuration file + * + * @param $configurationFile + * + * @return AbacLoader + * + * @throws \Exception + */ + private function getLoader($configurationFile) + { + foreach ($this->loaders as $AbacLoader) { + if ($AbacLoader::supportsExtension($configurationFile)) { + return $AbacLoader; + } + } + throw new \Exception('Loader not found for the file ' . $configurationFile); + } + + /** + * @return array + */ + public function getAttributes() + { + return $this->attributes; + } + + /** + * @return array + */ + public function getRules() + { + return $this->rules; + } +} diff --git a/src/Manager/PolicyRuleManager.php b/src/Manager/PolicyRuleManager.php index 5a71de3..fda3660 100644 --- a/src/Manager/PolicyRuleManager.php +++ b/src/Manager/PolicyRuleManager.php @@ -70,7 +70,7 @@ public function processRuleAttributes($attributes, $user, $resource) ->setComparison($attribute['comparison']) ->setComparisonType($attribute['comparison_type']) ->setValue((isset($attribute['value'])) ? $attribute['value'] : null) - ->setGetterParams( isset( $attribute[ 'getter_params' ] ) ? $attribute[ 'getter_params' ] : [] ); + ->setGetterParams(isset($attribute[ 'getter_params' ]) ? $attribute[ 'getter_params' ] : []); $this->processRuleAttributeComparisonType($pra, $user, $resource); // In the case the user configured more keys than the basic ones // it will be stored as extra data diff --git a/src/Model/AbstractAttribute.php b/src/Model/AbstractAttribute.php index eedd862..3a13967 100644 --- a/src/Model/AbstractAttribute.php +++ b/src/Model/AbstractAttribute.php @@ -92,5 +92,4 @@ public function getValue() { return $this->value; } - } diff --git a/src/Model/Attribute.php b/src/Model/Attribute.php index 780015d..1438c25 100644 --- a/src/Model/Attribute.php +++ b/src/Model/Attribute.php @@ -11,7 +11,8 @@ class Attribute extends AbstractAttribute * @param string $property * @return static */ - public function setProperty($property) { + public function setProperty($property) + { $this->property = $property; return $this; @@ -20,7 +21,8 @@ public function setProperty($property) { /** * @return string */ - public function getProperty() { + public function getProperty() + { return $this->property; } } diff --git a/src/Model/PolicyRuleAttribute.php b/src/Model/PolicyRuleAttribute.php index 7d3b28a..3f4bd8a 100644 --- a/src/Model/PolicyRuleAttribute.php +++ b/src/Model/PolicyRuleAttribute.php @@ -14,8 +14,8 @@ class PolicyRuleAttribute protected $value; /** @var array **/ protected $extraData; - /** @var array Extended parameter */ - protected $getter_params_a = []; + /** @var array Extended parameter */ + protected $getter_params_a = []; /** * @param \PhpAbac\Model\AbstractAttribute $attribute @@ -101,7 +101,8 @@ public function getValue() * @param array $extraData * @return \PhpAbac\Model\PolicyRuleAttribute */ - public function setExtraData($extraData) { + public function setExtraData($extraData) + { $this->extraData = $extraData; return $this; @@ -112,7 +113,8 @@ public function setExtraData($extraData) { * @param string $value * @return \PhpAbac\Model\PolicyRuleAttribute */ - public function addExtraData($key, $value) { + public function addExtraData($key, $value) + { $this->extraData[$key] = $value; return $this; @@ -122,8 +124,9 @@ public function addExtraData($key, $value) { * @param string $key * @return \PhpAbac\Model\PolicyRuleAttribute */ - public function removeExtraData($key) { - if(isset($this->extraData[$key])) { + public function removeExtraData($key) + { + if (isset($this->extraData[$key])) { unset($this->extraData[$key]); } return $this; @@ -132,25 +135,28 @@ public function removeExtraData($key) { /** * @return array */ - public function getExtraData() { + public function getExtraData() + { return $this->extraData; } - /** - * @param array $value - * - * @return static - */ - public function setGetterParams($value) { - $this->getter_params_a = $value; - - return $this; - } - - /** - * @return array - */ - public function getGetterParams() { - return $this->getter_params_a; - } + /** + * @param array $value + * + * @return static + */ + public function setGetterParams($value) + { + $this->getter_params_a = $value; + + return $this; + } + + /** + * @return array + */ + public function getGetterParams() + { + return $this->getter_params_a; + } } diff --git a/tests/AbacTest.php b/tests/AbacTest.php index 87d6597..9f49084 100644 --- a/tests/AbacTest.php +++ b/tests/AbacTest.php @@ -10,11 +10,11 @@ class AbacTest extends \PHPUnit_Framework_TestCase { /** @var Abac[] $abac_a * */ protected $abac_a; - /** @var Abac[] $abac_array_a * */ + /** @var Abac[] $abac_array_a * */ protected $abac_array_a; - /** @var Abac[] $abac_getter_params_a * */ + /** @var Abac[] $abac_getter_params_a * */ protected $abac_getter_params_a; - /** @var Abac[] $abac_import_a * */ + /** @var Abac[] $abac_import_a * */ protected $abac_import_a; public function setUp() @@ -26,17 +26,15 @@ public function setUp() $this->abac_array_a = [ new Abac([__DIR__ . '/fixtures/policy_rules_with_array.yml']), new Abac([__DIR__ . '/fixtures/policy_rules_with_array.json']), - new Abac(['policy_rules_with_array.yml'],[],__DIR__.'/fixtures/'), - new Abac(['policy_rules_with_array.json'],[],__DIR__.'/fixtures/'), + new Abac(['policy_rules_with_array.yml'], [], __DIR__.'/fixtures/'), + new Abac(['policy_rules_with_array.json'], [], __DIR__.'/fixtures/'), ]; $this->abac_getter_params_a = [ - new Abac(['policy_rules_with_getter_params.yml'],[],__DIR__.'/fixtures/'), - ]; - $this->abac_import_a = [ - new Abac(['policy_rules_with_import.yml'],[],__DIR__.'/fixtures/'), - ]; - - + new Abac(['policy_rules_with_getter_params.yml'], [], __DIR__.'/fixtures/'), + ]; + $this->abac_import_a = [ + new Abac(['policy_rules_with_import.yml'], [], __DIR__.'/fixtures/'), + ]; } public function testEnforce() @@ -47,7 +45,6 @@ public function testEnforce() $vehicles = include('tests/fixtures/vehicles.php'); foreach ($this->abac_a as $abac) { - $this->assertTrue($abac->enforce('nationality-access', $users[3])); $this->assertEquals([ 'japd', @@ -87,42 +84,40 @@ public function testEnforceArray() foreach ($this->abac_array_a as $abac) { // for this test, the attribute in error are the attributes tester une the last rule of the ruleset - $this->assertEquals(['age','code-iso-du-pays'],$abac->enforce('gunlaw', $users[2])); + $this->assertEquals(['age','code-iso-du-pays'], $abac->enforce('gunlaw', $users[2])); $this->assertTrue($abac->enforce('gunlaw', $users[4])); $this->assertTrue($abac->enforce('gunlaw', $users[0])); $this->assertTrue($abac->enforce('gunlaw', $users[1])); - } } - - public function testEnforceGetterParams() { - $countries = include('tests/fixtures/countries.php'); - $visas = include('tests/fixtures/visas.php'); - $users = include('tests/fixtures/users.php'); - - foreach ($this->abac_getter_params_a as $abac) { - - // for this test, the attribute in error are the attributes tester une the last rule of the ruleset - $this->assertEquals(['visa-specific'],$abac->enforce('travel-to-foreign-country', $users[0], $countries[2])); - $this->assertTrue($abac->enforce('travel-to-foreign-country', $users[1], $countries[2])); - - } - - } + + public function testEnforceGetterParams() + { + $countries = include('tests/fixtures/countries.php'); + $visas = include('tests/fixtures/visas.php'); + $users = include('tests/fixtures/users.php'); + + foreach ($this->abac_getter_params_a as $abac) { + + // for this test, the attribute in error are the attributes tester une the last rule of the ruleset + $this->assertEquals(['visa-specific'], $abac->enforce('travel-to-foreign-country', $users[0], $countries[2])); + $this->assertTrue($abac->enforce('travel-to-foreign-country', $users[1], $countries[2])); + } + } - - public function testEnforceImport() { - $countries = include('tests/fixtures/countries.php'); - $visas = include('tests/fixtures/visas.php'); - $users = include('tests/fixtures/users.php'); - foreach ($this->abac_import_a as $abac) { - - // for this test, the attribute in error are the attributes tester une the last rule of the ruleset - $this->assertEquals(['visa-specific'],$abac->enforce('travel-to-foreign-country', $users[0], $countries[2])); - $this->assertTrue($abac->enforce('travel-to-foreign-country', $users[1], $countries[2])); - - } - } + + public function testEnforceImport() + { + $countries = include('tests/fixtures/countries.php'); + $visas = include('tests/fixtures/visas.php'); + $users = include('tests/fixtures/users.php'); + foreach ($this->abac_import_a as $abac) { + + // for this test, the attribute in error are the attributes tester une the last rule of the ruleset + $this->assertEquals(['visa-specific'], $abac->enforce('travel-to-foreign-country', $users[0], $countries[2])); + $this->assertTrue($abac->enforce('travel-to-foreign-country', $users[1], $countries[2])); + } + } } diff --git a/tests/Cache/Item/MemoryCacheItemTest.php b/tests/Cache/Item/MemoryCacheItemTest.php index 041e43a..fefb1ab 100644 --- a/tests/Cache/Item/MemoryCacheItemTest.php +++ b/tests/Cache/Item/MemoryCacheItemTest.php @@ -4,41 +4,49 @@ use PhpAbac\Cache\Item\MemoryCacheItem; -class MemoryCacheItemTest extends \PHPUnit_Framework_TestCase { +class MemoryCacheItemTest extends \PHPUnit_Framework_TestCase +{ /** @var \PhpAbac\Cache\Item\MemoryCacheItem **/ protected $item; - public function setUp() { + public function setUp() + { $this->item = new MemoryCacheItem('php_abac.test'); } - public function testSet() { + public function testSet() + { $this->item->set('test'); $this->assertEquals('test', $this->item->get()); } - public function testIsHit() { + public function testIsHit() + { $this->assertTrue($this->item->isHit()); } - public function testIsHitWithMissItem() { + public function testIsHitWithMissItem() + { $this->item->expiresAt((new \DateTime())->setTimestamp(time() - 100)); $this->assertFalse($this->item->isHit()); } - public function testGetKey() { + public function testGetKey() + { $this->assertEquals('php_abac.test', $this->item->getKey()); } - public function testGet() { + public function testGet() + { $this->item->set('test'); $this->assertEquals('test', $this->item->get()); } - public function testExpiresAt() { + public function testExpiresAt() + { $time = time(); $this->item->expiresAt((new \DateTime())->setTimestamp($time)); @@ -46,11 +54,12 @@ public function testExpiresAt() { $this->assertEquals($time, $this->item->getExpirationDate()->getTimestamp()); } - public function testExpiresAfter() { + public function testExpiresAfter() + { $time = time() + 1500; $this->item->expiresAfter(1500); $this->assertEquals($time, $this->item->getExpirationDate()->getTimestamp()); } -} \ No newline at end of file +} diff --git a/tests/Cache/Item/TextCacheItemTest.php b/tests/Cache/Item/TextCacheItemTest.php index 851d2b1..a32bf88 100644 --- a/tests/Cache/Item/TextCacheItemTest.php +++ b/tests/Cache/Item/TextCacheItemTest.php @@ -4,41 +4,49 @@ use PhpAbac\Cache\Item\TextCacheItem; -class TextCacheItemTest extends \PHPUnit_Framework_TestCase { +class TextCacheItemTest extends \PHPUnit_Framework_TestCase +{ /** @var \PhpAbac\Cache\Item\TextCacheItem **/ protected $item; - public function setUp() { + public function setUp() + { $this->item = new TextCacheItem('php_abac.test'); } - public function testSet() { + public function testSet() + { $this->item->set('test'); $this->assertEquals('test', $this->item->get()); } - public function testIsHit() { + public function testIsHit() + { $this->assertTrue($this->item->isHit()); } - public function testIsHitWithMissItem() { + public function testIsHitWithMissItem() + { $this->item->expiresAt((new \DateTime())->setTimestamp(time() - 100)); $this->assertFalse($this->item->isHit()); } - public function testGetKey() { + public function testGetKey() + { $this->assertEquals('php_abac.test', $this->item->getKey()); } - public function testGet() { + public function testGet() + { $this->item->set('test'); $this->assertEquals('test', $this->item->get()); } - public function testExpiresAt() { + public function testExpiresAt() + { $time = time(); $this->item->expiresAt((new \DateTime())->setTimestamp($time)); @@ -46,7 +54,8 @@ public function testExpiresAt() { $this->assertEquals($time, $this->item->getExpirationDate()->getTimestamp()); } - public function testExpiresAfter() { + public function testExpiresAfter() + { $time = time() + 1500; $this->item->expiresAfter(1500); diff --git a/tests/Cache/Pool/MemoryCacheItemPoolTest.php b/tests/Cache/Pool/MemoryCacheItemPoolTest.php index 4738c93..82ebc59 100644 --- a/tests/Cache/Pool/MemoryCacheItemPoolTest.php +++ b/tests/Cache/Pool/MemoryCacheItemPoolTest.php @@ -5,14 +5,17 @@ use PhpAbac\Cache\Pool\MemoryCacheItemPool; use PhpAbac\Cache\Item\MemoryCacheItem; -class MemoryCacheItemPoolTest extends \PHPUnit_Framework_TestCase { +class MemoryCacheItemPoolTest extends \PHPUnit_Framework_TestCase +{ protected $pool; - public function setUp() { + public function setUp() + { $this->pool = new MemoryCacheItemPool(); } - public function testGetItem() { + public function testGetItem() + { $this->pool->save((new MemoryCacheItem('php_abac.test'))->set('test')); $item = $this->pool->getItem('php_abac.test'); @@ -21,7 +24,8 @@ public function testGetItem() { $this->assertEquals('test', $item->get()); } - public function testGetUnknownItem() { + public function testGetUnknownItem() + { $item = $this->pool->getItem('php_abac.test'); $this->assertInstanceOf('PhpAbac\\Cache\\Item\\MemoryCacheItem', $item); @@ -29,7 +33,8 @@ public function testGetUnknownItem() { $this->assertNull($item->get()); } - public function testGetItems() { + public function testGetItems() + { $this->pool->save((new MemoryCacheItem('php_abac.test1'))->set('test 1')); $this->pool->save((new MemoryCacheItem('php_abac.test2'))->set('test 2')); $this->pool->save((new MemoryCacheItem('php_abac.test3'))->set('test 3')); @@ -44,14 +49,16 @@ public function testGetItems() { $this->assertEquals('test 2', $items['php_abac.test2']->get()); } - public function testHasItem() { + public function testHasItem() + { $this->pool->save(new MemoryCacheItem('php_abac.test')); $this->assertFalse($this->pool->hasItem('php_abac.unknown_value')); $this->assertTrue($this->pool->hasItem('php_abac.test')); } - public function testSave() { + public function testSave() + { $this->pool->save((new MemoryCacheItem('php_abac.test'))->set('test')); $item = $this->pool->getItem('php_abac.test'); @@ -60,7 +67,8 @@ public function testSave() { $this->assertEquals('test', $item->get()); } - public function testSaveDeferred() { + public function testSaveDeferred() + { $this->pool->saveDeferred(new MemoryCacheItem('php_abac.test')); $this->assertFalse($this->pool->hasItem('php_abac.test')); @@ -70,7 +78,8 @@ public function testSaveDeferred() { $this->assertTrue($this->pool->hasItem('php_abac.test')); } - public function testCommit() { + public function testCommit() + { $key = 'php_abac.test_deferred'; $value = 'Cached value'; @@ -83,14 +92,16 @@ public function testCommit() { $this->assertEquals($value, $this->pool->getItem($key)->get()); } - public function testClear() { + public function testClear() + { $this->pool->save(new MemoryCacheItem('php_abac.test')); $this->assertTrue($this->pool->clear()); $this->assertFalse($this->pool->hasItem('php_abac.test')); } - public function testDeleteItem() { + public function testDeleteItem() + { $this->pool->save(new MemoryCacheItem('php_abac.test1')); $this->pool->deleteItem('php_abac.test1'); @@ -98,7 +109,8 @@ public function testDeleteItem() { $this->assertFalse($this->pool->hasItem('php_abac.test1')); } - public function testDeleteItems() { + public function testDeleteItems() + { $this->pool->save((new MemoryCacheItem('php_abac.test1'))->set('test 1')); $this->pool->save((new MemoryCacheItem('php_abac.test2'))->set('test 2')); $this->pool->save((new MemoryCacheItem('php_abac.test3'))->set('test 3')); @@ -115,4 +127,4 @@ public function testDeleteItems() { $this->assertCount(1, $items); $this->assertArrayHasKey('php_abac.test1', $items); } -} \ No newline at end of file +} diff --git a/tests/Cache/Pool/TextCacheItemPoolTest.php b/tests/Cache/Pool/TextCacheItemPoolTest.php index 203eef6..cdc0d32 100644 --- a/tests/Cache/Pool/TextCacheItemPoolTest.php +++ b/tests/Cache/Pool/TextCacheItemPoolTest.php @@ -5,20 +5,24 @@ use PhpAbac\Cache\Pool\TextCacheItemPool; use PhpAbac\Cache\Item\TextCacheItem; -class TextCacheItemPoolTest extends \PHPUnit_Framework_TestCase { +class TextCacheItemPoolTest extends \PHPUnit_Framework_TestCase +{ protected $pool; - public function setUp() { + public function setUp() + { $this->pool = new TextCacheItemPool([ 'cache_folder' => __DIR__ . '/../../../data/cache/test' ]); } - public function tearDown() { + public function tearDown() + { $this->pool->clear(); } - public function testGetItem() { + public function testGetItem() + { $this->pool->save((new TextCacheItem('php_abac.test'))->set('test')); $item = $this->pool->getItem('php_abac.test'); @@ -27,7 +31,8 @@ public function testGetItem() { $this->assertEquals('test', $item->get()); } - public function testGetUnknownItem() { + public function testGetUnknownItem() + { $item = $this->pool->getItem('php_abac.test'); $this->assertInstanceOf('PhpAbac\\Cache\\Item\\TextCacheItem', $item); @@ -35,7 +40,8 @@ public function testGetUnknownItem() { $this->assertNull($item->get()); } - public function testGetItems() { + public function testGetItems() + { $this->pool->save((new TextCacheItem('php_abac.test1'))->set('test 1')); $this->pool->save((new TextCacheItem('php_abac.test2'))->set('test 2')); $this->pool->save((new TextCacheItem('php_abac.test3'))->set('test 3')); @@ -50,14 +56,16 @@ public function testGetItems() { $this->assertEquals('test 2', $items['php_abac.test2']->get()); } - public function testHasItem() { + public function testHasItem() + { $this->pool->save(new TextCacheItem('php_abac.test')); $this->assertFalse($this->pool->hasItem('php_abac.unknown_value')); $this->assertTrue($this->pool->hasItem('php_abac.test')); } - public function testSave() { + public function testSave() + { $this->pool->save((new TextCacheItem('php_abac.test'))->set('test')); $item = $this->pool->getItem('php_abac.test'); @@ -66,7 +74,8 @@ public function testSave() { $this->assertEquals('test', $item->get()); } - public function testSaveDeferred() { + public function testSaveDeferred() + { $this->pool->saveDeferred(new TextCacheItem('php_abac.test')); $this->assertFalse($this->pool->hasItem('php_abac.test')); @@ -76,7 +85,8 @@ public function testSaveDeferred() { $this->assertTrue($this->pool->hasItem('php_abac.test')); } - public function testCommit() { + public function testCommit() + { $key = 'php_abac.test_deferred'; $value = 'Cached value'; @@ -89,14 +99,16 @@ public function testCommit() { $this->assertEquals($value, $this->pool->getItem($key)->get()); } - public function testClear() { + public function testClear() + { $this->pool->save(new TextCacheItem('php_abac.test')); $this->assertTrue($this->pool->clear()); $this->assertFalse($this->pool->hasItem('php_abac.test')); } - public function testDeleteItem() { + public function testDeleteItem() + { $this->pool->save(new TextCacheItem('php_abac.test1')); $this->pool->deleteItem('php_abac.test1'); @@ -104,7 +116,8 @@ public function testDeleteItem() { $this->assertFalse($this->pool->hasItem('php_abac.test1')); } - public function testDeleteItems() { + public function testDeleteItems() + { $this->pool->save((new TextCacheItem('php_abac.test1'))->set('test 1')); $this->pool->save((new TextCacheItem('php_abac.test2'))->set('test 2')); $this->pool->save((new TextCacheItem('php_abac.test3'))->set('test 3')); diff --git a/tests/Comparison/ArrayComparisonTest.php b/tests/Comparison/ArrayComparisonTest.php index 3bcb71a..ff50dd0 100644 --- a/tests/Comparison/ArrayComparisonTest.php +++ b/tests/Comparison/ArrayComparisonTest.php @@ -87,7 +87,8 @@ public function testDoNotIntersect() ])); } - public function testContains() { + public function testContains() + { $countries = include(__DIR__ . '/../fixtures/countries.php'); $visas = include(__DIR__ . '/../fixtures/visas.php'); $policyRuleAttributes = [ @@ -115,7 +116,7 @@ public function testContains() { ->setValue('-1Y'), ]; $extraData = [ - 'attribute' => + 'attribute' => (new Attribute()) ->setProperty('visas') ->setName('Visas') @@ -123,7 +124,7 @@ public function testContains() { ->setType('resource') ->setValue([$visas[0], $visas[1]]) , - 'user' => + 'user' => (new User()) ->setId(1) ->setName('John Doe') diff --git a/tests/Comparison/BooleanComparisonTest.php b/tests/Comparison/BooleanComparisonTest.php index f821b64..f140908 100644 --- a/tests/Comparison/BooleanComparisonTest.php +++ b/tests/Comparison/BooleanComparisonTest.php @@ -31,12 +31,14 @@ public function testBoolOr() $this->assertFalse($this->comparison->boolOr(false, false)); } - public function testIsNull() { + public function testIsNull() + { $this->assertTrue($this->comparison->isNull(true, null)); $this->assertFalse($this->comparison->isNull(true, true)); } - public function testIsNotNull() { + public function testIsNotNull() + { $this->assertTrue($this->comparison->isNotNull(true, true)); $this->assertFalse($this->comparison->isNotNull(true, null)); } diff --git a/tests/Comparison/DatetimeComparisonTest.php b/tests/Comparison/DatetimeComparisonTest.php index 58789e0..56dae17 100644 --- a/tests/Comparison/DatetimeComparisonTest.php +++ b/tests/Comparison/DatetimeComparisonTest.php @@ -26,12 +26,14 @@ public function testIsBetween() $this->assertFalse($this->comparison->isBetween($start, $end, new \DateTime('2015-07-18'))); } - public function testIsMoreRecentThan() { + public function testIsMoreRecentThan() + { $this->assertTrue($this->comparison->isMoreRecentThan('-2Y', new \DateTime())); $this->assertFalse($this->comparison->isMoreRecentThan('-2Y', new \DateTime('2010-01-02'))); } - public function testIsLessRecentThan() { + public function testIsLessRecentThan() + { $this->assertTrue($this->comparison->isLessRecentThan('-2Y', new \DateTime('2010-01-02'))); $this->assertFalse($this->comparison->isLessRecentThan('-2Y', new \DateTime())); } diff --git a/tests/Comparison/ObjectComparisonTest.php b/tests/Comparison/ObjectComparisonTest.php index bb16daa..187dde8 100644 --- a/tests/Comparison/ObjectComparisonTest.php +++ b/tests/Comparison/ObjectComparisonTest.php @@ -29,7 +29,8 @@ public function setUp() $this->comparison = new ObjectComparison(new ComparisonManager(new AttributeManager($configuration->getAttributes()))); } - public function testIsFieldEqual() { + public function testIsFieldEqual() + { $countries = include(__DIR__ . '/../fixtures/countries.php'); $visas = include(__DIR__ . '/../fixtures/visas.php'); $extraData = [ diff --git a/tests/Comparison/UserComparisonTest.php b/tests/Comparison/UserComparisonTest.php index 546c860..89e621b 100644 --- a/tests/Comparison/UserComparisonTest.php +++ b/tests/Comparison/UserComparisonTest.php @@ -29,7 +29,8 @@ public function setUp() $this->comparison = new UserComparison(new ComparisonManager(new AttributeManager($configuration->getAttributes()))); } - public function testIsFieldEqual() { + public function testIsFieldEqual() + { $countries = include(__DIR__ . '/../fixtures/countries.php'); $visas = include(__DIR__ . '/../fixtures/visas.php'); $extraData = [ diff --git a/tests/Loader/JsonAbacLoaderTest.php b/tests/Loader/JsonAbacLoaderTest.php index 98ac2f9..fce1636 100644 --- a/tests/Loader/JsonAbacLoaderTest.php +++ b/tests/Loader/JsonAbacLoaderTest.php @@ -5,34 +5,34 @@ use PhpAbac\Loader\JsonAbacLoader; use Symfony\Component\Config\FileLocator; - class JsonAbacLoaderTest extends \PHPUnit_Framework_TestCase { - public function setUp() { - } - /** - * @expectedException \Exception - */ - public function testLoaderJsonInvalidJsonFile() { - $JSON_loader = new JsonAbacLoader(new FileLocator(__DIR__.'/../fixtures/bad')); - $JSON_loader->setCurrentDir(__DIR__.'/../fixtures/bad'); - $JSON_loader->import('policy_rules.json'); - } - - public function testLoaderJsonValidJsonFile() { - $JSON_loader = new JsonAbacLoader(new FileLocator(__DIR__.'/../fixtures')); - $JSON_loader->setCurrentDir(__DIR__.'/../fixtures'); - $this->assertThat($JSON_loader->import('policy_rules.json'),$this->isType('array')); - } - - public function testSupports() { - $JSON_loader = new JsonAbacLoader(new FileLocator(__DIR__.'/../fixtures')); - $JSON_loader->setCurrentDir(__DIR__.'/../fixtures'); - $this->assertTrue($JSON_loader->supports('json.json')); - $this->assertFalse($JSON_loader->supports('json.yaml')); - $this->assertFalse($JSON_loader->supports('json.xml')); - } + /** + * @expectedException \Exception + */ + public function testLoaderJsonInvalidJsonFile() + { + $JSON_loader = new JsonAbacLoader(new FileLocator(__DIR__.'/../fixtures/bad')); + $JSON_loader->setCurrentDir(__DIR__.'/../fixtures/bad'); + $JSON_loader->import('policy_rules.json'); + } + + public function testLoaderJsonValidJsonFile() + { + $JSON_loader = new JsonAbacLoader(new FileLocator(__DIR__.'/../fixtures')); + $JSON_loader->setCurrentDir(__DIR__.'/../fixtures'); + $this->assertThat($JSON_loader->import('policy_rules.json'), $this->isType('array')); + } + + public function testSupports() + { + $JSON_loader = new JsonAbacLoader(new FileLocator(__DIR__.'/../fixtures')); + $JSON_loader->setCurrentDir(__DIR__.'/../fixtures'); + $this->assertTrue($JSON_loader->supports('json.json')); + $this->assertFalse($JSON_loader->supports('json.yaml')); + $this->assertFalse($JSON_loader->supports('json.xml')); + } } diff --git a/tests/Loader/YamlAbacLoaderTest.php b/tests/Loader/YamlAbacLoaderTest.php index c0f244a..0d0cc56 100644 --- a/tests/Loader/YamlAbacLoaderTest.php +++ b/tests/Loader/YamlAbacLoaderTest.php @@ -5,34 +5,34 @@ use PhpAbac\Loader\YamlAbacLoader; use Symfony\Component\Config\FileLocator; - class YamlAbacLoaderTest extends \PHPUnit_Framework_TestCase { - public function setUp() { - } - /** - * @expectedException \Exception - */ - public function testLoaderYamlInvalidYamlFile() { - $YAML_loader = new YamlAbacLoader(new FileLocator(__DIR__.'/../fixtures/bad')); - $YAML_loader->setCurrentDir(__DIR__.'/../fixtures/bad'); - $YAML_loader->import('policy_rules.yml'); - } - - public function testLoaderYamlValidYamlFile() { - $YAML_loader = new YamlAbacLoader(new FileLocator(__DIR__.'/../fixtures')); - $YAML_loader->setCurrentDir(__DIR__.'/../fixtures'); - $this->assertThat($YAML_loader->import('policy_rules.yml'),$this->isType('array')); - } - - public function testSupports() { - $YAML_loader = new YamlAbacLoader(new FileLocator(__DIR__.'/../fixtures')); - $YAML_loader->setCurrentDir(__DIR__.'/../fixtures'); - $this->assertTrue($YAML_loader->supports('yaml.yaml')); - $this->assertFalse($YAML_loader->supports('yaml.json')); - $this->assertFalse($YAML_loader->supports('yaml.xml')); - } + /** + * @expectedException \Exception + */ + public function testLoaderYamlInvalidYamlFile() + { + $YAML_loader = new YamlAbacLoader(new FileLocator(__DIR__.'/../fixtures/bad')); + $YAML_loader->setCurrentDir(__DIR__.'/../fixtures/bad'); + $YAML_loader->import('policy_rules.yml'); + } + + public function testLoaderYamlValidYamlFile() + { + $YAML_loader = new YamlAbacLoader(new FileLocator(__DIR__.'/../fixtures')); + $YAML_loader->setCurrentDir(__DIR__.'/../fixtures'); + $this->assertThat($YAML_loader->import('policy_rules.yml'), $this->isType('array')); + } + + public function testSupports() + { + $YAML_loader = new YamlAbacLoader(new FileLocator(__DIR__.'/../fixtures')); + $YAML_loader->setCurrentDir(__DIR__.'/../fixtures'); + $this->assertTrue($YAML_loader->supports('yaml.yaml')); + $this->assertFalse($YAML_loader->supports('yaml.json')); + $this->assertFalse($YAML_loader->supports('yaml.xml')); + } } diff --git a/tests/Manager/AttributeManagerTest.php b/tests/Manager/AttributeManagerTest.php index ec851a6..7c5e2d4 100644 --- a/tests/Manager/AttributeManagerTest.php +++ b/tests/Manager/AttributeManagerTest.php @@ -22,7 +22,8 @@ public function setUp() $this->manager = new AttributeManager($configuration->getAttributes()); } - public function testGetClassicAttribute() { + public function testGetClassicAttribute() + { $attribute = $this->manager->getAttribute('main_user.age'); $this->assertInstanceOf('PhpAbac\Model\Attribute', $attribute); @@ -33,7 +34,8 @@ public function testGetClassicAttribute() { $this->assertNull($attribute->getValue()); } - public function testGetEnvironmentAttribute() { + public function testGetEnvironmentAttribute() + { $attribute = $this->manager->getAttribute('environment.service_state'); $this->assertInstanceOf('PhpAbac\Model\EnvironmentAttribute', $attribute); @@ -44,14 +46,16 @@ public function testGetEnvironmentAttribute() { $this->assertNull($attribute->getValue()); } - public function testRetrieveClassicAttribute() { + public function testRetrieveClassicAttribute() + { $this->assertEquals(18, $this->manager->retrieveAttribute( $this->manager->getAttribute('main_user.age'), (new User())->setAge(18) )); } - public function testRetrieveEnvironmentAttribute() { + public function testRetrieveEnvironmentAttribute() + { $this->assertEquals('OPEN', $this->manager->retrieveAttribute( $this->manager->getAttribute('environment.service_state'), (new User())->setAge(18) diff --git a/tests/Manager/CacheManagerTest.php b/tests/Manager/CacheManagerTest.php index 82a4e57..f66346b 100644 --- a/tests/Manager/CacheManagerTest.php +++ b/tests/Manager/CacheManagerTest.php @@ -4,15 +4,18 @@ use PhpAbac\Manager\CacheManager; -class CacheManagerTest extends \PHPUnit_Framework_TestCase { +class CacheManagerTest extends \PHPUnit_Framework_TestCase +{ /** @var \PhpAbac\Manager\CacheManager **/ protected $cacheManager; - public function setUp() { + public function setUp() + { $this->cacheManager = new CacheManager(); } - public function testSave() { + public function testSave() + { $item = $this->cacheManager->getItemPool('memory')->getItem('php_abac.test'); $item->set('Test Value'); @@ -24,7 +27,8 @@ public function testSave() { $this->assertEquals('Test Value', $savedItem->get()); } - public function testGetItem() { + public function testGetItem() + { $item = $this->cacheManager->getItemPool('memory')->getItem('php_abac.test'); $this->assertInstanceOf('Psr\\Cache\\CacheItemInterface', $item); @@ -32,7 +36,8 @@ public function testGetItem() { $this->assertNull($item->get()); } - public function testGetItemPool() { + public function testGetItemPool() + { $pool = $this->cacheManager->getItemPool('memory'); $item = $pool->getItem('php_abac.test'); $this->cacheManager->save($item); diff --git a/tests/Manager/ComparisonManagerTest.php b/tests/Manager/ComparisonManagerTest.php index 2aace06..aaef0ed 100644 --- a/tests/Manager/ComparisonManagerTest.php +++ b/tests/Manager/ComparisonManagerTest.php @@ -7,15 +7,18 @@ use PhpAbac\Model\PolicyRuleAttribute; use PhpAbac\Model\Attribute; -class ComparisonManagerTest extends \PHPUnit_Framework_TestCase { +class ComparisonManagerTest extends \PHPUnit_Framework_TestCase +{ /** @var \PhpAbac\Manager\ComparisonManager **/ protected $manager; - public function setUp() { + public function setUp() + { $this->manager = new ComparisonManager($this->getAttributeManagerMock()); } - public function testCompare() { + public function testCompare() + { $this->assertTrue($this->manager->compare( (new PolicyRuleAttribute()) ->setAttribute( @@ -31,7 +34,8 @@ public function testCompare() { $this->assertTrue($this->manager->getResult()); } - public function testCompareWithInvalidAttribute() { + public function testCompareWithInvalidAttribute() + { $this->assertFalse($this->manager->compare( (new PolicyRuleAttribute()) ->setAttribute( @@ -53,7 +57,8 @@ public function testCompareWithInvalidAttribute() { * @expectedException \InvalidArgumentException * @expectedExceptionMessage The requested comparison class does not exist */ - public function testCompareWithInvalidType() { + public function testCompareWithInvalidType() + { $this->manager->compare( (new PolicyRuleAttribute()) ->setAttribute( @@ -72,7 +77,8 @@ public function testCompareWithInvalidType() { * @expectedException \InvalidArgumentException * @expectedExceptionMessage The requested comparison method does not exist */ - public function testCompareWithInvalidMethod() { + public function testCompareWithInvalidMethod() + { $this->manager->compare( (new PolicyRuleAttribute()) ->setAttribute( @@ -87,7 +93,8 @@ public function testCompareWithInvalidMethod() { ); } - public function testDynamicAttributes() { + public function testDynamicAttributes() + { $this->manager->setDynamicAttributes([ 'owner-id' => 13 ]); @@ -98,11 +105,13 @@ public function testDynamicAttributes() { * @expectedException \InvalidArgumentException * @expectedExceptionMessage The dynamic value for attribute owner-id was not given */ - public function testGetMissingDynamicAttribute() { + public function testGetMissingDynamicAttribute() + { $this->manager->getDynamicAttribute('owner-id'); } - public function getAttributeManagerMock() { + public function getAttributeManagerMock() + { $managerMock = $this ->getMockBuilder('PhpAbac\Manager\AttributeManager') ->disableOriginalConstructor() @@ -110,4 +119,4 @@ public function getAttributeManagerMock() { ; return $managerMock; } -} \ No newline at end of file +} diff --git a/tests/Manager/ConfigurationManagerTest.php b/tests/Manager/ConfigurationManagerTest.php index 6efe292..44866db 100644 --- a/tests/Manager/ConfigurationManagerTest.php +++ b/tests/Manager/ConfigurationManagerTest.php @@ -6,15 +6,18 @@ use PhpAbac\Manager\ConfigurationManager; -class ConfigurationManagerTest extends \PHPUnit_Framework_TestCase { - public function setUp() { +class ConfigurationManagerTest extends \PHPUnit_Framework_TestCase +{ + public function setUp() + { $this->manager = new ConfigurationManager(new FileLocator()); } - public function testParseConfigurationFile() { + public function testParseConfigurationFile() + { $this->manager->parseConfigurationFile([__DIR__.'/../fixtures/policy_rules.yml']); $this->assertCount(5, $this->manager->getAttributes()); $this->assertCount(4, $this->manager->getRules()); } -} \ No newline at end of file +} diff --git a/tests/fixtures/users.php b/tests/fixtures/users.php index c7be61d..1f72e27 100644 --- a/tests/fixtures/users.php +++ b/tests/fixtures/users.php @@ -44,4 +44,4 @@ ->setHasDoneJapd(true) ->setHasDrivingLicense(false) ->setCountry('US') -]; \ No newline at end of file +]; diff --git a/tests/fixtures/visas.php b/tests/fixtures/visas.php index e583b8e..15e8077 100644 --- a/tests/fixtures/visas.php +++ b/tests/fixtures/visas.php @@ -18,4 +18,4 @@ ->setCountry($countries[2]) ->setLastRenewal(new \DateTime()) ->setCreatedAt(new \DateTime()), -]; \ No newline at end of file +];