@@ -31,15 +31,15 @@ discard block |
||
31 | 31 | */ |
32 | 32 | public function getRule($ruleName, $user, $resource) |
33 | 33 | { |
34 | - if(!isset($this->rules[$ruleName])) { |
|
35 | - throw new \InvalidArgumentException('The given rule "' . $ruleName . '" is not configured'); |
|
34 | + if (!isset($this->rules[$ruleName])) { |
|
35 | + throw new \InvalidArgumentException('The given rule "'.$ruleName.'" is not configured'); |
|
36 | 36 | } |
37 | 37 | $rule = |
38 | 38 | (new PolicyRule()) |
39 | 39 | ->setName($ruleName) |
40 | 40 | ; |
41 | 41 | // For each policy rule attribute, the data is formatted |
42 | - foreach($this->processRuleAttributes($this->rules[$ruleName]['attributes'], $user, $resource) as $pra) { |
|
42 | + foreach ($this->processRuleAttributes($this->rules[$ruleName]['attributes'], $user, $resource) as $pra) { |
|
43 | 43 | $rule->addPolicyRuleAttribute($pra); |
44 | 44 | } |
45 | 45 | return $rule; |
@@ -53,7 +53,7 @@ discard block |
||
53 | 53 | * @param object $resource |
54 | 54 | */ |
55 | 55 | public function processRuleAttributes($attributes, $user, $resource) { |
56 | - foreach($attributes as $attributeName => $attribute) { |
|
56 | + foreach ($attributes as $attributeName => $attribute) { |
|
57 | 57 | $pra = (new PolicyRuleAttribute()) |
58 | 58 | ->setAttribute($this->attributeManager->getAttribute($attributeName)) |
59 | 59 | ->setComparison($attribute['comparison']) |
@@ -63,8 +63,8 @@ discard block |
||
63 | 63 | $this->processRuleAttributeComparisonType($pra, $user, $resource); |
64 | 64 | // In the case the user configured more keys than the basic ones |
65 | 65 | // it will be stored as extra data |
66 | - foreach($attribute as $key => $value) { |
|
67 | - if(!in_array($key, ['comparison', 'comparison_type', 'value'])) { |
|
66 | + foreach ($attribute as $key => $value) { |
|
67 | + if (!in_array($key, ['comparison', 'comparison_type', 'value'])) { |
|
68 | 68 | $pra->addExtraData($key, $value); |
69 | 69 | } |
70 | 70 | } |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | * @param object $resource |
82 | 82 | */ |
83 | 83 | public function processRuleAttributeComparisonType(PolicyRuleAttribute $pra, $user, $resource) { |
84 | - switch($pra->getComparisonType()) { |
|
84 | + switch ($pra->getComparisonType()) { |
|
85 | 85 | case 'user': $pra->setExtraData(['user' => $user]); break; |
86 | 86 | case 'object': $pra->setExtraData(['resource' => $resource]); break; |
87 | 87 | } |
@@ -60,22 +60,22 @@ discard block |
||
60 | 60 | : $pra->getValue() |
61 | 61 | ; |
62 | 62 | // Checking that the configured comparison type is available |
63 | - if(!isset($this->comparisons[$pra->getComparisonType()])) { |
|
63 | + if (!isset($this->comparisons[$pra->getComparisonType()])) { |
|
64 | 64 | throw new \InvalidArgumentException('The requested comparison class does not exist'); |
65 | 65 | } |
66 | 66 | // The comparison class will perform the attribute check with the configured method |
67 | 67 | // For more complex comparisons, the comparison manager is injected |
68 | 68 | $comparison = new $this->comparisons[$pra->getComparisonType()]($this); |
69 | - if(!method_exists($comparison, $pra->getComparison())) { |
|
69 | + if (!method_exists($comparison, $pra->getComparison())) { |
|
70 | 70 | throw new \InvalidArgumentException('The requested comparison method does not exist'); |
71 | 71 | } |
72 | 72 | // Then the comparison is performed with needed |
73 | 73 | $result = $comparison->{$pra->getComparison()}($praValue, $attribute->getValue(), $pra->getExtraData()); |
74 | 74 | // If the checked attribute is not valid, the attribute slug is marked as rejected |
75 | 75 | // The rejected attributes will be returned instead of the expected true boolean |
76 | - if($result !== true) { |
|
76 | + if ($result !== true) { |
|
77 | 77 | // In case of sub comparing, the error reporting is disabled |
78 | - if(!in_array($attribute->getSlug(), $this->rejectedAttributes) && $subComparing === false) { |
|
78 | + if (!in_array($attribute->getSlug(), $this->rejectedAttributes) && $subComparing === false) { |
|
79 | 79 | $this->rejectedAttributes[] = $attribute->getSlug(); |
80 | 80 | } |
81 | 81 | return false; |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | * @throws \InvalidArgumentException |
103 | 103 | */ |
104 | 104 | public function getDynamicAttribute($attributeSlug) { |
105 | - if(!isset($this->dynamicAttributes[$attributeSlug])) { |
|
105 | + if (!isset($this->dynamicAttributes[$attributeSlug])) { |
|
106 | 106 | throw new \InvalidArgumentException("The dynamic value for attribute $attributeSlug was not given"); |
107 | 107 | } |
108 | 108 | return $this->dynamicAttributes[$attributeSlug]; |
@@ -55,23 +55,23 @@ |
||
55 | 55 | * @return boolean |
56 | 56 | */ |
57 | 57 | public function contains($policyRuleAttributes, $attributes, $extraData = []) { |
58 | - foreach($extraData['attribute']->getValue() as $attribute) { |
|
58 | + foreach ($extraData['attribute']->getValue() as $attribute) { |
|
59 | 59 | $result = true; |
60 | 60 | // For each attribute, we check the whole rules set |
61 | - foreach($policyRuleAttributes as $pra) { |
|
61 | + foreach ($policyRuleAttributes as $pra) { |
|
62 | 62 | $attributeData = $pra->getAttribute(); |
63 | 63 | $attributeData->setValue( |
64 | 64 | $this->comparisonManager->getAttributeManager()->retrieveAttribute($attributeData, $extraData['user'], $attribute) |
65 | 65 | ); |
66 | 66 | // If one field is not matched, the whole attribute is rejected |
67 | - if(!$this->comparisonManager->compare($pra, true)) { |
|
67 | + if (!$this->comparisonManager->compare($pra, true)) { |
|
68 | 68 | //var_dump($attributeData->getName(), $attributeData->getValue(), $pra->getValue()); |
69 | 69 | $result = false; |
70 | 70 | break; |
71 | 71 | } |
72 | 72 | } |
73 | 73 | // If the result is still true at the end of the attribute check, the rule is enforced |
74 | - if($result === true) { |
|
74 | + if ($result === true) { |
|
75 | 75 | return true; |
76 | 76 | } |
77 | 77 | } |
@@ -71,18 +71,18 @@ discard block |
||
71 | 71 | public function enforce($ruleName, $user, $resource = null, $options = []) { |
72 | 72 | // If there is dynamic attributes, we pass them to the comparison manager |
73 | 73 | // When a comparison will be performed, the passed values will be retrieved and used |
74 | - if(isset($options['dynamic_attributes'])) { |
|
74 | + if (isset($options['dynamic_attributes'])) { |
|
75 | 75 | $this->comparisonManager->setDynamicAttributes($options['dynamic_attributes']); |
76 | 76 | } |
77 | 77 | // Retrieve cache value for the current rule and values if cache item is valid |
78 | - if(($cacheResult = isset($options['cache_result']) && $options['cache_result'] === true) === true) { |
|
78 | + if (($cacheResult = isset($options['cache_result']) && $options['cache_result'] === true) === true) { |
|
79 | 79 | $cacheItem = $this->cacheManager->getItem( |
80 | - "$ruleName-{$user->getId()}-" . (($resource !== null) ? $resource->getId() : ''), |
|
80 | + "$ruleName-{$user->getId()}-".(($resource !== null) ? $resource->getId() : ''), |
|
81 | 81 | (isset($options['cache_driver'])) ? $options['cache_driver'] : null, |
82 | 82 | (isset($options['cache_ttl'])) ? $options['cache_ttl'] : null |
83 | 83 | ); |
84 | 84 | // We check if the cache value s valid before returning it |
85 | - if(($cacheValue = $cacheItem->get()) !== null) { |
|
85 | + if (($cacheValue = $cacheItem->get()) !== null) { |
|
86 | 86 | return $cacheValue; |
87 | 87 | } |
88 | 88 | } |
@@ -91,7 +91,7 @@ discard block |
||
91 | 91 | foreach ($policyRule->getPolicyRuleAttributes() as $pra) { |
92 | 92 | $attribute = $pra->getAttribute(); |
93 | 93 | $attribute->setValue($this->attributeManager->retrieveAttribute($attribute, $user, $resource)); |
94 | - if(count($pra->getExtraData()) > 0) { |
|
94 | + if (count($pra->getExtraData()) > 0) { |
|
95 | 95 | $this->processExtraData($pra, $user, $resource); |
96 | 96 | } |
97 | 97 | $this->comparisonManager->compare($pra); |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | // The given result could be an array of rejected attributes or true |
100 | 100 | // True means that the rule is correctly enforced for the given user and resource |
101 | 101 | $result = $this->comparisonManager->getResult(); |
102 | - if($cacheResult) { |
|
102 | + if ($cacheResult) { |
|
103 | 103 | $cacheItem->set($result); |
104 | 104 | $this->cacheManager->save($cacheItem); |
105 | 105 | } |
@@ -112,8 +112,8 @@ discard block |
||
112 | 112 | * @param object $resource |
113 | 113 | */ |
114 | 114 | public function processExtraData(PolicyRuleAttribute $pra, $user, $resource) { |
115 | - foreach($pra->getExtraData() as $key => $data) { |
|
116 | - switch($key) { |
|
115 | + foreach ($pra->getExtraData() as $key => $data) { |
|
116 | + switch ($key) { |
|
117 | 117 | case 'with': |
118 | 118 | // This data has to be removed for it will be stored elsewhere |
119 | 119 | // in the policy rule attribute |
@@ -123,7 +123,7 @@ discard block |
||
123 | 123 | $subPolicyRuleAttributes = []; |
124 | 124 | $extraData = []; |
125 | 125 | |
126 | - foreach($this->policyRuleManager->processRuleAttributes($data, $user, $resource) as $subPolicyRuleAttribute) { |
|
126 | + foreach ($this->policyRuleManager->processRuleAttributes($data, $user, $resource) as $subPolicyRuleAttribute) { |
|
127 | 127 | $subPolicyRuleAttributes[] = $subPolicyRuleAttribute; |
128 | 128 | } |
129 | 129 | $pra->setValue($subPolicyRuleAttributes); |
@@ -128,7 +128,7 @@ |
||
128 | 128 | public function clear() { |
129 | 129 | $items = glob("{$this->cacheFolder}/*.txt"); // get all file names |
130 | 130 | foreach($items as $item){ // iterate files |
131 | - if(is_file($item)) |
|
131 | + if(is_file($item)) |
|
132 | 132 | unlink($item); // delete file |
133 | 133 | } |
134 | 134 | $this->deferredItems = []; |
@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | $this->cacheFolder = |
28 | 28 | (isset($options['cache_folder'])) |
29 | 29 | ? "{$options['cache_folder']}/text" |
30 | - : __DIR__ . '/../../../data/cache/text' |
|
30 | + : __DIR__.'/../../../data/cache/text' |
|
31 | 31 | ; |
32 | 32 | } |
33 | 33 | |
@@ -35,10 +35,10 @@ discard block |
||
35 | 35 | * {@inheritdoc} |
36 | 36 | */ |
37 | 37 | public function deleteItem($key) { |
38 | - if(is_file("{$this->cacheFolder}/$key.txt")) { |
|
38 | + if (is_file("{$this->cacheFolder}/$key.txt")) { |
|
39 | 39 | unlink("{$this->cacheFolder}/$key.txt"); |
40 | 40 | } |
41 | - if(isset($this->deferredItems[$key])) { |
|
41 | + if (isset($this->deferredItems[$key])) { |
|
42 | 42 | unset($this->deferredItems[$key]); |
43 | 43 | } |
44 | 44 | return true; |
@@ -48,8 +48,8 @@ discard block |
||
48 | 48 | * {@inheritdoc} |
49 | 49 | */ |
50 | 50 | public function deleteItems(array $keys) { |
51 | - foreach($keys as $key) { |
|
52 | - if(!$this->deleteItem($key)) { |
|
51 | + foreach ($keys as $key) { |
|
52 | + if (!$this->deleteItem($key)) { |
|
53 | 53 | return false; |
54 | 54 | } |
55 | 55 | } |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | * {@inheritdoc} |
81 | 81 | */ |
82 | 82 | public function commit() { |
83 | - foreach($this->deferredItems as $key => $item) { |
|
83 | + foreach ($this->deferredItems as $key => $item) { |
|
84 | 84 | $this->save($item); |
85 | 85 | unset($this->deferredItems[$key]); |
86 | 86 | } |
@@ -99,10 +99,10 @@ discard block |
||
99 | 99 | */ |
100 | 100 | public function getItem($key) { |
101 | 101 | $item = new TextCacheItem($key); |
102 | - if(!$this->hasItem($key)) { |
|
102 | + if (!$this->hasItem($key)) { |
|
103 | 103 | return $item; |
104 | 104 | } |
105 | - $data = explode(';',file_get_contents("{$this->cacheFolder}/{$key}.txt")); |
|
105 | + $data = explode(';', file_get_contents("{$this->cacheFolder}/{$key}.txt")); |
|
106 | 106 | return $item |
107 | 107 | ->set($data[0]) |
108 | 108 | ->expiresAt((new \DateTime($data[1]))) |
@@ -114,8 +114,8 @@ discard block |
||
114 | 114 | */ |
115 | 115 | public function getItems(array $keys = array()) { |
116 | 116 | $items = []; |
117 | - foreach($keys as $key) { |
|
118 | - if($this->hasItem($key)) { |
|
117 | + foreach ($keys as $key) { |
|
118 | + if ($this->hasItem($key)) { |
|
119 | 119 | $items[$key] = $this->getItem($key); |
120 | 120 | } |
121 | 121 | } |
@@ -127,8 +127,8 @@ discard block |
||
127 | 127 | */ |
128 | 128 | public function clear() { |
129 | 129 | $items = glob("{$this->cacheFolder}/*.txt"); // get all file names |
130 | - foreach($items as $item){ // iterate files |
|
131 | - if(is_file($item)) |
|
130 | + foreach ($items as $item) { // iterate files |
|
131 | + if (is_file($item)) |
|
132 | 132 | unlink($item); // delete file |
133 | 133 | } |
134 | 134 | $this->deferredItems = []; |
@@ -128,8 +128,10 @@ |
||
128 | 128 | public function clear() { |
129 | 129 | $items = glob("{$this->cacheFolder}/*.txt"); // get all file names |
130 | 130 | foreach($items as $item){ // iterate files |
131 | - if(is_file($item)) |
|
132 | - unlink($item); // delete file |
|
131 | + if(is_file($item)) { |
|
132 | + unlink($item); |
|
133 | + } |
|
134 | + // delete file |
|
133 | 135 | } |
134 | 136 | $this->deferredItems = []; |
135 | 137 | return true; |