Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
EventHandler.php
1<?php
2
4
11use Bitrix\Main\Entity\Query\Filter\ConditionTree;
12use Bitrix\Main\Entity\Query\Join;
19use Exception;
20
22{
30 public static function onAfterUserAdd($fields): void
31 {
32 // failed registration or settings were not converted
33 if (
34 $fields['RESULT'] === false
36 )
37 {
38 return;
39 }
40
41 $externalAuthId = $fields['EXTERNAL_AUTH_ID'] ?? null;
42 if (in_array($externalAuthId, UserTable::getExternalUserTypes(), true))
43 {
44 return;
45 }
46
47 $userId = (int)$fields['ID'];
48
49 // there are no departments in BUS
50 if (!isset($fields['UF_DEPARTMENT']) || !Loader::includeModule('intranet'))
51 {
53
54 return;
55 }
56
57 if (!is_array($fields['UF_DEPARTMENT']))
58 {
59 $departmentId = $fields['UF_DEPARTMENT'];
60
61 if (!is_numeric($departmentId))
62 {
64
65 return;
66 }
67
68 $accessCodes = self::findAllAccessCodes((int)$departmentId);
69
70 $topDepartmentId = Department::getTopDepartmentId();
71 $baseAccessCode = $topDepartmentId ? 'DR' . $topDepartmentId : 'AU';
72
73 $accessCodes = !empty($accessCodes) ? $accessCodes : [$baseAccessCode];
74
75 $presetId = self::getTopSortGroupIdByAccessCodes($accessCodes);
76
77 self::setGroup($userId, $presetId);
78
79 return;
80 }
81
82 // no department selected
83 if (!$fields['UF_DEPARTMENT'][0])
84 {
86
87 return;
88 }
89
90 $presetId = self::findTopSortPresetId($fields['UF_DEPARTMENT']);
91
92 self::setGroup($userId, $presetId);
93 }
94
102 public static function onAfterUserUpdate($fields): void
103 {
104 if (
105 !isset($fields['UF_DEPARTMENT'])
106 || !Loader::includeModule('intranet')
108 )
109 {
110 return;
111 }
112
113 $userId = (int)$fields['ID'];
114
115 if (!is_array($fields['UF_DEPARTMENT']))
116 {
117 $departmentId = $fields['UF_DEPARTMENT'];
118 if (!is_numeric($departmentId))
119 {
120 return;
121 }
122
123 $accessCodes = self::findAllAccessCodes((int)$departmentId);
124
125 $topDepartmentId = Department::getTopDepartmentId();
126 $baseAccessCode = $topDepartmentId ? 'DR' . $topDepartmentId : 'AU';
127
128 $accessCodes = !empty($accessCodes) ? $accessCodes : [$baseAccessCode];
129
130 $presetId = self::getTopSortGroupIdByAccessCodes($accessCodes);
131
132 self::updateUserGroups($presetId, $userId);
133
134 return;
135 }
136
137 if (!$fields['UF_DEPARTMENT'][0])
138 {
139 return;
140 }
141
142 $presetId = self::findTopSortPresetId($fields['UF_DEPARTMENT']);
143 self::updateUserGroups($presetId, $userId);
144 }
145
153 public static function onAfterUserDelete($userId): void
154 {
155 $userId = (int)$userId;
156
157 $row = OptionUserTable::getById($userId);
158 if (!$row->fetch())
159 {
160 return;
161 }
162
163 $connection = Application::getConnection();
164 $connection->query("DELETE FROM b_im_option_user where USER_ID = " . $userId);
165
166 $userGroupId =
167 OptionGroupTable::query()
168 ->addSelect('ID')
169 ->where('USER_ID', $userId)
170 ->fetch()['ID']
171 ;
172
173 if (!$userGroupId)
174 {
175 return;
176 }
177
178 $userGroupId = (int)$userGroupId;
179 $accessCode = 'U' . $userId;
180
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);
184 }
185
191 private static function getTopSortGroupIdByAccessCodes(array $accessCodes): int
192 {
193 $group =
194 OptionAccessTable::query()
195 ->addSelect('GROUP_ID')
196 ->registerRuntimeField(
197 'OPTION_GROUP',
198 new Reference(
199 'OPTION_GROUP',
200 OptionGroupTable::class,
201 Join::on('this.GROUP_ID', 'ref.ID')
202 )
203 )
204 ->whereIn('ACCESS_CODE', $accessCodes)
205 ->setOrder([
206 'OPTION_GROUP.SORT' => 'DESC',
207 'GROUP_ID' => 'DESC'
208 ])
209 ->setLimit(1)
210 ->fetch()
211 ;
212
213 return $group ? (int)$group['GROUP_ID'] : Configuration::getDefaultPresetId();
214 }
215
221 private static function isSelectedPersonalGroup(int $userId, ConditionTree $join): bool
222 {
223 $selectedGroup =
224 OptionUserTable::query()
225 ->addSelect('USER_ID')
226 ->registerRuntimeField(
227 'OPTION_ACCESS',
228 new Reference(
229 'OPTION_ACCESS',
230 OptionAccessTable::class,
231 $join,
232 ['join_type' => Join::TYPE_INNER]
233 )
234 )
235 ->where('USER_ID', $userId)
236 ->where('OPTION_ACCESS.ACCESS_CODE', 'U' . $userId)
237 ->fetch();
238
239 return $selectedGroup !== false;
240 }
241
248 private static function updateUserGroups($groupId, $userId): void
249 {
250 $notifyJoin = Join::on('this.NOTIFY_GROUP_ID', 'ref.GROUP_ID');
251 if (!self::isSelectedPersonalGroup($userId, $notifyJoin))
252 {
253 OptionUserTable::update($userId, ['NOTIFY_GROUP_ID' => $groupId]);
254 }
255
256 $generalJoin = Join::on('this.GENERAL_GROUP_ID', 'ref.GROUP_ID');
257 if (!self::isSelectedPersonalGroup($userId, $generalJoin))
258 {
259 OptionUserTable::update($userId, ['GENERAL_GROUP_ID' => $groupId]);
260 }
261 }
262
263 private static function findAllAccessCodes(int $departmentId): array
264 {
265 $department = new Department($departmentId);
266 $fullDepartmentsId = $department->getPathFromHeadToDepartment();
267
268 if (empty($fullDepartmentsId))
269 {
270 return ['AU'];
271 }
272
273 return $department->getAccessCodes($fullDepartmentsId);
274 }
275
279 public static function setGroup(int $userId, int $groupId): void
280 {
281 $insertFields = [
282 'USER_ID' => $userId,
283 'GENERAL_GROUP_ID' => $groupId,
284 'NOTIFY_GROUP_ID' => $groupId
285 ];
286 $updateFields = [
287 'GENERAL_GROUP_ID' => $groupId,
288 'NOTIFY_GROUP_ID' => $groupId,
289 ];
290
291 OptionUserTable::merge($insertFields, $updateFields);
292 }
293
299 private static function findTopSortPresetId($departmentIds): int
300 {
301 $accessCodes = [];
302
303 foreach ($departmentIds as $departmentId)
304 {
305 $accessCodes = array_merge($accessCodes, self::findAllAccessCodes((int)$departmentId));
306 }
307 $accessCodes = array_unique($accessCodes);
308
309 $topDepartmentId = Department::getTopDepartmentId();
310 $baseAccessCode = $topDepartmentId ? 'DR' . $topDepartmentId : 'AU';
311
312 $accessCodes = !empty($accessCodes) ? $accessCodes : [$baseAccessCode];
313
314 return self::getTopSortGroupIdByAccessCodes($accessCodes);
315 }
316}
static setGroup(int $userId, int $groupId)
static getConnection($name="")