-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ensure admin has verified email address
- Loading branch information
1 parent
26e0ea5
commit 646391f
Showing
4 changed files
with
69 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Rules; | ||
|
||
use App\Models\User; | ||
use Closure; | ||
use Illuminate\Contracts\Validation\ValidationRule; | ||
|
||
/** | ||
* Ensure an email belongs to a user with a verified email address. | ||
*/ | ||
class VerifiedUserEmail implements ValidationRule | ||
{ | ||
public function validate(string $attribute, mixed $value, Closure $fail): void | ||
{ | ||
$user = User::where('email', $value)->first(); | ||
|
||
if ($user === null) { | ||
$fail('No user with that email address'); | ||
|
||
return; | ||
} | ||
|
||
if (!$user->email_verified) { | ||
$fail('User does not have a verified email address'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -345,6 +345,39 @@ public function testAddsAdmins(): void | |
); | ||
} | ||
|
||
public function testEmailOfAddedAdminMustBelongToExistingUser(): void | ||
{ | ||
User::factory()->withEmail('[email protected]')->admin()->create(); | ||
|
||
$this->browse( | ||
fn(Browser $browser) => $browser | ||
->loginAs('[email protected]') | ||
->assertAuthenticated() | ||
->visit('/admin/admins') | ||
->type('email', '[email protected]') | ||
->press('Add') | ||
->assertDontSeeIn('main table', '[email protected]') | ||
->assertSeeIn('.invalid-feedback', 'No user with that email address') | ||
); | ||
} | ||
|
||
public function testEmailOfAddedAdminMustBeVerified(): void | ||
{ | ||
User::factory()->withEmail('[email protected]')->admin()->create(); | ||
User::factory()->withEmail('[email protected]', verified: false)->create(); | ||
|
||
$this->browse( | ||
fn(Browser $browser) => $browser | ||
->loginAs('[email protected]') | ||
->assertAuthenticated() | ||
->visit('/admin/admins') | ||
->type('email', '[email protected]') | ||
->press('Add') | ||
->assertDontSeeIn('main table', '[email protected]') | ||
->assertSeeIn('.invalid-feedback', 'User does not have a verified email address') | ||
); | ||
} | ||
|
||
public function testRemovesAdmins(): void | ||
{ | ||
User::factory()->withEmail('[email protected]')->admin()->create(); | ||
|