Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
recoverycodes.php
1<?php
2namespace Bitrix\Security\Mfa;
3
4
9
10/*
11CREATE TABLE `b_sec_recovery_codes` (
12 `ID` int NOT NULL AUTO_INCREMENT,
13 `USER_ID` int NOT NULL,
14 `CODE` varchar(255) NOT NULL,
15 `USED` varchar(1) NOT NULL,
16 `USING_DATE` DATETIME NULL,
17 `USING_IP` VARCHAR(255) NULL,
18 PRIMARY KEY(`ID`),
19 INDEX ix_b_sec_recovery_codes_user_id (USER_ID)
20)
21 */
22
40 extends Entity\DataManager
41{
42 const CODES_PER_USER = 10;
43 const CODE_PATTERN = '#^[a-z0-9]{4}-[a-z0-9]{4}$#D';
44
50 public static function getTableName()
51 {
52 return 'b_sec_recovery_codes';
53 }
54
60 public static function getMap()
61 {
62 return array(
63 new Entity\IntegerField('ID', array(
64 'primary' => true,
65 'autocomplete' => true
66 )),
67 new Entity\IntegerField('USER_ID', array(
68 'required' => true
69 )),
70 new Entity\StringField('CODE', array(
71 'required' => true,
72 'format' => static::CODE_PATTERN
73 )),
74 new Entity\BooleanField('USED', array(
75 'values' => array('Y', 'N'),
76 'default' => 'N'
77 )),
78 new Entity\DatetimeField('USING_DATE'),
79 new Entity\StringField('USING_IP'),
80 new Entity\ReferenceField(
81 'USER',
82 'Bitrix\Main\User',
83 array('=this.USER_ID' => 'ref.ID'),
84 array('join_type' => 'INNER')
85 ),
86 );
87 }
88
97 public static function clearByUser($userId)
98 {
99 $userId = (int) $userId;
100 if ($userId <= 0)
101 throw new ArgumentTypeException('userId', 'positive integer');
102
103 $codes = static::getList(array(
104 'select' => array('ID'),
105 'filter' => array('=USER_ID' => $userId)
106 ));
107
108 while (($code = $codes->fetch()))
109 {
110 static::delete($code['ID']);
111 }
112
113 return true;
114 }
115
124 public static function regenerateCodes($userId)
125 {
126 $userId = (int) $userId;
127 if ($userId <= 0)
128 throw new ArgumentTypeException('userId', 'positive integer');
129
130 static::clearByUser($userId);
131
132 $randomVector = Random::getString(static::CODES_PER_USER * 8);
133 $randomVector = str_split($randomVector, 4);
134 for ($i = 0; $i < static::CODES_PER_USER; $i++)
135 {
136 $code = array(
137 'USER_ID' => $userId,
138 'USED' => 'N',
139 'CODE' => sprintf('%s-%s', $randomVector[$i * 2], $randomVector[($i * 2) + 1])
140 );
141
142 static::add($code);
143 }
144
145 return true;
146 }
147
157 public static function useCode($userId, $searchCode)
158 {
159 $userId = (int) $userId;
160 if ($userId <= 0)
161 throw new ArgumentTypeException('userId', 'positive integer');
162
163 if (!preg_match(static::CODE_PATTERN, $searchCode))
164 throw new ArgumentTypeException('searchCode', sprintf('string, check pattern "%s"', static::CODE_PATTERN));
165
166 $codes = static::getList(array(
167 'select' => array('ID', 'CODE'),
168 'filter' => array('=USER_ID' => $userId, '=USED' => 'N'),
169 ));
170
171 $found = false;
172 while (($code = $codes->fetch()))
173 {
174 if($code['CODE'] === $searchCode)
175 {
176 static::update($code['ID'], array(
177 'USED' => 'Y',
178 'USING_DATE' => new Type\DateTime,
179 'USING_IP' => \Bitrix\Main\Application::getInstance()->getContext()->getRequest()->getRemoteAddress()
180 ));
181 $found = true;
182 break;
183 }
184 }
185
186 return $found;
187 }
188}
static useCode($userId, $searchCode)