Handler   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 213
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 213
rs 10
c 0
b 0
f 0
wmc 22
lcom 0
cbo 5

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getValueGroup() 0 9 2
A getValueUser() 0 13 4
A updatePlugin() 0 15 4
B _setValueGroup() 0 26 2
B initiateDefault() 0 35 6
A initiateGroup() 0 10 2
A loadPermissions() 0 20 2
1
<?php
2
3
/**
4
 * Handler 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
 * Handler 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 Handler
30
{
31
32
    /**
33
     * ...
34
     *
35
     * @param string $plugin     ...
36
     * @param mixed  $permission ...
37
     * @param mixed  $group      ...
38
     *
39
     * @return mixed ...
40
     */
41
    public function getValueGroup($plugin, $permission, $group)
42
    {
43
        $table = new \csphere\core\datamapper\Finder("access", "group");
44
        $table->where("access_group_permission", "=", $plugin . "." . $permission);
45
        $table->where("group_id", "=", $group);
46
        $res = $table->first();
47
48
        return isset($res['access_group_value']) ? $res['access_group_value'] : 0;
49
    }
50
51
    /**
52
     * ...
53
     *
54
     * @param mixed $permission ...
55
     * @param int   $userID     ...
56
     *
57
     * @return mixed ...
58
     */
59
    public function getValueUser($permission, $userID)
60
    {
61
        if ($permission && $userID) {
62
            $table = new \csphere\core\datamapper\Finder("access", "user");
63
            $table->where("access_user_permission", "=", $permission);
64
            $table->where("group_id", "=", $userID);
65
            $res = $table->first();
66
67
            return isset($res['access_user_value']) ? $res['access_user_value'] : 0;
68
        }
69
70
        return false;
71
    }
72
73
    /**
74
     * Updates the plugin
75
     *
76
     * @param string $plugin the name of the plugin
77
     *
78
     * @return void
79
     */
80
    public function updatePlugin($plugin)
81
    {
82
        $groupFinder = new \csphere\core\datamapper\Finder("groups");
83
        $groups = $groupFinder->find(0, 0);
84
85
        foreach ($groups as $group) {
86
            $id = $group['group_id'];
87
            $name = $group['group_name'];
88
            if (!empty($_POST[$name])) {
89
                foreach ($_POST[$name] as $permission => $value) {
90
                    $this->_setValueGroup($plugin, $permission, $value, $id);
91
                }
92
            }
93
        }
94
    }
95
96
97
    /**
98
     * Sets the data of a value group. The value group must exist.
99
     *
100
     * @param string $plugin     the name of the plugin
101
     * @param string $permission ...
102
     * @param mixed  $value      ...
103
     * @param int    $groupID    ...
104
     *
105
     * @return bool true if the group exists and was successfully set
106
     */
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
107
108
    private function _setValueGroup($plugin, $permission, $value, $groupID)
109
    {
110
        $table = new \csphere\core\datamapper\Finder("access", "group");
111
        $table->columns("access_group_id");
112
        $table->where("access_group_permission", "=", $plugin . "." . $permission);
113
        $table->where("group_id", "=", $groupID);
114
        $res = $table->first();
115
116
        if (!empty($res)) {
117
118
            $data = [];
119
            $data['access_group_id'] = $res['access_group_id'];
120
            $data['group_id'] = $groupID;
121
            $data['access_group_permission'] = $plugin . "." . $permission;
122
            $data['access_group_value'] = intval($value);
123
124
            $model = new \csphere\core\datamapper\Model("access", "group");
125
            $model->update($data);
126
127
            $result = true;
128
        } else {
129
            $result = false;
130
        }
131
132
        return $result;
133
    }
134
135
    /**
136
     * Sets the value for a user.
137
     *
138
     * @param mixed $permission ...
139
     * @param int   $userID     ...
140
     *
141
     * @return true if the permission was set successfully
142
     */
143
    /*
144
    private function _setValueUser($permission, $userID)
145
    {
146
      // TODO
147
    }
148
    */
149
150
151
    /**
152
     * Inserts the default permissions for the given plugin from the access.xml.
153
     *
154
     * @param string $plugin the name of the plugin
155
     * @param array  $groups the ...
156
     *
157
     * @return void
158
     */
159
    public function initiateDefault($plugin, $groups = [])
160
    {
161
162
        if ($permissions = $this->loadPermissions($plugin)) {
163
164
            $finder = new \csphere\core\datamapper\Finder("access", "group");
165
            $model = new \csphere\core\datamapper\Model("access", "group");
166
167
168
            if (empty($groups)) {
169
                $groupFinder = new \csphere\core\datamapper\Finder("groups");
170
                $tmpGroups = $groupFinder->find(0, $groupFinder->count());
171
172
                foreach ($tmpGroups as $tmpGroup) {
173
                    $groups[] = $tmpGroup['group_id'];
174
                }
175
            }
176
177
            foreach (array_keys($permissions) as $permission) {
178
                $databasePerm = $plugin . "." . $permission;
179
                $finder->where("access_group_permission", "=", $databasePerm);
180
                $finder->remove();
181
182
                $data = [];
183
                $data['access_group_permission'] = $databasePerm;
184
                $data['access_group_value'] = 0;
185
186
                foreach ($groups as $group) {
187
                    $data['group_id'] = $group;
188
                    $model->insert($data);
189
                }
190
            }
191
        }
192
193
    }
194
195
    /**
196
     * ...
197
     *
198
     * @param int $groupID ...
199
     *
200
     * @return void
201
     */
202
    public function initiateGroup($groupID)
203
    {
204
        // Get plugin metadata
205
        $meta = new \csphere\core\plugins\Metadata();
206
207
        $plugins = $meta->details();
208
        foreach ($plugins as $plugin) {
209
            $this->initiateDefault($plugin['short'], [$groupID]);
210
        }
211
    }
212
213
    /**
214
     * Loads the permissions for the given plugin.
215
     *
216
     * @param string $plugin the name of the plugin
217
     *
218
     * @return array an array with all permissions that are defined for the given
219
     *               plugin
220
     */
221
    public function loadPermissions($plugin)
222
    {
223
224
        $loader = \csphere\core\service\Locator::get();
225
        // Check for database XML file
226
        $path = \csphere\core\init\path();
227
        $file = $path . 'csphere/plugins/' . $plugin . '/access.xml';
228
229
        if (file_exists($file)) {
230
231
            // Get access content of plugin
232
            $xml = $loader->load('xml', 'access');
233
            $permissions = $xml->source('plugin', $plugin);
234
235
        } else {
236
            $permissions = [];
237
        }
238
239
        return $permissions;
240
    }
241
}
242