3namespace Bitrix\Im\Configuration;
5use Bitrix\Im\Model\OptionAccessTable;
6use Bitrix\Im\Model\OptionGroupTable;
7use Bitrix\Im\Model\OptionUserTable;
8use Bitrix\Main\Application;
9use Bitrix\Main\ArgumentException;
10use Bitrix\Main\DB\SqlQueryException;
11use Bitrix\Main\Entity\Query\Filter\ConditionTree;
12use Bitrix\Main\Entity\Query\Join;
13use Bitrix\Main\Loader;
14use Bitrix\Main\LoaderException;
15use Bitrix\Main\ObjectPropertyException;
16use Bitrix\Main\ORM\Fields\Relations\Reference;
17use Bitrix\Main\SystemException;
18use Bitrix\Main\UserTable;
41 $externalAuthId =
$fields[
'EXTERNAL_AUTH_ID'] ??
null;
42 if (in_array($externalAuthId, UserTable::getExternalUserTypes(),
true))
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')
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);
157 $row = OptionUserTable::getById(
$userId);
167 OptionGroupTable::query()
178 $userGroupId = (int)$userGroupId;
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();
224 OptionUserTable::query()
225 ->addSelect(
'USER_ID')
226 ->registerRuntimeField(
230 OptionAccessTable::class,
232 [
'join_type' => Join::TYPE_INNER]
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);
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);
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
static getDefaultPresetId()
static getTopDepartmentId()
static onAfterUserAdd($fields)
static onAfterUserDelete($userId)
static onAfterUserUpdate($fields)
static setGroup(int $userId, int $groupId)
static isSettingsMigrated()
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)