Completed
Push — master ( ed8b23...8ad280 )
by Nazar
04:53
created

Permission   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 223
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 26
c 1
b 0
f 1
lcom 1
cbo 6
dl 0
loc 223
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A cdb() 0 3 1
A construct() 0 3 1
A add() 0 19 2
A set() 0 22 3
B del() 0 27 3
B get_all() 0 25 4
A del_all_cache() 0 4 1
C get() 0 57 11
1
<?php
2
/**
3
 * @package   CleverStyle CMS
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2013-2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs;
9
use
10
	cs\Cache\Prefix,
11
	cs\DB\Accessor;
12
13
/**
14
 * Class for permissions manipulating
15
 *
16
 * @method static Permission instance($check = false)
17
 */
18
class Permission {
19
	use
20
		Accessor,
21
		Singleton;
22
	/**
23
	 * Array of all permissions for quick selecting
24
	 * @var array
25
	 */
26
	protected $permissions_table = [];
27
	/**
28
	 * @var Prefix
29
	 */
30
	protected $cache;
31
	/**
32
	 * Returns database index
33
	 *
34
	 * @return int
35
	 */
36
	protected function cdb () {
37
		return Config::instance()->module('System')->db('users');
38
	}
39
	protected function construct () {
40
		$this->cache = new Prefix('permissions');
41
	}
42
	/**
43
	 * Get permission data<br>
44
	 * If <b>$group</b> or/and <b>$label</b> parameter is specified, <b>$id</b> is ignored.
45
	 *
46
	 * @param int|null    $id
47
	 * @param null|string $group
48
	 * @param null|string $label
49
	 * @param string      $condition and|or
50
	 *
51
	 * @return array|false If only <b>$id</b> specified - result is array of permission data, in other cases result will be array of arrays of corresponding
1 ignored issue
show
Documentation introduced by
Should the return type not be integer|string|array|false? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
52
	 *                     permissions data
53
	 */
54
	function get ($id = null, $group = null, $label = null, $condition = 'and') {
55
		$condition = $condition == 'or' ? 'OR' : 'AND';
56
		if ($group !== null && $label !== null) {
57
			return $this->db()->qfa(
58
				[
59
					"SELECT
60
						`id`,
61
						`label`,
62
						`group`
63
					FROM `[prefix]permissions`
64
					WHERE
65
						`group` = '%s' $condition
66
						`label` = '%s'",
67
					$group,
68
					$label
69
				]
70
			) ?: [];
71
		} /** @noinspection NotOptimalIfConditionsInspection */ elseif ($group !== null) {
72
			return $this->db()->qfa(
73
				[
74
					"SELECT
75
						`id`,
76
						`label`,
77
						`group`
78
					FROM `[prefix]permissions`
79
					WHERE `group` = '%s'",
80
					$group
81
				]
82
			) ?: [];
83
		} /** @noinspection NotOptimalIfConditionsInspection */ elseif ($label !== null) {
84
			return $this->db()->qfa(
85
				[
86
					"SELECT
87
						`id`,
88
						`label`,
89
						`group`
90
					FROM `[prefix]permissions`
91
					WHERE `label` = '%s'",
92
					$label
93
				]
94
			) ?: [];
95
		} else {
96
			$id = (int)$id;
97
			if (!$id) {
98
				return false;
99
			}
100
			return $this->db()->qf(
101
				"SELECT
102
					`id`,
103
					`label`,
104
					`group`
105
				FROM `[prefix]permissions`
106
				WHERE `id` = '$id'
107
				LIMIT 1"
108
			) ?: false;
109
		}
110
	}
111
	/**
112
	 * Add permission
113
	 *
114
	 * @param string $group
115
	 * @param string $label
116
	 *
117
	 * @return false|int Group id or <b>false</b> on failure
118
	 */
119
	function add ($group, $label) {
120
		if ($this->db_prime()->q(
121
			"INSERT INTO `[prefix]permissions`
122
				(
123
					`label`,
124
					`group`
125
				) VALUES (
126
					'%s',
127
					'%s'
128
				)",
129
			xap($label),
130
			xap($group)
131
		)
132
		) {
133
			$this->del_all_cache();
134
			return $this->db_prime()->id();
135
		}
136
		return false;
137
	}
138
	/**
139
	 * Set permission
140
	 *
141
	 * @param int    $id
142
	 * @param string $group
143
	 * @param string $label
144
	 *
145
	 * @return bool
146
	 */
147
	function set ($id, $group, $label) {
148
		$id = (int)$id;
149
		if (!$id) {
150
			return false;
151
		}
152
		if ($this->db_prime()->q(
153
			"UPDATE `[prefix]permissions`
154
			SET
155
				`label` = '%s',
156
				`group` = '%s'
157
			WHERE `id` = '$id'
158
			LIMIT 1",
159
			xap($label),
160
			xap($group)
161
		)
162
		) {
163
			$this->del_all_cache();
164
			return true;
165
		} else {
166
			return false;
167
		}
168
	}
169
	/**
170
	 * Deletion of permission or array of permissions
171
	 *
172
	 * @param int|int[] $id
173
	 *
174
	 * @return bool
175
	 */
176
	function del ($id) {
177
		if (!$id) {
178
			return false;
179
		}
180
		$id = implode(',', (array)_int($id));
181
		if ($this->db_prime()->q(
182
			[
183
				"DELETE FROM `[prefix]permissions`
184
				WHERE `id` IN ($id)",
185
				"DELETE FROM `[prefix]users_permissions`
186
				WHERE `permission` IN ($id)",
187
				"DELETE FROM `[prefix]groups_permissions`
188
				WHERE `permission` IN ($id)"
189
			]
190
		)
191
		) {
192
			$Cache = $this->cache;
193
			unset(
194
				$Cache->users,
195
				$Cache->groups
196
			);
197
			$this->del_all_cache();
198
			return true;
199
		} else {
200
			return false;
201
		}
202
	}
203
	/**
204
	 * Returns array of all permissions grouped by permissions groups
205
	 *
206
	 * @return array    Format of array: ['group']['label'] = <i>permission_id</i>
207
	 */
208
	function get_all () {
209
		if (empty($this->permissions_table)) {
210
			$this->permissions_table = $this->cache->get(
1 ignored issue
show
Documentation Bug introduced by
It seems like $this->cache->get('all',...rn $all_permissions; }) of type * is incompatible with the declared type array of property $permissions_table.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
211
				'all',
212
				function () {
213
					$data = $this->db()->qfa(
214
						'SELECT
215
							`id`,
216
							`label`,
217
							`group`
218
						FROM `[prefix]permissions`'
219
					);
220
					if (!is_array($data)) {
221
						return [];
222
					}
223
					$all_permissions = [];
224
					foreach ($data as $item) {
225
						$all_permissions[$item['group']][$item['label']] = (int)$item['id'];
226
					}
227
					return $all_permissions;
228
				}
229
			);
230
		}
231
		return $this->permissions_table;
232
	}
233
	/**
234
	 * Deletion of permission table (is used after adding, setting or deletion of permission)
235
	 */
236
	protected function del_all_cache () {
237
		$this->permissions_table = [];
238
		unset($this->cache->all);
239
	}
240
}
241