42 CREATE TABLE IF NOT EXISTS b_catalog_role (
43 ID INT UNSIGNED NOT NULL AUTO_INCREMENT,
44 NAME VARCHAR(250) NOT NULL,
50 CREATE TABLE IF NOT EXISTS b_catalog_role_relation (
51 ID INT UNSIGNED NOT NULL AUTO_INCREMENT,
52 ROLE_ID INT UNSIGNED NOT NULL,
53 RELATION VARCHAR(8) NOT NULL DEFAULT '',
55 INDEX ROLE_ID (ROLE_ID),
56 INDEX RELATION (RELATION)
61 CREATE TABLE IF NOT EXISTS b_catalog_permission (
62 ID INT UNSIGNED NOT NULL AUTO_INCREMENT,
63 ROLE_ID INT UNSIGNED NOT NULL,
64 PERMISSION_ID VARCHAR(32) NOT NULL DEFAULT '0',
65 VALUE INT NOT NULL DEFAULT '0',
67 INDEX ROLE_ID (ROLE_ID),
68 INDEX PERMISSION_ID (PERMISSION_ID)
77 (
new self($db))->install();
84 (
new self($db))->install(
false);
87 public function install($convertExisted =
true): void
90 if (!$this->db->lock($lockName, 600))
99 $this->db->startTransaction();
102 $this->fillSystemPermissions();
106 $this->fillDefaultSystemPermissions();
108 $this->db->commitTransaction();
114 $this->db->rollbackTransaction();
119 $this->db->unlock($lockName);
123 private function fillSystemPermissions(): void
125 if (PermissionTable::getCount())
130 $catalogGroupTasks = GroupTaskTable::getList([
132 'TASK.MODULE_ID' =>
'catalog',
133 '!=TASK.LETTER' =>
'D'
137 'GROUP_NAME' =>
'GROUP.NAME',
144 if (!$catalogGroupTasks)
146 $this->fillDefaultSystemPermissions();
151 $this->fillGroupTaskPermissions($catalogGroupTasks);
152 $this->fillDefaultSystemPermissions([RoleDictionary::ROLE_STOCKMAN]);
155 private function fillGroupTaskPermissions(array $catalogGroupTasks): void
157 $taskIds = array_column($catalogGroupTasks,
'TASK_ID');
158 $taskOperations = TaskOperationTable::getList([
160 'TASK_ID' => $taskIds,
164 'OPERATION_NAME' =>
'OPERATION.NAME',
169 $storeDocumentsInstallerMap = [
177 $permissionMap = array_merge($permissionMap, $storeDocumentsInstallerMap);
179 $taskPermissionMap = [];
180 while ($taskOperation = $taskOperations->fetch())
182 $taskId = $taskOperation[
'TASK_ID'];
183 $taskPermissionMap[$taskId] ??= [];
186 foreach ($newActions as $newAction)
188 $permission = $permissionMap[$newAction] ??
null;
194 if (in_array($permission, $storeDocumentsInstallerMap,
true))
197 if ($permission === PermissionDictionary::CATALOG_STORE_DOCUMENT_ALLOW_NEGATION_PRODUCT_QUANTITY)
205 $taskPermissionMap[$taskId] = array_merge(
206 $taskPermissionMap[$taskId],
207 PermissionDictionary::getStoreDocumentPermissionRules([$permission], $documents)
212 $taskPermissionMap[$taskId][] = $permission;
218 foreach ($catalogGroupTasks as $groupTask)
220 $groups[$groupTask[
'GROUP_ID']] ??= [];
221 $groups[$groupTask[
'GROUP_ID']][
'NAME'] = $groupTask[
'GROUP_NAME'];
222 $groups[$groupTask[
'GROUP_ID']][
'PERMISSIONS'][] = $taskPermissionMap[$groupTask[
'TASK_ID']];
225 $crmAdminGroupIds = [];
230 while ($crmAdminGroup = $crmAdminGroups->fetch())
232 $crmAdminGroupIds[] = (int)$crmAdminGroup[
'ID'];
235 foreach ($groups as $groupId => &$group)
237 $group[
'PERMISSIONS'] = array_unique(array_merge(...$group[
'PERMISSIONS']));
238 if (in_array($groupId, $crmAdminGroupIds,
true))
240 $group[
'PERMISSIONS'][] = PermissionDictionary::CATALOG_SETTINGS_EDIT_RIGHTS;
244 $groupRoleMap = $this->fillGroupPermissions($groups);
245 $this->fillGroupUserRoleRelations($groupRoleMap);
248 private function fillGroupPermissions(array $groups): array
252 foreach ($groups as $groupId => $groupData)
254 if (!is_array($groupData[
'PERMISSIONS']) || !$groupData[
'PERMISSIONS'])
259 $role = RoleTable::add([
260 'NAME' => $groupData[
'NAME']
263 if (!$role->isSuccess())
268 $roleId = $role->getId();
269 foreach ($groupData[
'PERMISSIONS'] as $permissionId)
271 if ($permissionId === PermissionDictionary::CATALOG_PRODUCT_EDIT_ENTITY_PRICE && Option::get(
'crm',
'enable_product_price_edit') !==
'Y')
276 $value = PermissionDictionary::getDefaultPermissionValue($permissionId);
277 $query[] =
"('{$roleId}', '{$permissionId}', '{$value}')";
280 $result[$groupId] = $roleId;
285 RoleUtil::insertPermissions($query);
291 private function fillGroupUserRoleRelations(array $groupRoleMap): void
293 $userGroups = UserGroupTable::getList([
294 'select' => [
'USER_ID',
'GROUP_ID'],
296 '=GROUP_ID' => array_keys($groupRoleMap),
297 '=USER.ACTIVE' =>
'Y',
298 '=USER.IS_REAL_USER' =>
'Y',
303 while ($user = $userGroups->fetch())
305 $groupId = (int)($groupRoleMap[$user[
'GROUP_ID']] ?? 0);
308 $valuesData[] =
new SqlExpression(
"(?, ?)", $groupId,
"U{$user['USER_ID']}");
318 INSERT INTO b_catalog_role_relation
320 VALUES ' . implode(
',', $valuesData) .
'
326 private function fillDefaultSystemPermissions(array $roles =
null): void
332 $map = array_intersect_key($map, array_flip($roles));
341 foreach ($map as $roleName => $roleClass)
343 if (is_subclass_of($roleClass, Role\Base::class))
345 $roleMapItem =
new $roleClass();
352 $role = RoleTable::add([
356 if (!$role->isSuccess())
361 $roleId = $role->getId();
362 $roleNameIdMap[$roleName] = $roleId;
363 foreach ($roleMapItem->getMap() as $item)
365 $query[] =
new SqlExpression(
368 $item[
'permissionId'],
374 RoleUtil::insertPermissions($query);
376 if (!array_intersect_key($map, array_flip([RoleDictionary::ROLE_DIRECTOR, RoleDictionary::ROLE_SALESMAN])))
388 'select' => [
'ID',
'STRING_ID']
391 $defaultGroupRoleMap = [];
392 while ($userGroup = $userGroups->fetch())
396 ? $roleNameIdMap[RoleDictionary::ROLE_DIRECTOR]
397 : $roleNameIdMap[RoleDictionary::ROLE_SALESMAN]
400 $defaultGroupRoleMap[$userGroup[
'ID']] = $role;
403 if (!$defaultGroupRoleMap)
408 $this->fillGroupUserRoleRelations($defaultGroupRoleMap);