Skip to content

Commit fee6249

Browse files
committed
Allow verifying phone from form field
1 parent 974e986 commit fee6249

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

core/components/twilio/src/Snippet/SendVerification.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,39 @@ public function process()
2929
$allowedChannels = Utils::explodeAndClean($allowedChannels);
3030
$limit = intval($this->getOption('twilioSendLimit', '15')) * 60; // to minutes
3131

32+
$phone = $this->modx->getPlaceholder('twilio.phone');
33+
34+
$phoneField = $this->getOption('twilioPhoneField', '');
35+
if (!empty($phoneField)) {
36+
$phone = $hook->getValue($phoneField);
37+
if (empty($phone)) {
38+
$hook->addError($phoneField, "Phone is required");
39+
return false;
40+
}
41+
42+
$_SESSION['twilio_phone'] = $phone;
43+
}
44+
3245
$channel = $hook->getValue('channel');
3346
if (!in_array($channel, $allowedChannels)) {
3447
$hook->addError('channel', "Invalid channel");
3548
return false;
3649
}
3750

38-
$username = $this->base64urlDecode($_REQUEST['lu']);
51+
if (empty($phoneField)) {
52+
$username = $this->base64urlDecode($_REQUEST['lu']);
3953

40-
/** @var modUser $user */
41-
$user = $this->modx->getObject(modUser::class, ['username' => $username]);
54+
/** @var \modUser $user */
55+
$user = $this->modx->getObject(modUser::class, ['username' => $username]);
56+
} else {
57+
$user = $this->modx->user;
58+
}
4259

43-
/** @var modUserProfile $profile */
60+
/** @var \modUserProfile $profile */
4461
$profile = $user->getOne('Profile');
4562

4663
$extended = $profile->get('extended');
47-
$lastSend = !empty($extended['twilio_last_send']) ? (int)$extended['twilio_last_send'] : 0;
64+
$lastSend = !empty($extended['twilio_last_send']) ? intval($extended['twilio_last_send']) : 0;
4865
$now = time();
4966

5067
if ($limit !== 0 && $lastSend !== 0 && ($lastSend + $limit) > $now) {
@@ -59,7 +76,7 @@ public function process()
5976

6077
$verification = $twilio->verify->v2->services($this->service)
6178
->verifications
62-
->create($this->modx->getPlaceholder('twilio.phone'), $channel);
79+
->create($phone, $channel);
6380

6481
if ($verification->status !== 'pending') {
6582
$hook->addError('channel', "Requesting verification code failed.");

core/components/twilio/src/Snippet/Verify.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class Verify extends Snippet
1414
private string $token;
1515
private string $service;
1616

17+
private bool $phoneFromSession = false;
18+
1719
public function process()
1820
{
1921
$this->sid = $this->modx->getOption('twilio.account_sid');
@@ -40,6 +42,12 @@ private function verifyPhone(): bool
4042
$hook =& $this->sp['hook'];
4143
$code = $hook->getValue('code');
4244
$phone = $this->modx->getPlaceholder('twilio.phone');
45+
$this->phoneFromSession = intval($this->getOption('twilioPhoneFromSession', '0')) === 1;
46+
$twilioPersistPhone = $this->getOption('twilioPersistPhone', '');
47+
48+
if ($this->phoneFromSession) {
49+
$phone = $_SESSION['twilio_phone'];
50+
}
4351

4452
try {
4553
$twilio = new Client($this->sid, $this->token);
@@ -52,6 +60,7 @@ private function verifyPhone(): bool
5260
/** @var modUser $user */
5361
$user = $this->getUser();
5462

63+
if (empty($twilioPersistPhone)) {
5564
$user->set('active', true);
5665
$user->_fields['cachepwd'] = '';
5766
$user->setDirty('cachepwd');
@@ -60,8 +69,22 @@ private function verifyPhone(): bool
6069
$this->modx->invokeEvent('OnUserActivate', [
6170
'user' => &$user,
6271
]);
72+
} else {
73+
if ($twilioPersistPhone !== 'phone') {
74+
$twilioPersistPhone = 'mobilephone';
75+
}
76+
77+
$profile = $user->getOne('Profile');
78+
$profile->set($twilioPersistPhone, $phone);
79+
$profile->save();
80+
81+
unset($_SESSION['twilio_phone']);
82+
}
83+
84+
if (!$this->phoneFromSession) {
85+
$this->autoLogIn($user);
86+
}
6387

64-
$this->autoLogIn($user);
6588
$this->redirect();
6689

6790
return true;
@@ -129,6 +152,10 @@ private function verifyTotp(): bool
129152

130153
private function getUser()
131154
{
155+
if ($this->phoneFromSession) {
156+
return $this->modx->user;
157+
}
158+
132159
$username = $this->base64urlDecode($_REQUEST['lu']);
133160
/** @var modUser $user */
134161
$user = $this->modx->getObject(modUser::class, ['username' => $username]);

0 commit comments

Comments
 (0)