mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Validate phone numbers
This commit is contained in:
parent
cececa90a7
commit
270b0106fc
@ -16,6 +16,7 @@ use App\Factory\UserFactory;
|
||||
use App\Http\Requests\Request;
|
||||
use App\Http\ValidationRules\Ninja\CanAddUserRule;
|
||||
use App\Http\ValidationRules\User\AttachableUser;
|
||||
use App\Http\ValidationRules\User\HasValidPhoneNumber;
|
||||
use App\Http\ValidationRules\ValidUserForCompany;
|
||||
use App\Libraries\MultiDB;
|
||||
use App\Models\User;
|
||||
@ -49,6 +50,7 @@ class StoreUserRequest extends Request
|
||||
|
||||
if (Ninja::isHosted()) {
|
||||
$rules['id'] = new CanAddUserRule();
|
||||
$rules['phone'] = ['sometimes', new HasValidPhoneNumber()];
|
||||
}
|
||||
|
||||
return $rules;
|
||||
|
@ -13,9 +13,13 @@ namespace App\Http\Requests\User;
|
||||
|
||||
use App\Http\Requests\Request;
|
||||
use App\Http\ValidationRules\UniqueUserRule;
|
||||
use App\Http\ValidationRules\User\HasValidPhoneNumber;
|
||||
|
||||
class UpdateUserRequest extends Request
|
||||
{
|
||||
|
||||
private bool $phone_has_changed = false;
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
@ -38,6 +42,9 @@ class UpdateUserRequest extends Request
|
||||
$rules['email'] = ['email', 'sometimes', new UniqueUserRule($this->user, $input['email'])];
|
||||
}
|
||||
|
||||
if(Ninja::isHosted() && $this->phone_has_changed)
|
||||
$rules['phone'] = ['sometimes', new HasValidPhoneNumber()];
|
||||
|
||||
return $rules;
|
||||
}
|
||||
|
||||
@ -57,6 +64,10 @@ class UpdateUserRequest extends Request
|
||||
$input['last_name'] = strip_tags($input['last_name']);
|
||||
}
|
||||
|
||||
if(strlen($input['phone']) > 1 && ($this->user->phone != $input['phone']))
|
||||
$this->phone_has_changed = true;
|
||||
|
||||
$this->replace($input);
|
||||
}
|
||||
|
||||
}
|
||||
|
77
app/Http/ValidationRules/User/HasValidPhoneNumber.php
Normal file
77
app/Http/ValidationRules/User/HasValidPhoneNumber.php
Normal file
@ -0,0 +1,77 @@
|
||||
<?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()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function passes($attribute, $value)
|
||||
{
|
||||
|
||||
$sid = config('ninja.twilio_account_sid');
|
||||
$token = config('ninja.twilio_auth_token');
|
||||
|
||||
if(!$sid)
|
||||
return true; // no twilio api credentials provided, bail.
|
||||
|
||||
$twilio = new Twilio\Rest\Client($sid, $token);
|
||||
|
||||
$country = auth()->user()->account?->companies()?->first()?->country();
|
||||
|
||||
if(!$country || strlen($user->phone) < 2)
|
||||
return true;
|
||||
|
||||
$countryCode = $country->iso_3166_2;
|
||||
|
||||
try{
|
||||
|
||||
$phone_number = $twilio->lookups->v1->phoneNumbers($value)
|
||||
->fetch(["countryCode" => $countryCode]);
|
||||
|
||||
$user = auth()->user();
|
||||
$user->phone = $phone_number->phoneNumber;
|
||||
$user->verified_phone_number = true;
|
||||
$user->save();
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
catch(\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function messages()
|
||||
{
|
||||
return [
|
||||
'phone' => ctrans('texts.phone_validation_error'),
|
||||
];
|
||||
}
|
||||
}
|
@ -22,7 +22,6 @@ use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
|
@ -4777,7 +4777,7 @@ $LANG = array(
|
||||
'invoice_task_project' => 'Invoice Task Project',
|
||||
'invoice_task_project_help' => 'Add the project to the invoice line items',
|
||||
'bulk_action' => 'Bulk Action',
|
||||
|
||||
'phone_validation_error' => 'This phone number is not valid, please enter in E.164 format',
|
||||
);
|
||||
|
||||
return $LANG;
|
||||
|
Loading…
x
Reference in New Issue
Block a user