PrivilegeFile::createUserPrivilegesFile()   B
last analyzed

Complexity

Conditions 8
Paths 48

Size

Total Lines 41
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 8

Importance

Changes 0
Metric Value
eloc 32
c 0
b 0
f 0
dl 0
loc 41
rs 8.1635
ccs 28
cts 28
cp 1
cc 8
nc 48
nop 1
crap 8
1
<?php
2
/**
3
 * Privilege File basic class.
4
 *
5
 * @package App
6
 *
7
 * @license YetiForce Public License 6.5 (licenses/LicenseEN.txt or yetiforce.com)
8
 * @author  Mariusz Krzaczkowski <[email protected]>
9
 * @author  Radosław Skrzypczak <[email protected]>
10
 */
11
12
namespace App;
13
14
/**
15
 * PrivilegeFile class.
16
 */
17
class PrivilegeFile
18
{
19
	protected static $usersFile = 'user_privileges/users.php';
20
	protected static $usersFileCache = false;
21
22
	/**
23
	 * Create users privileges file.
24
	 */
25
	public static function createUsersFile()
26
	{
27
		$entityData = Module::getEntityInfo('Users');
28
		$dataReader = (new \App\Db\Query())->select(['id', 'first_name', 'last_name', 'is_admin', 'cal_color', 'status', 'email1', 'user_name', 'deleted'])->from('vtiger_users')->createCommand()->query();
29
		$users = [];
30
		// Get the id and the name.
31
		while ($row = $dataReader->read()) {
32
			$fullName = '';
33
			foreach ($entityData['fieldnameArr'] as $field) {
34
				$fullName .= ' ' . $row[$field];
35
			}
36
			$row['fullName'] = trim($fullName);
37
			$users['id'][$row['id']] = array_map('\App\Purifier::encodeHtml', $row);
38
			$users['userName'][$row['user_name']] = $row['id'];
39
		}
40
		Utils::saveToFile(static::$usersFile, $users, '', 0, true);
41
	}
42
43
	/**
44
	 * get general users privileges file.
45
	 *
46
	 * @param string $type
47
	 *
48
	 * @return array
49
	 */
50
	public static function getUser($type)
51
	{
52
		if (false === static::$usersFileCache) {
53
			static::$usersFileCache = require static::$usersFile;
54
		}
55
		return static::$usersFileCache[$type] ?? false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::usersFileCache[$type] ?? 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...
56
	}
57
58
	/**
59
	 * Creates a file with all the user, user-role,user-profile, user-groups informations.
60
	 *
61
	 * @param int $userId
62
	 */
63 5778
	public static function createUserPrivilegesFile($userId)
64
	{
65 5778
		$file = ROOT_DIRECTORY . \DIRECTORY_SEPARATOR . 'user_privileges' . \DIRECTORY_SEPARATOR . "user_privileges_$userId.php";
66 5778
		$user = [];
67 5778
		$userInstance = \CRMEntity::getInstance('Users');
68 5778
		$userInstance->retrieveEntityInfo($userId, 'Users');
69 5778
		$userInstance->column_fields['is_admin'] = 'on' === $userInstance->is_admin;
0 ignored issues
show
Bug Best Practice introduced by
The property column_fields does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
70
71 5778
		$exclusionEncodeHtml = ['currency_symbol', 'date_format', 'currency_id', 'currency_decimal_separator', 'currency_grouping_separator', 'othereventduration', 'imagename'];
72 5778
		foreach ($userInstance->column_fields as $field => $value) {
73 5778
			if (!\in_array($field, $exclusionEncodeHtml)) {
74 5778
				$userInstance->column_fields[$field] = is_numeric($value) ? $value : \App\Purifier::encodeHtml($value);
75
			}
76
		}
77
78 5778
		$displayName = '';
79 5778
		foreach (Module::getEntityInfo('Users')['fieldnameArr'] as $field) {
80 5778
			$displayName .= ' ' . $userInstance->column_fields[$field];
81
		}
82 5778
		$userRoleInfo = PrivilegeUtil::getRoleDetail($userInstance->column_fields['roleid']);
83 5778
		$user['details'] = $userInstance->column_fields;
84 5778
		$user['displayName'] = trim($displayName);
85 5778
		$user['profiles'] = PrivilegeUtil::getProfilesByRole($userInstance->column_fields['roleid']);
86 5778
		$user['groups'] = PrivilegeUtil::getAllGroupsByUser($userId);
87 5778
		$user['leadersByGroup'] = PrivilegeUtil::getLeadersGroupByUserId($userId);
88 5778
		$user['leader'] = PrivilegeUtil::getGroupsWhereUserIsLeader($userId);
89 5778
		$user['parent_roles'] = $userRoleInfo['parentRoles'];
90
		$user['parent_role_seq'] = $userRoleInfo['parentrole'];
91 5778
		$user['roleName'] = $userRoleInfo['rolename'];
92 5778
93 5778
		$logo = null;
94 5778
		if (Record::isExists($userRoleInfo['company'], 'MultiCompany')) {
95 5778
			$multiCompany = \Vtiger_Record_Model::getInstanceById($userRoleInfo['company'], 'MultiCompany');
96 5778
			$logo = Json::isEmpty($multiCompany->get('logo')) ? [] : current(Json::decode($multiCompany->get('logo')));
97 5778
			$user['multiCompanyId'] = $multiCompany->getId();
98
		} else {
99
			$user['multiCompanyId'] = null;
100
		}
101
		$user['multiCompanyLogo'] = $logo;
102
		$user['multiCompanyLogoUrl'] = $logo ? "file.php?module=MultiCompany&action=Logo&record={$userId}&key={$logo['key']}" : '';
103
		file_put_contents($file, 'return ' . Utils::varExport($user) . ';' . PHP_EOL, FILE_APPEND);
104
	}
105
}
106