1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
user.php
См. документацию.
1<?php
2namespace Bitrix\Rest\Api;
3
4use Bitrix\Intranet\Invitation;
5use Bitrix\Main\ObjectPropertyException;
6use Bitrix\Main\Loader;
7use Bitrix\Main\ModuleManager;
8use Bitrix\Main\UserTable;
9use Bitrix\Rest\AppTable;
10use Bitrix\Rest\NonLoggedExceptionDecorator;
11use Bitrix\Rest\RestException;
12use Bitrix\Rest\Controller\File;
13use Bitrix\Rest\Exceptions\ArgumentException;
14
15class User extends \IRestService
16{
17 public const SCOPE_USER = 'user';
18 public const SCOPE_USER_BASIC = 'user_basic';
19 public const SCOPE_USER_BRIEF = 'user_brief';
20
21 private const ALLOWED_USER_NAME_FIELDS = [
22 'ID',
23 'XML_ID',
24 'ACTIVE',
25 'NAME',
26 'LAST_NAME',
27 'SECOND_NAME',
28 'TITLE',
29 'IS_ONLINE',
30 'TIME_ZONE',
31 'TIMESTAMP_X',
32 'DATE_REGISTER',
33 'PERSONAL_PROFESSION',
34 'PERSONAL_GENDER',
35 'PERSONAL_BIRTHDAY',
36 'PERSONAL_PHOTO',
37 'PERSONAL_CITY',
38 'PERSONAL_STATE',
39 'PERSONAL_COUNTRY',
40 'WORK_POSITION',
41 'WORK_CITY',
42 'WORK_STATE',
43 'WORK_COUNTRY',
44 'LAST_ACTIVITY_DATE',
45 'UF_EMPLOYMENT_DATE',
46 'UF_TIMEMAN',
47 'UF_SKILLS',
48 'UF_INTERESTS',
49 'UF_DEPARTMENT',
50 'UF_PHONE_INNER',
51 ];
52
53 private const ALLOWED_USER_BASIC_FIELDS = [
54 'ID',
55 'XML_ID',
56 'ACTIVE',
57 'NAME',
58 'LAST_NAME',
59 'SECOND_NAME',
60 'TITLE',
61 'EMAIL',
62 'PERSONAL_PHONE',
63 'WORK_PHONE',
64 'WORK_POSITION',
65 'WORK_COMPANY',
66 'IS_ONLINE',
67 'TIME_ZONE',
68 'TIMESTAMP_X',
69 'DATE_REGISTER',
70 'LAST_ACTIVITY_DATE',
71 'PERSONAL_PROFESSION',
72 'PERSONAL_GENDER',
73 'PERSONAL_BIRTHDAY',
74 'PERSONAL_PHOTO',
75 'PERSONAL_PHOTO',
76 'PERSONAL_PHONE',
77 'PERSONAL_FAX',
78 'PERSONAL_MOBILE',
79 'PERSONAL_PAGER',
80 'PERSONAL_STREET',
81 'PERSONAL_MAILBOX',
82 'PERSONAL_CITY',
83 'PERSONAL_STATE',
84 'PERSONAL_ZIP',
85 'PERSONAL_COUNTRY',
86 'PERSONAL_NOTES',
87 'WORK_COMPANY',
88 'WORK_DEPARTMENT',
89 'WORK_POSITION',
90 'WORK_WWW',
91 'WORK_PHONE',
92 'WORK_FAX',
93 'WORK_PAGER',
94 'WORK_STREET',
95 'WORK_MAILBOX',
96 'WORK_CITY',
97 'WORK_STATE',
98 'WORK_ZIP',
99 'WORK_COUNTRY',
100 'WORK_PROFILE',
101 'WORK_LOGO',
102 'WORK_NOTES',
103 'UF_DEPARTMENT',
104 'UF_DISTRICT',
105 'UF_SKYPE',
106 'UF_SKYPE_LINK',
107 'UF_ZOOM',
108 'UF_TWITTER',
109 'UF_FACEBOOK',
110 'UF_LINKEDIN',
111 'UF_XING',
112 'UF_WEB_SITES',
113 'UF_PHONE_INNER',
114 'UF_EMPLOYMENT_DATE',
115 'UF_TIMEMAN',
116 'UF_SKILLS',
117 'UF_INTERESTS',
118 ];
119
120 private static $entityUser = 'USER';
121 private static $nameFieldFullPrefix = 'UF_USR_';
122 private static $userUserFieldList;
123
124 protected static $allowedUserFields = array(
125 'ID',
126 'XML_ID',
127 'ACTIVE',
128 'NAME',
129 'LAST_NAME',
130 'SECOND_NAME',
131 'TITLE',
132 'EMAIL',
133 'LAST_LOGIN',
134 'DATE_REGISTER',
135 'TIME_ZONE',
136 'IS_ONLINE',
137 'TIMESTAMP_X',
138 'LAST_ACTIVITY_DATE',
139 'PERSONAL_GENDER',
140 'PERSONAL_PROFESSION',
141 'PERSONAL_WWW',
142 'PERSONAL_BIRTHDAY',
143 'PERSONAL_PHOTO',
144 'PERSONAL_ICQ',
145 'PERSONAL_PHONE',
146 'PERSONAL_FAX',
147 'PERSONAL_MOBILE',
148 'PERSONAL_PAGER',
149 'PERSONAL_STREET',
150 'PERSONAL_CITY',
151 'PERSONAL_STATE',
152 'PERSONAL_ZIP',
153 'PERSONAL_COUNTRY',
154 'PERSONAL_MAILBOX',
155 'PERSONAL_NOTES',
156 'PERSONAL_PROFESSION',
157 'PERSONAL_GENDER',
158 'PERSONAL_BIRTHDAY',
159
160 'WORK_PHONE',
161 'WORK_COMPANY',
162 'WORK_POSITION',
163 'WORK_DEPARTMENT',
164 'WORK_WWW',
165 'WORK_FAX',
166 'WORK_PAGER',
167 'WORK_STREET',
168 'WORK_MAILBOX',
169 'WORK_CITY',
170 'WORK_STATE',
171 'WORK_ZIP',
172 'WORK_COUNTRY',
173 'WORK_PROFILE',
174 'WORK_LOGO',
175 'WORK_NOTES',
176
177 'UF_SKYPE_LINK',
178 'UF_ZOOM',
179 'UF_EMPLOYMENT_DATE',
180 'UF_TIMEMAN',
181 'UF_DEPARTMENT',
182 'UF_INTERESTS',
183 'UF_SKILLS',
184 'UF_WEB_SITES',
185 'UF_XING',
186 'UF_LINKEDIN',
187 'UF_FACEBOOK',
188 'UF_TWITTER',
189 'UF_SKYPE',
190 'UF_DISTRICT',
191 'UF_PHONE_INNER',
192 );
193
194 protected static $holdEditFields = [
195 "LAST_LOGIN",
196 "DATE_REGISTER",
197 "IS_ONLINE",
198 ];
199
200 public static function getDefaultAllowedUserFields()
201 {
202 $result = static::$allowedUserFields;
203
204 if (Loader::includeModule('intranet'))
205 {
206 $result[] = 'USER_TYPE';
207 }
208
209 return $result;
210 }
211
212 private static function isMainScope(\CRestServer $server)
213 {
214 return in_array(static::SCOPE_USER, $server->getAuthScope());
215 }
216
217 private static function getErrorScope()
218 {
219 return [
220 'error' => 'insufficient_scope',
221 'error_description' => 'The request requires higher privileges than provided by the access token',
222 ];
223 }
224
225 private static function getAllowedUserFields($scopeList): array
226 {
227 $result = [];
228 if (in_array(static::SCOPE_USER, $scopeList))
229 {
230 $result = static::getDefaultAllowedUserFields();
231 }
232 else
233 {
234 if (in_array(static::SCOPE_USER_BASIC, $scopeList))
235 {
236 $result = static::ALLOWED_USER_BASIC_FIELDS;
237 }
238 elseif (in_array(static::SCOPE_USER_BRIEF, $scopeList))
239 {
240 $result = static::ALLOWED_USER_NAME_FIELDS;
241 }
242
243 if (Loader::includeModule('intranet'))
244 {
245 $result[] = 'USER_TYPE';
246 }
247
248 if (in_array(UserField::SCOPE_USER_USERFIELD, $scopeList))
249 {
250 $result = array_merge($result, static::getUserFields());
251 }
252 }
253
254 return $result;
255 }
256
257 public static function unsetDefaultAllowedUserField($key)
258 {
259 unset(static::$allowedUserFields[$key]);
260 }
261
262 public static function setDefaultAllowedUserField($field)
263 {
264 static::$allowedUserFields[] = $field;
265 }
266
267 public static function onRestServiceBuildDescription()
268 {
269 $result = array(
270 \CRestUtil::GLOBAL_SCOPE => array(
271 'user.admin' => array(__CLASS__, 'isAdmin'),
272 'user.access' => array(__CLASS__, 'hasAccess'),
273 'access.name' => array(__CLASS__, 'getAccess'),
274 ),
275 );
276
278 {
279 $result[static::SCOPE_USER] = array(
280 'user.fields' => array(__CLASS__, 'getFields'),
281 'user.current' => array(__CLASS__, 'userCurrent'),
282 'user.get' => array(__CLASS__, 'userGet'),
283 'user.search' => array(__CLASS__, 'userGet'),
284 'user.add' => array(__CLASS__, 'userAdd'),
285 'user.update' => array(__CLASS__, 'userUpdate'),
286 'user.online' => array(__CLASS__, 'userOnline'),
287 'user.counters' => array(__CLASS__, 'userCounters'),
288 \CRestUtil::EVENTS => array(
289 'OnUserAdd' => array('main', 'OnUserInitialize', array(__CLASS__, 'onUserInitialize')),
290 ),
291 );
292 $result[static::SCOPE_USER_BRIEF] = [
293 'user.fields' => array(__CLASS__, 'getFields'),
294 'user.current' => array(__CLASS__, 'userCurrent'),
295 'user.get' => array(__CLASS__, 'userGet'),
296 'user.search' => array(__CLASS__, 'userGet'),
297 'user.online' => array(__CLASS__, 'userOnline'),
298 'user.counters' => array(__CLASS__, 'userCounters'),
299 \CRestUtil::EVENTS => array(
300 'OnUserAdd' => array('main', 'OnUserInitialize', array(__CLASS__, 'onUserInitialize')),
301 ),
302 ];
303 $result[static::SCOPE_USER_BASIC] = [
304 'user.fields' => array(__CLASS__, 'getFields'),
305 'user.current' => array(__CLASS__, 'userCurrent'),
306 'user.get' => array(__CLASS__, 'userGet'),
307 'user.search' => array(__CLASS__, 'userGet'),
308 'user.online' => array(__CLASS__, 'userOnline'),
309 'user.counters' => array(__CLASS__, 'userCounters'),
310 \CRestUtil::EVENTS => array(
311 'OnUserAdd' => array('main', 'OnUserInitialize', array(__CLASS__, 'onUserInitialize')),
312 ),
313 ];
315 'user.userfield.add' => [UserField::class, 'addRest'],
316 'user.userfield.update' => [UserField::class, 'updateRest'],
317 'user.userfield.delete' => [UserField::class, 'deleteRest'],
318 'user.userfield.list' => [UserField::class, 'getListRest'],
319 'user.userfield.file.get' => [__CLASS__, 'getFile'],
320 ];
321 }
322
323 return $result;
324 }
325
326 private static function getUserFields()
327 {
328 if (is_null(static::$userUserFieldList))
329 {
330 static::$userUserFieldList = [];
331 global $USER_FIELD_MANAGER;
332
333 $fields = $USER_FIELD_MANAGER->GetUserFields("USER");
334
335 foreach ($fields as $code => $field)
336 {
337 if (mb_strpos($code, static::$nameFieldFullPrefix) === 0)
338 {
339 static::$userUserFieldList[] = $code;
340 }
341 }
342 }
343
344 return static::$userUserFieldList;
345 }
346
347 protected static function checkAllowedFields()
348 {
349 global $USER_FIELD_MANAGER;
350
351 $fields = $USER_FIELD_MANAGER->GetUserFields("USER");
352
353 foreach(static::getDefaultAllowedUserFields() as $key => $field)
354 {
355 if(mb_substr($field, 0, 3) === 'UF_' && !array_key_exists($field, $fields))
356 {
357 static::unsetDefaultAllowedUserField($key);
358 }
359 }
360
361 foreach ($fields as $code => $field)
362 {
363 if (mb_strpos($code, static::$nameFieldFullPrefix) === 0)
364 {
365 static::setDefaultAllowedUserField($code);
366 }
367 }
368 }
369
370 public static function onUserInitialize($arParams, $arHandler)
371 {
372 $ID = $arParams[0];
373
374 $dbRes = \CUser::GetByID($ID);
375 $arUser = $dbRes->Fetch();
376
377 if(in_array($arUser['EXTERNAL_AUTH_ID'], UserTable::getExternalUserTypes()))
378 {
379 throw new RestException('Unnecessary event call for this user type');
380 }
381
382 $allowedFields = null;
383 if ($arHandler['APP_ID'] > 0)
384 {
385 $app = AppTable::getByClientId($arHandler['APP_CODE']);
386 if ($app['SCOPE'])
387 {
388 $scope = explode(',', $app['SCOPE']);
389 $allowedFields = static::getAllowedUserFields($scope);
390 }
391 }
392
394 if($arUser['PERSONAL_PHOTO'] > 0)
395 {
396 $arRes['PERSONAL_PHOTO'] = \CRestUtil::GetFile($arUser["PERSONAL_PHOTO"]);
397 }
398
399 return $arRes;
400 }
401
402 public static function isAdmin()
403 {
404 return \CRestUtil::isAdmin();
405 }
406
407 public static function hasAccess($params)
408 {
409 global $USER;
410
411 $params = array_change_key_case($params, CASE_UPPER);
412
413 if(!isset($params['ACCESS']) || !is_array($params['ACCESS']))
414 {
415 $params['ACCESS'] = [$params['ACCESS'] ?? null];
416 }
417
418 return self::isAdmin() || $USER->canAccess($params['ACCESS']);
419 }
420
421 public static function getAccess($params)
422 {
423 $params = array_change_key_case($params, CASE_UPPER);
424
425 if(!isset($params['ACCESS']) || !is_array($params['ACCESS']) || count($params['ACCESS']) <= 0)
426 {
427 return false;
428 }
429 else
430 {
431 $ob = new \CAccess();
432 $res = $ob->getNames($params['ACCESS']);
433 foreach($res as $key => $value)
434 {
435 if(!in_array($key, $params['ACCESS']))
436 unset($res[$key]);
437 }
438
439 return $res;
440 }
441 }
442
443 public static function getFields($query = [], $nav = 0, \CRestServer $server = null)
444 {
445 global $USER_FIELD_MANAGER;
446
447 static::checkAllowedFields();
448
449 $res = array();
450
451 $langMessages = array_merge(
452 IncludeModuleLangFile('/bitrix/modules/main/admin/user_edit.php', false, true),
453 IncludeModuleLangFile('/bitrix/modules/main/admin/user_admin.php', false, true),
454 );
455 $fieldsList = $USER_FIELD_MANAGER->getUserFields('USER', 0, LANGUAGE_ID);
456 if (!is_null($server))
457 {
458 $allowedFields = static::getAllowedUserFields($server->getAuthScope());
459 }
460 else
461 {
462 $allowedFields = static::getDefaultAllowedUserFields();
463 }
464 foreach ($allowedFields as $key)
465 {
466 if(mb_substr($key, 0, 3) != 'UF_')
467 {
468 $lkey = isset($langMessages[$key]) ? $key : str_replace('PERSONAL_', 'USER_', $key);
469 $res[$key] = $langMessages[$lkey] ?? $key;
470 if(mb_substr($res[$key], -1) == ':')
471 {
472 $res[$key] = mb_substr($res[$key], 0, -1);
473 }
474 }
475 else
476 {
477 $res[$key] = $fieldsList[$key]['EDIT_FORM_LABEL'];
478 }
479 }
480
481 return $res;
482 }
483
484 public static function userCurrent($query, $n, \CRestServer $server)
485 {
486 global $USER;
487
488 static::checkAllowedFields();
489
490 $dbRes = \CUser::getByID($USER->getID());
491 $userFields = $dbRes->fetch();
492
493 $allowedFields = static::getAllowedUserFields($server->getAuthScope());
495 if($userFields['PERSONAL_PHOTO'] > 0)
496 {
497 $result['PERSONAL_PHOTO'] = \CRestUtil::GetFile($userFields["PERSONAL_PHOTO"]);
498 }
499
500 $server->setSecurityState(array(
501 "ID" => $result['ID'],
502 "EMAIL" => $result['EMAIL'] ?? '',
503 "NAME" => $result['NAME'],
504 ));
505
506 return $result;
507 }
508
509 public static function userGet($query, $nav = 0, \CRestServer $server = null)
510 {
511 global $USER;
512
513 static::checkAllowedFields();
514
515 static $moduleAdminList = false;
516
517 $query = array_change_key_case($query, CASE_UPPER);
518
519 $sort = $query['SORT'] ?? null;
520 $order = $query['ORDER'] ?? null;
521 $select = $query['SELECT'] ?? null;
522 $adminMode = false;
523
524 //getting resize preset before user data preparing
525 $resizePresets = [
526 "small"=>["width"=>150, "height" => 150],
527 "medium"=>["width"=>300, "height" => 300],
528 "large"=>["width"=>1000, "height" => 1000],
529 ];
530
531 $presetName = $query["IMAGE_RESIZE"] ?? null;
532 $resize = ($presetName && $resizePresets[$presetName]
533 ? $resizePresets[$presetName]
534 : false);
535
536 if (isset($query['ADMIN_MODE']) && $query['ADMIN_MODE'])
537 {
538 if ($moduleAdminList === false && Loader::includeModule('socialnetwork'))
539 {
541 }
542
543 if (is_array($moduleAdminList))
544 {
545 $adminMode = (array_key_exists($USER->getID(), $moduleAdminList));
546 }
547 }
548
549 $allowedUserFields = static::getAllowedUserFields($server->getAuthScope());
550 $allowedUserFields[] = 'IS_ONLINE';
551 $allowedUserFields[] = 'HAS_DEPARTAMENT';
552 $allowedUserFields[] = 'NAME_SEARCH';
553 $allowedUserFields[] = 'EXTERNAL_AUTH_ID';
554 if ($server->getMethod() == "user.search")
555 {
556 $allowedUserFields[] = 'FIND';
557 $allowedUserFields[] = 'UF_DEPARTMENT_NAME';
558 $allowedUserFields[] = 'CONFIRM_CODE';
559 }
560
561 if (isset($query['FILTER']) && is_array($query['FILTER']))
562 {
567 $query = array_change_key_case($query['FILTER'], CASE_UPPER);
568 }
569
570 $filter = self::prepareUserFilter(
571 $query,
573 [
574 'HAS_DEPARTAMENT',
575 'NAME_SEARCH',
576 'FIND',
577 ],
578 );
579
580 if (isset($filter['NAME_SEARCH']) || isset($filter['FIND']))
581 {
582 $nameSearch = $filter['NAME_SEARCH'] ?? $filter['FIND'];
583 unset($filter['NAME_SEARCH']);
584 unset($filter['FIND']);
585
587 'FIND' => $nameSearch,
588 )));
589 }
590 else if ($server->getMethod() == "user.search")
591 {
592 $previousFilter = $filter;
593 unset($filter['NAME']);
594 unset($filter['LAST_NAME']);
595 unset($filter['SECOND_NAME']);
596 unset($filter['WORK_POSITION']);
597 unset($filter['UF_DEPARTMENT_NAME']);
598
600 'NAME' => $previousFilter['NAME'] ?? null,
601 'LAST_NAME' => $previousFilter['LAST_NAME'] ?? null,
602 'SECOND_NAME' => $previousFilter['SECOND_NAME'] ?? null,
603 'WORK_POSITION' => $previousFilter['WORK_POSITION'] ?? null,
604 'UF_DEPARTMENT_NAME' => $previousFilter['UF_DEPARTMENT_NAME'] ?? null,
605 )));
606 }
607
608 if (
609 !$adminMode
610 && Loader::includeModule("extranet")
611 )
612 {
613 $filteredUserIDs = \CExtranet::getMyGroupsUsersSimple(\CExtranet::getExtranetSiteID());
614
615 if (\CExtranet::isIntranetUser())
616 {
617 if (
618 !isset($filter["ID"])
619 || !Loader::includeModule('socialnetwork')
620 || !\CSocNetUser::IsCurrentUserModuleAdmin(\CSite::getDefSite(), false)
621 )
622 {
623 if (!empty($filteredUserIDs))
624 {
625 $filter[] = [
626 'LOGIC' => 'OR',
627 '!UF_DEPARTMENT' => false,
628 'ID' => $filteredUserIDs,
629 ];
630 }
631 else
632 {
633 $filter[] = [
634 'LOGIC' => 'AND',
635 '!UF_DEPARTMENT' => false,
636 ];
637 }
638 }
639 }
640 else
641 {
642 $filteredUserIDs[] = $USER->getId();
643 $filter["ID"] = (isset($filter["ID"]) ? array_intersect((is_array($filter["ID"]) ? $filter["ID"] : array($filter["ID"])), $filteredUserIDs) : $filteredUserIDs);
644 }
645 }
646
647 if (array_key_exists('HAS_DEPARTAMENT', $filter))
648 {
649 if ($filter['HAS_DEPARTAMENT'] === 'Y')
650 {
651 $filter[] = [
652 'LOGIC' => 'AND',
653 '!UF_DEPARTMENT' => false,
654 ];
655 }
656
657 unset($filter['HAS_DEPARTAMENT']);
658 }
659
660 $filter['=IS_REAL_USER'] = 'Y';
661
662 $getListClassName = '\Bitrix\Main\UserTable';
663 if (Loader::includeModule('intranet'))
664 {
665 $getListClassName = '\Bitrix\Intranet\UserTable';
666 }
667 $getListMethodName = 'getList';
668
669 $navParams = self::getNavData($nav, true);
670
671 $querySort = [];
672 if ($sort && $order)
673 {
674 $querySort[$sort] = $order;
675 }
676
677 $allowedFields = static::getAllowedUserFields($server->getAuthScope());
678
679 if (is_array($select) && !empty($select) && !in_array('*', $select, true))
680 {
681 if (in_array('UF_*', $select, true))
682 {
683 $allowedAllUF = array_filter(
685 static fn($value) => $value && str_starts_with($value, 'UF_'),
686 );
687 }
688
689 $allowedFields = array_merge(array_intersect($allowedFields, $select), $allowedAllUF ?? []);
690 }
691
692 $dbRes = $getListClassName::$getListMethodName(
693 [
694 'order' => $querySort,
695 'filter' => $filter,
696 'select' => $allowedFields,
697 'limit' => $navParams['limit'],
698 'offset' => $navParams['offset'],
699 'data_doubling' => false,
700 'count_total' => $nav !== -1,
701 ],
702 );
703
704 $result = [];
705 $files = [];
706
707 while ($userInfo = $dbRes->fetch())
708 {
710
711 if ($userInfo['PERSONAL_PHOTO'] > 0)
712 {
713 $files[] = $userInfo['PERSONAL_PHOTO'];
714 }
715 }
716
717 if (count($files) > 0)
718 {
719 $files = \CRestUtil::getFile($files, $resize);
720
721 foreach ($result as $key => $userInfo)
722 {
723 if (isset($userInfo['PERSONAL_PHOTO']) && $userInfo['PERSONAL_PHOTO'] > 0)
724 {
725 $result[$key]['PERSONAL_PHOTO'] = $files[$userInfo['PERSONAL_PHOTO']];
726 }
727 }
728 }
729
730 if ($result)
731 {
732 $count = 0;
733 if ($nav !== -1)
734 {
735 try
736 {
737 $count = $dbRes->getCount();
738 }
740 {
741 }
742 }
743
744 return self::setNavData(
745 $result,
746 [
747 'count' => $count,
748 'offset' => $navParams['offset'],
749 ],
750 );
751 }
752
753 return $result;
754 }
755
756 public static function userOnline()
757 {
758 $dbRes = UserTable::getList(array(
759 'filter' => array(
760 'IS_ONLINE' => 'Y',
761 ),
762 'select' => array('ID'),
763 ));
764
765 $onlineUsers = array();
766 while($userData = $dbRes->fetch())
767 {
768 $onlineUsers[] = $userData['ID'];
769 }
770
771 return $onlineUsers;
772 }
773
774 public static function userCounters($arParams)
775 {
776 $arParams = array_change_key_case($arParams, CASE_UPPER);
777
778 global $USER;
779
780 $counters = \CUserCounter::GetAllValues($USER->getID());
781
782 if (!isset($arParams['SKIP_LIVEFEED_GROUP']) || $arParams['SKIP_LIVEFEED_GROUP'] != 'Y')
783 {
784 $counters = \CUserCounter::getGroupedCounters($counters);
785 }
786
787 return $counters;
788 }
789
790 public static function userAdd($userFields, $nav = 0, \CRestServer $server = null)
791 {
792 if (!is_null($server) && !static::isMainScope($server))
793 {
794 return static::getErrorScope();
795 }
796
797 global $APPLICATION, $USER;
798
799 static::checkAllowedFields();
800
801 $bB24 = ModuleManager::isModuleInstalled('bitrix24');
802 $res = false;
803
804 if(
805 (
806 $bB24 && $USER->canDoOperation('bitrix24_invite')
807 || $USER->canDoOperation('edit_all_users')
808 )
809 && Loader::includeModule('intranet'))
810 {
811 $userFields = array_change_key_case($userFields, CASE_UPPER);
812
813 $bExtranet = false;
814
815 if (
816 isset($userFields["EXTRANET"])
817 && $userFields["EXTRANET"] == "Y"
818 )
819 {
820 if (IsModuleInstalled('extranet'))
821 {
822 $bExtranet = true;
823 $userFields["UF_DEPARTMENT"] = array();
824
825 if (!empty($userFields["SONET_GROUP_ID"]))
826 {
827 $sonetGroupId = $userFields["SONET_GROUP_ID"];
828 if (!is_array($sonetGroupId))
829 {
830 $sonetGroupId = array($sonetGroupId);
831 }
832
833 unset($userFields["SONET_GROUP_ID"]);
834 }
835 else
836 {
837 throw new ArgumentException('no_sonet_group_for_extranet');
838 }
839 }
840
841 unset($userFields["EXTRANET"]);
842 }
843
844 self::checkTypeFields($userFields);
845
846 $inviteFields = self::prepareSaveData($userFields);
847
848 $userFields["EMAIL"] = trim($userFields["EMAIL"] ?? '');
849 if(check_email($userFields["EMAIL"]))
850 {
852
853 if(\CIntranetInviteDialog::checkUsersCount(1))
854 {
855 if (
856 IsModuleInstalled('extranet')
857 && empty($inviteFields["UF_DEPARTMENT"])
858 && !$bExtranet
859 )
860 {
861 throw new ArgumentException('no_extranet_field');
862 }
863
864 $inviteFields['EMAIL'] = $userFields["EMAIL"];
865 $inviteFields['ACTIVE'] = ($inviteFields['ACTIVE'] ?? 'Y');
866 $inviteFields['GROUP_ID'] = \CIntranetInviteDialog::getUserGroups($siteId, $bExtranet);
867 $inviteFields["CONFIRM_CODE"] = randString(8);
868
869 $ID = \CIntranetInviteDialog::RegisterUser($inviteFields);
870 if(is_array($ID))
871 {
872 throw new ArgumentException(implode("\n", $ID));
873 }
874 elseif($ID > 0)
875 {
876 $obUser = new \CUser;
877 if(!$obUser->update($ID, $inviteFields))
878 {
879 throw new \Exception($obUser->LAST_ERROR);
880 }
881
882 $inviteFields['ID'] = $ID;
883
884 Invitation::add([
885 'USER_ID' => $ID,
886 'TYPE' => Invitation::TYPE_EMAIL
887 ]);
888
889 \CIntranetInviteDialog::InviteUser(
890 $inviteFields,
891 (isset($userFields["MESSAGE_TEXT"])) ? htmlspecialcharsbx($userFields["MESSAGE_TEXT"]) : GetMessage("BX24_INVITE_DIALOG_INVITE_MESSAGE_TEXT_1")
892 );
893
894 if (
895 isset($sonetGroupId)
896 && is_array($sonetGroupId)
897 && \CModule::IncludeModule('socialnetwork')
898 )
899 {
900 foreach($sonetGroupId as $groupId)
901 {
902 if (!\CSocNetUserToGroup::SendRequestToJoinGroup($USER->GetID(), $ID, $groupId, "", false))
903 {
904 if ($e = $APPLICATION->GetException())
905 {
906 throw new \Exception($e->GetString());
907 }
908 }
909 }
910 }
911
912 $res = $ID;
913 }
914 }
915 else
916 {
917 throw new ArgumentException('user_count_exceeded');
918 }
919 }
920 else
921 {
922 throw new ArgumentException('wrong_email');
923 }
924 }
925 else
926 {
927 throw new NonLoggedExceptionDecorator(new \Exception('access_denied'));
928 }
929
930 return $res;
931 }
932
933 private static function checkTypeFields($fields): void
934 {
935 $notStringTypeField = ['PERSONAL_PHOTO', 'WORK_LOGO'];
936 foreach ($fields as $key => $field)
937 {
938 $fieldMustBeString = str_contains($key, 'WORK_')
939 || str_contains($key, 'PERSONAL_')
940 && !in_array($key, $notStringTypeField, true)
941 ;
942
943 if ($fieldMustBeString && !is_string($field))
944 {
945 throw new ArgumentException('invalid_type_field', $key);
946 }
947 }
948 }
949
950 public static function userUpdate($userFields, $nav = 0, \CRestServer $server = null)
951 {
952 if (!is_null($server) && !static::isMainScope($server))
953 {
954 return static::getErrorScope();
955 }
956
957 global $USER;
958
959 static::checkAllowedFields();
960
961 $bB24 = ModuleManager::isModuleInstalled('bitrix24');
962
963 $bAdmin = ($bB24 && $USER->canDoOperation('bitrix24_invite'))
964 || $USER->canDoOperation('edit_all_users');
965
966 $userFields = array_change_key_case($userFields, CASE_UPPER);
967
968 if(isset($userFields['ID']) && $userFields['ID'] > 0)
969 {
970 if($bAdmin || ($USER->getID() == $userFields['ID'] && $USER->CanDoOperation('edit_own_profile')))
971 {
972 $updateFields = self::prepareSaveData($userFields);
973
974 // security
975 if(!$bAdmin)
976 {
977 unset($updateFields['ACTIVE']);
978 unset($updateFields['UF_DEPARTMENT']);
979 }
980 // \security
981
982 $obUser = new \CUser;
983 if(!$obUser->update($userFields['ID'], $updateFields))
984 {
985 throw new NonLoggedExceptionDecorator(new \Exception($obUser->LAST_ERROR));
986 }
987 else
988 {
989 $res = true;
990 }
991 }
992 else
993 {
994 throw new NonLoggedExceptionDecorator(new \Exception('access_denied'));
995 }
996 }
997 else
998 {
999 throw new NonLoggedExceptionDecorator(new \Exception('access_denied'));
1000 }
1001
1002 return $res;
1003 }
1004
1005 private static function prepareUserField($params, $data)
1006 {
1007 $result = $data;
1008 switch ($params['USER_TYPE_ID'])
1009 {
1010 case 'datetime':
1011 $result = \CRestUtil::unConvertDateTime($data);
1012 break;
1013 case 'date':
1014 $result = \CRestUtil::unConvertDate($data);
1015 break;
1016 case 'file':
1017 if (is_array($data))
1018 {
1019 if ($params['MULTIPLE'] === 'N')
1020 {
1021 if (!empty($data['fileData']))
1022 {
1023 $result = \CRestUtil::saveFile($data['fileData']);
1024 $result['old_id'] = $params['VALUE'];
1025 }
1026 $id = isset($data['id']) ? (int)$data['id'] : 0;
1027 $remove = isset($data['remove']) && is_string($data['remove']) && mb_strtoupper($data['remove']) === 'Y';
1028 if ($remove && $id > 0)
1029 {
1030 $result = [
1031 'old_id' => $id,
1032 'del' => 'Y',
1033 ];
1034 }
1035 }
1036 else
1037 {
1038 if ($params['VALUE'])
1039 {
1040 $result = array_merge($result, $params['VALUE']);
1041 }
1042
1043 foreach ($result as $key => $value)
1044 {
1045 if ($value['fileData'])
1046 {
1047 $result[$key] = \CRestUtil::saveFile($value['fileData']);
1048 }
1049 else
1050 {
1051 $id = isset($value['id']) ? (int)$value['id'] : 0;
1052 $remove = isset($value['remove']) && is_string($value['remove']) && mb_strtoupper($value['remove']) === 'Y';
1053 if ($remove && $id > 0)
1054 {
1055 $result[$key] = [
1056 'old_id' => $id,
1057 'del' => 'Y',
1058 ];
1059 }
1060 elseif ($value > 0)
1061 {
1062 $result[$key] = [
1063 'old_id' => $value,
1064 'error' => 'Y',
1065 ];
1066 }
1067 }
1068 }
1069 }
1070 }
1071 break;
1072 }
1073 return $result;
1074 }
1075
1082 protected static function prepareUserData($userData, $allowedUserFields = null)
1083 {
1084 $user = array();
1085
1086 if (!$allowedUserFields)
1087 {
1088 $allowedUserFields = static::getDefaultAllowedUserFields();
1089 }
1090 foreach($userData as $key => $value)
1091 {
1092 if(in_array($key, $allowedUserFields, true))
1093 {
1094 $user[$key] = static::prepareUserValue($key, $value);
1095 }
1096 }
1097
1098 return $user;
1099 }
1100
1101 private static function prepareUserValue($code, $value)
1102 {
1103 switch ($code):
1104 case 'ID':
1105 if (is_array($value) && !empty($value))
1106 {
1107 $value = array_map('intval', $value);
1108 }
1109 else
1110 {
1111 $value = (int)($value);
1112 }
1113 break;
1114 case 'ACTIVE':
1115 case 'IS_ONLINE':
1116 $value = ($value && $value !== 'N')? 'Y' : 'N';
1117 break;
1118 case 'AUTO_TIME_ZONE':
1119 $value = $value === 'Y'? 'Y' : 'N';
1120 break;
1121 case 'PERSONAL_BIRTHDAY':
1122 $value = \CRestUtil::unConvertDate($value);
1123 break;
1124 case 'PERSONAL_PHOTO':
1125
1126 $value = \CRestUtil::saveFile($value);
1127
1128 if(!$value)
1129 {
1130 $value = [
1131 'del' => 'Y',
1132 ];
1133 }
1134 break;
1135 case 'UF_DEPARTMENT':
1136 if(!is_array($value) && !empty($value))
1137 {
1138 $value = [
1139 $value,
1140 ];
1141 }
1142 break;
1143 case 'CONFIRM_CODE':
1144 if($value === '0')
1145 {
1146 $value = false;
1147 }
1148 break;
1149 endswitch;
1150
1151 return $value;
1152 }
1153
1154 private static function prepareUserFilter($query, $allowedUserFields = null, $clearFilterType = []): array
1155 {
1156 $filter = [];
1157
1158 if (!$allowedUserFields)
1159 {
1160 $allowedUserFields = static::getDefaultAllowedUserFields();
1161 }
1162
1163 foreach ($query as $code => $value)
1164 {
1165 $filterType = '';
1166 $matches = [];
1167 if (preg_match('/^([\W]{1,2})(.+)/', $code, $matches) && $matches[2])
1168 {
1169 $filterType = $matches[1];
1170 $code = $matches[2];
1171 }
1172
1173 if (in_array($code, $allowedUserFields, true))
1174 {
1175 if ($filterType !== '' && in_array($code, $clearFilterType, true))
1176 {
1177 $filterType = '';
1178 }
1179 elseif ($filterType === '' && in_array($code, ['USER_TYPE', 'ACTIVE'], true))
1180 {
1181 $filterType = '=';
1182 }
1183
1184 $filter[$filterType . $code] = static::prepareUserValue($code, $value);
1185 }
1186 }
1187
1188 return $filter;
1189 }
1190
1191 protected static function prepareSaveData($userData, $allowedUserFields = null)
1192 {
1193 global $USER_FIELD_MANAGER;
1194 $user = array();
1195
1196 if (!$allowedUserFields)
1197 {
1198 $allowedUserFields = static::getDefaultAllowedUserFields();
1199 }
1200
1201 $userId = (int)($userData['ID'] ?? 0);
1202
1203 $fieldsList = $USER_FIELD_MANAGER->getUserFields('USER', $userId, LANGUAGE_ID);
1204
1205 foreach ($userData as $key => $value)
1206 {
1207 if (in_array($key, $allowedUserFields, true))
1208 {
1209 if (mb_strpos($key, static::$nameFieldFullPrefix) === 0)
1210 {
1211 $user[$key] = static::prepareUserField($fieldsList[$key], $value);
1212 }
1213 else
1214 {
1215 $user[$key] = $value;
1216 }
1217 }
1218 }
1219
1220
1221 if (isset($user['ACTIVE']))
1222 $user['ACTIVE'] = ($user['ACTIVE'] && $user['ACTIVE'] != 'N') ? 'Y' : 'N';
1223
1224 if (isset($user['PERSONAL_BIRTHDAY']))
1225 $user['PERSONAL_BIRTHDAY'] = \CRestUtil::unConvertDate($user['PERSONAL_BIRTHDAY']);
1226
1227 if (!empty($user['UF_DEPARTMENT']) && !is_array($user['UF_DEPARTMENT']))
1228 $user['UF_DEPARTMENT'] = array($user['UF_DEPARTMENT']);
1229
1230 if (isset($user['PERSONAL_PHOTO']))
1231 {
1232 $user['PERSONAL_PHOTO'] = \CRestUtil::saveFile($user['PERSONAL_PHOTO']);
1233
1234 if (!$user['PERSONAL_PHOTO'])
1235 {
1236 $user['PERSONAL_PHOTO'] = array('del' => 'Y');
1237 }
1238 }
1239
1240 $user = array_diff_key($user, array_fill_keys(static::$holdEditFields, 'Y'));
1241
1242 return $user;
1243 }
1244
1245 protected static function getUserData($userFields, $allowedFields = null)
1246 {
1247 static $extranetModuleInstalled = null;
1248 if ($extranetModuleInstalled === null)
1249 {
1250 $extranetModuleInstalled = ModuleManager::isModuleInstalled('extranet');
1251 }
1252 global $USER_FIELD_MANAGER;
1253 $fieldsList = $USER_FIELD_MANAGER->getUserFields(static::$entityUser, 0, LANGUAGE_ID);
1254
1256
1257 $res = array();
1258 if (is_null($allowedFields))
1259 {
1260 $allowedFields = static::getDefaultAllowedUserFields();
1261 }
1262 foreach ($allowedFields as $key)
1263 {
1264 switch ($key)
1265 {
1266 case 'ACTIVE':
1267 $res[$key] = $userFields[$key] == 'Y';
1268 break;
1269 case 'PERSONAL_BIRTHDAY':
1270 case 'DATE_REGISTER':
1271 $res[$key] = \CRestUtil::convertDate($userFields[$key]);
1272 break;
1273 case 'LAST_LOGIN':
1274 $res[$key] = \CRestUtil::convertDateTime($userFields[$key]);
1275 break;
1276 case 'EXTERNAL_AUTH_ID':
1277 $res['IS_NETWORK'] = $userFields[$key] == 'replica';
1278 $res['IS_EMAIL'] = $userFields[$key] == 'email';
1279 unset($userFields[$key]);
1280 break;
1281 default:
1282 if (!empty($fieldsList[$key]))
1283 {
1284 if ($fieldsList[$key]['USER_TYPE_ID'] === 'date')
1285 {
1286 if ($fieldsList[$key]['MULTIPLE'] === 'Y' && is_array($userFields[$key]))
1287 {
1288 foreach ($userFields[$key] as $k => $value)
1289 {
1290 $res[$key][$k] = \CRestUtil::convertDate($userFields[$key][$k]);
1291 }
1292 }
1293 else
1294 {
1295 $res[$key] = \CRestUtil::convertDate($userFields[$key]);
1296 }
1297 }
1298 elseif ($fieldsList[$key]['USER_TYPE_ID'] === 'datetime')
1299 {
1300 if ($fieldsList[$key]['MULTIPLE'] === 'Y' && is_array($userFields[$key]))
1301 {
1302 foreach ($userFields[$key] as $k => $value)
1303 {
1304 $res[$key][$k] = \CRestUtil::convertDateTime($userFields[$key][$k]);
1305 }
1306 }
1307 else
1308 {
1309 $res[$key] = \CRestUtil::convertDateTime($userFields[$key]);
1310 }
1311 }
1312 elseif ($fieldsList[$key]['USER_TYPE_ID'] === 'file')
1313 {
1314 if ($fieldsList[$key]['MULTIPLE'] === 'Y' && is_array($userFields[$key]))
1315 {
1316 foreach ($userFields[$key] as $k => $value)
1317 {
1318 $res[$key][$k] = [
1319 'id' => $userFields[$key][$k],
1320 'showUrl' => $urlManager->create(
1321 'rest.file.get',
1322 [
1323 'entity' => static::$entityUser,
1324 'id' => $userFields['ID'],
1325 'field' => $key,
1326 'value' => $userFields[$key],
1327 ],
1328 ),
1329 'downloadData' => [
1330 'id' => $userFields['ID'],
1331 'field' => $key,
1332 'value' => $userFields[$key][$k],
1333 ],
1334 ];
1335 }
1336 }
1337 else
1338 {
1339 $res[$key] = [
1340 'id' => $userFields[$key],
1341 'showUrl' => $urlManager->create(
1342 'rest.file.get',
1343 [
1344 'entity' => static::$entityUser,
1345 'id' => $userFields['ID'],
1346 'field' => $key,
1347 'value' => $userFields[$key],
1348 ],
1349 ),
1350 'downloadData' => [
1351 'id' => $userFields['ID'],
1352 'field' => $key,
1353 'value' => $userFields[$key],
1354 ],
1355 ];
1356 }
1357 }
1358 }
1359
1360 if (!isset($res[$key]) && isset($userFields[$key]))
1361 {
1362 $res[$key] = $userFields[$key];
1363 }
1364 break;
1365 }
1366 }
1367
1368 return $res;
1369 }
1370
1371 public static function getFile($query, $n, \CRestServer $server)
1372 {
1373 $file = new File();
1374 return $file->getAction(static::$entityUser, $query['id'], $query['field'], $query['value'], $server);
1375 }
1376
1377 protected static function getDefaultSite()
1378 {
1379 return \CSite::getDefSite();
1380 }
1381}
$arParams
Определения access_dialog.php:21
$count
Определения admin_tab.php:4
$allowedFields
Определения push.php:9
global $APPLICATION
Определения include.php:80
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static getInstance()
Определения urlmanager.php:28
static isModuleInstalled($moduleName)
Определения modulemanager.php:125
static getUserSearchFilter(array $fields)
Определения userutils.php:18
const SCOPE_USER_USERFIELD
Определения userfield.php:27
Определения user.php:16
static userOnline()
Определения user.php:756
static onRestServiceBuildDescription()
Определения user.php:267
const SCOPE_USER
Определения user.php:17
static getUserData($userFields, $allowedFields=null)
Определения user.php:1245
const SCOPE_USER_BASIC
Определения user.php:18
static $allowedUserFields
Определения user.php:124
static userCurrent($query, $n, \CRestServer $server)
Определения user.php:484
static checkAllowedFields()
Определения user.php:347
static userCounters($arParams)
Определения user.php:774
static userGet($query, $nav=0, \CRestServer $server=null)
Определения user.php:509
static prepareSaveData($userData, $allowedUserFields=null)
Определения user.php:1191
static getDefaultSite()
Определения user.php:1377
static setDefaultAllowedUserField($field)
Определения user.php:262
static getDefaultAllowedUserFields()
Определения user.php:200
static $holdEditFields
Определения user.php:194
static userAdd($userFields, $nav=0, \CRestServer $server=null)
Определения user.php:790
static unsetDefaultAllowedUserField($key)
Определения user.php:257
static isAdmin()
Определения user.php:402
static hasAccess($params)
Определения user.php:407
static getFields($query=[], $nav=0, \CRestServer $server=null)
Определения user.php:443
static userUpdate($userFields, $nav=0, \CRestServer $server=null)
Определения user.php:950
static getFile($query, $n, \CRestServer $server)
Определения user.php:1371
const SCOPE_USER_BRIEF
Определения user.php:19
static onUserInitialize($arParams, $arHandler)
Определения user.php:370
static prepareUserData($userData, $allowedUserFields=null)
Определения user.php:1082
static getAccess($params)
Определения user.php:421
static getByClientId($clientId)
Определения app.php:967
static getModuleAdminList($siteIdList)
Определения user.php:19
Определения rest.php:24
getAuthScope()
Определения rest.php:329
setSecurityState($state=null)
Определения rest.php:382
Определения rest.php:896
static setNavData($result, $dbRes)
Определения rest.php:927
static getNavData($start, $bORM=false)
Определения rest.php:899
$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
while($group=$gr->Fetch()) $bAdmin
Определения file_new.php:154
$res
Определения filter_act.php:7
global $USER_FIELD_MANAGER
Определения attempt.php:6
$result
Определения get_property_values.php:14
$query
Определения get_search.php:11
if($ajaxMode) $ID
Определения get_user.php:27
$select
Определения iblock_catalog_list.php:194
$filter
Определения iblock_catalog_list.php:54
$app
Определения proxy.php:8
global $USER
Определения csv_new_run.php:40
$navParams
Определения csv_new_run.php:35
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
$siteId
Определения ajax.php:8
IsModuleInstalled($module_id)
Определения tools.php:5301
htmlspecialcharsbx($string, $flags=ENT_COMPAT, $doubleEncode=true)
Определения tools.php:2701
IncludeModuleLangFile($filepath, $lang=false, $bReturnArray=false)
Определения tools.php:3778
GetMessage($name, $aReplace=null)
Определения tools.php:3397
check_email($email, $strict=false, $domainCheck=false)
Определения tools.php:4571
randString($pass_len=10, $pass_chars=false)
Определения tools.php:2154
$value
Определения Param.php:39
Определения Image.php:9
$user
Определения mysql_to_pgsql.php:33
$files
Определения mysql_to_pgsql.php:30
$order
Определения payment.php:8
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if(empty($signedUserToken)) $key
Определения quickway.php:257
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$matches
Определения index.php:22
$counters
Определения options.php:100
$arRes
Определения options.php:104
const SITE_ID
Определения sonet_set_content_view.php:12
$k
Определения template_pdf.php:567
$n
Определения update_log.php:107
$dbRes
Определения yandex_detail.php:168
$fields
Определения yandex_run.php:501