PrivilegeAdvanced::checkPermissions()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 23
ccs 0
cts 16
cp 0
rs 9.1111
c 0
b 0
f 0
cc 6
nc 6
nop 3
crap 42
1
<?php
2
3
namespace App;
4
5
\Vtiger_Loader::includeOnce('~/modules/com_vtiger_workflow/VTJsonCondition.php');
6
7
/**
8
 * Advanced privilege class.
9
 *
10
 * @package App
11
 *
12
 * @copyright YetiForce S.A.
13
 * @license YetiForce Public License 6.5 (licenses/LicenseEN.txt or yetiforce.com)
14
 * @author Mariusz Krzaczkowski <[email protected]>
15
 */
16
class PrivilegeAdvanced
17
{
18
	protected static $cacheFile = 'user_privileges/advancedPermission.php';
19
	protected static $cache = false;
20
	public static $webservice = true;
21
22
	/**
23 12
	 * Update advanced permissions cache.
24
	 */
25 12
	public static function reloadCache()
26 12
	{
27 12
		$db = Db::getInstance('admin');
28 12
		$query = (new Db\Query())->from('a_#__adv_permission')->where(['status' => 0])->orderBy(['priority' => SORT_DESC]);
29 12
		$dataReader = $query->createCommand($db)->query();
30 1
		$cache = [];
31 1
		while ($row = $dataReader->read()) {
32 1
			$members = \App\Json::decode($row['members']);
33 1
			$users = [];
34 1
			if (!empty($members)) {
35
				foreach ($members as &$member) {
36 1
					$users = array_merge($users, PrivilegeUtil::getUserByMember($member));
37
				}
38 1
				$users = array_unique($users);
39 1
			}
40 1
			$cache[$row['tabid']][$row['id']] = [
41 1
				'action' => $row['action'],
42
				'conditions' => $row['conditions'],
43
				'members' => array_flip($users),
44 12
			];
45 12
		}
46 12
		$content = '<?php return ' . Utils::varExport($cache) . ';' . PHP_EOL;
47 12
		file_put_contents(static::$cacheFile, $content, LOCK_EX);
48
		\App\Cache::resetFileCache(static::$cacheFile);
49
	}
50
51
	/**
52
	 * Load advanced permission rules for specific module.
53
	 *
54
	 * @param string $moduleName
55
	 *
56
	 * @return array
57
	 */
58
	public static function get($moduleName)
59
	{
60
		if (false === static::$cache) {
61
			static::$cache = require static::$cacheFile;
62
		}
63
		$tabid = Module::getModuleId($moduleName);
64
65
		return static::$cache[$tabid] ?? false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::cache[$tabid] ?? false could also return false which is incompatible with the documented return type array. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
66
	}
67
68
	/**
69
	 * Check advanced permissions.
70
	 *
71
	 * @param int    $record
72
	 * @param string $moduleName
73
	 * @param mixed  $userId
74
	 *
75
	 * @return bool|int
76
	 */
77
	public static function checkPermissions($record, $moduleName, $userId)
78
	{
79
		$privileges = static::get($moduleName);
80
		if (false === $privileges) {
0 ignored issues
show
introduced by
The condition false === $privileges is always false.
Loading history...
81
			return false;
82
		}
83
		Log::trace("Check advanced permissions: $record,$moduleName,$userId");
84
		foreach ($privileges as $id => &$privilege) {
85
			if (!isset($privilege['members'][$userId])) {
86
				continue;
87
			}
88
			static::$webservice = false;
89
			$recordModel = \Vtiger_Record_Model::getInstanceById($record, $moduleName);
90
			$test = (new \VTJsonCondition())->evaluate($privilege['conditions'], $recordModel);
91
			static::$webservice = true;
92
			if ($test) {
93
				Log::trace("Check advanced permissions test OK,action: {$privilege['action']},id: $id");
94
95
				return 0 === $privilege['action'] ? 1 : 0;
96
			}
97
			Log::trace("Check advanced permissions test FALSE , id: $id");
98
		}
99
		return false;
100
	}
101
}
102