Privilege::checkPermission()   F
last analyzed

Complexity

Conditions 104
Paths 3758

Size

Total Lines 276
Code Lines 208

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 7672.5398

Importance

Changes 0
Metric Value
eloc 208
dl 0
loc 276
ccs 23
cts 205
cp 0.1122
rs 0
c 0
b 0
f 0
cc 104
nc 3758
nop 4
crap 7672.5398

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App;
4
5
/**
6
 * Privilege 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 Privilege
16
{
17
	public static $isPermittedLevel;
18
19
	/**
20
	 * Interpreter for privilege.
21
	 *
22
	 * @var string
23
	 */
24
	private static $interpreter;
25
26
	/**
27
	 * Sets interpreter.
28
	 *
29
	 * @param string $className
30
	 *
31
	 * @return void
32
	 */
33
	public static function setPermissionInterpreter(string $className)
34
	{
35
		static::$interpreter = $className;
0 ignored issues
show
Bug introduced by
Since $interpreter is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $interpreter to at least protected.
Loading history...
36
	}
37
38
	/**
39
	 * Invokes function to check permission .
40
	 *
41
	 * @param string   $moduleName
42
	 * @param string   $actionName
43
	 * @param bool|int $record
44
	 * @param mixed    $userId
45
	 *
46 14
	 * @return bool
47
	 */
48 14
	public static function isPermitted($moduleName, $actionName = null, $record = false, $userId = false)
49
	{
50
		if (!empty(static::$interpreter) && class_exists(static::$interpreter)) {
0 ignored issues
show
Bug introduced by
Since $interpreter is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $interpreter to at least protected.
Loading history...
51 14
			return (static::$interpreter)::isPermitted($moduleName, $actionName, $record, $userId);
52
		}
53
		return static::checkPermission($moduleName, $actionName, $record, $userId);
54
	}
55
56
	/**
57
	 * Function to check permission for a Module/Action/Record.
58
	 *
59
	 * @param string   $moduleName
60
	 * @param string   $actionName
61
	 * @param bool|int $record
62
	 * @param mixed    $userId
63
	 *
64 14
	 * @return bool
65
	 */
66 14
	public static function checkPermission($moduleName, $actionName = null, $record = false, $userId = false)
67 14
	{
68 14
		\App\Log::trace("Entering isPermitted($moduleName,$actionName,$record,$userId) method ...");
69
		if (!$userId) {
70 14
			$userId = \App\User::getCurrentUserId();
71 14
		}
72 14
		$userId = (int) $userId;
73 14
		$userPrivileges = \App\User::getPrivilegesFile($userId);
74 14
		$permission = false;
75 14
		$tabId = Module::getModuleId($moduleName);
76
		if ('Settings' !== Request::_get('parent')) {
0 ignored issues
show
Bug introduced by
The method _get() does not exist on App\Request. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

76
		if ('Settings' !== Request::/** @scrutinizer ignore-call */ _get('parent')) {
Loading history...
77
			if ('Users' === $moduleName && $record == \App\User::getCurrentUserId()) {
78
				static::$isPermittedLevel = 'SEC_IS_CURRENT_USER';
79
				\App\Log::trace('Exiting isPermitted method ... - yes');
80
				return true;
81 14
			}
82
		} elseif (false === $tabId) {
83
			$permission = $userPrivileges['is_admin'] ? true : false;
84
			static::$isPermittedLevel = 'SEC_ADMINISTRATION_MODULE_' . ($permission ? 'YES' : 'NO');
85
			\App\Log::trace('Exiting isPermitted method ... - ' . ($permission) ? 'YES' : 'NO');
86
			return $permission;
87
		}
88
		if (!Module::isModuleActive($moduleName)) {
89
			static::$isPermittedLevel = 'SEC_MODULE_IS_INACTIVE';
90
			\App\Log::trace('Exiting isPermitted method ... - yes');
91
			return false;
92 14
		}
93 1
		$actionId = Module::getActionId($actionName);
94 1
		//Checking whether the user is admin
95 1
		if ($userPrivileges['is_admin']) {
96
			if ($record && 'Users' !== $moduleName) {
97 14
				$recordMetaData = \vtlib\Functions::getCRMRecordMetadata($record);
98
				if (empty($recordMetaData)) {
99 14
					static::$isPermittedLevel = 'SEC_RECORD_DOES_NOT_EXIST';
100 14
					\App\Log::trace('Exiting isPermitted method ... - SEC_RECORD_DOES_NOT_EXIST');
101 9
					return false;
102 9
				}
103
				if (0 !== $recordMetaData['deleted'] && (1 === $actionId || 0 === $actionId || 17 === $actionId)) {
104
					switch ($recordMetaData['deleted']) {
105
						case 1:
106
							static::$isPermittedLevel = 'SEC_RECORD_DELETED';
107 9
							\App\Log::trace('Exiting isPermitted method ... - SEC_RECORD_DELETED');
108
							break;
109
						case 2:
110
							static::$isPermittedLevel = 'SEC_RECORD_ARCHIVED';
111
							\App\Log::trace('Exiting isPermitted method ... - SEC_RECORD_ARCHIVED');
112
							break;
113
						default:
114
							break;
115
					}
116
					return false;
117
				}
118
			}
119
			static::$isPermittedLevel = 'SEC_USER_IS_ADMIN';
120
			\App\Log::trace('Exiting isPermitted method ... - SEC_USER_IS_ADMIN');
121
			return true;
122
		}
123 14
		//If no actionid, then allow action is vtiger_tab permission is available
124 14
		if ('' === $actionId || null === $actionId) {
125 14
			if (isset($userPrivileges['profile_tabs_permission'][$tabId]) && 0 == $userPrivileges['profile_tabs_permission'][$tabId]) {
126
				$permission = true;
127
			} else {
128
				$permission = false;
129
			}
130
			static::$isPermittedLevel = 'SEC_NO_ACTION_MODULE_PERMISSIONS' . ($permission ? 'YES' : 'NO');
131
			\App\Log::trace('Exiting isPermitted method ... - ' . static::$isPermittedLevel);
132
			return $permission;
133
		}
134
		//Checking for vtiger_tab permission
135
		if (!isset($userPrivileges['profile_tabs_permission'][$tabId]) || 0 != $userPrivileges['profile_tabs_permission'][$tabId]) {
136
			static::$isPermittedLevel = 'SEC_MODULE_PERMISSIONS_NO';
137
			\App\Log::trace('Exiting isPermitted method ... - SEC_MODULE_PERMISSIONS_NO');
138
			return false;
139
		}
140
		if (false === $actionId) {
0 ignored issues
show
introduced by
The condition false === $actionId is always false.
Loading history...
141
			static::$isPermittedLevel = 'SEC_ACTION_DOES_NOT_EXIST';
142
			\App\Log::trace('Exiting isPermitted method ... - SEC_ACTION_DOES_NOT_EXIST');
143
			return false;
144
		}
145
		//Checking for Action Permission
146
		if (!isset($userPrivileges['profile_action_permission'][$tabId][$actionId])) {
147
			static::$isPermittedLevel = 'SEC_MODULE_NO_ACTION_TOOL';
148
			\App\Log::trace('Exiting isPermitted method ... - SEC_MODULE_NO_ACTION_TOOL');
149
			return false;
150
		}
151
		if (\strlen($userPrivileges['profile_action_permission'][$tabId][$actionId]) < 1 && '' === $userPrivileges['profile_action_permission'][$tabId][$actionId]) {
152
			static::$isPermittedLevel = 'SEC_MODULE_RIGHTS_TO_ACTION';
153
			\App\Log::trace('Exiting isPermitted method ... - SEC_MODULE_RIGHTS_TO_ACTION');
154
			return true;
155
		}
156
		if (0 != $userPrivileges['profile_action_permission'][$tabId][$actionId] && '' != $userPrivileges['profile_action_permission'][$tabId][$actionId]) {
157
			static::$isPermittedLevel = 'SEC_MODULE_NO_RIGHTS_TO_ACTION';
158
			\App\Log::trace('Exiting isPermitted method ... - SEC_MODULE_NO_RIGHTS_TO_ACTION');
159
			return false;
160
		}
161
		//Checking for view all permission
162
		if ((0 == $userPrivileges['profile_global_permission'][1] || 0 == $userPrivileges['profile_global_permission'][2]) && (3 == $actionId || 4 == $actionId)) {
163
			static::$isPermittedLevel = 'SEC_MODULE_VIEW_ALL_PERMISSION';
164
			\App\Log::trace('Exiting isPermitted method ... - SEC_MODULE_VIEW_ALL_PERMISSION');
165
			return true;
166
		}
167
		//Checking for edit all permission
168
		if (0 == $userPrivileges['profile_global_permission'][2] && (3 == $actionId || 4 == $actionId || 0 == $actionId || 1 == $actionId)) {
169
			static::$isPermittedLevel = 'SEC_MODULE_EDIT_ALL_PERMISSION';
170
			\App\Log::trace('Exiting isPermitted method ... - SEC_MODULE_EDIT_ALL_PERMISSION');
171
			return true;
172
		}
173
		//Checking and returning true if recorid is null
174
		if (empty($record)) {
175
			static::$isPermittedLevel = 'SEC_RECORD_ID_IS_NULL';
176
			\App\Log::trace('Exiting isPermitted method ... - SEC_RECORD_ID_IS_NULL');
177
			return true;
178
		}
179
		//If modules is Products,Vendors,Faq,PriceBook then no sharing
180
		if (1 === Module::getModuleOwner($tabId)) {
181
			static::$isPermittedLevel = 'SEC_MODULE_IS_OWNEDBY';
182
			\App\Log::trace('Exiting isPermitted method ... - SEC_MODULE_IS_OWNEDBY');
183
			return true;
184
		}
185
186
		$recordMetaData = \vtlib\Functions::getCRMRecordMetadata($record);
187
		if (empty($recordMetaData)) {
188
			static::$isPermittedLevel = 'SEC_RECORD_DOES_NOT_EXIST';
189
			\App\Log::trace('Exiting isPermitted method ... - SEC_RECORD_DOES_NOT_EXIST');
190
			return false;
191
		}
192
		if (0 !== $recordMetaData['deleted'] && (1 === $actionId || 0 === $actionId || 17 === $actionId)) {
193
			switch ($recordMetaData['deleted']) {
194
				case 1:
195
					static::$isPermittedLevel = 'SEC_RECORD_DELETED';
196
					\App\Log::trace('Exiting isPermitted method ... - SEC_RECORD_DELETED');
197
					break;
198
				case 2:
199
					static::$isPermittedLevel = 'SEC_RECORD_ARCHIVED';
200
					\App\Log::trace('Exiting isPermitted method ... - SEC_RECORD_ARCHIVED');
201
					break;
202
				default:
203
					break;
204
			}
205
			return false;
206
		}
207
		if (\App\Config::security('PERMITTED_BY_PRIVATE_FIELD') && $recordMetaData['private']
208
			&& ($fieldInfo = \App\Field::getFieldInfo('private', $recordMetaData['setype'])) && \in_array($fieldInfo['presence'], [0, 2])) {
209
			$level = 'SEC_PRIVATE_RECORD_NO';
210
			$isPermittedPrivateRecord = false;
211
			$recOwnId = $recordMetaData['smownerid'];
212
			$recOwnType = \App\Fields\Owner::getType($recOwnId);
213
			if ('Users' === $recOwnType) {
0 ignored issues
show
introduced by
The condition 'Users' === $recOwnType is always false.
Loading history...
214
				if ($userId === $recOwnId) {
215
					$level = 'SEC_PRIVATE_RECORD_OWNER_CURRENT_USER';
216
					$isPermittedPrivateRecord = true;
217
				}
218
			} elseif ('Groups' === $recOwnType) {
0 ignored issues
show
introduced by
The condition 'Groups' === $recOwnType is always false.
Loading history...
219
				if (\in_array($recOwnId, $userPrivileges['groups'])) {
220
					$level = 'SEC_PRIVATE_RECORD_OWNER_CURRENT_GROUP';
221
					$isPermittedPrivateRecord = true;
222
				}
223
			}
224
			if (!$isPermittedPrivateRecord && \App\Config::security('PERMITTED_BY_SHARED_OWNERS')) {
0 ignored issues
show
introduced by
The condition $isPermittedPrivateRecord is always false.
Loading history...
225
				$shownerIds = Fields\SharedOwner::getById($record);
226
				if (\in_array($userId, $shownerIds) || \count(array_intersect($shownerIds, $userPrivileges['groups'])) > 0) {
227
					$level = 'SEC_PRIVATE_RECORD_SHARED_OWNER';
228
					$isPermittedPrivateRecord = true;
229
				}
230
			}
231
			static::$isPermittedLevel = $level;
232
			\App\Log::trace('Exiting isPermitted method ... - ' . static::$isPermittedLevel);
233
			return $isPermittedPrivateRecord;
234
		}
235
		// Check advanced permissions
236
		if (\App\Config::security('PERMITTED_BY_ADVANCED_PERMISSION')) {
237
			$prvAdv = PrivilegeAdvanced::checkPermissions($record, $moduleName, $userId);
238
			if (false !== $prvAdv) {
239
				if (0 === $prvAdv) {
240
					static::$isPermittedLevel = 'SEC_ADVANCED_PERMISSION_NO';
241
					\App\Log::trace('Exiting isPermitted method ... - SEC_ADVANCED_PERMISSION_NO');
242
					return false;
243
				}
244
				static::$isPermittedLevel = 'SEC_ADVANCED_PERMISSION_YES';
245
				\App\Log::trace('Exiting isPermitted method ... - SEC_ADVANCED_PERMISSION_YES');
246
				return true;
247
			}
248
		}
249
		if (($modules = \App\Config::security('permittedModulesByCreatorField')) && \in_array($moduleName, $modules) && $userId === $recordMetaData['smcreatorid']) {
250
			if (3 == $actionId || 4 == $actionId) {
251
				static::$isPermittedLevel = 'SEC_RECORD_CREATOR_CURRENT_USER_READ_ACCESS';
252
				\App\Log::trace('Exiting isPermitted method ... - SEC_RECORD_CREATOR_CURRENT_USER_READ_ACCESS');
253
				return true;
254
			}
255
			if (\App\Config::security('permittedWriteAccessByCreatorField') && (0 == $actionId || 1 == $actionId)) {
256
				static::$isPermittedLevel = 'SEC_RECORD_CREATOR_CURRENT_USER_WRITE_ACCESS';
257
				\App\Log::trace('Exiting isPermitted method ... - SEC_RECORD_CREATOR_CURRENT_USER_WRITE_ACCESS');
258
				return true;
259
			}
260
		}
261
		if (\App\Config::security('PERMITTED_BY_SHARED_OWNERS')) {
262
			$shownerids = Fields\SharedOwner::getById($record);
263
			if (\in_array($userId, $shownerids) || \count(array_intersect($shownerids, $userPrivileges['groups'])) > 0) {
264
				static::$isPermittedLevel = 'SEC_RECORD_SHARED_OWNER';
265
				\App\Log::trace('Exiting isPermitted method ... - SEC_RECORD_SHARED_OWNER');
266
				return true;
267
			}
268
		}
269
		//Retreiving the RecordOwnerId
270
		$recOwnId = $recordMetaData['smownerid'];
271
		$recOwnType = Fields\Owner::getType($recOwnId);
272
		if ('Users' === $recOwnType) {
0 ignored issues
show
introduced by
The condition 'Users' === $recOwnType is always false.
Loading history...
273
			//Checking if the Record Owner is the current User
274
			if ($userId === $recOwnId) {
275
				static::$isPermittedLevel = 'SEC_RECORD_OWNER_CURRENT_USER';
276
				\App\Log::trace('Exiting isPermitted method ... - SEC_RECORD_OWNER_CURRENT_USER');
277
				return true;
278
			}
279
			if (\App\Config::security('PERMITTED_BY_ROLES')) {
280
				//Checking if the Record Owner is the Subordinate User
281
				foreach ($userPrivileges['subordinate_roles_users'] as $usersByRole) {
282
					if (isset($usersByRole[$recOwnId])) {
283
						static::$isPermittedLevel = 'SEC_RECORD_OWNER_SUBORDINATE_USER';
284
						\App\Log::trace('Exiting isPermitted method ... - SEC_RECORD_OWNER_SUBORDINATE_USER');
285
						return true;
286
					}
287
				}
288
			}
289
		} elseif ('Groups' === $recOwnType) {
0 ignored issues
show
introduced by
The condition 'Groups' === $recOwnType is always false.
Loading history...
290
			//Checking if the record owner is the current user's group
291
			if (\in_array($recOwnId, $userPrivileges['groups'])) {
292
				static::$isPermittedLevel = 'SEC_RECORD_OWNER_CURRENT_GROUP';
293
				\App\Log::trace('Exiting isPermitted method ... - SEC_RECORD_OWNER_CURRENT_GROUP');
294
				return true;
295
			}
296
		}
297
		if (\App\Config::security('PERMITTED_BY_RECORD_HIERARCHY')) {
298
			$userPrivilegesModel = \Users_Privileges_Model::getInstanceById($userId);
299
			$role = $userPrivilegesModel->getRoleDetail();
300
			if (((3 == $actionId || 4 == $actionId) && 0 != $role->get('previewrelatedrecord')) || ((0 == $actionId || 1 == $actionId) && 0 != $role->get('editrelatedrecord'))) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $role->get('editrelatedrecord') of type mixed|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
introduced by
Consider adding parentheses for clarity. Current Interpretation: (3 == $actionId || 4 == ...et('editrelatedrecord'), Probably Intended Meaning: 3 == $actionId || 4 == $...t('editrelatedrecord'))
Loading history...
Bug Best Practice introduced by
It seems like you are loosely comparing $role->get('previewrelatedrecord') of type mixed|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
301
				$parentRecord = \Users_Privileges_Model::getParentRecord($record, $moduleName, $role->get('previewrelatedrecord'), $actionId);
0 ignored issues
show
Bug introduced by
$actionId of type integer is incompatible with the type type expected by parameter $actionid of Users_Privileges_Model::getParentRecord(). ( Ignorable by Annotation )

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

301
				$parentRecord = \Users_Privileges_Model::getParentRecord($record, $moduleName, $role->get('previewrelatedrecord'), /** @scrutinizer ignore-type */ $actionId);
Loading history...
302
				if ($parentRecord && Record::isExists($parentRecord)) {
303
					$recordMetaData = \vtlib\Functions::getCRMRecordMetadata($parentRecord);
304
					$permissionsRoleForRelatedField = $role->get('permissionsrelatedfield');
305
					$permissionsRelatedField = '' === $permissionsRoleForRelatedField ? [] : explode(',', $role->get('permissionsrelatedfield'));
306
					$relatedPermission = false;
307
					foreach ($permissionsRelatedField as $row) {
308
						switch ($row) {
309
							case 0:
310
								$relatedPermission = $recordMetaData['smownerid'] === $userId || \in_array($recordMetaData['smownerid'], $userPrivileges['groups']);
311
								break;
312
							case 1:
313
								$relatedPermission = \in_array($userId, Fields\SharedOwner::getById($parentRecord));
314
								break;
315
							case 2:
316
								if (\App\Config::security('PERMITTED_BY_SHARING')) {
317
									$relatedPermission = static::isPermittedBySharing($recordMetaData['setype'], Module::getModuleId($recordMetaData['setype']), $actionId, $parentRecord, $userId);
318
								}
319
								break;
320
							case 3:
321
								$relatedPermission = static::isPermitted($recordMetaData['setype'], 'DetailView', $parentRecord);
322
								break;
323
							default:
324
								break;
325
						}
326
						if ($relatedPermission) {
327
							static::$isPermittedLevel = 'SEC_RECORD_HIERARCHY_USER';
328
							\App\Log::trace('Exiting isPermitted method ... - SEC_RECORD_HIERARCHY_USER');
329
							return true;
330
						}
331
					}
332
				}
333
			}
334
		}
335
		if (\App\Config::security('PERMITTED_BY_SHARING')) {
336
			$permission = static::isPermittedBySharing($moduleName, $tabId, $actionId, $record, $userId);
337
		}
338
		static::$isPermittedLevel = 'SEC_RECORD_BY_SHARING_' . ($permission ? 'YES' : 'NO');
339
		\App\Log::trace('Exiting isPermitted method ... - ' . static::$isPermittedLevel);
340
341
		return $permission;
342
	}
343
344
	public static function isPermittedBySharing($moduleName, $tabId, $actionId, $recordId, $userId)
345
	{
346
		$sharingPrivileges = \App\User::getSharingFile($userId);
347
		//Retreiving the default Organisation sharing Access
348
		$othersPermissionId = $sharingPrivileges['defOrgShare'][$tabId];
349
		//Checking for Default Org Sharing permission
350
		if (0 == $othersPermissionId) {
351
			if (1 === $actionId || 0 === $actionId) {
352
				return static::isReadWritePermittedBySharing($moduleName, $tabId, $actionId, $recordId, $userId);
353
			}
354
			return 2 !== $actionId;
355
		}
356
		if (1 == $othersPermissionId) {
357
			return 2 !== $actionId;
358
		}
359
		if (2 == $othersPermissionId) {
360
			return true;
361
		}
362
		if (3 == $othersPermissionId) {
363
			if (3 === $actionId || 4 === $actionId) {
364
				return static::isReadPermittedBySharing($moduleName, $tabId, $actionId, $recordId, $userId);
365
			}
366
			if (0 === $actionId || 1 === $actionId) {
367
				return static::isReadWritePermittedBySharing($moduleName, $tabId, $actionId, $recordId, $userId);
368
			}
369
			return 2 !== $actionId;
370
		}
371
		return true;
372
	}
373
374
	/** Function to check if the currently logged in user has Read Access due to Sharing for the specified record.
375
	 * @param $moduleName -- Module Name:: Type varchar
0 ignored issues
show
Documentation Bug introduced by
The doc comment -- at position 0 could not be parsed: Unknown type name '--' at position 0 in --.
Loading history...
376
	 * @param $actionId   -- Action Id:: Type integer
377
	 * @param $recordId   -- Record Id:: Type integer
378
	 * @param $tabId      -- Tab Id:: Type integer
379
	 * @param mixed $userId
380
	 * @returns yes or no. If Yes means this action is allowed for the currently logged in user. If no means this action is not allowed for the currently logged in user
381
	 */
382
	public static function isReadPermittedBySharing($moduleName, $tabId, $actionId, $recordId, $userId)
383
	{
384
		\App\Log::trace("Entering isReadPermittedBySharing($moduleName,$tabId,$actionId,$recordId,$userId) method ...");
385
		$sharingPrivileges = \App\User::getSharingFile($userId);
386
387
		if (!isset($sharingPrivileges['permission'][$moduleName])) {
388
			return false;
389
		}
390
		$sharingPrivilegesModule = $sharingPrivileges['permission'][$moduleName];
391
392
		$recordMetaData = \vtlib\Functions::getCRMRecordMetadata($recordId);
393
		$ownerId = $recordMetaData['smownerid'];
394
		$ownerType = \App\Fields\Owner::getType($ownerId);
395
396
		$read = $sharingPrivilegesModule['read'];
397
		if ('Users' == $ownerType) {
398
			//Checking the Read Sharing Permission Array in Role Users
399
			foreach ($read['ROLE'] as $userids) {
400
				if (\in_array($ownerId, $userids)) {
401
					\App\Log::trace('Exiting isReadPermittedBySharing method ...');
402
403
					return true;
404
				}
405
			}
406
			//Checking the Read Sharing Permission Array in Groups Users
407
			foreach ($read['GROUP'] as $userids) {
408
				if (\in_array($ownerId, $userids)) {
409
					\App\Log::trace('Exiting isReadPermittedBySharing method ...');
410
411
					return true;
412
				}
413
			}
414
		} else {
415
			if (isset($read['GROUP'][$ownerId])) {
416
				\App\Log::trace('Exiting isReadPermittedBySharing method ...');
417
418
				return true;
419
			}
420
		}
421
422
		//Checking for the Related Sharing Permission
423
		$relatedModuleArray = null;
424
		if (isset($sharingPrivileges['relatedModuleShare'][$tabId])) {
425
			$relatedModuleArray = $sharingPrivileges['relatedModuleShare'][$tabId];
426
		}
427
		if (\is_array($relatedModuleArray)) {
428
			foreach ($relatedModuleArray as $parModId) {
429
				$parRecordOwner = PrivilegeUtil::getParentRecordOwner($tabId, $parModId, $recordId);
430
				if (\count($parRecordOwner) > 0) {
431
					$parModName = Module::getModuleName($parModId);
432
					if (isset($sharingPrivileges['permission'][$parModName . '_' . $moduleName])) {
433
						$readRelated = $sharingPrivileges['permission'][$parModName . '_' . $moduleName]['read'];
434
435
						$relOwnerType = '';
436
						$relOwnerId = '';
437
						foreach ($parRecordOwner as $rel_type => $rel_id) {
438
							$relOwnerType = $rel_type;
439
							$relOwnerId = $rel_id;
440
						}
441
						if ('Users' == $relOwnerType) {
442
							//Checking in Role Users
443
							foreach ($readRelated['ROLE'] as $userids) {
444
								if (\in_array($relOwnerId, $userids)) {
445
									\App\Log::trace('Exiting isReadPermittedBySharing method ...');
446
447
									return true;
448
								}
449
							}
450
							//Checking in Group Users
451
							foreach ($readRelated['GROUP'] as $userids) {
452
								if (\in_array($relOwnerId, $userids)) {
453
									\App\Log::trace('Exiting isReadPermittedBySharing method ...');
454
455
									return true;
456
								}
457
							}
458
						} else {
459
							if (isset($readRelated['GROUP'][$relOwnerId])) {
460
								\App\Log::trace('Exiting isReadPermittedBySharing method ...');
461
462
								return true;
463
							}
464
						}
465
					}
466
				}
467
			}
468
		}
469
		\App\Log::trace('Exiting isReadPermittedBySharing method ...');
470
471
		return false;
472
	}
473
474
	/** Function to check if the currently logged in user has Write Access due to Sharing for the specified record.
475
	 * @param $moduleName -- Module Name:: Type varchar
0 ignored issues
show
Documentation Bug introduced by
The doc comment -- at position 0 could not be parsed: Unknown type name '--' at position 0 in --.
Loading history...
476
	 * @param $actionId   -- Action Id:: Type integer
477
	 * @param $recordid   -- Record Id:: Type integer
478
	 * @param $tabId      -- Tab Id:: Type integer
479
	 * @param mixed $recordId
480
	 * @param mixed $userId
481
	 * @returns yes or no. If Yes means this action is allowed for the currently logged in user. If no means this action is not allowed for the currently logged in user
482
	 */
483
	public static function isReadWritePermittedBySharing($moduleName, $tabId, $actionId, $recordId, $userId)
484
	{
485
		\App\Log::trace("Entering isReadWritePermittedBySharing($moduleName,$tabId,$actionId,$recordId,$userId) method ...");
486
		$sharingPrivileges = \App\User::getSharingFile($userId);
487
		if (!isset($sharingPrivileges['permission'][$moduleName])) {
488
			return false;
489
		}
490
		$sharingPrivilegesModule = $sharingPrivileges['permission'][$moduleName];
491
492
		$recordMetaData = \vtlib\Functions::getCRMRecordMetadata($recordId);
493
		$ownerId = $recordMetaData['smownerid'];
494
		$ownerType = \App\Fields\Owner::getType($ownerId);
495
496
		$write = $sharingPrivilegesModule['write'];
497
		if ('Users' == $ownerType) {
498
			//Checking the Write Sharing Permission Array in Role Users
499
			foreach ($write['ROLE'] as $userids) {
500
				if (\in_array($ownerId, $userids)) {
501
					\App\Log::trace('Exiting isReadWritePermittedBySharing method ...');
502
503
					return true;
504
				}
505
			}
506
			//Checking the Write Sharing Permission Array in Groups Users
507
			foreach ($write['GROUP'] as $userids) {
508
				if (\in_array($ownerId, $userids)) {
509
					\App\Log::trace('Exiting isReadWritePermittedBySharing method ...');
510
511
					return true;
512
				}
513
			}
514
		} elseif ('Groups' == $ownerType) {
515
			if (isset($write['GROUP'][$ownerId])) {
516
				\App\Log::trace('Exiting isReadWritePermittedBySharing method ...');
517
518
				return true;
519
			}
520
		}
521
		//Checking for the Related Sharing Permission
522
		if (isset($sharingPrivileges['relatedModuleShare'][$tabId]) && \is_array($sharingPrivileges['relatedModuleShare'][$tabId])) {
523
			foreach ($sharingPrivileges['relatedModuleShare'][$tabId] as $parModId) {
524
				$parRecordOwner = PrivilegeUtil::getParentRecordOwner($tabId, $parModId, $recordId);
525
				if (!empty($parRecordOwner)) {
526
					$parModName = Module::getModuleName($parModId);
527
					if (isset($sharingPrivileges['permission'][$parModName . '_' . $moduleName])) {
528
						$writeRelated = $sharingPrivileges['permission'][$parModName . '_' . $moduleName]['write'];
529
						$relOwnerType = '';
530
						$relOwnerId = '';
531
						foreach ($parRecordOwner as $rel_type => $rel_id) {
532
							$relOwnerType = $rel_type;
533
							$relOwnerId = $rel_id;
534
						}
535
						if ('Users' == $relOwnerType) {
536
							//Checking in Role Users
537
							foreach ($writeRelated['ROLE'] as $userids) {
538
								if (\in_array($relOwnerId, $userids)) {
539
									\App\Log::trace('Exiting isReadWritePermittedBySharing method ...');
540
541
									return true;
542
								}
543
							}
544
							//Checking in Group Users
545
							foreach ($writeRelated['GROUP'] as $userids) {
546
								if (\in_array($relOwnerId, $userids)) {
547
									\App\Log::trace('Exiting isReadWritePermittedBySharing method ...');
548
549
									return true;
550
								}
551
							}
552
						} else {
553
							if (isset($writeRelated['GROUP'][$relOwnerId])) {
554
								\App\Log::trace('Exiting isReadWritePermittedBySharing method ...');
555
556
								return true;
557
							}
558
						}
559
					}
560
				}
561
			}
562
		}
563
		\App\Log::trace('Exiting isReadWritePermittedBySharing method ...');
564
		return false;
565
	}
566
567
	/**
568
	 * Add to global permissions update queue.
569
	 *
570
	 * @param string $moduleName Module name
571
	 * @param int    $record     If type = 1 starting number if type = 0 record ID
572
	 * @param int    $priority
573
	 * @param int    $type
574
	 */
575
	public static function setUpdater($moduleName, $record = false, $priority = false, $type = 1)
576
	{
577
		PrivilegeUpdater::setUpdater($moduleName, $record, $priority, $type);
578
	}
579
580 5
	public static function setAllUpdater()
581
	{
582 5
		PrivilegeUpdater::setAllUpdater();
583 5
	}
584
}
585