21class Event extends \IRestService
33 \CRestUtil::GLOBAL_SCOPE => array(
34 'events' => array(__CLASS__,
'eventsList'),
35 'event.bind' => array(__CLASS__,
'eventBind'),
36 'event.unbind' => array(__CLASS__,
'eventUnBind'),
37 'event.get' => array(__CLASS__,
'eventGet'),
38 'event.offline.get' => array(__CLASS__,
'eventOfflineGet'),
39 'event.offline.clear' => array(__CLASS__,
'eventOfflineClear'),
40 'event.offline.error' => array(__CLASS__,
'eventOfflineError'),
41 'event.offline.list' => array(__CLASS__,
'eventOfflineList'),
43 'event.test' => array(
44 'callback' => array(__CLASS__,
'eventTest'),
47 \CRestUtil::EVENTS => array(
48 'onOfflineEvent' => array(
50 'onAfterOfflineEventCall',
51 array(EventOfflineTable::class,
'prepareOfflineEvent'),
53 "sendRefreshToken" =>
true,
54 "disableOffline" =>
true,
83 public static function eventsList($query, $n, \CRestServer $server)
85 if($server->getAuthType() !== Auth::AUTH_TYPE)
90 $serviceDescription = $server->getServiceDescription();
92 $scopeList = array(\CRestUtil::GLOBAL_SCOPE);
95 $query = array_change_key_case($query, CASE_UPPER);
97 if(isset($query[
'SCOPE']))
99 if($query[
'SCOPE'] !=
'')
101 $scopeList = array($query[
'SCOPE']);
104 elseif($query[
'FULL'] ==
true)
106 $scopeList = array_keys($serviceDescription);
110 $scopeList = $server->getAuthScope();
111 $scopeList[] = \CRestUtil::GLOBAL_SCOPE;
114 foreach ($serviceDescription as $scope => $scopeMethods)
116 if(in_array($scope, $scopeList) && isset($scopeMethods[\CRestUtil::EVENTS]))
118 $result = array_merge($result, array_keys($scopeMethods[\CRestUtil::EVENTS]));
151 public static function eventBind($query, $n, \CRestServer $server)
155 if($server->getAuthType() !== \
Bitrix\Rest\OAuth\Auth::AUTH_TYPE)
160 $query = array_change_key_case($query, CASE_UPPER);
162 $eventName = ToUpper($query[
'EVENT']);
163 $eventType = ToLower($query[
'EVENT_TYPE']);
164 $eventUser = intval($query[
'AUTH_TYPE']);
165 $eventCallback = $query[
'HANDLER'];
166 $options = is_array($query[
'OPTIONS']) ? $query[
'OPTIONS'] : [];
170 if(!\CRestUtil::isAdmin() && $eventUser !== intval($USER->GetID()))
172 throw new AccessException(
'Event binding with AUTH_TYPE requires administrator access rights');
175 elseif(!\CRestUtil::isAdmin())
177 $eventUser = intval($USER->GetID());
180 $authData = $server->getAuthData();
182 $connectorId = isset($authData[
'auth_connector']) ? $authData[
'auth_connector'] :
'';
203 if(!\CRestUtil::isAdmin())
205 throw new AccessException(
'Offline events binding requires administrator access rights');
220 $scopeList = $server->getAuthScope();
221 $scopeList[] = \CRestUtil::GLOBAL_SCOPE;
223 $serviceDescription = $server->getServiceDescription();
225 foreach($scopeList as $scope)
228 isset($serviceDescription[$scope])
229 && is_array($serviceDescription[$scope][\CRestUtil::EVENTS])
230 && array_key_exists($eventName, $serviceDescription[$scope][\CRestUtil::EVENTS])
233 $eventInfo = $serviceDescription[$scope][\CRestUtil::EVENTS][$eventName];
234 if(is_array($eventInfo))
236 $eventHandlerFields = array(
237 'APP_ID' => $clientInfo[
'ID'],
238 'EVENT_NAME' => $eventName,
239 'EVENT_HANDLER' => $eventCallback,
240 'CONNECTOR_ID' => $connectorId,
246 $eventHandlerFields[
'USER_ID'] = $eventUser;
250 $eventCallback ===
''
251 && isset($eventInfo[3][
'disableOffline'])
252 && $eventInfo[3][
'disableOffline'] ===
true
258 if (!empty($options) && isset($eventInfo[3][
'allowOptions']) && is_array($eventInfo[3][
'allowOptions']))
260 foreach ($eventInfo[3][
'allowOptions'] as $code => $type)
262 if (isset($options[$code]))
266 $eventHandlerFields[
'OPTIONS'][$code] = (int) $options[$code];
268 elseif($type ===
'str' && is_string($options[$code]))
270 $eventHandlerFields[
'OPTIONS'][$code] = $options[$code];
276 $result = EventTable::add($eventHandlerFields);
277 if($result->isSuccess())
279 \Bitrix\Rest\Event\Sender::bind($eventInfo[0], $eventInfo[1]);
283 $errorMessage = $result->getErrorMessages();
328 public static function eventUnbind($query, $n, \CRestServer $server)
332 if($server->getAuthType() !== Auth::AUTH_TYPE)
337 $query = array_change_key_case($query, CASE_UPPER);
339 $eventName = ToUpper($query[
'EVENT']);
340 $eventType = ToLower($query[
'EVENT_TYPE']);
341 $eventCallback = $query[
'HANDLER'];
362 if(!\CRestUtil::isAdmin())
364 throw new AccessException(
'Offline events unbinding requires administrator access rights');
369 elseif($eventCallback ==
'')
377 '=APP_ID' => $clientInfo[
"ID"],
378 '=EVENT_NAME' => $eventName,
379 '=EVENT_HANDLER' => $eventCallback,
384 $authData = $server->getAuthData();
385 $filter[
'=CONNECTOR_ID'] = isset($authData[
'auth_connector']) ? $authData[
'auth_connector'] :
'';
389 if(isset($query[
'AUTH_TYPE']))
391 if(!\CRestUtil::isAdmin() && $query[
'AUTH_TYPE'] !== intval($USER->GetID()))
393 throw new AccessException(
'Event unbinding with AUTH_TYPE requires administrator access rights');
396 $filter[
'=USER_ID'] = intval($query[
'AUTH_TYPE']);
398 elseif(!\CRestUtil::isAdmin())
400 $filter[
'=USER_ID'] = intval($USER->GetID());
404 $dbRes = EventTable::getList(array(
410 while($eventInfo = $dbRes->fetch())
412 $result = EventTable::delete($eventInfo[
"ID"]);
413 if($result->isSuccess())
420 return array(
'count' => $cnt);
424 public static function eventGet($query, $n, \CRestServer $server)
428 if($server->getAuthType() !== Auth::AUTH_TYPE)
438 "=APP_ID" => $clientInfo[
"ID"],
441 if(!\CRestUtil::isAdmin())
443 $filter[
'=USER_ID'] = $USER->GetID();
446 $dbRes = EventTable::getList(array(
452 while($eventHandler = $dbRes->fetch())
454 if($eventHandler[
'EVENT_HANDLER'] <>
'')
457 "event" => $eventHandler[
'EVENT_NAME'],
458 "handler" => $eventHandler[
'EVENT_HANDLER'],
459 "auth_type" => $eventHandler[
'USER_ID'],
466 "event" => $eventHandler[
'EVENT_NAME'],
467 "connector_id" => $eventHandler[
'CONNECTOR_ID'] ===
null ?
'' : $eventHandler[
'CONNECTOR_ID'],
477 public static function eventTest($query, $n, \CRestServer $server)
479 if($server->getAuthType() !== Auth::AUTH_TYPE)
486 foreach(GetModuleEvents(
"rest",
"OnRestAppTest",
true) as $event)
488 ExecuteModuleEventEx($event, array(array(
489 "APP_ID" => $clientInfo[
"ID"],
498 public static function eventOfflineGet($query, $n, \CRestServer $server)
500 if ($server->getAuthType() !== Auth::AUTH_TYPE)
505 if (!\CRestUtil::isAdmin())
510 $query = array_change_key_case($query, CASE_LOWER);
512 $clearEvents = !isset($query[
'clear']) ? 1 : intval($query[
'clear']);
513 $processId = isset($query[
'process_id']) ? trim($query[
'process_id']) : null;
515 if (!$clearEvents && !static::isExtendedModeEnabled())
517 throw new LicenseException(
'extended offline events handling');
520 $filter = isset($query[
'filter']) ? $query[
'filter'] : array();
521 $order = isset($query[
'order']) ? $query[
'order'] : array(
'TIMESTAMP_X' =>
'ASC');
522 $limit = isset($query[
'limit']) ? intval($query[
'limit']) : static::LIST_LIMIT;
524 $getErrors = isset($query[
'error']) && intval($query[
'error']) === 1;
526 $authData = $server->getAuthData();
527 $connectorId = isset($authData[
'auth_connector']) ? $authData[
'auth_connector'] :
'';
529 $returnProcessId = !$clearEvents;
533 throw new Exceptions\ArgumentException(
'Value must be positive integer',
'LIMIT');
536 $queryFilter = static::sanitizeFilter($filter);
538 $order = static::sanitizeOrder($order);
542 $queryFilter[
'=APP_ID'] = $clientInfo[
'ID'];
543 $queryFilter[
'=CONNECTOR_ID'] = $connectorId;
544 $queryFilter[
'=ERROR'] = $getErrors ? 1 : 0;
546 if ($processId ===
null)
548 $queryFilter[
'=PROCESS_ID'] =
'';
553 $returnProcessId =
true;
556 $queryFilter[
'=PROCESS_ID'] = $processId;
558 $dbRes = EventOfflineTable::getList(array(
560 'ID',
'TIMESTAMP_X',
'EVENT_NAME',
'EVENT_DATA',
'EVENT_ADDITIONAL',
'MESSAGE_ID'
562 'filter' => $queryFilter,
569 while ($event = $dbRes->fetch())
572 $ts = $event[
'TIMESTAMP_X'];
574 $event[
'TIMESTAMP_X'] = \CRestUtil::convertDateTime($ts->toString());
576 if (isset($event[
'EVENT_ADDITIONAL'][Auth::PARAM_LOCAL_USER]))
578 $event[
'EVENT_ADDITIONAL'] = [
579 'user_id' => $event[
'EVENT_ADDITIONAL'][Auth::PARAM_LOCAL_USER],
586 if ($clearEvents && count($result) > 0)
592 'process_id' => $returnProcessId ? $processId : null,
599 if ($server->getAuthType() !== Auth::AUTH_TYPE)
604 if (!\CRestUtil::isAdmin())
609 $query = array_change_key_case($query, CASE_LOWER);
611 $processId = isset($query[
'process_id']) ? trim($query[
'process_id']) :
null;
613 $authData = $server->getAuthData();
614 $connectorId = isset($authData[
'auth_connector']) ? $authData[
'auth_connector'] :
'';
616 if ($processId ===
null)
623 if (isset($query[
'message_id']))
626 if (!is_array($query[
'message_id']))
631 foreach($query[
'message_id'] as $messageId)
633 $messageId = trim($messageId);
635 if (mb_strlen($messageId) !== 32)
640 $listIds[] = $messageId;
648 if (isset($query[
'id']))
650 if (!is_array($query[
'id']))
655 foreach($query[
'id'] as $id)
676 if($server->getAuthType() !== Auth::AUTH_TYPE)
681 if(!\CRestUtil::isAdmin())
686 $query = array_change_key_case($query, CASE_LOWER);
688 $processId = isset($query[
'process_id']) ? trim($query[
'process_id']) :
null;
689 $messageId = isset($query[
'message_id']) ? $query[
'message_id'] :
null;
691 $authData = $server->getAuthData();
692 $connectorId = isset($authData[
'auth_connector']) ? $authData[
'auth_connector'] :
'';
694 if($processId ===
null)
699 if(!is_array($messageId))
701 throw new ArgumentException(
'Value must be array of MESSAGE_ID values',
'message_id');
705 if(count($messageId) > 0)
713 public static function eventOfflineList($query, $n, \CRestServer $server)
715 if($server->getAuthType() !== Auth::AUTH_TYPE)
720 if(!\CRestUtil::isAdmin())
725 $query = array_change_key_case($query, CASE_LOWER);
727 $filter = isset($query[
'filter']) ? $query[
'filter'] : array();
728 $order = isset($query[
'order']) ? $query[
'order'] : array(
'ID' =>
'ASC');
730 $authData = $server->getAuthData();
731 $connectorId = isset($authData[
'auth_connector']) ? $authData[
'auth_connector'] :
'';
733 $queryFilter = static::sanitizeFilter($filter, array(
'ID',
'TIMESTAMP_X',
'EVENT_NAME',
'MESSAGE_ID',
'PROCESS_ID',
'ERROR'));
735 $order = static::sanitizeOrder($order, array(
'ID',
'TIMESTAMP_X',
'EVENT_NAME',
'MESSAGE_ID',
'PROCESS_ID',
'ERROR'));
739 $queryFilter[
'=APP_ID'] = $clientInfo[
'ID'];
741 $getEventQuery = EventOfflineTable::query();
743 if ($connectorId ===
'')
745 $getEventQuery->where(
'CONNECTOR_ID',
'');
749 $queryFilter[
'=CONNECTOR_ID'] = $connectorId;
752 $navParams = static::getNavData($n,
true);
755 ->setSelect([
'ID',
'TIMESTAMP_X',
'EVENT_NAME',
'EVENT_DATA',
'EVENT_ADDITIONAL',
'MESSAGE_ID',
'PROCESS_ID',
'ERROR'])
756 ->setFilter($queryFilter)
758 ->setLimit($navParams[
'limit'])
759 ->setOffset($navParams[
'offset']);
762 $dbRes = $getEventQuery->exec();
764 while($event = $dbRes->fetch())
767 $ts = $event[
'TIMESTAMP_X'];
769 $event[
'TIMESTAMP_X'] = \CRestUtil::convertDateTime($ts->toString());
771 if (isset($event[
'EVENT_ADDITIONAL'][Auth::PARAM_LOCAL_USER]))
773 $event[
'EVENT_ADDITIONAL'] = [
774 'user_id' => $event[
'EVENT_ADDITIONAL'][Auth::PARAM_LOCAL_USER],
781 return static::setNavData($result, array(
782 "count" => $getEventQuery->queryCountTotal(),
783 "offset" => $navParams[
'offset']
787 protected static function sanitizeFilter($filter, array $availableFields =
null, $valueCallback =
null, array $availableOperations =
null)
789 static $defaultFields = array(
'ID',
'TIMESTAMP_X',
'EVENT_NAME',
'MESSAGE_ID');
791 if($availableFields ===
null)
793 $availableFields = $defaultFields;
796 return parent::sanitizeFilter(
799 function($field, $value)
814 protected static function sanitizeOrder($order, array $availableFields =
null)
816 static $defaultFields = array(
'ID',
'TIMESTAMP_X',
'EVENT_NAME',
'MESSAGE_ID');
818 if($availableFields ===
null)
820 $availableFields = $defaultFields;
823 return parent::sanitizeOrder($order, $availableFields);
828 return !Loader::includeModule(
'bitrix24')
829 || Feature::isFeatureEnabled(static::FEATURE_EXTENDED_MODE);
static createFromUserTime($timeString)