· 5 years ago · Sep 28, 2020, 01:34 PM
1<?php
2use Bitrix\Main\Event;
3use Bitrix\Main\EventResult;
4use \atlant\LKK\CChildsTableModel;
5use \atlant\LKK\CLKKModel;
6use \atlant\API\CApiLoader;
7
8class CUserEventHandler
9{
10
11 public function updateLocalChildList()
12 {
13 global $USER;
14 if (!$USER->IsAuthorized()) {
15 return false;
16 }
17
18 $userId = $USER->GetID();
19 $model = new CChildsTableModel();
20 $model->setUserId($userId);
21
22 $lkkModel = new CLKKModel();
23 $childs = $lkkModel->getChilds();
24
25 if ($childs == false) {
26 return false;
27 }
28
29 $model->clearList();
30
31 foreach ($childs as $child) {
32 $model->add($child["NAME"], $child["BODY"], $child["START_DATE"]);
33 }
34
35 return true;
36 }
37
38 function OnExternalUserCheck(Event $e)
39 {
40 /** @var CUser $USER */
41 global $USER;
42
43 $arData = $e->getParameter("DATA");
44
45 if (empty($arData["SAVEARR"]["XML_ID"])) {
46 return new EventResult(EventResult::ERROR);
47 }
48
49 //vk
50 if (isset($arData["SOCDATA"]["response"][0]["photo_max_orig"])) {
51 $arData["SAVEARR"]["PERSONAL_PHOTO_LINK"] = $arData["SOCDATA"]["response"][0]["photo_max_orig"];
52 }
53 //fb
54 if (isset($arData["SOCDATA"]["picture"]["data"]["url"])) {
55 $arData["SAVEARR"]["PERSONAL_PHOTO_LINK"] = $arData["SOCDATA"]["picture"]["data"]["url"];
56 }
57 //ok
58 if (isset($arData["SOCDATA"]["pic_2"])) {
59 $arData["SAVEARR"]["PERSONAL_PHOTO_LINK"] = $arData["SOCDATA"]["pic_2"];
60 }
61 //google
62 if (isset($arData["SOCDATA"]["picture"])) {
63 $arData["SAVEARR"]["PERSONAL_PHOTO_LINK"] = $arData["SOCDATA"]["picture"];
64 }
65 //ya
66 if (isset($arData["SOCDATA"]["default_avatar_id"])) {
67 $arData["SAVEARR"]["PERSONAL_PHOTO_LINK"] = "https://avatars.yandex.net/get-yapic/" . $arData["SOCDATA"]["default_avatar_id"] . "/islands-200";
68 }
69
70 $currentUserData = [];
71
72 if ($USER->IsAuthorized()) {
73 $currentUserData = $USER->GetByID($USER->GetID())->Fetch();
74 $arData["SAVEARR"]["USER_ID"] = $USER->GetID();
75 }
76
77 if ($currentUserData["UF_API_ID"]) {
78
79 $arToSend["IM"][] = [
80 'VALUE_TYPE' => $arData["SOCID"],
81 'VALUE' => $arData["SAVEARR"]["XML_ID"],
82 ];
83
84 CApiLoader::getInstance()
85 ->flush()
86 ->setUrl("/lkk/user/{$currentUserData["UF_API_ID"]}/")
87 ->setData($arToSend)
88 ->setMethod("PATCH")
89 ->exec();
90
91 CSocServAuthDB::Add( $arData["SAVEARR"]); // adds socialservices entry to table b_socialservices_user
92 //$arUserAPIResult = CSapRequestEvent::mapUserResult(CCRMRequest::getInstance()->getClient($currentUserData["UF_API_ID"])["result"]);
93
94 }else{
95 /*
96 if user not auth find him by soc. network in api, and return api user id,
97 if not fid user in api, register new user and return new user api id, and soc. data.
98 */
99 $arDataSend = [
100 "socialData" => $arData["SAVEARR"],
101 "networkName" => $arData["SOCID"]
102 ];
103
104 $allowField = [
105 "EXTERNAL_AUTH_ID",
106 "XML_ID",
107 "EMAIL",
108 "NAME",
109 "LAST_NAME",
110 "PERSONAL_GENDER",
111 "PERSONAL_BIRTHDAY",
112 "PERSONAL_PHOTO_LINK",
113 "CRM_ID"
114 ];
115 foreach (array_keys($arDataSend["socialData"]) as $key) {
116 if (!in_array($key, $allowField)) {
117 unset($arDataSend["socialData"][$key]);
118 }
119 }
120
121
122 $result = CSapRequest::getInstance()
123 ->setMethod("lkk/auth/social/")
124 ->setAction("POST")
125 ->setSendData($arDataSend)
126 ->send();
127
128
129 if ($result->getType() != EventResult::SUCCESS) {
130 $rewrite["STATUS"] = "SUCCESS_WITH_ERROR";
131 }
132
133 CSapRequestEvent::logSave($result, "lkk/auth/social/", $rewrite);
134
135
136 if ($result->getType() != EventResult::SUCCESS) {
137 return $result;
138 }
139
140 $apiResult = $result->getParameters();
141 $arUserAPIResult = CSapRequestEvent::mapUserResult($apiResult["RESPONSE"]["DATA"]["data"]["user"]);
142 }
143
144// if (empty($arUserAPIResult["UF_API_ID"])) {
145// return new EventResult(EventResult::ERROR);
146// }
147
148 $socialUserData = $apiUserData = [];
149
150 $socialUserID = \Bitrix\Socialservices\UserTable::query()
151 ->setFilter([
152 '=XML_ID' => $arData["SAVEARR"]["XML_ID"],
153 '=EXTERNAL_AUTH_ID' => $arData["SAVEARR"]["EXTERNAL_AUTH_ID"],
154 ])
155 ->setSelect(["ID", "USER_ID"])
156 ->exec()->fetch();
157
158
159 if ($socialUserID) {
160 $socialUserData = $socialUserID["USER_ID"] != $USER->GetID() ? $USER->GetByID($socialUserID["USER_ID"])->Fetch() : $currentUserData;
161 }
162
163
164 if(!empty($arUserAPIResult)){
165 $dbl = \Sh\DBLayer\OrmTableInstance::query("b_uts_user")
166 ->setFilter(["=UF_API_ID" => $arUserAPIResult["UF_API_ID"]])
167 ->setSelect(["USER_ID" => "VALUE_ID"])
168 ->exec();
169
170 while ($res = $dbl->fetch()) {
171
172 if ($res["USER_ID"] == $USER->GetID()) {
173 continue;
174 }
175
176 $apiUserData[$res["USER_ID"]] = $USER->GetByID($res["USER_ID"])->Fetch();
177 }
178
179 if ($arUserAPIResult["EMAIL"]) {
180 $dbl = \Bitrix\Main\UserTable::query()
181 ->setFilter(["=EMAIL" => $arUserAPIResult["EMAIL"]])
182 ->setSelect(["USER_ID" => "ID"])
183 ->exec();
184
185 while ($res = $dbl->fetch()) {
186
187 if ($res["USER_ID"] == $USER->GetID()) {
188 continue;
189 }
190
191 $apiUserData[$res["USER_ID"]] = $USER->GetByID($res["USER_ID"])->Fetch();
192 }
193 }
194 }
195
196
197
198 if ($currentUserData) {
199
200
201 if ($socialUserData && $socialUserData["ID"] != $currentUserData["ID"]) {
202 \Bitrix\Socialservices\UserTable::delete($socialUserID["ID"]);
203 }
204
205
206 foreach ($apiUserData as $userID => $userParams) {
207 $USER->Update($userID, ["UF_API_ID" => "", "UF_SAP_ID" => "", "UF_TOKEN" => "", "EMAIL" => "", "PERSONAL_MOBILE" => "", "LOGIN" => "delUser_" . md5($userID)]);
208 }
209
210
211 //$USER->Update($currentUserData["ID"], $arUserAPIResult);
212
213
214 return new EventResult(EventResult::SUCCESS);
215 }
216
217 $delApiID = $apiUserData;
218 $apiUserData = array_shift($delApiID);
219
220 foreach ($delApiID as $userID => $userParams) {
221 $USER->Update($userID, ["UF_API_ID" => "", "UF_SAP_ID" => "", "UF_TOKEN" => "", "EMAIL" => "", "PERSONAL_MOBILE" => "", "LOGIN" => "delUser_" . md5($userID)]);
222 }
223
224
225 if (empty($apiUserData)) {
226
227 $arUserAPIResult["PASSWORD"] = randString();
228
229
230 $userID = $USER->Add($arUserAPIResult);
231
232 if (empty($userID)) {
233 return new EventResult(EventResult::ERROR);
234 }
235 $apiUserData = $USER->GetByID($userID)->Fetch();
236 } else {
237 AddMessage2Log(var_export([
238 'user update',
239 $apiUserData["ID"],
240 $arUserAPIResult
241 ], true), '', 0);
242
243 $USER->Update($apiUserData["ID"], $arUserAPIResult);
244 }
245
246
247 if ($socialUserData && $socialUserData["ID"] == $apiUserData["ID"]) {
248 return new EventResult(EventResult::SUCCESS);
249 } elseif ($socialUserData && $socialUserData["ID"] != $apiUserData["ID"]) {
250 \Bitrix\Socialservices\UserTable::delete($socialUserID["ID"]);
251 }
252
253
254 $socservUserFields = $arData["SAVEARR"];
255 $socservUserFields["USER_ID"] = $apiUserData["ID"];
256 $result = \Bitrix\Socialservices\UserTable::add(\Bitrix\Socialservices\UserTable::filterFields($socservUserFields));
257 if (!$result->getId()) {
258 return new EventResult(EventResult::ERROR);
259 }
260
261 return new EventResult(EventResult::SUCCESS);
262 }
263
264}