Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
phoneauth.php
1<?php
9
10use Bitrix\Main;
13
15{
16 const SIGNATURE_SALT = 'phone_auth_sms';
17
18 public function resendCodeAction($signedData)
19 {
20 if(($params = static::extractData($signedData)) === false)
21 {
22 $this->addError(new Main\Error(Loc::getMessage("main_register_incorrect_request"), "ERR_SIGNATURE"));
23 return null;
24 }
25 if($params["phoneNumber"] == '')
26 {
27 $this->addError(new Main\Error(Loc::getMessage("main_register_incorrect_request"), "ERR_PARAMS"));
28 return null;
29 }
30 if($params["smsTemplate"] == '')
31 {
32 $params["smsTemplate"] = "SMS_USER_CONFIRM_NUMBER";
33 }
34
35 $result = \CUser::SendPhoneCode($params["phoneNumber"], $params["smsTemplate"]);
36
37 if(!$result->isSuccess())
38 {
39 $this->addErrors($result->getErrors());
40 return null;
41 }
42
43 return [
44 'DATA_SIGN' => static::signData([
45 'phoneNumber' => $params["phoneNumber"],
46 'smsTemplate' => $params["smsTemplate"]
47 ]),
48 'DATE_SEND' => \CUser::PHONE_CODE_RESEND_INTERVAL,
49 ];
50 }
51
52 public function confirmAction($code, $signedData)
53 {
54 global $USER;
55
56 try
57 {
58 $signer = new Main\Security\Sign\Signer();
59 $userId = $signer->unsign($signedData, static::SIGNATURE_SALT);
60 }
61 catch(\Bitrix\Main\SystemException $exception)
62 {
63 $this->addError(new Main\Error(Loc::getMessage('main_register_incorrect_request'), 'ERR_SIGNATURE'));
64 return null;
65 }
66
67 if(!preg_match('/^[0-9]{6}$/', $code))
68 {
69 $this->addError(new Main\Error(Loc::getMessage('main_err_confirm_code_format'), 'ERR_CONFIRM_CODE'));
70 return null;
71 }
72
73 $phoneRecord = Main\UserPhoneAuthTable::getList([
74 'filter' => [
75 '=USER_ID' => $userId
76 ],
77 'select' => ['USER_ID', 'PHONE_NUMBER', 'USER.ID', 'USER.ACTIVE'],
78 ])->fetchObject();
79
80 if(!$phoneRecord)
81 {
82 $this->addError(new Main\Error(Loc::getMessage('main_register_no_user'), 'ERR_NOT_FOUND'));
83 return null;
84 }
85
86 if(\CUser::VerifyPhoneCode($phoneRecord->getPhoneNumber(), $code))
87 {
88 if($phoneRecord->getUser()->getActive() && !$USER->IsAuthorized())
89 {
90 $USER->Authorize($userId);
91 }
92
93 return true;
94 }
95 else
96 {
97 $this->addError(new Main\Error(Loc::getMessage('main_err_confirm'), 'ERR_CONFIRM_CODE'));
98 return null;
99 }
100 }
101
102 public function configureActions()
103 {
104 return [
105 'resendCode' => [
106 '-prefilters' => [
107 Main\Engine\ActionFilter\Authentication::class,
108 ],
109 ],
110 'confirm' => [
111 '-prefilters' => [
112 Main\Engine\ActionFilter\Authentication::class,
113 ],
114 ],
115 ];
116 }
117
123 public static function signData(array $data)
124 {
125 return Component\ParameterSigner::signParameters(self::SIGNATURE_SALT, $data);
126 }
127
133 public static function extractData($signedData)
134 {
135 try
136 {
137 return Component\ParameterSigner::unsignParameters(self::SIGNATURE_SALT, $signedData);
138 }
139 catch(Main\SystemException $exception)
140 {
141 return false;
142 }
143 }
144}
static extractData($signedData)
confirmAction($code, $signedData)
Definition phoneauth.php:52
static signData(array $data)
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29