Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
sharinguser.php
1<?php
2
4
8
9final class SharingUser
10{
11 public const EXTERNAL_AUTH_ID = 'calendar_sharing';
12 public const COOKIE_AUTH_HASH_NAME = 'CALENDAR_SHARING_HASH';
13 private static ?SharingUser $instance = null;
14
15 protected function __construct()
16 {
17 }
18
22 public static function getInstance(): SharingUser
23 {
24 if (!self::$instance)
25 {
26 self::$instance = new self();
27 }
28
29 return self::$instance;
30 }
31
40 public function login(bool $needToCreateUser = false, array $userParams = [])
41 {
42 global $USER;
43
44 if (
45 $USER->isAuthorized()
46 && in_array($USER->GetParam('EXTERNAL_AUTH_ID'), ['calendar_sharing', null], true)
47 )
48 {
49 if ($needToCreateUser)
50 {
51 $this->updateUserPersonalInfo($USER->GetID(), $userParams);
52 }
53
54 return $USER->GetID();
55 }
56
57 $user = $this->getByHash();
58
59 if (!$user && $needToCreateUser)
60 {
61 $user = $this->createUser($userParams);
62 }
63
64 if ($user)
65 {
66 $this->saveAuthHashToCookie($user->getXmlId());
67
68 if ($needToCreateUser)
69 {
70 $this->updateUserPersonalInfo($USER->GetID(), $userParams);
71 }
72
73 return $user->getId();
74 }
75
76 return null;
77 }
78
86 public function getAnonymousUserForOwner(array $userParams): ?int
87 {
88 $user = $this->getByHash();
89
90 if (!$user)
91 {
92 $user = $this->createUser($userParams);
93 }
94
95 if ($user)
96 {
97 $this->saveAuthHashToCookie($user->getXmlId());
98
99 $this->updateUserPersonalInfo($user->getId(), $userParams);
100
101 return $user->getId();
102 }
103
104 return null;
105 }
106
113 private function getByHash(): ?\Bitrix\Main\EO_User
114 {
115 $request = Context::getCurrent()->getRequest();
116 $authHash = $request->getCookieRaw(self::COOKIE_AUTH_HASH_NAME);
117
118 if (!($authHash && is_string($authHash)))
119 {
120 return null;
121 }
122 if (!preg_match('/^[0-9a-f]{32}$/', $authHash))
123 {
124 return null;
125 }
126
127 $xmlId = self::EXTERNAL_AUTH_ID . '|' . $authHash;
128
129 return \Bitrix\Main\UserTable::query()
130 ->setSelect(['*'])
131 ->where('ACTIVE', 'Y')
132 ->where('EXTERNAL_AUTH_ID', self::EXTERNAL_AUTH_ID)
133 ->where('XML_ID', $xmlId)
134 ->exec()->fetchObject();
135 }
136
145 private function createUser(array $userParams = []): ?\Bitrix\Main\EO_User
146 {
147 $name = $userParams['NAME'] ?? 'Guest';
148 $lastName = $userParams['LAST_NAME'] ?? '';
149 $personalPhone = '';
150 $personalMailbox = '';
151
152 if (SharingEventManager::isEmailCorrect($userParams['CONTACT_DATA']))
153 {
154 $personalMailbox = $userParams['CONTACT_DATA'];
155 }
156 if (SharingEventManager::isPhoneNumberCorrect($userParams['CONTACT_DATA']))
157 {
158 $personalPhone = $userParams['CONTACT_DATA'];
159 }
160
161 $login = 'calendar_sharing_' . random_int(10000, 99999) . \Bitrix\Main\Security\Random::getString(8);
162 $password = md5($login . '|' . random_int(10000, 99999). '|' . time());
163 $xmlId = self::EXTERNAL_AUTH_ID . '|' . md5($login . $password . time() . \Bitrix\Main\Security\Random::getString(8));
164
165 $userManager = new \CUser();
166 $userId = $userManager->add([
167 'NAME' => $name,
168 'LAST_NAME' => $lastName,
169 'LOGIN' => $login,
170 'PASSWORD' => $password,
171 'CONFIRM_PASSWORD' => $password,
172 'EXTERNAL_AUTH_ID' => self::EXTERNAL_AUTH_ID,
173 'XML_ID' => $xmlId,
174 'ACTIVE' => 'Y',
175 'PERSONAL_PHONE' => $personalPhone,
176 'PERSONAL_MAILBOX' => $personalMailbox,
177 ]);
178
179 if ($userId)
180 {
181 if (Loader::includeModule("socialnetwork"))
182 {
183 \CSocNetUserPerms::SetPerm($userId, 'message', SONET_RELATIONS_TYPE_NONE);
184 }
185
186 return \Bitrix\Main\UserTable::getById($userId)->fetchObject();
187 }
188
189 return null;
190 }
191
197 private function updateUserPersonalInfo(int $userId, array $userParams = []): void
198 {
199 $user = \CUser::GetByID($userId)->Fetch();
200
201 if (($user['EXTERNAL_AUTH_ID'] ?? null) !== 'calendar_sharing')
202 {
203 return;
204 }
205
206 if (
207 $user['NAME'] === $userParams['NAME']
208 && (
209 $user['PERSONAL_PHONE'] === $userParams['CONTACT_DATA']
210 || $user['PERSONAL_MAILBOX'] === $userParams['CONTACT_DATA']
211 )
212 )
213 {
214 return;
215 }
216
217 $name = $userParams['NAME'] ?? 'Guest';
218 $personalPhone = '';
219 $personalMailbox = '';
220
221 if (SharingEventManager::isEmailCorrect($userParams['CONTACT_DATA']))
222 {
223 $personalMailbox = $userParams['CONTACT_DATA'];
224 }
225 if (SharingEventManager::isPhoneNumberCorrect($userParams['CONTACT_DATA']))
226 {
227 $personalPhone = $userParams['CONTACT_DATA'];
228 }
229
230 $userManager = new \CUser();
231 $userManager->update($userId, [
232 'NAME' => $name,
233 'PERSONAL_PHONE' => $personalPhone,
234 'PERSONAL_MAILBOX' => $personalMailbox,
235 ]);
236 }
237
242 private function saveAuthHashToCookie(string $userXmlId): void
243 {
244 $authHash = str_replace(self::EXTERNAL_AUTH_ID . '|', '', $userXmlId);
245 $cookie = new Cookie(self::COOKIE_AUTH_HASH_NAME, $authHash, null, false);
246 Context::getCurrent()->getResponse()->addCookie($cookie);
247 }
248}
getAnonymousUserForOwner(array $userParams)
login(bool $needToCreateUser=false, array $userParams=[])
static getCurrent()
Definition context.php:241