Mercurial > hg > isophonics-drupal-site
comparison core/lib/Drupal/Core/Condition/ConditionAccessResolverTrait.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Core\Condition; | |
4 | |
5 use Drupal\Component\Plugin\Exception\ContextException; | |
6 | |
7 /** | |
8 * Resolves a set of conditions. | |
9 */ | |
10 trait ConditionAccessResolverTrait { | |
11 | |
12 /** | |
13 * Resolves the given conditions based on the condition logic ('and'/'or'). | |
14 * | |
15 * @param \Drupal\Core\Condition\ConditionInterface[] $conditions | |
16 * A set of conditions. | |
17 * @param string $condition_logic | |
18 * The logic used to compute access, either 'and' or 'or'. | |
19 * | |
20 * @return bool | |
21 * Whether these conditions grant or deny access. | |
22 */ | |
23 protected function resolveConditions($conditions, $condition_logic) { | |
24 foreach ($conditions as $condition) { | |
25 try { | |
26 $pass = $condition->execute(); | |
27 } | |
28 catch (ContextException $e) { | |
29 // If a condition is missing context and is not negated, consider that a | |
30 // fail. | |
31 $pass = $condition->isNegated(); | |
32 } | |
33 | |
34 // If a condition fails and all conditions were needed, deny access. | |
35 if (!$pass && $condition_logic == 'and') { | |
36 return FALSE; | |
37 } | |
38 // If a condition passes and only one condition was needed, grant access. | |
39 elseif ($pass && $condition_logic == 'or') { | |
40 return TRUE; | |
41 } | |
42 } | |
43 | |
44 // Return TRUE if logic was 'and', meaning all rules passed. | |
45 // Return FALSE if logic was 'or', meaning no rule passed. | |
46 return $condition_logic == 'and'; | |
47 } | |
48 | |
49 } |