Skip to content

Commit ccc43c3

Browse files
committed
Profile: add phone number verification
This forces us to take GB numbers and then we store them in e164 ready for nexmo fix #210
1 parent 41cdfb3 commit ccc43c3

File tree

8 files changed

+277
-9
lines changed

8 files changed

+277
-9
lines changed

app/HMS/User/ProfileManager.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
use HMS\Entities\User;
77
use HMS\Entities\Profile;
88
use Illuminate\Http\Request;
9+
use libphonenumber\RegionCode;
910
use HMS\Repositories\MetaRepository;
1011
use HMS\Repositories\UserRepository;
1112
use HMS\Repositories\ProfileRepository;
13+
use Propaganistas\LaravelPhone\PhoneNumber;
1214

1315
class ProfileManager
1416
{
@@ -85,7 +87,8 @@ public function create(
8587
$profile->setAddressCity($addressCity);
8688
$profile->setAddressCounty($addressCounty);
8789
$profile->setAddressPostcode($addressPostcode);
88-
$profile->setContactNumber($contactNumber);
90+
$e164 = PhoneNumber::make($contactNumber, RegionCode::GB)->formatE164();
91+
$profile->setContactNumber($e164);
8992

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

144147
if ($request['contactNumber']) {
145-
$profile->setContactNumber($request['contactNumber']);
148+
$e164 = PhoneNumber::make($request['contactNumber'], RegionCode::GB)->formatE164();
149+
$profile->setContactNumber($e164);
146150
}
147151

148152
// Nullable field

app/Http/Controllers/Auth/RegisterController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ protected function validator(array $data)
8686
'addressCity' => 'required|max:100',
8787
'addressCounty' => 'required|max:100',
8888
'addressPostcode' => 'required|max:10',
89-
'contactNumber' => 'required|max:50',
89+
'contactNumber' => 'required|max:50|phone:GB',
9090
'dateOfBirth' => 'nullable|date_format:Y-m-d',
9191
]);
9292
}

app/Http/Controllers/MembershipController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ public function updateDetails(User $user, Request $request)
237237
'addressCity' => 'required|max:100',
238238
'addressCounty' => 'required|max:100',
239239
'addressPostcode' => 'required|max:10',
240-
'contactNumber' => 'required|max:50',
240+
'contactNumber' => 'required|max:50|phone:GB',
241241
'dateOfBirth' => 'nullable|date_format:Y-m-d',
242242
]);
243243

app/Http/Controllers/UserController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function update(Request $request, User $user)
119119
'addressCity' => 'sometimes|required|max:100',
120120
'addressCounty' => 'sometimes|required|max:100',
121121
'addressPostcode' => 'sometimes|required|max:10',
122-
'contactNumber' => 'sometimes|required|max:50',
122+
'contactNumber' => 'sometimes|required|max:50|phone:GB',
123123
'dateOfBirth' => 'sometimes|nullable|date_format:Y-m-d',
124124
'unlockText' => 'sometimes|nullable|max:95',
125125
]);

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"pragmarx/google2fa-laravel": "^1.0",
3232
"pragmarx/recovery": "^0.1.0",
3333
"predis/predis": "^1.1",
34+
"propaganistas/laravel-phone": "^4.2",
3435
"spatie/laravel-cookie-consent": "^2.0",
3536
"tremby/laravel-git-version": "^1.1"
3637
},

composer.lock

Lines changed: 236 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

database/seeds/ProfileTableSeeder.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
use HMS\Entities\Role;
44
use HMS\Entities\Profile;
5+
use libphonenumber\RegionCode;
56
use Illuminate\Database\Seeder;
67
use HMS\Repositories\RoleRepository;
78
use HMS\Repositories\UserRepository;
9+
use Faker\Generator as FakerGenerator;
810
use Doctrine\ORM\EntityManagerInterface;
11+
use libphonenumber\NumberParseException;
12+
use Propaganistas\LaravelPhone\PhoneNumber;
913

1014
class ProfileTableSeeder extends Seeder
1115
{
@@ -24,18 +28,29 @@ class ProfileTableSeeder extends Seeder
2428
*/
2529
protected $entityManager;
2630

31+
/**
32+
* @var FakerGenerator
33+
*/
34+
protected $faker;
35+
2736
/**
2837
* Create a new TableSeeder instance.
2938
*
3039
* @param RoleRepository $roleRepository
31-
* @param UserRepository $userRepository,
40+
* @param UserRepository $userRepository
3241
* @param EntityManagerInterface $entityManager
42+
* @param FakerGenerator $faker
3343
*/
34-
public function __construct(RoleRepository $roleRepository, UserRepository $userRepository, EntityManagerInterface $entityManager)
35-
{
44+
public function __construct(
45+
RoleRepository $roleRepository,
46+
UserRepository $userRepository,
47+
EntityManagerInterface $entityManager,
48+
FakerGenerator $faker
49+
) {
3650
$this->roleRepository = $roleRepository;
3751
$this->userRepository = $userRepository;
3852
$this->entityManager = $entityManager;
53+
$this->faker = $faker;
3954
}
4055

4156
/**
@@ -65,6 +80,18 @@ public function run()
6580
$p = entity(Profile::class)->make(['user' => $user]);
6681
break;
6782
}
83+
84+
// validate and format phoneNumbers
85+
$e164 = null;
86+
do {
87+
try {
88+
$e164 = PhoneNumber::make($this->faker->phoneNumber, RegionCode::GB)->formatE164();
89+
} catch (NumberParseException $e) {
90+
//
91+
}
92+
} while ($e164 == null);
93+
$p->setContactNumber($e164);
94+
6895
$this->entityManager->persist($p);
6996
}
7097
}

0 commit comments

Comments
 (0)