1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Privilege File basic class. |
7
|
|
|
* |
8
|
|
|
* @package App |
9
|
|
|
* |
10
|
|
|
* @copyright YetiForce S.A. |
11
|
|
|
* @license YetiForce Public License 6.5 (licenses/LicenseEN.txt or yetiforce.com) |
12
|
|
|
* @author Mariusz Krzaczkowski <[email protected]> |
13
|
|
|
* @author Radosław Skrzypczak <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class PrivilegeQuery |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Interpreter for privilege. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private static $interpreter; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Sets interpreter. |
26
|
|
|
* |
27
|
|
|
* @param string $className |
28
|
|
|
* |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
public static function setPermissionInterpreter(string $className) |
32
|
|
|
{ |
33
|
|
|
static::$interpreter = $className; |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Undocumented function. |
38
|
|
|
* |
39
|
|
|
* @param \App\Db\Query $query |
40
|
|
|
* @param string $moduleName |
41
|
|
|
* @param int $user |
42
|
|
|
* @param int $relatedRecord |
43
|
3 |
|
* |
44
|
|
|
* @return void |
45
|
3 |
|
*/ |
46
|
|
|
public static function getConditions(Db\Query $query, string $moduleName, $user = false, $relatedRecord = null) |
47
|
|
|
{ |
48
|
3 |
|
if (!empty(static::$interpreter) && class_exists(static::$interpreter)) { |
|
|
|
|
49
|
3 |
|
return (static::$interpreter)::getConditions($query, $moduleName, $user, $relatedRecord); |
50
|
|
|
} |
51
|
|
|
static::getPrivilegeQuery($query, $moduleName, $user, $relatedRecord); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Undocumented function. |
56
|
|
|
* |
57
|
|
|
* @param \App\Db\Query $query |
58
|
|
|
* @param string $moduleName |
59
|
|
|
* @param int|\App\User $user |
60
|
|
|
* @param int $relatedRecord |
61
|
3 |
|
* |
62
|
|
|
* @return void |
63
|
3 |
|
*/ |
64
|
3 |
|
public static function getPrivilegeQuery(Db\Query $query, $moduleName, $user = false, $relatedRecord = null) |
65
|
|
|
{ |
66
|
|
|
if ($user && $user instanceof User) { |
67
|
|
|
$userId = $user->getId(); |
68
|
3 |
|
} elseif (!$user) { |
69
|
3 |
|
$userId = \App\User::getCurrentUserId(); |
70
|
3 |
|
} |
71
|
3 |
|
$userModel = \Users_Privileges_Model::getInstanceById($userId); |
|
|
|
|
72
|
|
|
if (!$userModel->isAdminUser() && \App\Config::security('PERMITTED_BY_PRIVATE_FIELD') && ($fieldInfo = \App\Field::getFieldInfo('private', $moduleName)) && \in_array($fieldInfo['presence'], [0, 2])) { |
73
|
|
|
$owners = array_merge([$userId], $userModel->groups); |
|
|
|
|
74
|
|
|
$conditions = ['or']; |
75
|
|
|
$conditions[] = ['vtiger_crmentity.private' => 0]; |
76
|
|
|
$subConditions = ['or', ['vtiger_crmentity.smownerid' => $owners]]; |
77
|
3 |
|
if (\App\Config::security('PERMITTED_BY_SHARED_OWNERS')) { |
78
|
|
|
$subQuery = (new \App\Db\Query())->select(['crmid'])->distinct() |
79
|
|
|
->from('u_yf_crmentity_showners') |
80
|
|
|
->where(['userid' => $owners]); |
81
|
|
|
$subConditions[] = ['vtiger_crmentity.crmid' => $subQuery]; |
82
|
|
|
} |
83
|
|
|
$conditions[] = ['and', ['vtiger_crmentity.private' => 1], $subConditions]; |
84
|
|
|
$query->andWhere($conditions); |
85
|
3 |
|
} |
86
|
3 |
|
if (\App\Config::security('PERMITTED_BY_RECORD_HIERARCHY') && !empty($relatedRecord)) { |
87
|
|
|
$role = $userModel->getRoleDetail(); |
88
|
|
|
if (2 == $role->get('listrelatedrecord')) { |
89
|
|
|
$parentRecord = \Users_Privileges_Model::getParentRecord($relatedRecord, false, $role->get('listrelatedrecord')); |
90
|
|
|
if ($parentRecord) { |
91
|
|
|
$relatedRecord = $parentRecord; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
if (0 != $role->get('listrelatedrecord')) { |
|
|
|
|
95
|
|
|
$recordMetaData = \vtlib\Functions::getCRMRecordMetadata($relatedRecord); |
96
|
|
|
$recordPermission = Privilege::isPermitted($recordMetaData['setype'], 'DetailView', $relatedRecord, $userId); |
97
|
|
|
if ($recordPermission) { |
98
|
|
|
return ''; |
|
|
|
|
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
$tabId = Module::getModuleId($moduleName); |
103
|
|
|
if (!$userModel->isAdminUser() && 1 == $userModel->profile_global_permission[1] && 1 == $userModel->profile_global_permission[2] && 3 === ($userModel->defaultOrgSharingPermission[$tabId] ?? null)) { |
|
|
|
|
104
|
|
|
$conditions = ['or']; |
105
|
|
|
$conditions[] = ['vtiger_crmentity.smownerid' => $userId]; |
106
|
|
|
if (!empty($userModel->groups)) { |
107
|
|
|
$conditions[] = ['vtiger_crmentity.smownerid' => $userModel->groups]; |
108
|
|
|
} |
109
|
|
|
if (($modules = \App\Config::security('permittedModulesByCreatorField')) && \in_array($moduleName, $modules)) { |
110
|
|
|
$conditions[] = ['vtiger_crmentity.smcreatorid' => $userId]; |
111
|
|
|
} |
112
|
|
|
if (\App\Config::security('PERMITTED_BY_ROLES')) { |
113
|
|
|
$parentRoleSeq = $userModel->parent_role_seq; |
|
|
|
|
114
|
|
|
$subQuery = (new \App\Db\Query())->select(['userid']) |
115
|
|
|
->from('vtiger_user2role') |
116
|
|
|
->innerJoin('vtiger_users', 'vtiger_user2role.userid = vtiger_users.id') |
117
|
|
|
->innerJoin('vtiger_role', 'vtiger_user2role.roleid = vtiger_role.roleid') |
118
|
|
|
->where(['like', 'vtiger_role.parentrole', "$parentRoleSeq::%", false]); |
119
|
|
|
$conditions[] = ['vtiger_crmentity.smownerid' => $subQuery]; |
120
|
|
|
} |
121
|
|
|
if (\App\Config::security('PERMITTED_BY_SHARING')) { |
122
|
|
|
$sharingPrivileges = \App\User::getSharingFile($userId); |
123
|
|
|
if (isset($sharingPrivileges['permission'][$moduleName])) { |
124
|
|
|
$sharingPrivilegesModule = $sharingPrivileges['permission'][$moduleName]; |
125
|
|
|
$sharingRuleInfo = $sharingPrivilegesModule['read']; |
126
|
|
|
if (!empty($sharingRuleInfo['ROLE'])) { |
127
|
|
|
$subQuery = (new \App\Db\Query())->select(['shareduserid']) |
128
|
|
|
->from('vtiger_tmp_read_user_sharing_per') |
129
|
|
|
->where(['userid' => $userId, 'tabid' => $tabId]); |
130
|
|
|
$conditions[] = ['vtiger_crmentity.smownerid' => $subQuery]; |
131
|
|
|
} |
132
|
|
|
if (!empty($sharingRuleInfo['GROUP'])) { |
133
|
3 |
|
$subQuery = (new \App\Db\Query())->select(['sharedgroupid']) |
134
|
|
|
->from('vtiger_tmp_read_group_sharing_per') |
135
|
|
|
->where(['userid' => $userId, 'tabid' => $tabId]); |
136
|
|
|
$conditions[] = ['vtiger_crmentity.smownerid' => $subQuery]; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
if (\App\Config::security('PERMITTED_BY_SHARED_OWNERS')) { |
141
|
|
|
$subQuery = (new \App\Db\Query())->select(['crmid'])->distinct() |
142
|
|
|
->from('u_yf_crmentity_showners') |
143
|
|
|
->where(['userid' => array_merge([$userId], $userModel->groups)]); |
144
|
|
|
$conditions[] = ['vtiger_crmentity.crmid' => $subQuery]; |
145
|
|
|
} |
146
|
|
|
if (!empty($conditions)) { |
147
|
|
|
$query->andWhere($conditions); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|