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

84 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
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Http\ValidationRules\User;
use App\Models\CompanyUser;
use App\Models\User;
use Illuminate\Contracts\Validation\Rule;
/**
* Class HasValidPhoneNumber.
*/
class HasValidPhoneNumber implements Rule
{
public $message;
public function __construct()
{
}
2022-11-02 23:06:14 +01:00
public function message()
{
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)
{
2022-11-02 11:30:25 +01:00
$sid = config('ninja.twilio_account_sid');
$token = config('ninja.twilio_auth_token');
if(!$sid)
2022-11-02 11:36:57 +01:00
return true;
2022-11-02 11:30:25 +01:00
2022-11-06 21:44:19 +01:00
if(is_null($value))
return false;
2022-11-03 06:45:56 +01:00
$twilio = new \Twilio\Rest\Client($sid, $token);
2022-11-02 11:30:25 +01:00
$country = auth()->user()->account?->companies()?->first()?->country();
2022-11-03 06:45:56 +01:00
if(!$country || strlen(auth()->user()->phone) < 2)
2022-11-02 11:30:25 +01:00
return true;
$countryCode = $country->iso_3166_2;
2022-11-02 11:30:25 +01:00
try{
$phone_number = $twilio->lookups->v1->phoneNumbers($value)
->fetch(["countryCode" => $countryCode]);
$user = auth()->user();
request()->merge(['validated_phone' => $phone_number->phoneNumber ]);
2022-11-09 12:22:52 +01:00
$user->verified_phone_number = false;
2022-11-02 11:30:25 +01:00
$user->save();
return true;
}
catch(\Exception $e) {
return false;
}
}
}