diff --git a/composer.json b/composer.json index da088a6..4986739 100644 --- a/composer.json +++ b/composer.json @@ -1,35 +1,39 @@ { - "name": "craftcamp/php-abac", - "description": "Library used to implement Attribute-Based Access Control in a PHP application", - "type": "library", - "keywords": ["access-control", "attributes", "security"], - "license": "MIT", - "minimum-stability": "stable", - "authors": [ - { - "name": "Axel Venet", - "email": "kern046@gmail.com", - "role": "Developer" - } - ], - "require": { - "php": ">=7.0", - "psr/cache": "~1.0", - "symfony/config": "~3.0|^4.0", - "symfony/yaml" : "~3.0|^4.0", - "friendsofphp/php-cs-fixer": "^2.12" - }, - "support": { - "email": "kern046@gmail.com" - }, - "autoload" : { - "psr-4": { - "PhpAbac\\": "src/", - "PhpAbac\\Example\\": "example/", - "PhpAbac\\Test\\": "tests/" - } - }, - "require-dev": { - "phpunit/phpunit": "^6.5" + "name": "craftcamp/php-abac", + "description": "Library used to implement Attribute-Based Access Control in a PHP application", + "type": "library", + "keywords": [ + "access-control", + "attributes", + "security" + ], + "license": "MIT", + "minimum-stability": "stable", + "authors": [ + { + "name": "Axel Venet", + "email": "kern046@gmail.com", + "role": "Developer" } + ], + "require": { + "php": ">=7.0", + "psr/cache": "~1.0", + "symfony/config": "~3.0|^4.0", + "symfony/yaml": "~3.0|^4.0", + "friendsofphp/php-cs-fixer": "^2.12" + }, + "support": { + "email": "kern046@gmail.com" + }, + "autoload": { + "psr-4": { + "PhpAbac\\": "src/", + "PhpAbac\\Example\\": "example/", + "PhpAbac\\Test\\": "tests/" + } + }, + "require-dev": { + "phpunit/phpunit": "^6.5" + } } diff --git a/src/Abac.php b/src/Abac.php index 1af19f3..f7012e0 100644 --- a/src/Abac.php +++ b/src/Abac.php @@ -4,33 +4,60 @@ use PhpAbac\Manager\{ AttributeManager, + AttributeManagerInterface, CacheManager, + CacheManagerInterface, ComparisonManager, - PolicyRuleManager + ComparisonManagerInterface, + PolicyRuleManager, + PolicyRuleManagerInterface }; +use PhpAbac\Model\PolicyRule; use PhpAbac\Model\PolicyRuleAttribute; final class Abac { - /** @var PolicyRuleManager **/ + /** + * @var PolicyRuleManager|PolicyRuleManager + */ private $policyRuleManager; - /** @var AttributeManager **/ + /** + * @var AttributeManager|AttributeManagerInterface + */ private $attributeManager; - /** @var CacheManager **/ + /** + * @var CacheManager| CacheManagerInterface + */ private $cacheManager; - /** @var ComparisonManager **/ + /** + * @var ComparisonManager | ComparisonManagerInterface + */ private $comparisonManager; - /** @var array **/ + /** + * @var array + */ private $errors; - - public function __construct(PolicyRuleManager $policyRuleManager, AttributeManager $attributeManager, ComparisonManager $comparisonManager, CacheManager $cacheManager) - { + + /** + * Abac constructor. + * + * @param PolicyRuleManagerInterface $policyRuleManager + * @param AttributeManagerInterface $attributeManager + * @param ComparisonManagerInterface $comparisonManager + * @param CacheManagerInterface $cacheManager + */ + public function __construct( + PolicyRuleManagerInterface $policyRuleManager, + AttributeManagerInterface $attributeManager, + ComparisonManagerInterface $comparisonManager, + CacheManagerInterface $cacheManager + ){ $this->attributeManager = $attributeManager; $this->policyRuleManager = $policyRuleManager; $this->cacheManager = $cacheManager; $this->comparisonManager = $comparisonManager; } - + /** * 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 @@ -45,6 +72,13 @@ public function __construct(PolicyRuleManager $policyRuleManager, AttributeManag * * Available cache drivers are : * * memory + * + * @param string $ruleName + * @param object $user + * @param null|object $resource + * @param array $options + * + * @return bool */ public function enforce(string $ruleName, $user, $resource = null, array $options = []): bool { @@ -55,8 +89,14 @@ public function enforce(string $ruleName, $user, $resource = null, array $option $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); + $cacheResult = (isset($options[ 'cache_result' ]) && $options[ 'cache_result' ] === true); + + if ($cacheResult === 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; @@ -66,12 +106,17 @@ public function enforce(string $ruleName, $user, $resource = null, array $option foreach ($policyRules as $policyRule) { // For each policy rule attribute, we retrieve the attribute value and proceed configured extra data + /** + * @var PolicyRule $policyRule + */ foreach ($policyRule->getPolicyRuleAttributes() as $pra) { /** @var PolicyRuleAttribute $pra */ $attribute = $pra->getAttribute(); $getter_params = $this->prepareGetterParams($pra->getGetterParams(), $user, $resource); - $attribute->setValue($this->attributeManager->retrieveAttribute($attribute, $user, $resource, $getter_params)); + $attribute->setValue( + $this->attributeManager->retrieveAttribute($attribute, $user, $resource, $getter_params) + ); if (count($pra->getExtraData()) > 0) { $this->processExtraData($pra, $user, $resource); } @@ -97,7 +142,8 @@ public function getErrors(): array } /** - * Function to prepare Getter Params when getter require parameters ( this parameters must be specified in configuration file) + * Function to prepare Getter Params when getter require parameters + * ( this parameters must be specified in configuration file) * * @param $getter_params * @param $user @@ -116,7 +162,12 @@ private function prepareGetterParams($getter_params, $user, $resource) 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); + $values[$getter_name][] = $this->attributeManager + ->retrieveAttribute( + $this->attributeManager->getAttribute($param[ 'param_value' ]), + $user, + $resource + ); } } } @@ -134,8 +185,8 @@ private function processExtraData(PolicyRuleAttribute $pra, $user, $resource) // 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 = []; - - foreach ($this->policyRuleManager->processRuleAttributes($data, $user, $resource) as $subPolicyRuleAttribute) { + $subs = $this->policyRuleManager->processRuleAttributes($data, $user, $resource); + foreach ($subs as $subPolicyRuleAttribute) { $subPolicyRuleAttributes[] = $subPolicyRuleAttribute; } $pra->setValue($subPolicyRuleAttributes); @@ -147,4 +198,5 @@ private function processExtraData(PolicyRuleAttribute $pra, $user, $resource) } } } + } diff --git a/src/AbacFactory.php b/src/AbacFactory.php index 22d281d..48dd63e 100644 --- a/src/AbacFactory.php +++ b/src/AbacFactory.php @@ -4,7 +4,6 @@ use PhpAbac\Configuration\ConfigurationInterface; use PhpAbac\Configuration\Configuration; - use PhpAbac\Manager\PolicyRuleManager; use PhpAbac\Manager\PolicyRuleManagerInterface; use PhpAbac\Manager\AttributeManager; @@ -14,53 +13,118 @@ use PhpAbac\Manager\ComparisonManager; use PhpAbac\Manager\ComparisonManagerInterface; - +/** + * Class AbacFactory + * + * @package PhpAbac + */ final class AbacFactory { - /** @var ConfigurationInterface **/ + /** + * @var ConfigurationInterface + */ protected static $configuration; - /** @var PolicyRuleManagerInterface **/ + /** + * @var PolicyRuleManagerInterface * + */ protected static $policyRuleManager; - /** @var AttributeManagerInterface **/ + /** + * @var AttributeManagerInterface * + */ protected static $attributeManager; - /** @var CacheManagerInterface **/ + /** + * @var CacheManagerInterface * + */ protected static $cacheManager; - /** @var ComparisonManagerInterface **/ + /** + * @var ComparisonManagerInterface * + */ protected static $comparisonManager; - + + /** + * @param ConfigurationInterface $configuration + */ public static function setConfiguration(ConfigurationInterface $configuration) { self::$configuration = $configuration; } - + + /** + * @param PolicyRuleManagerInterface $policyRuleManager + */ public static function setPolicyRuleManager(PolicyRuleManagerInterface $policyRuleManager) { self::$policyRuleManager = $policyRuleManager; } - + + /** + * @param AttributeManagerInterface $attributeManager + */ public static function setAttributeManager(AttributeManagerInterface $attributeManager) { self::$attributeManager = $attributeManager; } - + + /** + * @param CacheManagerInterface $cacheManager + */ public static function setCacheManager(CacheManagerInterface $cacheManager) { self::$cacheManager = $cacheManager; } - + + /** + * @param ComparisonManagerInterface $comparisonManager + */ public static function setComparisonManager(ComparisonManagerInterface $comparisonManager) { self::$comparisonManager = $comparisonManager; } - - public static function getAbac(array $configurationFiles, string $configDir = null, array $attributeOptions = [], array $cacheOptions = []) + + /** + * @param array $configurationFiles + * @param string|null $configDir + * @param array $attributeOptions + * @param array $cacheOptions + * + * @return Abac + */ + public static function getAbac( + array $configurationFiles, + string $configDir = null, + array $attributeOptions = [], + array $cacheOptions = []) { - $configuration = (self::$configuration !== null) ? self::$configuration : new Configuration($configurationFiles, $configDir); - $attributeManager = (self::$attributeManager !== null) ? self::$attributeManager : new AttributeManager($configuration, $attributeOptions); - $policyRuleManager = (self::$policyRuleManager !== null) ? self::$policyRuleManager : new PolicyRuleManager($configuration, $attributeManager); - $comparisonManager = (self::$comparisonManager !== null) ? self::$comparisonManager : new ComparisonManager($attributeManager); - $cacheManager = (self::$cacheManager !== null) ? self::$cacheManager : new CacheManager($cacheOptions); + $configuration = + (self::$configuration !== null) + ? self::$configuration + : new Configuration($configurationFiles, $configDir); + + $attributeManager = + (self::$attributeManager !== null) + ? self::$attributeManager + : new AttributeManager($configuration, $attributeOptions); + + $policyRuleManager = + (self::$policyRuleManager !== null) + ? self::$policyRuleManager + : new PolicyRuleManager($configuration, $attributeManager); + + $comparisonManager = + (self::$comparisonManager !== null) + ? self::$comparisonManager + : new ComparisonManager($attributeManager); + + $cacheManager = + (self::$cacheManager !== null) + ? self::$cacheManager + : new CacheManager($cacheOptions); return new Abac($policyRuleManager, $attributeManager, $comparisonManager, $cacheManager); } + + public static function getRules() + { + return self::$configuration != null ? self::$configuration->getRules() : []; + } } \ No newline at end of file diff --git a/src/Comparison/ArrayComparison.php b/src/Comparison/ArrayComparison.php index 37cb7a1..cf35dab 100644 --- a/src/Comparison/ArrayComparison.php +++ b/src/Comparison/ArrayComparison.php @@ -2,37 +2,100 @@ namespace PhpAbac\Comparison; +use PhpAbac\Model\PolicyRuleAttribute; + class ArrayComparison extends AbstractComparison { + /** + * @param array $haystack + * @param $needle + * + * @return bool + */ public function isIn(array $haystack, $needle): bool { return in_array($needle, $haystack); } + /** + * @param array $haystack + * @param $needle + * + * @return bool + */ public function isNotIn(array $haystack, $needle): bool { return !$this->isIn($haystack, $needle); } + /** + * @param array $array1 + * @param array $array2 + * + * @return bool + */ public function intersect(array $array1, array $array2): bool { return count(array_intersect($array1, $array2)) > 0; } + /** + * @param array $array1 + * @param array $array2 + * + * @return bool + */ public function doNotIntersect(array $array1, array $array2): bool { return !$this->intersect($array1, $array2); } + /** + * check is array2 have any values that not presents in array1 + * @param array $array1 + * @param array $array2 + * + * @return bool + */ + public function containsDiffs(array $array1, array $array2): bool + { + return count(array_diff($array2, $array1)) ? true : false; + } + + /** + * check is array2 have all values that presents in array1 + * @param array $array1 + * @param array $array2 + * + * @return bool + */ + public function NotContainsDiffs(array $array1, array $array2): bool + { + return !count(array_diff($array2, $array1)) ? true : false; + } + + /** + * @param array $policyRuleAttributes + * @param array $attributes + * @param array $extraData + * + * @return bool + */ public function contains(array $policyRuleAttributes, array $attributes, array $extraData = []): bool { foreach ($extraData['attribute']->getValue() as $attribute) { $result = true; // For each attribute, we check the whole rules set foreach ($policyRuleAttributes as $pra) { + /** + * @var PolicyRuleAttribute $pra + */ $attributeData = $pra->getAttribute(); $attributeData->setValue( - $this->comparisonManager->getAttributeManager()->retrieveAttribute($attributeData, $extraData['user'], $attribute) + $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)) { diff --git a/src/Comparison/BooleanComparison.php b/src/Comparison/BooleanComparison.php index 5ad5aa7..469cb96 100644 --- a/src/Comparison/BooleanComparison.php +++ b/src/Comparison/BooleanComparison.php @@ -4,21 +4,45 @@ class BooleanComparison extends AbstractComparison { + /** + * @param bool $expected + * @param bool $value + * + * @return bool + */ public function boolAnd(bool $expected, bool $value): bool { return $expected && $value; } + /** + * @param $expected + * @param $value + * + * @return bool + */ public function boolOr($expected, $value): bool { return $expected || $value; } - + + /** + * @param $expected + * @param $value + * + * @return bool + */ public function isNull($expected, $value): bool { return $value === null; } - + + /** + * @param $expected + * @param $value + * + * @return bool + */ public function isNotNull($expected, $value): bool { return $value !== null; diff --git a/src/Comparison/DatetimeComparison.php b/src/Comparison/DatetimeComparison.php index 735a7fb..b87086c 100644 --- a/src/Comparison/DatetimeComparison.php +++ b/src/Comparison/DatetimeComparison.php @@ -4,21 +4,45 @@ class DatetimeComparison extends AbstractComparison { + /** + * @param \DateTime $start + * @param \DateTime $end + * @param \DateTime $datetime + * + * @return bool + */ public function isBetween(\DateTime $start, \DateTime $end, \DateTime $datetime): bool { return $start <= $datetime && $end >= $datetime; } + /** + * @param string $format + * @param \DateTime $datetime + * + * @return bool + */ public function isMoreRecentThan(string $format, \DateTime $datetime): bool { return $this->getDatetimeFromFormat($format) <= $datetime; } + /** + * @param string $format + * @param \DateTime $datetime + * + * @return bool + */ public function isLessRecentThan(string $format, \DateTime $datetime): bool { return $this->getDatetimeFromFormat($format) >= $datetime; } + /** + * @param string $format + * + * @return \DateTime + */ public function getDatetimeFromFormat(string $format): \DateTime { $formats = [ diff --git a/src/Comparison/NumericComparison.php b/src/Comparison/NumericComparison.php index 7ece3e4..b9e1db6 100644 --- a/src/Comparison/NumericComparison.php +++ b/src/Comparison/NumericComparison.php @@ -4,26 +4,56 @@ class NumericComparison extends AbstractComparison { + /** + * @param int $expected + * @param int $value + * + * @return bool + */ public function isEqual(int $expected, int $value): bool { return $expected === $value; } + /** + * @param int $expected + * @param int $value + * + * @return bool + */ public function isLesserThan(int $expected, int $value): bool { return $expected > $value; } + /** + * @param int $expected + * @param int $value + * + * @return bool + */ public function isLesserThanOrEqual(int $expected, int $value): bool { return $expected >= $value; } + /** + * @param int $expected + * @param int $value + * + * @return bool + */ public function isGreaterThan(int $expected, int $value): bool { return $expected < $value; } + /** + * @param int $expected + * @param int $value + * + * @return bool + */ public function isGreaterThanOrEqual(int $expected, int $value): bool { return $expected <= $value; diff --git a/src/Comparison/ObjectComparison.php b/src/Comparison/ObjectComparison.php index 84a4f9f..7797f06 100644 --- a/src/Comparison/ObjectComparison.php +++ b/src/Comparison/ObjectComparison.php @@ -4,14 +4,23 @@ class ObjectComparison extends AbstractComparison { + /** + * @param string $attributeId + * @param $value + * @param array $extraData + * + * @return bool + */ public function isFieldEqual(string $attributeId, $value, array $extraData = []): bool { $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( - $attributeManager->getAttribute($attributeId), - null, - $extraData['resource'] - ) === $value; + // Create an attribute out of the extra data we have and + // compare its retrieved value to the expected one + $result = $attributeManager->retrieveAttribute( + $attributeManager->getAttribute($attributeId), + null, + $extraData['resource'] + ); + return $result === $value; } } diff --git a/src/Comparison/StringComparison.php b/src/Comparison/StringComparison.php index f9a6b4d..f5a04f0 100644 --- a/src/Comparison/StringComparison.php +++ b/src/Comparison/StringComparison.php @@ -4,11 +4,23 @@ class StringComparison extends AbstractComparison { + /** + * @param string $expected + * @param $value + * + * @return bool + */ public function isEqual(string $expected, $value): bool { return $expected === $value; } + /** + * @param string $expected + * @param $value + * + * @return bool + */ public function isNotEqual(string $expected, $value): bool { return !$this->isEqual($expected, $value); diff --git a/src/Comparison/UserComparison.php b/src/Comparison/UserComparison.php index 30a77e9..5f02733 100644 --- a/src/Comparison/UserComparison.php +++ b/src/Comparison/UserComparison.php @@ -4,13 +4,21 @@ class UserComparison extends AbstractComparison { + /** + * @param string $attributeId + * @param $value + * @param array $extraData + * + * @return bool + */ public function isFieldEqual(string $attributeId, $value, array $extraData = []): bool { $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( - $attributeManager->getAttribute($attributeId), - $extraData['user'] - ) === $value; + $result = $attributeManager->retrieveAttribute( + $attributeManager->getAttribute($attributeId), + $extraData['user'] + ); + return $result === $value; } } diff --git a/src/Configuration/Configuration.php b/src/Configuration/Configuration.php index 999d271..de12143 100644 --- a/src/Configuration/Configuration.php +++ b/src/Configuration/Configuration.php @@ -10,20 +10,37 @@ class Configuration implements ConfigurationInterface { - /** @var AbacLoader[] * */ + /** + * @var array + */ protected $loaders = []; - /** @var array * */ + /** + * @var array + */ protected $rules = []; - /** @var array * */ + /** + * @var array + */ protected $attributes = []; - /** @var array List of File Already Loaded */ + /** + * @var array List of File Already Loaded + */ protected $loadedFiles = []; - + //TODO: need to make it more flexible const LOADERS = [ JsonLoader::class, YamlLoader::class ]; - + + /** + * Configuration constructor. + * + * @param array $configurationFiles + * @param string|null $configDir + * + * @throws \Symfony\Component\Config\Exception\FileLoaderImportCircularReferenceException + * @throws \Symfony\Component\Config\Exception\LoaderLoadException + */ public function __construct(array $configurationFiles, string $configDir = null) { $this->initLoaders($configDir); @@ -34,16 +51,29 @@ protected function initLoaders(string $configDir = null) { $locator = new FileLocator($configDir); foreach (self::LOADERS as $loaderClass) { + /** + * @var YamlLoader|JsonLoader $loader + */ $loader = new $loaderClass($locator); $loader->setCurrentDir($configDir); $this->loaders[] = $loader; } } - + + /** + * @param array $configurationFiles + * + * @throws \Symfony\Component\Config\Exception\FileLoaderImportCircularReferenceException + * @throws \Symfony\Component\Config\Exception\LoaderLoadException + */ protected function parseFiles(array $configurationFiles) { foreach ($configurationFiles as $configurationFile) { - $config = $this->getLoader($configurationFile)->import($configurationFile, pathinfo($configurationFile, PATHINFO_EXTENSION)); + $config = $this->getLoader($configurationFile) + ->import( + $configurationFile, + pathinfo($configurationFile, PATHINFO_EXTENSION) + ); if (in_array($configurationFile, $this->loadedFiles)) { continue; @@ -64,7 +94,13 @@ protected function parseFiles(array $configurationFiles) } } } - + + /** + * @param string $configurationFile + * + * @return LoaderInterface|YamlLoader|JsonLoader + * @throws \Exception + */ protected function getLoader(string $configurationFile): LoaderInterface { foreach ($this->loaders as $abacLoader) { @@ -74,12 +110,18 @@ protected function getLoader(string $configurationFile): LoaderInterface } throw new \Exception('Loader not found for the file ' . $configurationFile); } - + + /** + * @return array + */ public function getAttributes(): array { return $this->attributes; } - + + /** + * @return array + */ public function getRules(): array { return $this->rules; diff --git a/src/Loader/JsonLoader.php b/src/Loader/JsonLoader.php index 3a78070..4d38264 100644 --- a/src/Loader/JsonLoader.php +++ b/src/Loader/JsonLoader.php @@ -6,13 +6,25 @@ class JsonLoader extends FileLoader { - public function load($resource, $type = null) + /** + * @param string $filename + * @param null $type + * + * @return mixed + */ + public function load($filename, $type = null) { - return json_decode(file_get_contents($resource), true); + return json_decode(file_get_contents($filename), true); } - public function supports($resource, $type = null): bool + /** + * @param string $filename + * @param null $type + * + * @return bool + */ + public function supports($filename, $type = null): bool { - return pathinfo($resource, PATHINFO_EXTENSION) === 'json'; + return pathinfo($filename, PATHINFO_EXTENSION) === 'json'; } } diff --git a/src/Loader/YamlLoader.php b/src/Loader/YamlLoader.php index 0371fd9..00ebe09 100644 --- a/src/Loader/YamlLoader.php +++ b/src/Loader/YamlLoader.php @@ -8,13 +8,25 @@ class YamlLoader extends FileLoader { - public function load($resource, $type = null) + /** + * @param string $filename + * @param null $type + * + * @return mixed + */ + public function load($filename, $type = null) { - return Yaml::parse(file_get_contents($resource)); + return Yaml::parse(file_get_contents($filename)); } - public function supports($resource, $type = null): bool + /** + * @param string $filename + * @param null $type + * + * @return bool + */ + public function supports($filename, $type = null): bool { - return in_array(pathinfo($resource, PATHINFO_EXTENSION), ['yml','yaml']); + return in_array(pathinfo($filename, PATHINFO_EXTENSION), ['yml', 'yaml']); } } diff --git a/src/Manager/AttributeManager.php b/src/Manager/AttributeManager.php index f962444..a258282 100644 --- a/src/Manager/AttributeManager.php +++ b/src/Manager/AttributeManager.php @@ -2,8 +2,7 @@ namespace PhpAbac\Manager; -use PhpAbac\Configuration\Configuration; - +use PhpAbac\Configuration\ConfigurationInterface; use PhpAbac\Model\{ AbstractAttribute, Attribute, @@ -18,14 +17,18 @@ class AttributeManager implements AttributeManagerInterface private $getter_prefix = 'get'; /** @var string Function to apply on the getter name ( before adding prefix ) (default)'ucfirst' */ private $getter_name_transformation_function = 'ucfirst'; - + /** * 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' + * 'getter_name_transformation_function' => Function to apply on + * the getter name ( before adding prefix ) (default)'ucfirst' + * + * @param ConfigurationInterface $configuration + * @param array $options */ - public function __construct(Configuration $configuration, array $options = []) + public function __construct(ConfigurationInterface $configuration, array $options = []) { $this->attributes = $configuration->getAttributes(); @@ -76,8 +79,12 @@ private function getEnvironmentAttribute(array $attributeData, string $key): Env ; } - public function retrieveAttribute(AbstractAttribute $attribute, $user = null, $object = null, array $getter_params = []) - { + public function retrieveAttribute( + AbstractAttribute $attribute, + $user = null, + $object = null, + array $getter_params = [] + ){ switch ($attribute->getType()) { case 'user': return $this->retrieveClassicAttribute($attribute, $user, $getter_params); @@ -96,7 +103,11 @@ private function retrieveClassicAttribute(Attribute $attribute, $object, array $ $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])) { - throw new \InvalidArgumentException('There is no getter for the "'.$attribute->getProperty().'" attribute for object "'.get_class($propertyValue).'" with getter "'.$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, diff --git a/src/Manager/AttributeManagerInterface.php b/src/Manager/AttributeManagerInterface.php index 9dd32b5..46253c0 100644 --- a/src/Manager/AttributeManagerInterface.php +++ b/src/Manager/AttributeManagerInterface.php @@ -8,5 +8,10 @@ interface AttributeManagerInterface { public function getAttribute(string $attributeId): AbstractAttribute; - public function retrieveAttribute(AbstractAttribute $attribute, $user = null, $object = null, array $getter_params = []); + public function retrieveAttribute( + AbstractAttribute $attribute, + $user = null, + $object = null, + array $getter_params = [] + ); } diff --git a/src/Manager/CacheManager.php b/src/Manager/CacheManager.php index bfb904e..a842eb1 100644 --- a/src/Manager/CacheManager.php +++ b/src/Manager/CacheManager.php @@ -2,6 +2,7 @@ namespace PhpAbac\Manager; +use PhpCsFixer\Cache\Cache; use Psr\Cache\{ CacheItemInterface, CacheItemPoolInterface @@ -21,6 +22,9 @@ public function __construct(array $options = []) $this->options = $options; } + /** + * @param CacheItemInterface $item + */ public function save(CacheItemInterface $item) { $this->getItemPool($item->getDriver())->save($item); diff --git a/src/Manager/ComparisonManager.php b/src/Manager/ComparisonManager.php index f60d2dc..fefdf57 100644 --- a/src/Manager/ComparisonManager.php +++ b/src/Manager/ComparisonManager.php @@ -31,7 +31,7 @@ class ComparisonManager implements ComparisonManagerInterface /** @var array **/ protected $rejectedAttributes = []; - public function __construct(AttributeManager $manager) + public function __construct(AttributeManagerInterface $manager) { $this->attributeManager = $manager; } diff --git a/src/Manager/PolicyRuleManager.php b/src/Manager/PolicyRuleManager.php index a84a063..30347df 100644 --- a/src/Manager/PolicyRuleManager.php +++ b/src/Manager/PolicyRuleManager.php @@ -4,6 +4,7 @@ use PhpAbac\Configuration\Configuration; +use PhpAbac\Configuration\ConfigurationInterface; use PhpAbac\Model\{ PolicyRule, PolicyRuleAttribute @@ -16,7 +17,7 @@ class PolicyRuleManager implements PolicyRuleManagerInterface /** @var array **/ private $rules = []; - public function __construct(Configuration $configuration, AttributeManager $attributeManager) + public function __construct(ConfigurationInterface $configuration, AttributeManagerInterface $attributeManager) { $this->attributeManager = $attributeManager; $this->rules = $configuration->getRules(); diff --git a/src/Model/PolicyRuleAttribute.php b/src/Model/PolicyRuleAttribute.php index 61c9024..d37542c 100644 --- a/src/Model/PolicyRuleAttribute.php +++ b/src/Model/PolicyRuleAttribute.php @@ -2,21 +2,43 @@ namespace PhpAbac\Model; +/** + * Class PolicyRuleAttribute + * + * @package PhpAbac\Model + */ class PolicyRuleAttribute { - /** @var AbstractAttribute **/ + /** + * @var AbstractAttribute + */ protected $attribute; - /** @var string **/ + /** + * @var string + */ protected $comparisonType; - /** @var string **/ + /** + * @var string + */ protected $comparison; - /** @var mixed **/ + /** + * @var mixed + */ protected $value; - /** @var array **/ + /** + * @var array + */ protected $extraData = []; - /** @var array Extended parameter */ + /** + * @var array Extended parameter + */ protected $getter_params_a = []; + /** + * @param AbstractAttribute $attribute + * + * @return PolicyRuleAttribute + */ public function setAttribute(AbstractAttribute $attribute): PolicyRuleAttribute { $this->attribute = $attribute; @@ -24,11 +46,19 @@ public function setAttribute(AbstractAttribute $attribute): PolicyRuleAttribute return $this; } + /** + * @return AbstractAttribute + */ public function getAttribute(): AbstractAttribute { return $this->attribute; } + /** + * @param string $comparisonType + * + * @return PolicyRuleAttribute + */ public function setComparisonType(string $comparisonType): PolicyRuleAttribute { $this->comparisonType = $comparisonType; @@ -36,11 +66,19 @@ public function setComparisonType(string $comparisonType): PolicyRuleAttribute return $this; } + /** + * @return string + */ public function getComparisonType(): string { return $this->comparisonType; } + /** + * @param string $comparison + * + * @return PolicyRuleAttribute + */ public function setComparison(string $comparison): PolicyRuleAttribute { $this->comparison = $comparison; @@ -48,11 +86,19 @@ public function setComparison(string $comparison): PolicyRuleAttribute return $this; } + /** + * @return string + */ public function getComparison(): string { return $this->comparison; } + /** + * @param $value + * + * @return PolicyRuleAttribute + */ public function setValue($value): PolicyRuleAttribute { $this->value = $value; @@ -60,25 +106,44 @@ public function setValue($value): PolicyRuleAttribute return $this; } + /** + * @return mixed + */ public function getValue() { return $this->value; } - + + /** + * @param array $extraData + * + * @return PolicyRuleAttribute + */ public function setExtraData(array $extraData): PolicyRuleAttribute { $this->extraData = $extraData; return $this; } - + + /** + * @param string $key + * @param $value + * + * @return PolicyRuleAttribute + */ public function addExtraData(string $key, $value): PolicyRuleAttribute { $this->extraData[$key] = $value; return $this; } - + + /** + * @param string $key + * + * @return PolicyRuleAttribute + */ public function removeExtraData(string $key): PolicyRuleAttribute { if (isset($this->extraData[$key])) { @@ -86,19 +151,30 @@ public function removeExtraData(string $key): PolicyRuleAttribute } return $this; } - + + /** + * @return array + */ public function getExtraData(): array { return $this->extraData; } - + + /** + * @param array $value + * + * @return PolicyRuleAttribute + */ public function setGetterParams(array $value): PolicyRuleAttribute { $this->getter_params_a = $value; return $this; } - + + /** + * @return array + */ public function getGetterParams(): array { return $this->getter_params_a;