1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Http/ValidationRules/User/HasValidPhoneNumber.php

79 lines
1.8 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
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. 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
}
2022-11-06 21:44:19 +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;
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();
return true;
2023-02-16 02:36:09 +01:00
} catch(\Exception $e) {
return false;
}
2022-11-02 11:30:25 +01:00
}
}