Skip to content

Commit

Permalink
Profile: add phone number verification
Browse files Browse the repository at this point in the history
This forces us to take GB numbers and then we store them in e164 ready for nexmo

fix #210
  • Loading branch information
dpslwk committed May 26, 2019
1 parent 41cdfb3 commit ccc43c3
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 9 deletions.
8 changes: 6 additions & 2 deletions app/HMS/User/ProfileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
use HMS\Entities\User;
use HMS\Entities\Profile;
use Illuminate\Http\Request;
use libphonenumber\RegionCode;
use HMS\Repositories\MetaRepository;
use HMS\Repositories\UserRepository;
use HMS\Repositories\ProfileRepository;
use Propaganistas\LaravelPhone\PhoneNumber;

class ProfileManager
{
Expand Down Expand Up @@ -85,7 +87,8 @@ public function create(
$profile->setAddressCity($addressCity);
$profile->setAddressCounty($addressCounty);
$profile->setAddressPostcode($addressPostcode);
$profile->setContactNumber($contactNumber);
$e164 = PhoneNumber::make($contactNumber, RegionCode::GB)->formatE164();
$profile->setContactNumber($e164);

if (! empty($dateOfBirth)) {
$profile->setDateOfBirth(new Carbon($dateOfBirth));
Expand Down Expand Up @@ -142,7 +145,8 @@ public function updateUserProfileFromRequest(User $user, Request $request)
}

if ($request['contactNumber']) {
$profile->setContactNumber($request['contactNumber']);
$e164 = PhoneNumber::make($request['contactNumber'], RegionCode::GB)->formatE164();
$profile->setContactNumber($e164);
}

// Nullable field
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ protected function validator(array $data)
'addressCity' => 'required|max:100',
'addressCounty' => 'required|max:100',
'addressPostcode' => 'required|max:10',
'contactNumber' => 'required|max:50',
'contactNumber' => 'required|max:50|phone:GB',
'dateOfBirth' => 'nullable|date_format:Y-m-d',
]);
}
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/MembershipController.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public function updateDetails(User $user, Request $request)
'addressCity' => 'required|max:100',
'addressCounty' => 'required|max:100',
'addressPostcode' => 'required|max:10',
'contactNumber' => 'required|max:50',
'contactNumber' => 'required|max:50|phone:GB',
'dateOfBirth' => 'nullable|date_format:Y-m-d',
]);

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function update(Request $request, User $user)
'addressCity' => 'sometimes|required|max:100',
'addressCounty' => 'sometimes|required|max:100',
'addressPostcode' => 'sometimes|required|max:10',
'contactNumber' => 'sometimes|required|max:50',
'contactNumber' => 'sometimes|required|max:50|phone:GB',
'dateOfBirth' => 'sometimes|nullable|date_format:Y-m-d',
'unlockText' => 'sometimes|nullable|max:95',
]);
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"pragmarx/google2fa-laravel": "^1.0",
"pragmarx/recovery": "^0.1.0",
"predis/predis": "^1.1",
"propaganistas/laravel-phone": "^4.2",
"spatie/laravel-cookie-consent": "^2.0",
"tremby/laravel-git-version": "^1.1"
},
Expand Down
237 changes: 236 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 30 additions & 3 deletions database/seeds/ProfileTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

use HMS\Entities\Role;
use HMS\Entities\Profile;
use libphonenumber\RegionCode;
use Illuminate\Database\Seeder;
use HMS\Repositories\RoleRepository;
use HMS\Repositories\UserRepository;
use Faker\Generator as FakerGenerator;
use Doctrine\ORM\EntityManagerInterface;
use libphonenumber\NumberParseException;
use Propaganistas\LaravelPhone\PhoneNumber;

class ProfileTableSeeder extends Seeder
{
Expand All @@ -24,18 +28,29 @@ class ProfileTableSeeder extends Seeder
*/
protected $entityManager;

/**
* @var FakerGenerator
*/
protected $faker;

/**
* Create a new TableSeeder instance.
*
* @param RoleRepository $roleRepository
* @param UserRepository $userRepository,
* @param UserRepository $userRepository
* @param EntityManagerInterface $entityManager
* @param FakerGenerator $faker
*/
public function __construct(RoleRepository $roleRepository, UserRepository $userRepository, EntityManagerInterface $entityManager)
{
public function __construct(
RoleRepository $roleRepository,
UserRepository $userRepository,
EntityManagerInterface $entityManager,
FakerGenerator $faker
) {
$this->roleRepository = $roleRepository;
$this->userRepository = $userRepository;
$this->entityManager = $entityManager;
$this->faker = $faker;
}

/**
Expand Down Expand Up @@ -65,6 +80,18 @@ public function run()
$p = entity(Profile::class)->make(['user' => $user]);
break;
}

// validate and format phoneNumbers
$e164 = null;
do {
try {
$e164 = PhoneNumber::make($this->faker->phoneNumber, RegionCode::GB)->formatE164();
} catch (NumberParseException $e) {
//
}
} while ($e164 == null);
$p->setContactNumber($e164);

$this->entityManager->persist($p);
}
}
Expand Down
Loading

0 comments on commit ccc43c3

Please sign in to comment.