1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
user.php
См. документацию.
1<?php
2
9
10namespace Bitrix\Main;
11
12use Bitrix\HumanResources\Compatibility\Utils\DepartmentBackwardAccessCode;
13use Bitrix\HumanResources\Service\Container;
14use Bitrix\Main\DB\SqlExpression;
15use Bitrix\Main\Localization\Loc;
16use Bitrix\Main\ORM\Data\DataManager;
17use Bitrix\Main\ORM\Fields\BooleanField;
18use Bitrix\Main\ORM\Fields\DateField;
19use Bitrix\Main\ORM\Fields\DatetimeField;
20use Bitrix\Main\ORM\Fields\IntegerField;
21use Bitrix\Main\ORM\Fields\StringField;
22use Bitrix\Main\ORM\Fields\ExpressionField;
23use Bitrix\Main\ORM\Fields\Relations\OneToMany;
24use Bitrix\Main\ORM\Fields\Relations\Reference;
25use Bitrix\Main\ORM\Fields\TextField;
26use Bitrix\Main\ORM\Query\Join;
27use Bitrix\Main\Search\MapBuilder;
28
29Loc::loadMessages(__FILE__);
30
48{
49 public static function getTableName()
50 {
51 return 'b_user';
52 }
53
54 public static function getUfId()
55 {
56 return 'USER';
57 }
58
59 public static function getMap()
60 {
62 $helper = $connection->getSqlHelper();
63
64 return [
65 (new IntegerField('ID'))
66 ->configurePrimary()
67 ->configureAutocomplete(),
68
69 new StringField('LOGIN'),
70
71 (new StringField('PASSWORD'))
72 ->configurePrivate(),
73
74 new StringField('EMAIL'),
75
76 (new BooleanField('ACTIVE'))
77 ->configureValues('N', 'Y'),
78
79 (new BooleanField('BLOCKED'))
80 ->configureValues('N', 'Y'),
81
82 new DatetimeField('DATE_REGISTER'),
83
84 (new ExpressionField(
85 'DATE_REG_SHORT',
86 $helper->getDatetimeToDateFunction('%s'),
87 'DATE_REGISTER')
88 )->configureValueType(DatetimeField::class),
89
90 new DatetimeField('LAST_LOGIN'),
91
92 (new ExpressionField(
93 'LAST_LOGIN_SHORT',
94 $helper->getDatetimeToDateFunction('%s'),
95 'LAST_LOGIN')
96 )->configureValueType(DatetimeField::class),
97
98 new DatetimeField('LAST_ACTIVITY_DATE'),
99
100 new DatetimeField('TIMESTAMP_X'),
101
102 new StringField('NAME'),
103 new StringField('SECOND_NAME'),
104 new StringField('LAST_NAME'),
105 new StringField('TITLE'),
106 new StringField('EXTERNAL_AUTH_ID'),
107 new StringField('XML_ID'),
108 new StringField('BX_USER_ID'),
109 new StringField('CONFIRM_CODE'),
110 new StringField('LID'),
111 (new StringField('LANGUAGE_ID'))
112 ->addValidator(new ORM\Fields\Validators\RegExpValidator('/^[a-z0-9]{2}$/')),
113 new StringField('TIME_ZONE'),
114 new IntegerField('TIME_ZONE_OFFSET'),
115 new StringField('PERSONAL_PROFESSION'),
116 new StringField('PERSONAL_PHONE'),
117 new StringField('PERSONAL_MOBILE'),
118 new StringField('PERSONAL_WWW'),
119 new StringField('PERSONAL_ICQ'),
120 new StringField('PERSONAL_FAX'),
121 new StringField('PERSONAL_PAGER'),
122 new TextField('PERSONAL_STREET'),
123 new StringField('PERSONAL_MAILBOX'),
124 new StringField('PERSONAL_CITY'),
125 new StringField('PERSONAL_STATE'),
126 new StringField('PERSONAL_ZIP'),
127 new StringField('PERSONAL_COUNTRY'),
128 new DateField('PERSONAL_BIRTHDAY'),
129 new StringField('PERSONAL_GENDER'),
130 new IntegerField('PERSONAL_PHOTO'),
131 new TextField('PERSONAL_NOTES'),
132 new StringField('WORK_COMPANY'),
133 new StringField('WORK_DEPARTMENT'),
134 new StringField('WORK_PHONE'),
135 new StringField('WORK_POSITION'),
136 new StringField('WORK_WWW'),
137 new StringField('WORK_FAX'),
138 new StringField('WORK_PAGER'),
139 new TextField('WORK_STREET'),
140 new StringField('WORK_MAILBOX'),
141 new StringField('WORK_CITY'),
142 new StringField('WORK_STATE'),
143 new StringField('WORK_ZIP'),
144 new StringField('WORK_COUNTRY'),
145 new TextField('WORK_PROFILE'),
146 new IntegerField('WORK_LOGO'),
147 new TextField('WORK_NOTES'),
148 new TextField('ADMIN_NOTES'),
149
150 new ExpressionField(
151 'SHORT_NAME',
152 $helper->getConcatFunction(
153 "%s",
154 "' '",
155 "UPPER(" . $helper->getSubstrFunction("%s", 1, 1) . ")", "'.'"
156 ),
157 ['LAST_NAME', 'NAME']
158 ),
159
160 (new ExpressionField(
161 'IS_ONLINE',
162 'CASE WHEN %s > '
163 . $helper->addSecondsToDateTime('(-' . self::getSecondsForLimitOnline() . ')')
164 . ' THEN \'Y\' ELSE \'N\' END',
165 'LAST_ACTIVITY_DATE',
166 ['values' => ['N', 'Y']]
167 ))->configureValueType(BooleanField::class),
168
169 (new ExpressionField(
170 'IS_REAL_USER',
171 'CASE WHEN %s IN (\''
172 . join('\', \'', static::getExternalUserTypes())
173 . '\') THEN \'N\' ELSE \'Y\' END',
174 'EXTERNAL_AUTH_ID',
175 ['values' => ['N', 'Y']]
176 ))->configureValueType(BooleanField::class),
177
178 (new ExpressionField(
179 'REAL_USER',
180 "(
181 %s NOT IN ('" . join("', '", static::getExternalUserTypes()) . "')
182 OR %s IS NULL
183 )",
184 ['EXTERNAL_AUTH_ID', 'EXTERNAL_AUTH_ID'],
185 ))->configureValueType(BooleanField::class),
186
187 (new Reference(
188 'INDEX',
189 UserIndexTable::class,
190 Join::on('this.ID', 'ref.USER_ID')
191 ))->configureJoinType(Join::TYPE_INNER),
192
193 (new Reference(
194 'COUNTER',
195 UserCounterTable::class,
196 Join::on('this.ID', 'ref.USER_ID')->where('ref.CODE', 'tasks_effective')
197 )),
198 (new Reference(
199 'PHONE_AUTH',
200 UserPhoneAuthTable::class,
201 Join::on('this.ID', 'ref.USER_ID')
202 )),
203 (new OneToMany('GROUPS', UserGroupTable::class, 'USER'))
204 ->configureJoinType(Join::TYPE_INNER),
205
206 (new Reference(
207 'ACTIVE_LANGUAGE',
208 \Bitrix\Main\Localization\LanguageTable::class,
209 Join::on('this.LANGUAGE_ID', 'ref.LID')->where('ref.ACTIVE', 'Y')
210 )),
211
212 (new ExpressionField(
213 'NOTIFICATION_LANGUAGE_ID',
214 'CASE WHEN (%s IS NOT NULL AND %s = %s) THEN %s ELSE %s END',
215 [
216 'LANGUAGE_ID', 'LANGUAGE_ID', 'ACTIVE_LANGUAGE.LID', 'LANGUAGE_ID', function () {
217 return new SqlExpression("'" . (SiteTable::getDefaultLanguageId() ?? LANGUAGE_ID) . "'");
218 },
219 ],
220 ))->configureValueType(StringField::class),
221 ];
222 }
223
224 public static function getSecondsForLimitOnline()
225 {
226 $seconds = intval(ini_get("session.gc_maxlifetime"));
227
228 if ($seconds == 0)
229 {
230 $seconds = 1440;
231 }
232 elseif ($seconds < 120)
233 {
234 $seconds = 120;
235 }
236
237 return $seconds;
238 }
239
245 public static function getActiveUsersCount(Type\Date $lastLoginDate = null)
246 {
247 return Application::getInstance()->getLicense()->getActiveUsersCount($lastLoginDate);
248 }
249
250 public static function getUserGroupIds($userId): array
251 {
252 $groups = [];
253
254 // anonymous groups
256 'select' => ['ID'],
257 'filter' => [
258 '=ANONYMOUS' => 'Y',
259 '=ACTIVE' => 'Y',
260 ],
261 'cache' => ['ttl' => 86400],
262 ]);
263
264 while ($row = $result->fetch())
265 {
266 $groups[] = (int)$row['ID'];
267 }
268
269 $groups[] = 2;
270
271 if ($userId > 0)
272 {
273 $nowTimeExpression = new SqlExpression(
274 static::getEntity()->getConnection()->getSqlHelper()->getCurrentDateTimeFunction()
275 );
276
278 'select' => ['ID'],
279 'filter' => [
280 '=UserGroup:GROUP.USER_ID' => $userId,
281 '=ACTIVE' => 'Y',
282 [
283 'LOGIC' => 'OR',
284 '=UserGroup:GROUP.DATE_ACTIVE_FROM' => null,
285 '<=UserGroup:GROUP.DATE_ACTIVE_FROM' => $nowTimeExpression,
286 ],
287 [
288 'LOGIC' => 'OR',
289 '=UserGroup:GROUP.DATE_ACTIVE_TO' => null,
290 '>=UserGroup:GROUP.DATE_ACTIVE_TO' => $nowTimeExpression,
291 ],
292 ],
293 ]);
294
295 while ($row = $result->fetch())
296 {
297 $groups[] = (int)$row['ID'];
298 }
299 }
300
301 $groups = array_unique($groups, SORT_NUMERIC);
302 sort($groups);
303
304 return $groups;
305 }
306
307 public static function getExternalUserTypes()
308 {
309 static $types = [
310 'bot',
311 'email',
312 '__controller',
313 'replica',
314 'imconnector',
315 'sale',
316 'saleanonymous',
317 'shop',
318 'call',
319 'document_editor',
320 'calendar_sharing',
321 ];
322
323 return $types;
324 }
325
331 public static function getIndexedFields(): array
332 {
333 static $fields = [
334 'ID',
335 'NAME',
336 'SECOND_NAME',
337 'LAST_NAME',
338 'WORK_POSITION',
339 'PERSONAL_PROFESSION',
340 'PERSONAL_WWW',
341 'LOGIN',
342 'EMAIL',
343 'PERSONAL_MOBILE',
344 'PERSONAL_PHONE',
345 'PERSONAL_CITY',
346 'PERSONAL_STREET',
347 'PERSONAL_STATE',
348 'PERSONAL_COUNTRY',
349 'PERSONAL_ZIP',
350 'PERSONAL_MAILBOX',
351 'WORK_CITY',
352 'WORK_STREET',
353 'WORK_STATE',
354 'WORK_ZIP',
355 'WORK_COUNTRY',
356 'WORK_MAILBOX',
357 'WORK_PHONE',
358 'WORK_COMPANY',
359 ];
360
361 if (ModuleManager::isModuleInstalled('intranet'))
362 {
363 return array_merge($fields, ['UF_DEPARTMENT']);
364 }
365 return $fields;
366 }
367
374 public static function shouldReindex(array $fields): bool
375 {
376 if (isset($fields['ID']))
377 {
378 unset($fields['ID']);
379 }
380 return !empty(array_intersect(
381 static::getIndexedFields(),
382 array_keys($fields)
383 ));
384 }
385
386 public static function indexRecord($id)
387 {
388 $id = intval($id);
389 if ($id == 0)
390 {
391 return false;
392 }
393
394 $record = parent::getList([
395 'select' => static::getIndexedFields(),
396 'filter' => ['=ID' => $id],
397 ])->fetch();
398
399 if (!is_array($record))
400 {
401 return false;
402 }
403
404 $record['UF_DEPARTMENT_NAMES'] = [];
405 if (
406 Loader::includeModule('humanresources')
407 && isset($record['UF_DEPARTMENT'])
408 && is_array($record['UF_DEPARTMENT'])
409 )
410 {
411 $departments = Container::getNodeRepository()->findAllByAccessCodes(
412 array_map(
413 static fn($departmentId) => DepartmentBackwardAccessCode::makeById((int)$departmentId),
414 $record['UF_DEPARTMENT'],
415 ),
416 );
417
418 foreach ($departments as $department)
419 {
420 $record['UF_DEPARTMENT_NAMES'][] = $department->name;
421 }
422 }
423
424 $departmentName = $record['UF_DEPARTMENT_NAMES'][0] ?? '';
425 $searchDepartmentContent = implode(' ', $record['UF_DEPARTMENT_NAMES']);
426
428 'USER_ID' => $id,
429 'NAME' => (string)$record['NAME'],
430 'SECOND_NAME' => (string)$record['SECOND_NAME'],
431 'LAST_NAME' => (string)$record['LAST_NAME'],
432 'WORK_POSITION' => (string)$record['WORK_POSITION'],
433 'UF_DEPARTMENT_NAME' => (string)$departmentName,
434 'SEARCH_USER_CONTENT' => self::generateSearchUserContent($record),
435 'SEARCH_ADMIN_CONTENT' => self::generateSearchAdminContent($record),
436 'SEARCH_DEPARTMENT_CONTENT' => MapBuilder::create()->addText($searchDepartmentContent)->build(),
437 ]);
438
439 return true;
440 }
441
442 public static function deleteIndexRecord($id)
443 {
444 UserIndexTable::delete($id);
445 }
446
447 private static function generateSearchUserContent(array $fields)
448 {
449 $text = implode(' ', [
450 $fields['NAME'],
451 $fields['LAST_NAME'],
452 $fields['WORK_POSITION'],
453 ]);
454
455 $charsToReplace = ['(', ')', '[', ']', '{', '}', '<', '>', '-', '#', '"', '\''];
456
457 $clearedText = str_replace($charsToReplace, ' ', $text);
458 $clearedText = preg_replace('/\s\s+/', ' ', $clearedText);
459
461 ->addInteger($fields['ID'])
462 ->addText($clearedText)
463 ->build()
464 ;
465
466 return $result;
467 }
468
469 private static function generateSearchAdminContent(array $fields)
470 {
471 $personalCountry = (
472 isset($fields['PERSONAL_COUNTRY'])
473 && intval($fields['PERSONAL_COUNTRY'])
475 'VALUE' => intval($fields['PERSONAL_COUNTRY']),
476 ])
477 : ''
478 );
479 $workCountry = (
480 isset($fields['WORK_COUNTRY'])
481 && intval($fields['WORK_COUNTRY'])
483 'VALUE' => intval($fields['WORK_COUNTRY']),
484 ])
485 : ''
486 );
487 $department = (
488 isset($fields['UF_DEPARTMENT_NAMES'])
489 && is_array($fields['UF_DEPARTMENT_NAMES'])
490 ? implode(' ', $fields['UF_DEPARTMENT_NAMES'])
491 : ''
492 );
493
494 $ufContent = UserUtils::getUFContent($fields['ID']);
495 $tagsContent = UserUtils::getTagsContent($fields['ID']);
496
498 ->addInteger($fields['ID'])
499 ->addText($fields['NAME'])
500 ->addText($fields['SECOND_NAME'])
501 ->addText($fields['LAST_NAME'])
502 ->addEmail($fields['EMAIL'])
503 ->addText($fields['WORK_POSITION'])
504 ->addText($fields['PERSONAL_PROFESSION'])
505 ->addText($fields['PERSONAL_WWW'])
506 ->addText($fields['LOGIN'])
507 ->addPhone($fields['PERSONAL_MOBILE'])
508 ->addPhone($fields['PERSONAL_PHONE'])
509 ->addText($fields['PERSONAL_CITY'])
510 ->addText($fields['PERSONAL_STREET'])
511 ->addText($fields['PERSONAL_STATE'])
512 ->addText($fields['PERSONAL_ZIP'])
513 ->addText($fields['PERSONAL_MAILBOX'])
514 ->addText($fields['WORK_CITY'])
515 ->addText($fields['WORK_STREET'])
516 ->addText($fields['WORK_STATE'])
517 ->addText($fields['WORK_ZIP'])
518 ->addText($fields['WORK_MAILBOX'])
519 ->addPhone($fields['WORK_PHONE'])
520 ->addText($fields['WORK_COMPANY'])
521 ->addText($personalCountry)
522 ->addText($workCountry)
523 ->addText($department)
524 ->addText($ufContent)
525 ->addText($tagsContent)
526 ->build()
527 ;
528
529 return $result;
530 }
531
532 public static function add(array $data)
533 {
534 throw new NotImplementedException("Use CUser class.");
535 }
536
537 public static function update($primary, array $data)
538 {
539 throw new NotImplementedException("Use CUser class.");
540 }
541
542 public static function delete($primary)
543 {
544 throw new NotImplementedException("Use CUser class.");
545 }
546
547 public static function onAfterAdd(ORM\Event $event)
548 {
549 $id = $event->getParameter("id");
550 static::indexRecord($id);
551 return new ORM\EventResult();
552 }
553
554 public static function onAfterUpdate(ORM\Event $event)
555 {
556 $primary = $event->getParameter("id");
557 $id = $primary["ID"];
558 static::indexRecord($id);
559 return new ORM\EventResult();
560 }
561
562 public static function onAfterDelete(ORM\Event $event)
563 {
564 $primary = $event->getParameter("id");
565 $id = $primary["ID"];
566 static::deleteIndexRecord($id);
567 return new ORM\EventResult();
568 }
569
570 public static function postInitialize(ORM\Entity $entity)
571 {
572 // add uts inner reference
573
574 if ($entity->hasField('UTS_OBJECT'))
575 {
577 $leftUtsRef = $entity->getField('UTS_OBJECT');
578
579 $entity->addField((
580 new Reference(
581 'UTS_OBJECT_INNER', $leftUtsRef->getRefEntity(), $leftUtsRef->getReference()
582 ))
583 ->configureJoinType('inner')
584 );
585 }
586 }
587}
$connection
Определения actionsdefinitions.php:38
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static getInstance()
Определения application.php:98
static getConnection($name="")
Определения application.php:638
Определения event.php:5
static includeModule($moduleName)
Определения loader.php:67
static isModuleInstalled($moduleName)
Определения modulemanager.php:125
static getList(array $parameters=array())
Определения datamanager.php:431
static postInitialize(Entity $entity)
Определения datamanager.php:314
static create()
Определения mapbuilder.php:25
static getDefaultLanguageId()
Определения site.php:274
Определения date.php:9
static merge(array $data)
Определения userindex.php:91
Определения user.php:48
static getExternalUserTypes()
Определения user.php:307
static getSecondsForLimitOnline()
Определения user.php:224
static getMap()
Определения user.php:59
static getUfId()
Определения user.php:54
static onAfterDelete(ORM\Event $event)
Определения user.php:562
static shouldReindex(array $fields)
Определения user.php:374
static onAfterAdd(ORM\Event $event)
Определения user.php:547
static getIndexedFields()
Определения user.php:331
static add(array $data)
Определения user.php:532
static onAfterUpdate(ORM\Event $event)
Определения user.php:554
static getActiveUsersCount(Type\Date $lastLoginDate=null)
Определения user.php:245
static indexRecord($id)
Определения user.php:386
static deleteIndexRecord($id)
Определения user.php:442
static update($primary, array $data)
Определения user.php:537
static getTableName()
Определения user.php:49
static getUserGroupIds($userId)
Определения user.php:250
static getTagsContent($userId)
Определения userutils.php:501
static getUFContent($userId)
Определения userutils.php:386
static getCountryValue(array $params=[])
Определения userutils.php:357
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$entity
$groups
Определения options.php:30
Определения ufield.php:9
Определения collection.php:2
$event
Определения prolog_after.php:141
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$text
Определения template_pdf.php:79
$fields
Определения yandex_run.php:501