Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
manager.php
1<?php
2
4
5use Bitrix\Calendar\Core;
14
15abstract class Manager extends ServiceBase
16{
21 protected int $userId;
22
23 private static array $httpClients = [];
24
31 public function __construct(Connection $connection, int $userId)
32 {
33 parent::__construct($connection);
34 $this->userId = $userId;
35
36 if (!$this->initHttpClient())
37 {
38 $this->deactivateConnection();
39 }
40 }
41
50 private function initHttpClient(bool $force = false): bool
51 {
52 $success = true;
53 $userId = $this->userId;
54 if (!isset(self::$httpClients[$userId]) || $force)
55 {
56 if (!Loader::includeModule('socialservices'))
57 {
58 throw new SystemException('Module Socialservices not found');
59 }
60
61 $httpClient = new HttpClient();
62 $oAuthEntity = $this->prepareAuthEntity($userId);
63
64 if ($oAuthEntity->getToken())
65 {
66 $httpClient->setHeader('Authorization', 'Bearer ' . $oAuthEntity->getToken());
67 $httpClient->setHeader('Content-Type', 'application/json');
68 $httpClient->setHeader('Referer', \Bitrix\Calendar\Sync\Util\Helper::getDomain());
69 unset($oAuthEntity);
70 }
71 else
72 {
73 $success = false;
74 }
75
76 self::$httpClients[$userId] = new HttpQuery($httpClient, $userId);
77 }
78
79 $this->httpClient = self::$httpClients[$userId];
80
81 return $success;
82 }
83
89 private function prepareAuthEntity(int $userId): \CGoogleOAuthInterface|\CGoogleProxyOAuthInterface
90 {
91 if (\CSocServGoogleProxyOAuth::isProxyAuth())
92 {
93 $oAuth = new \CSocServGoogleProxyOAuth($userId);
94 }
95 else
96 {
97 $oAuth = new \CSocServGoogleOAuth($userId);
98 }
99
100 $oAuthEntity = $oAuth->getEntityOAuth();
101 $oAuthEntity->addScope(Helper::NEED_SCOPE);
102 $oAuthEntity->setUser($userId);
103
104 $tokens = $this->getStorageToken($userId);
105 if ($tokens)
106 {
107 $oAuthEntity->setToken($tokens['OATOKEN']);
108 $oAuthEntity->setAccessTokenExpires($tokens['OATOKEN_EXPIRES']);
109 $oAuthEntity->setRefreshToken($tokens['REFRESH_TOKEN']);
110 }
111
112 if (!$oAuthEntity->checkAccessToken())
113 {
114 $oAuthEntity->getNewAccessToken(
115 $oAuthEntity->getRefreshToken(),
116 $userId,
117 true,
118 );
119 }
120
121 return $oAuthEntity;
122 }
123
124 private function deactivateConnection()
125 {
126 if ($this->connection->getId())
127 {
128 $this->connection
129 ->setStatus('[401] Unauthorized')
130 ->setLastSyncTime(new Core\Base\Date())
131 ;
132
134 $mapperFactory = ServiceLocator::getInstance()->get('calendar.service.mappers.factory');
135 $mapperFactory->getConnection()->update($this->connection);
136
137 Util::addPullEvent('refresh_sync_status', $this->connection->getOwner()->getId(), [
138 'syncInfo' => [
139 'google' => [
140 'status' => false,
141 'type' => $this->connection->getAccountType(),
142 'connected' => true,
143 'id' => $this->connection->getId(),
144 'syncOffset' => 0
145 ],
146 ],
147 'requestUid' => Util::getRequestUid(),
148 ]);
149 }
150 }
151
152 protected function getStorageToken($userId)
153 {
154 return \Bitrix\Socialservices\UserTable::query()
155 ->setSelect(['USER_ID', 'EXTERNAL_AUTH_ID', 'OATOKEN', 'OATOKEN_EXPIRES', 'REFRESH_TOKEN'])
156 ->where('USER_ID', $userId)
157 ->where('EXTERNAL_AUTH_ID', 'GoogleOAuth')
158 ->exec()->fetch()
159 ;
160 }
161
165 protected function isRequestSuccess(): bool
166 {
167 return $this->httpClient->getStatus() === 200;
168 }
169
170 protected function isRequestDeleteSuccess(): bool
171 {
172 $acceptedCodes = [200, 201, 204, 404];
173
174 return in_array($this->httpClient->getStatus(), $acceptedCodes);
175 }
176
182 protected function handleUnauthorize(Connection $connection)
183 {
184 $userId = $connection->getOwner()->getId();
185 $oAuth = $this->prepareAuthEntity($userId);
186 $userTokenInfo = $this->getStorageToken($userId);
187 $refreshResult = false;
188
189 if ($userTokenInfo['REFRESH_TOKEN'])
190 {
191 $refreshResult = $oAuth->getNewAccessToken($userTokenInfo['REFRESH_TOKEN'], $userId, true);
192 }
193
194 if (!$refreshResult)
195 {
196 $this->deactivateConnection();
197 }
198 }
199
207 protected function request($params)
208 {
209 // TODO: implement it
210 }
211}
__construct(Connection $connection, int $userId)
Definition manager.php:31
handleUnauthorize(Connection $connection)
Definition manager.php:182
static addPullEvent(string $command, int $userId, array $params=[])
Definition util.php:373
static getRequestUid()
Definition util.php:520