1 | <?php |
||
21 | class CapabilitiesChecker |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * @type \EE_Capabilities $capabilities |
||
26 | */ |
||
27 | private $capabilities; |
||
28 | |||
29 | |||
30 | |||
31 | /** |
||
32 | * CapabilitiesChecker constructor |
||
33 | * |
||
34 | * @param \EE_Capabilities $capabilities |
||
35 | */ |
||
36 | public function __construct(\EE_Capabilities $capabilities) |
||
40 | |||
41 | |||
42 | |||
43 | /** |
||
44 | * @return \EE_Capabilities |
||
45 | */ |
||
46 | protected function capabilities() |
||
50 | |||
51 | |||
52 | |||
53 | /** |
||
54 | * Verifies that the current user has ALL of the capabilities listed in the CapCheck DTO. |
||
55 | * If any of the individual capability checks fails, then the command will NOT be executed. |
||
56 | * |
||
57 | * @param CapCheckInterface|CapCheckInterface[] $cap_check |
||
58 | * @return bool |
||
59 | * @throws InvalidClassException |
||
60 | * @throws InsufficientPermissionsException |
||
61 | */ |
||
62 | public function processCapCheck($cap_check) |
||
63 | { |
||
64 | if (is_array($cap_check)){ |
||
65 | foreach ($cap_check as $check) { |
||
66 | $this->processCapCheck($check); |
||
67 | } |
||
68 | return true; |
||
69 | } |
||
70 | // at this point, $cap_check should be an individual instance of CapCheck |
||
71 | if ( ! $cap_check instanceof CapCheckInterface) { |
||
72 | throw new InvalidClassException( |
||
73 | '\EventEspresso\core\domain\services\capabilities\CapCheckInterface' |
||
74 | ); |
||
75 | } |
||
76 | // sometimes cap checks are conditional, and no capabilities are required |
||
77 | if ($cap_check instanceof PublicCapabilities) { |
||
78 | return true; |
||
79 | } |
||
80 | $capabilities = (array) $cap_check->capability(); |
||
81 | foreach ($capabilities as $capability) { |
||
82 | if ( |
||
83 | ! $this->capabilities()->current_user_can( |
||
84 | $capability, |
||
85 | $cap_check->context(), |
||
86 | $cap_check->ID() |
||
87 | ) |
||
88 | ) { |
||
89 | throw new InsufficientPermissionsException($cap_check->context()); |
||
90 | } |
||
91 | } |
||
92 | return true; |
||
93 | } |
||
94 | |||
95 | |||
96 | |||
97 | /** |
||
98 | * @param string $capability - the capability to be checked, like: 'ee_edit_registrations' |
||
99 | * @param string $context - what the user is attempting to do, like: 'Edit Registration' |
||
100 | * @param int $ID - (optional) ID for item where current_user_can is being called from |
||
101 | * @return bool |
||
102 | */ |
||
103 | public function process($capability, $context, $ID = 0) |
||
107 | |||
108 | |||
109 | |||
110 | } |
||
111 | // End of file CapabilitiesChecker.php |
||
112 | // Location: /CapabilitiesChecker.php |