Completed
Branch BUG-9871-email-validation (e62b1a)
by
unknown
350:41 queued 333:27
created

CapabilitiesChecker::process()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 4
rs 10
1
<?php
2
namespace EventEspresso\core\domain\services\capabilities;
3
4
use EventEspresso\core\exceptions\InsufficientPermissionsException;
5
use EventEspresso\core\exceptions\InvalidClassException;
6
7
if ( ! defined('EVENT_ESPRESSO_VERSION')) {
8
    exit('No direct script access allowed');
9
}
10
11
12
13
/**
14
 * Class CapabilitiesChecker
15
 * Description
16
 *
17
 * @package       Event Espresso
18
 * @author        Brent Christensen
19
 * @since         4.9.0
20
 */
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)
37
    {
38
        $this->capabilities = $capabilities;
39
    }
40
41
42
43
    /**
44
     * @return \EE_Capabilities
45
     */
46
    protected function capabilities()
47
    {
48
        return $this->capabilities;
49
    }
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)
104
    {
105
        return $this->processCapCheck(new CapCheck($capability, $context, $ID));
106
    }
107
108
109
110
}
111
// End of file CapabilitiesChecker.php
112
// Location: /CapabilitiesChecker.php