1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
manager.php
См. документацию.
1<?php
2
3namespace Bitrix\Calendar\Sync\Google;
4
5use Bitrix\Calendar\Core;
6use Bitrix\Calendar\Integration\Pull\PushCommand;
7use Bitrix\Calendar\Sync\Connection\Connection;
8use Bitrix\Calendar\Sync\Managers\ServiceBase;
9use Bitrix\Calendar\Util;
10use Bitrix\Main\DI\ServiceLocator;
11use Bitrix\Main\Loader;
12use Bitrix\Main\LoaderException;
13use Bitrix\Main\SystemException;
14use Bitrix\Main\Web\HttpClient;
15
16abstract class Manager extends ServiceBase
17{
22 protected int $userId;
23
24 private static array $httpClients = [];
25
33 {
34 parent::__construct($connection);
35 $this->userId = $userId;
36
37 if (!$this->initHttpClient())
38 {
39 $this->deactivateConnection();
40 }
41 }
42
51 private function initHttpClient(bool $force = false): bool
52 {
53 $success = true;
55 if (!isset(self::$httpClients[$userId]) || $force)
56 {
57 if (!Loader::includeModule('socialservices'))
58 {
59 throw new SystemException('Module Socialservices not found');
60 }
61
62 $httpClient = new HttpClient();
63 $oAuthEntity = $this->prepareAuthEntity($userId);
64
65 if ($oAuthEntity->getToken())
66 {
67 $httpClient->setHeader('Authorization', 'Bearer ' . $oAuthEntity->getToken());
68 $httpClient->setHeader('Content-Type', 'application/json');
69 $httpClient->setHeader('Referer', \Bitrix\Calendar\Sync\Util\Helper::getDomain());
70 unset($oAuthEntity);
71 }
72 else
73 {
74 $success = false;
75 }
76
77 self::$httpClients[$userId] = new HttpQuery($httpClient, $userId);
78 }
79
80 $this->httpClient = self::$httpClients[$userId];
81
82 return $success;
83 }
84
90 private function prepareAuthEntity(int $userId): \CGoogleOAuthInterface|\CGoogleProxyOAuthInterface
91 {
92 if (\CSocServGoogleProxyOAuth::isProxyAuth())
93 {
94 $oAuth = new \CSocServGoogleProxyOAuth($userId);
95 }
96 else
97 {
98 $oAuth = new \CSocServGoogleOAuth($userId);
99 }
100
101 $oAuthEntity = $oAuth->getEntityOAuth();
102 $oAuthEntity->addScope([
103 'https://www.googleapis.com/auth/calendar',
104 'https://www.googleapis.com/auth/calendar.readonly'
105 ]);
106 $oAuthEntity->removeScope('https://www.googleapis.com/auth/drive');
107
108 $oAuthEntity->setUser($userId);
109
110 $tokens = $this->getStorageToken($userId);
111 if ($tokens)
112 {
113 $oAuthEntity->setToken($tokens['OATOKEN']);
114 $oAuthEntity->setAccessTokenExpires($tokens['OATOKEN_EXPIRES']);
115 $oAuthEntity->setRefreshToken($tokens['REFRESH_TOKEN']);
116 }
117
118 if (!$oAuthEntity->checkAccessToken())
119 {
120 $oAuthEntity->getNewAccessToken(
121 $oAuthEntity->getRefreshToken(),
122 $userId,
123 true,
124 );
125 }
126
127 return $oAuthEntity;
128 }
129
130 private function deactivateConnection()
131 {
132 if ($this->connection->getId())
133 {
134 $this->connection
135 ->setStatus('[401] Unauthorized')
136 ->setLastSyncTime(new Core\Base\Date())
137 ;
138
140 $mapperFactory = ServiceLocator::getInstance()->get('calendar.service.mappers.factory');
141 $mapperFactory->getConnection()->update($this->connection);
142
144 PushCommand::RefreshSyncStatus,
145 $this->connection->getOwner()->getId(),
146 [
147 'syncInfo' => [
148 'google' => [
149 'status' => false,
150 'type' => $this->connection->getAccountType(),
151 'connected' => true,
152 'id' => $this->connection->getId(),
153 'syncOffset' => 0
154 ],
155 ],
156 'requestUid' => Util::getRequestUid(),
157 ]
158 );
159 }
160 }
161
162 protected function getStorageToken($userId)
163 {
164 return \Bitrix\Socialservices\UserTable::query()
165 ->setSelect(['USER_ID', 'EXTERNAL_AUTH_ID', 'OATOKEN', 'OATOKEN_EXPIRES', 'REFRESH_TOKEN'])
166 ->where('USER_ID', $userId)
167 ->where('EXTERNAL_AUTH_ID', 'GoogleOAuth')
168 ->exec()->fetch()
169 ;
170 }
171
175 protected function isRequestSuccess(): bool
176 {
177 return $this->httpClient->getStatus() === 200;
178 }
179
180 protected function isRequestDeleteSuccess(): bool
181 {
182 $acceptedCodes = [200, 201, 204, 404];
183
184 return in_array($this->httpClient->getStatus(), $acceptedCodes);
185 }
186
193 {
194 $userId = $connection->getOwner()->getId();
195 $oAuth = $this->prepareAuthEntity($userId);
196 $userTokenInfo = $this->getStorageToken($userId);
197 $refreshResult = false;
198
199 if ($userTokenInfo['REFRESH_TOKEN'])
200 {
201 $refreshResult = $oAuth->getNewAccessToken($userTokenInfo['REFRESH_TOKEN'], $userId, true);
202 }
203
204 if (!$refreshResult)
205 {
206 $this->deactivateConnection();
207 }
208 }
209
217 protected function request($params)
218 {
219 // TODO: implement it
220 }
221}
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
__construct(Connection $connection, int $userId)
Определения manager.php:32
request($params)
Определения manager.php:217
getStorageToken($userId)
Определения manager.php:162
handleUnauthorize(Connection $connection)
Определения manager.php:192
HttpQuery $httpClient
Определения manager.php:21
static addPullEvent(PushCommand $command, int $userId, array $params=[])
Определения util.php:385
static getRequestUid()
Определения util.php:537
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$success
Определения mail_entry.php:69
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799