1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-23 01:41:34 +02:00
invoiceninja/app/Http/ValidationRules/User/HasValidPhoneNumber.php

79 lines
1.7 KiB
PHP
Raw Normal View History

2022-11-02 11:30:25 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
2022-11-02 11:30:25 +01:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Http\ValidationRules\User;
use Illuminate\Contracts\Validation\Rule;
/**
* Class HasValidPhoneNumber.
*/
class HasValidPhoneNumber implements Rule
{
public $message;
public function __construct()
{
}
2023-02-16 02:36:09 +01:00
public function message()
{
2022-11-02 23:06:14 +01:00
return [
'phone' => ctrans('texts.phone_validation_error'),
];
}
2022-11-02 11:30:25 +01:00
/**
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
2023-02-16 02:36:09 +01:00
$sid = config('ninja.twilio_account_sid');
$token = config('ninja.twilio_auth_token');
2023-02-16 02:36:09 +01:00
if (!$sid) {
return true;
}
2022-11-02 11:30:25 +01:00
2023-02-16 02:36:09 +01:00
if (is_null($value)) {
2022-11-06 21:44:19 +01:00
return false;
2023-02-16 02:36:09 +01:00
}
2024-01-14 05:05:00 +01:00
2023-02-16 02:36:09 +01:00
$twilio = new \Twilio\Rest\Client($sid, $token);
2022-11-02 11:30:25 +01:00
2023-02-16 02:36:09 +01:00
$country = auth()->user()->account?->companies()?->first()?->country();
2022-11-02 11:30:25 +01:00
2023-02-16 02:36:09 +01:00
if (!$country || strlen(auth()->user()->phone) < 2) {
return true;
}
2022-11-02 11:30:25 +01:00
2023-02-16 02:36:09 +01:00
$countryCode = $country->iso_3166_2;
2024-01-14 05:05:00 +01:00
2023-02-16 02:36:09 +01:00
try {
$phone_number = $twilio->lookups->v1->phoneNumbers($value)
->fetch(["countryCode" => $countryCode]);
2022-11-02 11:30:25 +01:00
$user = auth()->user();
request()->merge(['validated_phone' => $phone_number->phoneNumber ]);
2023-02-16 02:36:09 +01:00
$user->verified_phone_number = false;
2022-11-02 11:30:25 +01:00
$user->save();
2024-01-14 05:05:00 +01:00
2022-11-02 11:30:25 +01:00
return true;
2023-02-16 02:36:09 +01:00
} catch(\Exception $e) {
return false;
}
2022-11-02 11:30:25 +01:00
}
}