@@ -6,7 +6,6 @@ |
||
6 | 6 | use PhpAbac\Manager\PolicyRuleManager; |
7 | 7 | use PhpAbac\Manager\ConfigurationManager; |
8 | 8 | use PhpAbac\Manager\CacheManager; |
9 | - |
|
10 | 9 | use Symfony\Component\Config\FileLocator; |
11 | 10 | |
12 | 11 | class Abac |
@@ -27,26 +27,26 @@ discard block |
||
27 | 27 | * @param FileLocatorInterface $locator |
28 | 28 | * @param string|array $format A format or an array of format |
29 | 29 | */ |
30 | - public function __construct( FileLocatorInterface $locator, $format = [ |
|
30 | + public function __construct(FileLocatorInterface $locator, $format = [ |
|
31 | 31 | 'yaml', |
32 | 32 | 'json', |
33 | - ] ) { |
|
33 | + ]) { |
|
34 | 34 | $this->locator = $locator; |
35 | 35 | $this->attributes = []; |
36 | 36 | $this->rules = []; |
37 | 37 | $this->config_files_loaded = []; |
38 | - if ( in_array( 'yaml', $format ) ) { |
|
39 | - $this->loaders[ 'yaml' ] = new YamlAbacLoader( $locator, $this ); |
|
38 | + if (in_array('yaml', $format)) { |
|
39 | + $this->loaders['yaml'] = new YamlAbacLoader($locator, $this); |
|
40 | 40 | } |
41 | - if ( in_array( 'json', $format ) ) { |
|
42 | - $this->loaders[ 'json' ] = new JsonAbacLoader( $locator, $this ); |
|
41 | + if (in_array('json', $format)) { |
|
42 | + $this->loaders['json'] = new JsonAbacLoader($locator, $this); |
|
43 | 43 | } |
44 | 44 | |
45 | 45 | } |
46 | 46 | |
47 | 47 | public function setConfigPathRoot($configPaths_root = null) { |
48 | 48 | // $this->config_path_route = $configPaths_root; |
49 | - foreach($this->loaders as $loader) { |
|
49 | + foreach ($this->loaders as $loader) { |
|
50 | 50 | $loader->setCurrentDir($configPaths_root); |
51 | 51 | } |
52 | 52 | } |
@@ -54,11 +54,11 @@ discard block |
||
54 | 54 | /** |
55 | 55 | * @param array $configurationFiles |
56 | 56 | */ |
57 | - public function parseConfigurationFile( $configurationFiles ) { |
|
58 | - foreach ( $configurationFiles as $configurationFile ) { |
|
59 | - $config = $this->getLoader( $configurationFile )->import( $configurationFile, pathinfo( $configurationFile, PATHINFO_EXTENSION ) ); |
|
57 | + public function parseConfigurationFile($configurationFiles) { |
|
58 | + foreach ($configurationFiles as $configurationFile) { |
|
59 | + $config = $this->getLoader($configurationFile)->import($configurationFile, pathinfo($configurationFile, PATHINFO_EXTENSION)); |
|
60 | 60 | |
61 | - if (in_array($config['path'],$this->config_files_loaded)) { |
|
61 | + if (in_array($config['path'], $this->config_files_loaded)) { |
|
62 | 62 | continue; |
63 | 63 | } |
64 | 64 | |
@@ -69,11 +69,11 @@ discard block |
||
69 | 69 | unset($config['@import']); |
70 | 70 | } |
71 | 71 | |
72 | - if ( isset( $config[ 'attributes' ] ) ) { |
|
73 | - $this->attributes = array_merge( $this->attributes, $config[ 'attributes' ] ); |
|
72 | + if (isset($config['attributes'])) { |
|
73 | + $this->attributes = array_merge($this->attributes, $config['attributes']); |
|
74 | 74 | } |
75 | - if ( isset( $config[ 'rules' ] ) ) { |
|
76 | - $this->rules = array_merge( $this->rules, $config[ 'rules' ] ); |
|
75 | + if (isset($config['rules'])) { |
|
76 | + $this->rules = array_merge($this->rules, $config['rules']); |
|
77 | 77 | } |
78 | 78 | } |
79 | 79 | } |
@@ -89,14 +89,14 @@ discard block |
||
89 | 89 | * |
90 | 90 | * @throws \Exception |
91 | 91 | */ |
92 | - private function getLoader( $configurationFile ) { |
|
92 | + private function getLoader($configurationFile) { |
|
93 | 93 | |
94 | - foreach ( $this->loaders as $AbacLoader ) { |
|
95 | - if ( $AbacLoader::supportsExtension( $configurationFile ) ) { |
|
94 | + foreach ($this->loaders as $AbacLoader) { |
|
95 | + if ($AbacLoader::supportsExtension($configurationFile)) { |
|
96 | 96 | return $AbacLoader; |
97 | 97 | } |
98 | 98 | } |
99 | - throw new \Exception( 'Loader not found for the file ' . $configurationFile ); |
|
99 | + throw new \Exception('Loader not found for the file '.$configurationFile); |
|
100 | 100 | } |
101 | 101 | |
102 | 102 | /** |
@@ -103,7 +103,7 @@ discard block |
||
103 | 103 | * @return \PhpAbac\Example\User |
104 | 104 | */ |
105 | 105 | public function removeVisa(Visa $visa) { |
106 | - if(isset($this->visas[$visa->getId()])) { |
|
106 | + if (isset($this->visas[$visa->getId()])) { |
|
107 | 107 | unset($this->visas[$visa->getId()]); |
108 | 108 | } |
109 | 109 | return $this; |
@@ -126,7 +126,7 @@ discard block |
||
126 | 126 | public function getVisa($country_code) { |
127 | 127 | /** @var Visa $visa */ |
128 | 128 | $visas = []; |
129 | - foreach($this->visas as $visa) { |
|
129 | + foreach ($this->visas as $visa) { |
|
130 | 130 | if ($visa->getCountry()->getCode() == $country_code) |
131 | 131 | $visas[] = $visa; |
132 | 132 | } |
@@ -30,26 +30,26 @@ discard block |
||
30 | 30 | * @param string $configPaths_root The origin folder to find $configPaths |
31 | 31 | * @param array $options |
32 | 32 | */ |
33 | - public function __construct( $configPaths, $cacheOptions = [], $configPaths_root = null, $options = [] ) { |
|
34 | - $this->configure( $configPaths, $configPaths_root ); |
|
35 | - $this->attributeManager = new AttributeManager( $this->configuration->getAttributes(), $options ); |
|
36 | - $this->policyRuleManager = new PolicyRuleManager( $this->attributeManager, $this->configuration->getRules() ); |
|
37 | - $this->cacheManager = new CacheManager( $cacheOptions ); |
|
38 | - $this->comparisonManager = new ComparisonManager( $this->attributeManager ); |
|
33 | + public function __construct($configPaths, $cacheOptions = [], $configPaths_root = null, $options = []) { |
|
34 | + $this->configure($configPaths, $configPaths_root); |
|
35 | + $this->attributeManager = new AttributeManager($this->configuration->getAttributes(), $options); |
|
36 | + $this->policyRuleManager = new PolicyRuleManager($this->attributeManager, $this->configuration->getRules()); |
|
37 | + $this->cacheManager = new CacheManager($cacheOptions); |
|
38 | + $this->comparisonManager = new ComparisonManager($this->attributeManager); |
|
39 | 39 | } |
40 | 40 | |
41 | 41 | /** |
42 | 42 | * @param array $configPaths |
43 | 43 | * @param string $configPaths_root The origin folder to find $configPaths |
44 | 44 | */ |
45 | - public function configure( $configPaths, $configPaths_root = null ) { |
|
45 | + public function configure($configPaths, $configPaths_root = null) { |
|
46 | 46 | // foreach ( $configPaths as &$configPath ) { |
47 | 47 | // $configPath = $configPaths_root . $configPath; |
48 | 48 | // } |
49 | - $locator = new FileLocator( $configPaths_root ); |
|
50 | - $this->configuration = new ConfigurationManager( $locator ); |
|
51 | - $this->configuration->setConfigPathRoot( $configPaths_root ); |
|
52 | - $this->configuration->parseConfigurationFile( $configPaths ); |
|
49 | + $locator = new FileLocator($configPaths_root); |
|
50 | + $this->configuration = new ConfigurationManager($locator); |
|
51 | + $this->configuration->setConfigPathRoot($configPaths_root); |
|
52 | + $this->configuration->parseConfigurationFile($configPaths); |
|
53 | 53 | } |
54 | 54 | |
55 | 55 | /** |
@@ -74,47 +74,47 @@ discard block |
||
74 | 74 | * |
75 | 75 | * @return boolean|array |
76 | 76 | */ |
77 | - public function enforce( $ruleName, $user, $resource = null, $options = [] ) { |
|
77 | + public function enforce($ruleName, $user, $resource = null, $options = []) { |
|
78 | 78 | // If there is dynamic attributes, we pass them to the comparison manager |
79 | 79 | // When a comparison will be performed, the passed values will be retrieved and used |
80 | - if ( isset( $options[ 'dynamic_attributes' ] ) ) { |
|
81 | - $this->comparisonManager->setDynamicAttributes( $options[ 'dynamic_attributes' ] ); |
|
80 | + if (isset($options['dynamic_attributes'])) { |
|
81 | + $this->comparisonManager->setDynamicAttributes($options['dynamic_attributes']); |
|
82 | 82 | } |
83 | 83 | // Retrieve cache value for the current rule and values if cache item is valid |
84 | - if ( ( $cacheResult = isset( $options[ 'cache_result' ] ) && $options[ 'cache_result' ] === true ) === true ) { |
|
85 | - $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 ); |
|
84 | + if (($cacheResult = isset($options['cache_result']) && $options['cache_result'] === true) === true) { |
|
85 | + $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); |
|
86 | 86 | // We check if the cache value s valid before returning it |
87 | - if ( ( $cacheValue = $cacheItem->get() ) !== null ) { |
|
87 | + if (($cacheValue = $cacheItem->get()) !== null) { |
|
88 | 88 | return $cacheValue; |
89 | 89 | } |
90 | 90 | } |
91 | - $policyRule_a = $this->policyRuleManager->getRule( $ruleName, $user, $resource ); |
|
91 | + $policyRule_a = $this->policyRuleManager->getRule($ruleName, $user, $resource); |
|
92 | 92 | |
93 | - foreach ( $policyRule_a as $policyRule ) { |
|
93 | + foreach ($policyRule_a as $policyRule) { |
|
94 | 94 | // For each policy rule attribute, we retrieve the attribute value and proceed configured extra data |
95 | - foreach ( $policyRule->getPolicyRuleAttributes() as $pra ) { |
|
95 | + foreach ($policyRule->getPolicyRuleAttributes() as $pra) { |
|
96 | 96 | /** @var PolicyRuleAttribute $pra */ |
97 | 97 | $attribute = $pra->getAttribute(); |
98 | 98 | |
99 | 99 | $getter_params = $this->prepareGetterParams($pra->getGetterParams(), $user, $resource); |
100 | 100 | // var_dump($pra->getGetterParams()); |
101 | 101 | // var_dump($getter_params); |
102 | - $attribute->setValue( $this->attributeManager->retrieveAttribute( $attribute, $user, $resource, $getter_params ) ); |
|
103 | - if ( count( $pra->getExtraData() ) > 0 ) { |
|
104 | - $this->processExtraData( $pra, $user, $resource ); |
|
102 | + $attribute->setValue($this->attributeManager->retrieveAttribute($attribute, $user, $resource, $getter_params)); |
|
103 | + if (count($pra->getExtraData()) > 0) { |
|
104 | + $this->processExtraData($pra, $user, $resource); |
|
105 | 105 | } |
106 | - $this->comparisonManager->compare( $pra ); |
|
106 | + $this->comparisonManager->compare($pra); |
|
107 | 107 | } |
108 | 108 | // The given result could be an array of rejected attributes or true |
109 | 109 | // True means that the rule is correctly enforced for the given user and resource |
110 | 110 | $result = $this->comparisonManager->getResult(); |
111 | - if ( true === $result ) { |
|
111 | + if (true === $result) { |
|
112 | 112 | break; |
113 | 113 | } |
114 | 114 | } |
115 | - if ( $cacheResult ) { |
|
116 | - $cacheItem->set( $result ); |
|
117 | - $this->cacheManager->save( $cacheItem ); |
|
115 | + if ($cacheResult) { |
|
116 | + $cacheItem->set($result); |
|
117 | + $this->cacheManager->save($cacheItem); |
|
118 | 118 | } |
119 | 119 | |
120 | 120 | return $result; |
@@ -132,13 +132,13 @@ discard block |
||
132 | 132 | private function prepareGetterParams($getter_params, $user, $resource) { |
133 | 133 | if (empty($getter_params)) return []; |
134 | 134 | $values = []; |
135 | - foreach($getter_params as $getter_name=>$params) { |
|
136 | - foreach($params as $param) { |
|
137 | - if ( '@' !== $param[ 'param_name' ][ 0 ] ) { |
|
138 | - $values[$getter_name][] = $param[ 'param_value' ]; |
|
135 | + foreach ($getter_params as $getter_name=>$params) { |
|
136 | + foreach ($params as $param) { |
|
137 | + if ('@' !== $param['param_name'][0]) { |
|
138 | + $values[$getter_name][] = $param['param_value']; |
|
139 | 139 | } |
140 | 140 | else { |
141 | - $values[$getter_name][] = $this->attributeManager->retrieveAttribute( $this->attributeManager->getAttribute( $param[ 'param_value' ] ) , $user, $resource ); |
|
141 | + $values[$getter_name][] = $this->attributeManager->retrieveAttribute($this->attributeManager->getAttribute($param['param_value']), $user, $resource); |
|
142 | 142 | } |
143 | 143 | } |
144 | 144 | } |
@@ -150,26 +150,26 @@ discard block |
||
150 | 150 | * @param object $user |
151 | 151 | * @param object $resource |
152 | 152 | */ |
153 | - public function processExtraData( PolicyRuleAttribute $pra, $user, $resource ) { |
|
154 | - foreach ( $pra->getExtraData() as $key => $data ) { |
|
155 | - switch ( $key ) { |
|
153 | + public function processExtraData(PolicyRuleAttribute $pra, $user, $resource) { |
|
154 | + foreach ($pra->getExtraData() as $key => $data) { |
|
155 | + switch ($key) { |
|
156 | 156 | case 'with': |
157 | 157 | // This data has to be removed for it will be stored elsewhere |
158 | 158 | // in the policy rule attribute |
159 | - $pra->removeExtraData( 'with' ); |
|
159 | + $pra->removeExtraData('with'); |
|
160 | 160 | // The "with" extra data is an array of attributes, which are objects |
161 | 161 | // Once we process it as policy rule attributes, we set it as the main policy rule attribute value |
162 | 162 | $subPolicyRuleAttributes = []; |
163 | 163 | $extraData = []; |
164 | 164 | |
165 | - foreach ( $this->policyRuleManager->processRuleAttributes( $data, $user, $resource ) as $subPolicyRuleAttribute ) { |
|
165 | + foreach ($this->policyRuleManager->processRuleAttributes($data, $user, $resource) as $subPolicyRuleAttribute) { |
|
166 | 166 | $subPolicyRuleAttributes[] = $subPolicyRuleAttribute; |
167 | 167 | } |
168 | - $pra->setValue( $subPolicyRuleAttributes ); |
|
168 | + $pra->setValue($subPolicyRuleAttributes); |
|
169 | 169 | // This data can be used in complex comparisons |
170 | - $pra->addExtraData( 'attribute', $pra->getAttribute() ); |
|
171 | - $pra->addExtraData( 'user', $user ); |
|
172 | - $pra->addExtraData( 'resource', $resource ); |
|
170 | + $pra->addExtraData('attribute', $pra->getAttribute()); |
|
171 | + $pra->addExtraData('user', $user); |
|
172 | + $pra->addExtraData('resource', $resource); |
|
173 | 173 | break; |
174 | 174 | } |
175 | 175 | } |
@@ -21,8 +21,8 @@ discard block |
||
21 | 21 | /** @var ConfigurationManager The configuration manage instanciator and user of this AbacLoader Instance */ |
22 | 22 | protected $configurationManger; |
23 | 23 | |
24 | - public function __construct( FileLocatorInterface $locator ) { |
|
25 | - parent::__construct( $locator ); |
|
24 | + public function __construct(FileLocatorInterface $locator) { |
|
25 | + parent::__construct($locator); |
|
26 | 26 | } |
27 | 27 | |
28 | 28 | /** |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | * |
34 | 34 | * @return string |
35 | 35 | */ |
36 | - abstract public function load( $resource, $type = null ); |
|
36 | + abstract public function load($resource, $type = null); |
|
37 | 37 | |
38 | 38 | // /** |
39 | 39 | // * Method to check if a resource ( by his path ) is supported by the loader |
@@ -53,8 +53,8 @@ discard block |
||
53 | 53 | * |
54 | 54 | * @return boolean Return true if the extension of the ressource is supported by the loader |
55 | 55 | */ |
56 | - public static final function supportsExtension( $resource ) { |
|
57 | - return in_array( pathinfo( $resource, PATHINFO_EXTENSION ), self::getExtensionAllowed() ); |
|
56 | + public static final function supportsExtension($resource) { |
|
57 | + return in_array(pathinfo($resource, PATHINFO_EXTENSION), self::getExtensionAllowed()); |
|
58 | 58 | } |
59 | 59 | |
60 | 60 | /** |
@@ -6,7 +6,7 @@ |
||
6 | 6 | |
7 | 7 | class YamlAbacLoader extends AbacLoader |
8 | 8 | { |
9 | - protected static $_EXTENSION_ALLOWED_A = ['yml','yaml']; |
|
9 | + protected static $_EXTENSION_ALLOWED_A = ['yml', 'yaml']; |
|
10 | 10 | |
11 | 11 | public function load($resource, $type = null) |
12 | 12 | { |
@@ -14,7 +14,7 @@ |
||
14 | 14 | // $path_to_load = $this->locator->locate($resource); |
15 | 15 | $path_to_load = $resource; |
16 | 16 | |
17 | - return (new JsonDecode(true))->decode(file_get_contents($path_to_load),JsonEncoder::FORMAT,[ 'json_decode_associative' => true ] ) + ['path' => $path_to_load]; |
|
17 | + return (new JsonDecode(true))->decode(file_get_contents($path_to_load), JsonEncoder::FORMAT, ['json_decode_associative' => true]) + ['path' => $path_to_load]; |
|
18 | 18 | } |
19 | 19 | |
20 | 20 | public function supports($resource, $type = null) |
@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | public function getRule($ruleName, $user, $resource) |
33 | 33 | { |
34 | 34 | if (!isset($this->rules[$ruleName])) { |
35 | - throw new \InvalidArgumentException('The given rule "' . $ruleName . '" is not configured'); |
|
35 | + throw new \InvalidArgumentException('The given rule "'.$ruleName.'" is not configured'); |
|
36 | 36 | } |
37 | 37 | |
38 | 38 | // force to treat always arrays |
@@ -70,12 +70,12 @@ discard block |
||
70 | 70 | ->setComparison($attribute['comparison']) |
71 | 71 | ->setComparisonType($attribute['comparison_type']) |
72 | 72 | ->setValue((isset($attribute['value'])) ? $attribute['value'] : null) |
73 | - ->setGetterParams( isset( $attribute[ 'getter_params' ] ) ? $attribute[ 'getter_params' ] : [] ); |
|
73 | + ->setGetterParams(isset($attribute['getter_params']) ? $attribute['getter_params'] : []); |
|
74 | 74 | $this->processRuleAttributeComparisonType($pra, $user, $resource); |
75 | 75 | // In the case the user configured more keys than the basic ones |
76 | 76 | // it will be stored as extra data |
77 | 77 | foreach ($attribute as $key => $value) { |
78 | - if (!in_array($key, ['comparison', 'comparison_type', 'value','getter_params'])) { |
|
78 | + if (!in_array($key, ['comparison', 'comparison_type', 'value', 'getter_params'])) { |
|
79 | 79 | $pra->addExtraData($key, $value); |
80 | 80 | } |
81 | 81 | } |
@@ -28,12 +28,12 @@ discard block |
||
28 | 28 | { |
29 | 29 | $this->attributes = $attributes; |
30 | 30 | |
31 | - $options = array_intersect_key( $options, array_flip( [ |
|
31 | + $options = array_intersect_key($options, array_flip([ |
|
32 | 32 | 'getter_prefix', |
33 | 33 | 'getter_name_transformation_function', |
34 | - ] ) ); |
|
34 | + ])); |
|
35 | 35 | |
36 | - foreach($options as $name => $value) { |
|
36 | + foreach ($options as $name => $value) { |
|
37 | 37 | $this->$name = $value; |
38 | 38 | } |
39 | 39 | } |
@@ -95,7 +95,7 @@ discard block |
||
95 | 95 | */ |
96 | 96 | public function retrieveAttribute(AbstractAttribute $attribute, $user = null, $object = null, $getter_params = []) |
97 | 97 | { |
98 | - switch($attribute->getType()) { |
|
98 | + switch ($attribute->getType()) { |
|
99 | 99 | case 'user': |
100 | 100 | return $this->retrieveClassicAttribute($attribute, $user, $getter_params); |
101 | 101 | case 'resource': |
@@ -114,18 +114,18 @@ discard block |
||
114 | 114 | { |
115 | 115 | $propertyPath = explode('.', $attribute->getProperty()); |
116 | 116 | $propertyValue = $object; |
117 | - foreach($propertyPath as $property) { |
|
117 | + foreach ($propertyPath as $property) { |
|
118 | 118 | |
119 | 119 | |
120 | - $getter = $this->getter_prefix.call_user_func($this->getter_name_transformation_function,$property); |
|
120 | + $getter = $this->getter_prefix.call_user_func($this->getter_name_transformation_function, $property); |
|
121 | 121 | // Use is_callable, instead of method_exists, to deal with __call magic method |
122 | - if(!is_callable([$propertyValue,$getter])) { |
|
122 | + if (!is_callable([$propertyValue, $getter])) { |
|
123 | 123 | throw new \InvalidArgumentException('There is no getter for the "'.$attribute->getProperty().'" attribute for object "'.get_class($propertyValue).'" with getter "'.$getter.'"'); |
124 | 124 | } |
125 | - if ( ( $propertyValue = call_user_func_array( [ |
|
125 | + if (($propertyValue = call_user_func_array([ |
|
126 | 126 | $propertyValue, |
127 | 127 | $getter, |
128 | - ], isset( $getter_params[ $property ] ) ? $getter_params[ $property ] : [] ) ) === null |
|
128 | + ], isset($getter_params[$property]) ? $getter_params[$property] : [])) === null |
|
129 | 129 | ) { |
130 | 130 | return null; |
131 | 131 | } |