34 $fields[
'RESULT'] ===
false
41 $externalAuthId = $fields[
'EXTERNAL_AUTH_ID'] ??
null;
42 if (in_array($externalAuthId, UserTable::getExternalUserTypes(),
true))
47 $userId = (int)$fields[
'ID'];
50 if (!isset($fields[
'UF_DEPARTMENT']) || !Loader::includeModule(
'intranet'))
57 if (!is_array($fields[
'UF_DEPARTMENT']))
59 $departmentId = $fields[
'UF_DEPARTMENT'];
61 if (!is_numeric($departmentId))
68 $accessCodes = self::findAllAccessCodes((
int)$departmentId);
71 $baseAccessCode = $topDepartmentId ?
'DR' . $topDepartmentId :
'AU';
73 $accessCodes = !empty($accessCodes) ? $accessCodes : [$baseAccessCode];
75 $presetId = self::getTopSortGroupIdByAccessCodes($accessCodes);
83 if (!$fields[
'UF_DEPARTMENT'][0])
90 $presetId = self::findTopSortPresetId($fields[
'UF_DEPARTMENT']);
105 !isset($fields[
'UF_DEPARTMENT'])
106 || !Loader::includeModule(
'intranet')
113 $userId = (int)$fields[
'ID'];
115 if (!is_array($fields[
'UF_DEPARTMENT']))
117 $departmentId = $fields[
'UF_DEPARTMENT'];
118 if (!is_numeric($departmentId))
123 $accessCodes = self::findAllAccessCodes((
int)$departmentId);
126 $baseAccessCode = $topDepartmentId ?
'DR' . $topDepartmentId :
'AU';
128 $accessCodes = !empty($accessCodes) ? $accessCodes : [$baseAccessCode];
130 $presetId = self::getTopSortGroupIdByAccessCodes($accessCodes);
132 self::updateUserGroups($presetId, $userId);
137 if (!$fields[
'UF_DEPARTMENT'][0])
142 $presetId = self::findTopSortPresetId($fields[
'UF_DEPARTMENT']);
143 self::updateUserGroups($presetId, $userId);
155 $userId = (int)$userId;
157 $row = OptionUserTable::getById($userId);
164 $connection->query(
"DELETE FROM b_im_option_user where USER_ID = " . $userId);
167 OptionGroupTable::query()
169 ->where(
'USER_ID', $userId)
178 $userGroupId = (int)$userGroupId;
179 $accessCode =
'U' . $userId;
181 $connection->query(
"DELETE FROM b_im_option_access WHERE ACCESS_CODE = '$accessCode'");
182 $connection->query(
"DELETE FROM b_im_option_state WHERE GROUP_ID = " . $userGroupId);
183 $connection->query(
"DELETE FROM b_im_option_group WHERE ID = " . $userGroupId);
191 private static function getTopSortGroupIdByAccessCodes(array $accessCodes): int
194 OptionAccessTable::query()
195 ->addSelect(
'GROUP_ID')
196 ->registerRuntimeField(
200 OptionGroupTable::class,
201 Join::on(
'this.GROUP_ID',
'ref.ID')
204 ->whereIn(
'ACCESS_CODE', $accessCodes)
206 'OPTION_GROUP.SORT' =>
'DESC',
213 return $group ? (int)$group[
'GROUP_ID'] :
Configuration::getDefaultPresetId();
221 private static function isSelectedPersonalGroup(
int $userId,
ConditionTree $join): bool
224 OptionUserTable::query()
225 ->addSelect(
'USER_ID')
226 ->registerRuntimeField(
230 OptionAccessTable::class,
232 [
'join_type' => Join::TYPE_INNER]
235 ->where(
'USER_ID', $userId)
236 ->where(
'OPTION_ACCESS.ACCESS_CODE',
'U' . $userId)
239 return $selectedGroup !==
false;
248 private static function updateUserGroups($groupId, $userId): void
250 $notifyJoin = Join::on(
'this.NOTIFY_GROUP_ID',
'ref.GROUP_ID');
251 if (!self::isSelectedPersonalGroup($userId, $notifyJoin))
253 OptionUserTable::update($userId, [
'NOTIFY_GROUP_ID' => $groupId]);
256 $generalJoin = Join::on(
'this.GENERAL_GROUP_ID',
'ref.GROUP_ID');
257 if (!self::isSelectedPersonalGroup($userId, $generalJoin))
259 OptionUserTable::update($userId, [
'GENERAL_GROUP_ID' => $groupId]);
263 private static function findAllAccessCodes(
int $departmentId): array
265 $department =
new Department($departmentId);
266 $fullDepartmentsId = $department->getPathFromHeadToDepartment();
268 if (empty($fullDepartmentsId))
273 return $department->getAccessCodes($fullDepartmentsId);
279 public static function setGroup(
int $userId,
int $groupId): void
282 'USER_ID' => $userId,
283 'GENERAL_GROUP_ID' => $groupId,
284 'NOTIFY_GROUP_ID' => $groupId
287 'GENERAL_GROUP_ID' => $groupId,
288 'NOTIFY_GROUP_ID' => $groupId,
291 OptionUserTable::merge($insertFields, $updateFields);
299 private static function findTopSortPresetId($departmentIds): int
303 foreach ($departmentIds as $departmentId)
305 $accessCodes = array_merge($accessCodes, self::findAllAccessCodes((
int)$departmentId));
307 $accessCodes = array_unique($accessCodes);
310 $baseAccessCode = $topDepartmentId ?
'DR' . $topDepartmentId :
'AU';
312 $accessCodes = !empty($accessCodes) ? $accessCodes : [$baseAccessCode];
314 return self::getTopSortGroupIdByAccessCodes($accessCodes);
static getConnection($name="")