Check::_checkGroups()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 2
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Check Class for Access Module
5
 *
6
 * PHP Version 5
7
 *
8
 * @category  Plugins
9
 * @package   Access
10
 * @author    Daniel Schalla <[email protected]>
11
 * @copyright 2013 cSphere Team
12
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
13
 * @link      http://www.csphere.eu
14
 **/
15
16
namespace csphere\plugins\access\classes;
17
18
/**
19
 * Check Class for Access Module
20
 *
21
 * @category  Plugins
22
 * @package   Access
23
 * @author    Daniel Schalla <[email protected]>
24
 * @copyright 2013 cSphere Team
25
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
26
 * @link      http://www.csphere.eu
27
 **/
28
29
class Check
30
{
31
    private $_handler;
32
    private $_plugin;
33
    private $_userID;
34
35
    /**
36
     * Creates an new Check object for the given plugin and user.
37
     *
38
     * @param string $plugin the name of the plugin
39
     * @param int    $userID the id of the user (optional)
40
     */
41
    public function __construct($plugin, $userID = 0)
42
    {
43
        $this->_handler = new Handler();
44
        $this->setPlugin($plugin);
45
        $this->setUser($userID);
46
    }
47
48
    /**
49
     * sets the name of the plugin
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
50
     *
51
     * @param string $plugin the name of the plugin
52
     *
53
     * @return void
54
     */
55
    public function setPlugin($plugin)
56
    {
57
        $this->_plugin = $plugin;
58
    }
59
60
    /**
61
     * sets the user id
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
62
     *
63
     * @param int $userID the id of the user
64
     *
65
     * @return void
66
     */
67
    public function setUser($userID)
68
    {
69
        if (empty($userID)) {
70
            // $session = new \csphere\core\session\Session();
71
            // TODO: still correct or debug rest?
72
            $userID = 1; //$session->get("user_id");
73
74
        }
75
        $this->_userID = $userID;
76
    }
77
78
    /**
79
     * Checks whether the current user has the given permission.
80
     *
81
     * @param string $permission the permission that should be checked
82
     *
83
     * @return bool true if the user has the access.
84
     */
85
    public function checkAccess($permission)
86
    {
87
        $access = false;
88
89
        $groups = $this->_checkGroups($this->_userID, $permission);
90
        $user = $this->_checkUser($this->_userID, $permission);
91
92
        foreach ($groups as $group) {
93
            if ($group) {
94
                $access = true;
95
                break;
96
            }
97
        }
98
99
        if (!$access && $user) {
100
            $access = true;
101
        }
102
103
        return $access;
104
    }
105
106
    /**
107
     * checks whether an user has an specific permission in one of his groups.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
108
     *
109
     * @param int    $userID     the id of the user
110
     * @param string $permission the permission that should be checked
111
     *
112
     * @return array an array with the name of the group as index and the value for
113
     * the given permission
114
     */
115
    private function _checkGroups($userID, $permission)
116
    {
117
118
        $groups = \csphere\plugins\members\classes\Data::getUserGroups($userID);
119
120
        $list = [];
121
122
        foreach ($groups as $group) {
123
            $id = $group['group_id'];
124
            $name = $group['group_name'];
125
            $list[$name] = $this->_handler->getValueGroup(
126
                $this->_plugin, $permission, $id
127
            );
128
        }
129
130
        return $list;
131
    }
132
133
    /**
134
     * checks whether an user has an specific permission himself
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
135
     *
136
     * @param int    $userID     the id of the user
137
     * @param string $permission the permission to check
138
     *
139
     * @return bool true if the user has the permission
140
     */
141
    private function _checkUser($userID, $permission)
142
    {
143
        return $this->_handler->getValueUser($permission, $userID);
144
    }
145
}
146