26 self::$instance =
new self();
29 return self::$instance;
40 public function login(
bool $needToCreateUser =
false, array $userParams = [])
46 && in_array($USER->GetParam(
'EXTERNAL_AUTH_ID'), [
'calendar_sharing',
null],
true)
49 if ($needToCreateUser)
51 $this->updateUserPersonalInfo($USER->GetID(), $userParams);
54 return $USER->GetID();
57 $user = $this->getByHash();
59 if (!$user && $needToCreateUser)
61 $user = $this->createUser($userParams);
66 $this->saveAuthHashToCookie($user->getXmlId());
68 if ($needToCreateUser)
70 $this->updateUserPersonalInfo($USER->GetID(), $userParams);
73 return $user->getId();
88 $user = $this->getByHash();
92 $user = $this->createUser($userParams);
97 $this->saveAuthHashToCookie($user->getXmlId());
99 $this->updateUserPersonalInfo($user->getId(), $userParams);
101 return $user->getId();
113 private function getByHash(): ?\
Bitrix\Main\EO_User
116 $authHash = $request->getCookieRaw(self::COOKIE_AUTH_HASH_NAME);
118 if (!($authHash && is_string($authHash)))
122 if (!preg_match(
'/^[0-9a-f]{32}$/', $authHash))
127 $xmlId = self::EXTERNAL_AUTH_ID .
'|' . $authHash;
129 return \Bitrix\Main\UserTable::query()
131 ->where(
'ACTIVE',
'Y')
132 ->where(
'EXTERNAL_AUTH_ID', self::EXTERNAL_AUTH_ID)
133 ->where(
'XML_ID', $xmlId)
134 ->exec()->fetchObject();
145 private function createUser(array $userParams = []): ?\
Bitrix\Main\EO_User
147 $name = $userParams[
'NAME'] ??
'Guest';
148 $lastName = $userParams[
'LAST_NAME'] ??
'';
150 $personalMailbox =
'';
154 $personalMailbox = $userParams[
'CONTACT_DATA'];
158 $personalPhone = $userParams[
'CONTACT_DATA'];
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));
165 $userManager = new \CUser();
168 'LAST_NAME' => $lastName,
170 'PASSWORD' => $password,
171 'CONFIRM_PASSWORD' => $password,
172 'EXTERNAL_AUTH_ID' => self::EXTERNAL_AUTH_ID,
175 'PERSONAL_PHONE' => $personalPhone,
176 'PERSONAL_MAILBOX' => $personalMailbox,
181 if (Loader::includeModule(
"socialnetwork"))
183 \CSocNetUserPerms::SetPerm($userId,
'message', SONET_RELATIONS_TYPE_NONE);
186 return \Bitrix\Main\UserTable::getById($userId)->fetchObject();
197 private function updateUserPersonalInfo(
int $userId, array $userParams = []): void
199 $user = \CUser::GetByID($userId)->Fetch();
201 if (($user[
'EXTERNAL_AUTH_ID'] ??
null) !==
'calendar_sharing')
207 $user[
'NAME'] === $userParams[
'NAME']
209 $user[
'PERSONAL_PHONE'] === $userParams[
'CONTACT_DATA']
210 || $user[
'PERSONAL_MAILBOX'] === $userParams[
'CONTACT_DATA']
217 $name = $userParams[
'NAME'] ??
'Guest';
219 $personalMailbox =
'';
223 $personalMailbox = $userParams[
'CONTACT_DATA'];
227 $personalPhone = $userParams[
'CONTACT_DATA'];
230 $userManager = new \CUser();
231 $userManager->update($userId, [
233 'PERSONAL_PHONE' => $personalPhone,
234 'PERSONAL_MAILBOX' => $personalMailbox,
242 private function saveAuthHashToCookie(
string $userXmlId): void
244 $authHash = str_replace(self::EXTERNAL_AUTH_ID .
'|',
'', $userXmlId);
245 $cookie =
new Cookie(self::COOKIE_AUTH_HASH_NAME, $authHash,
null,
false);