|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Global privileges basic class. |
|
4
|
|
|
* |
|
5
|
|
|
* @package App |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright YetiForce S.A. |
|
8
|
|
|
* @license YetiForce Public License 6.5 (licenses/LicenseEN.txt or yetiforce.com) |
|
9
|
|
|
* @author Mariusz Krzaczkowski <[email protected]> |
|
10
|
|
|
* @author Radosław Skrzypczak <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace App; |
|
14
|
|
|
|
|
15
|
|
|
class PrivilegeUpdater |
|
16
|
|
|
{ |
|
17
|
|
|
private static $globalSearchPermissionsCache = []; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Checking if user can search globally. |
|
21
|
|
|
* |
|
22
|
|
|
* @param string $moduleName |
|
23
|
|
|
* @param int $userId |
|
24
|
1 |
|
* |
|
25
|
|
|
* @return bool |
|
26
|
1 |
|
*/ |
|
27
|
|
|
public static function checkGlobalSearchPermissions($moduleName, $userId = false) |
|
28
|
|
|
{ |
|
29
|
1 |
|
if (!$userId) { |
|
|
|
|
|
|
30
|
1 |
|
$userId = User::getCurrentUserId(); |
|
31
|
1 |
|
} |
|
32
|
1 |
|
if (!isset(static::$globalSearchPermissionsCache[$userId][$moduleName])) { |
|
|
|
|
|
|
33
|
|
|
$users = static::getGlobalSearchUsers(); |
|
34
|
|
|
$return = false; |
|
35
|
|
|
if (isset($users[$userId]) && \in_array($moduleName, $users[$userId])) { |
|
36
|
1 |
|
$return = true; |
|
37
|
|
|
} |
|
38
|
1 |
|
|
|
39
|
|
|
return static::$globalSearchPermissionsCache[$userId][$moduleName] = $return; |
|
40
|
|
|
} |
|
41
|
|
|
return static::$globalSearchPermissionsCache[$userId][$moduleName]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
private static $globalSearchUsersCache = false; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Loading a list of modules for users with permissions for global search. |
|
48
|
1 |
|
* |
|
49
|
|
|
* @return array |
|
50
|
1 |
|
*/ |
|
51
|
1 |
|
public static function getGlobalSearchUsers() |
|
52
|
1 |
|
{ |
|
53
|
1 |
|
if (!static::$globalSearchUsersCache) { |
|
|
|
|
|
|
54
|
1 |
|
static::$globalSearchUsersCache = []; |
|
55
|
1 |
|
$dataReader = (new Db\Query())->select(['userid', 'searchunpriv'])->from('vtiger_user2role') |
|
56
|
1 |
|
->leftJoin('vtiger_role', 'vtiger_user2role.roleid = vtiger_role.roleid') |
|
57
|
|
|
->where(['<>', 'vtiger_role.searchunpriv', '']) |
|
58
|
|
|
->createCommand()->query(); |
|
59
|
|
|
while ($row = $dataReader->read()) { |
|
60
|
1 |
|
static::$globalSearchUsersCache[$row['userid']] = explode(',', $row['searchunpriv']); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
return static::$globalSearchUsersCache; |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Updating permissions to records and global search. |
|
68
|
|
|
* |
|
69
|
1 |
|
* @param int $record |
|
70
|
|
|
* @param string $moduleName |
|
71
|
1 |
|
*/ |
|
72
|
1 |
|
public static function update($record, $moduleName) |
|
73
|
1 |
|
{ |
|
74
|
1 |
|
$searchUsers = $recordAccessUsers = ''; |
|
75
|
1 |
|
$users = Fields\Owner::getUsersIds(); |
|
76
|
1 |
|
$searchable = isset(\App\RecordSearch::getSearchableModules()[$moduleName]); |
|
77
|
1 |
|
foreach ($users as &$userId) { |
|
78
|
|
|
if (Privilege::isPermitted($moduleName, 'DetailView', $record, $userId)) { |
|
79
|
|
|
$recordAccessUsers .= ',' . $userId; |
|
80
|
|
|
$searchUsers .= ',' . $userId; |
|
81
|
1 |
|
} elseif ($searchable && static::checkGlobalSearchPermissions($moduleName, $userId)) { |
|
82
|
1 |
|
$searchUsers .= ',' . $userId; |
|
83
|
|
|
} |
|
84
|
1 |
|
} |
|
85
|
1 |
|
if (!empty($recordAccessUsers)) { |
|
86
|
|
|
$recordAccessUsers .= ','; |
|
87
|
1 |
|
} |
|
88
|
1 |
|
$createCommand = Db::getInstance()->createCommand(); |
|
89
|
1 |
|
$createCommand->update('vtiger_crmentity', ['users' => $recordAccessUsers], ['crmid' => $record])->execute(); |
|
90
|
1 |
|
if ($searchable) { |
|
91
|
1 |
|
$searchUsers = $searchUsers ? $searchUsers . ',' : $searchUsers; |
|
92
|
1 |
|
$createCommand->update('u_#__crmentity_search_label', ['userid' => $searchUsers], ['crmid' => $record])->execute(); |
|
93
|
1 |
|
} |
|
94
|
1 |
|
} |
|
95
|
1 |
|
|
|
96
|
1 |
|
/** |
|
97
|
1 |
|
* Updating permissions to global search. |
|
98
|
1 |
|
* |
|
99
|
|
|
* @param int $record |
|
100
|
|
|
* @param string $moduleName |
|
101
|
|
|
*/ |
|
102
|
|
|
public static function updateSearch($record, $moduleName) |
|
103
|
|
|
{ |
|
104
|
|
|
$searchUsers = ''; |
|
105
|
|
|
$users = Fields\Owner::getUsersIds(); |
|
106
|
1 |
|
foreach ($users as $userId) { |
|
107
|
|
|
if (static::checkGlobalSearchPermissions($moduleName, $userId) || Privilege::isPermitted($moduleName, 'DetailView', $record, $userId)) { |
|
108
|
1 |
|
$searchUsers .= ',' . $userId; |
|
109
|
1 |
|
} |
|
110
|
1 |
|
} |
|
111
|
1 |
|
if (!empty($searchUsers)) { |
|
112
|
1 |
|
$searchUsers .= ','; |
|
113
|
|
|
} |
|
114
|
|
|
Db::getInstance()->createCommand() |
|
115
|
1 |
|
->update('u_#__crmentity_search_label', ['userid' => $searchUsers], ['crmid' => $record])->execute(); |
|
116
|
1 |
|
} |
|
117
|
|
|
|
|
118
|
1 |
|
/** |
|
119
|
1 |
|
* Updating permissions to records. |
|
120
|
1 |
|
* |
|
121
|
1 |
|
* @param int $record |
|
122
|
1 |
|
* @param string $moduleName |
|
123
|
1 |
|
*/ |
|
124
|
|
|
public static function updateRecordAccess($record, $moduleName) |
|
125
|
|
|
{ |
|
126
|
|
|
$recordAccessUsers = ''; |
|
127
|
|
|
$users = Fields\Owner::getUsersIds(); |
|
128
|
|
|
foreach ($users as &$userId) { |
|
129
|
|
|
if (Privilege::isPermitted($moduleName, 'DetailView', $record, $userId)) { |
|
130
|
|
|
$recordAccessUsers .= ',' . $userId; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
if (!empty($recordAccessUsers)) { |
|
134
|
|
|
$recordAccessUsers .= ','; |
|
135
|
|
|
} |
|
136
|
|
|
Db::getInstance()->createCommand() |
|
137
|
|
|
->update('vtiger_crmentity', [ |
|
138
|
|
|
'users' => $recordAccessUsers, |
|
139
|
|
|
], 'crmid = ' . $record) |
|
140
|
|
|
->execute(); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Add to global permissions update queue. |
|
145
|
|
|
* |
|
146
|
|
|
* @param string $moduleName Module name |
|
147
|
|
|
* @param int $record If type = 1 starting number if type = 0 record ID |
|
148
|
|
|
* @param int $priority |
|
149
|
|
|
* @param int $type |
|
150
|
|
|
*/ |
|
151
|
|
|
public static function setUpdater($moduleName, $record = false, $priority = false, $type = 1) |
|
152
|
|
|
{ |
|
153
|
|
|
$params = [ |
|
154
|
|
|
'module' => $moduleName, |
|
155
|
|
|
'type' => $type, |
|
156
|
|
|
]; |
|
157
|
|
|
if ($record) { |
|
|
|
|
|
|
158
|
14 |
|
$params['crmid'] = $record; |
|
159
|
|
|
} |
|
160
|
|
|
if ($priority) { |
|
|
|
|
|
|
161
|
14 |
|
$params['priority'] = $priority; |
|
162
|
14 |
|
} |
|
163
|
|
|
$insert = $update = $row = false; |
|
|
|
|
|
|
164
|
14 |
|
$query = new Db\Query(); |
|
165
|
|
|
$row = $query->from('s_#__privileges_updater')->where(['module' => $moduleName, 'type' => 1])->limit(1)->one(); |
|
166
|
|
|
if ($row) { |
|
167
|
14 |
|
if (false === $record) { |
|
168
|
|
|
if (0 != $row['crmid']) { |
|
169
|
|
|
$update = true; |
|
170
|
14 |
|
$params['crmid'] = 0; |
|
171
|
14 |
|
} |
|
172
|
14 |
|
} elseif ($record < $row['crmid']) { |
|
173
|
14 |
|
$row = $query->from('s_#__privileges_updater')->where(['module' => $moduleName, 'type' => 0, 'crmid' => $record])->limit(1)->one(); |
|
174
|
13 |
|
if (false === $row) { |
|
175
|
13 |
|
$insert = true; |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
} elseif (false === $record) { |
|
179
|
|
|
$insert = true; |
|
180
|
|
|
} else { |
|
181
|
|
|
$row = $query->from('s_#__privileges_updater')->where(['module' => $moduleName, 'type' => 0, 'crmid' => $record])->limit(1)->one(); |
|
182
|
|
|
if (false === $row) { |
|
183
|
|
|
$insert = true; |
|
184
|
|
|
$params['type'] = 0; |
|
185
|
2 |
|
} |
|
186
|
2 |
|
} |
|
187
|
|
|
$db = Db::getInstance('admin'); |
|
188
|
|
|
if ($insert) { |
|
189
|
|
|
$db->createCommand()->insert('s_#__privileges_updater', $params)->execute(); |
|
190
|
|
|
} |
|
191
|
|
|
if ($update) { |
|
192
|
|
|
$db->createCommand()->update('s_#__privileges_updater', $params, ['module' => $moduleName, 'type' => $type])->execute(); |
|
193
|
|
|
} |
|
194
|
14 |
|
} |
|
195
|
14 |
|
|
|
196
|
2 |
|
/** |
|
197
|
|
|
* Updating permissions to all modules. |
|
198
|
14 |
|
*/ |
|
199
|
|
|
public static function setAllUpdater() |
|
200
|
|
|
{ |
|
201
|
14 |
|
Cache::clear(); |
|
202
|
|
|
$modules = \vtlib\Functions::getAllModules(); |
|
203
|
|
|
foreach ($modules as $module) { |
|
204
|
|
|
static::setUpdater($module['name']); |
|
205
|
|
|
} |
|
206
|
9 |
|
PrivilegeAdvanced::reloadCache(); |
|
207
|
|
|
if (Config::module('ModTracker', 'WATCHDOG')) { |
|
208
|
9 |
|
\Vtiger_Watchdog_Model::reloadCache(); |
|
209
|
9 |
|
} |
|
210
|
9 |
|
} |
|
211
|
|
|
|
|
212
|
9 |
|
/** |
|
213
|
9 |
|
* Update permissions while saving record. |
|
214
|
9 |
|
* |
|
215
|
|
|
* @param \Vtiger_Record_Model $record |
|
216
|
9 |
|
*/ |
|
217
|
9 |
|
public static function updateOnRecordSave(\Vtiger_Record_Model $record) |
|
218
|
|
|
{ |
|
219
|
|
|
if (!Config::security('CACHING_PERMISSION_TO_RECORD')) { |
|
220
|
|
|
return false; |
|
221
|
|
|
} |
|
222
|
|
|
static::setUpdater($record->getModuleName(), $record->getId(), 6, 0); |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: