Test Failed
Pull Request — master (#236)
by
unknown
04:56
created

Permissions::deletePermissionsByOwner()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 16
rs 10
1
<?php
2
3
/**
4
 * Copyright (c) 2018 Justin Kuenzel (jukusoft.com)
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
20
/**
21
 * Project: RocketCMS
22
 * License: Apache 2.0 license
23
 * User: Justin
24
 * Date: 23.03.2018
25
 * Time: 18:38
26
 */
27
28
class Permissions {
29
30
	public static function createOrUpdateCategory (string $category, string $title, int $order = 100, string $area = "global") {
0 ignored issues
show
Unused Code introduced by
The parameter $title is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

30
	public static function createOrUpdateCategory (string $category, /** @scrutinizer ignore-unused */ string $title, int $order = 100, string $area = "global") {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
31
		//validate values
32
		$category = Validator_AlphaNumeric::get($category);
33
		$title = Validator_AlphaNumeric::get($category);
34
		$area = Validator_AlphaNumeric::get($area);
35
		$order = intval($order);
36
37
		Database::getInstance()->execute("INSERT INTO `{praefix}permission_category` (
38
			`category`, `title`, `area`, `show`, `order`, `activated`
39
		) VALUES (
40
			:category, :title, :area, '1', :order, '1'
41
		) ON DUPLICATE KEY UPDATE `title` = :title, `area` = :area, `order` = :order, `activated` = '1'; ", array(
42
			'category' => $category,
43
			'title' => $title,
44
			'area' => $area,
45
			'order' => $order
46
		));
47
48
		//clear cache
49
		Cache::clear("permissions", "categories");
50
	}
51
52
	public static function deleteCategory (string $category) {
53
		//validate value
54
		$category = Validator_AlphaNumeric::get($category);
55
56
		//delete from database
57
		Database::getInstance()->execute("DELETE FROM `{praefix}permission_category` WHERE `category` = :category; ", array('category' => $category));
58
59
		//clear cache
60
		Cache::clear("permissions", "categories");
61
	}
62
63
	public static function createPermission (string $token, string $title, string $description, string $category = "general", string $owner = "system", int $order = 100) {
64
		//validate values
65
		$token = Validator_Token::get($token);
66
		$title = Validator_String::get($title);
67
		$description = Validator_String::get($description);
68
		$category = Validator_Filename::get($category);
69
		$owner = Validator_AlphaNumeric::get($owner);
70
		$order = intval($order);
71
72
		Database::getInstance()->execute("INSERT INTO `{praefix}permissions` (
73
			`token`, `title`, `description`, `category`, `owner`, `show`, `order`, `activated`
74
		) VALUES (
75
			:token, :title, :description, :category, :owner, '1', :order, '1'
76
		) ON DUPLICATE KEY UPDATE `title` = :title, `description` = :description, `category` = :category, `owner` = :owner, `order` = :order, `activated` = '1'; ", array(
77
			'token' => $token,
78
			'title' => $title,
79
			'description' => $description,
80
			'category' => $category,
81
			'owner' => $owner,
82
			'order' => $order
83
		));
84
85
		//clear cache
86
		Cache::clear("permissions", "permission_list");
87
	}
88
89
	public static function deletePermission (string $token) {
90
		//validate value
91
		$token = Validator_Token::get($token);
92
93
		//delete from database
94
		Database::getInstance()->execute("DELETE FROM `{praefix}permissions` WHERE `token` = :token; ", array('token' => $token));
95
96
		//cleanup group and user rights table
97
		self::deletePermissionsInGroupAndUserTable($token);
98
99
		//clear cache
100
		Cache::clear("permissions", "permission_list");
101
	}
102
103
	public static function deletePermissionsByOwner (string $owner) {
104
		//cleanup group and user permissions with this specific tokens
105
		Database::getInstance()->execute("DELETE `{praefix}group_rights` FROM `{praefix}group_rights` INNER JOIN `{praefix}permissions` ON `{praefix}permissions`.`token` = `{praefix}group_rights`.`token` WHERE `{praefix}permissions`.`owner` = :owner; ", array(
106
			'owner' => $owner
107
		));
108
109
		//cleanup group and user permissions with this specific tokens
110
		Database::getInstance()->execute("DELETE `{praefix}user_rights` FROM `{praefix}user_rights` INNER JOIN `{praefix}permissions` ON `{praefix}permissions`.`token` = `{praefix}user_rights`.`token` WHERE `{praefix}permissions`.`owner` = :owner; ", array(
111
			'owner' => $owner
112
		));
113
114
		//delete from database
115
		Database::getInstance()->execute("DELETE FROM `{praefix}permissions` WHERE `owner` = :owner; ", array('owner' => $owner));
116
117
		//clear cache
118
		Cache::clear("permissions", "permission_list");
119
	}
120
121
	protected static function deletePermissionsInGroupAndUserTable (string $token) {
122
		//delete permission in groups table
123
		Database::getInstance()->execute("DELETE FROM `{praefix}group_rights` WHERE `token` = :token; ", array('token' => $token));
124
125
		//delete permission in user table
126
		Database::getInstance()->execute("DELETE FROM `{praefix}user_rights` WHERE `token` = :token; ", array('token' => $token));
127
	}
128
129
	public static function listPermissions (string $category = "") : array {
130
		$suffix = "";
131
132
		if ($category != "") {
133
			$suffix = "_" . Validator_AlphaNumeric::get($category);
134
		}
135
136
		if (Cache::contains("permissions", "permission_list" . $suffix)) {
137
			return Cache::get("permissions", "permission_list" . $suffix);
138
		} else {
139
			if ($category == "") {
140
				$rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}permissions` WHERE `activated` = '1' ORDER BY `order`; ");
141
			} else {
142
				$rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}permissions` WHERE `category` = :category, AND `activated` = '1' ORDER BY `order`; ", array('category' => $category));
143
			}
144
145
			Cache::put("permissions", "permission_list" . $suffix, $rows);
146
147
			return $rows;
148
		}
149
	}
150
151
}
152
153
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
154