Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
userphoneauth.php
1<?php
9namespace Bitrix\Main;
10
16
34{
35 public static function getTableName()
36 {
37 return 'b_user_phone_auth';
38 }
39
40 public static function getMap()
41 {
42 return array(
43 new Fields\IntegerField("USER_ID", array(
44 'primary' => true,
45 )),
46
47 new Fields\StringField("PHONE_NUMBER", array(
48 'validation' => function() {
49 return array(
50 new Fields\Validators\LengthValidator(1, null, ["MIN" => Loc::getMessage("user_phone_auth_err_number")]),
51 array(__CLASS__, 'validatePhoneNumber'),
52 new Fields\Validators\UniqueValidator(Loc::getMessage("user_phone_auth_err_duplicte")),
53 );
54 }
55 )),
56
57 new Fields\SecretField("OTP_SECRET", array(
58 'crypto_enabled' => static::cryptoEnabled("OTP_SECRET"),
59 )),
60
61 new Fields\IntegerField("ATTEMPTS", array(
62 "default_value" => 0,
63 )),
64
65 new Fields\BooleanField("CONFIRMED", array(
66 "values" => array("N", "Y"),
67 "default_value" => "N",
68 )),
69
70 new Fields\DatetimeField("DATE_SENT"),
71
73 'USER',
74 UserTable::class,
75 Join::on('this.USER_ID', 'ref.ID')
76 ))->configureJoinType('inner'),
77 );
78 }
79
84 public static function validatePhoneNumber($value)
85 {
86 $phoneNumber = PhoneNumber\Parser::getInstance()->parse($value);
87 if($phoneNumber->isValid())
88 {
89 return true;
90 }
91 else
92 {
93 return Loc::getMessage("user_phone_auth_err_incorrect_number");
94 }
95 }
96
97 public static function onBeforeAdd(ORM\Event $event)
98 {
99 return static::modifyFields($event);
100 }
101
102 public static function onBeforeUpdate(ORM\Event $event)
103 {
104 return static::modifyFields($event);
105 }
106
107 protected static function modifyFields(ORM\Event $event)
108 {
109 $fields = $event->getParameter('fields');
110 $result = new ORM\EventResult();
111 $modifiedFields = array();
112
113 if(isset($fields["PHONE_NUMBER"]))
114 {
115 //normalize the number
116 $modifiedFields["PHONE_NUMBER"] = static::normalizePhoneNumber($fields["PHONE_NUMBER"]);
117 }
118
119 $result->modifyFields($modifiedFields);
120
121 return $result;
122 }
123
124 public static function normalizePhoneNumber($number, $defaultCountry = '')
125 {
126 $phoneNumber = PhoneNumber\Parser::getInstance()->parse($number, $defaultCountry);
127 return $phoneNumber->format(PhoneNumber\Format::E164);
128 }
129}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static onBeforeAdd(ORM\Event $event)
static normalizePhoneNumber($number, $defaultCountry='')
static onBeforeUpdate(ORM\Event $event)
static modifyFields(ORM\Event $event)