-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add users.email_validated_at, split from users.confirmed_at
Flag `confirmed_at` should be used for actual user account confirmation. This can be done either via confirmation link sent by email, via event, or manually via admin. Flag `email_validated_at` should be used for email deliverability settings. - Add API endpoints to set email address validated/invalidated by external services (i.e. Mailer). - Flag email_validated_at is reset if the user's email address is changed. remp/crm#1739
- Loading branch information
Showing
13 changed files
with
415 additions
and
25 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,118 @@ | ||
<?php | ||
|
||
namespace Crm\UsersModule\Api; | ||
|
||
use Crm\ApiModule\Api\ApiHandler; | ||
use Crm\ApiModule\Api\JsonResponse; | ||
use Crm\ApiModule\Authorization\ApiAuthorizationInterface; | ||
use Crm\ApiModule\Params\InputParam; | ||
use Crm\ApiModule\Params\ParamsProcessor; | ||
use Crm\UsersModule\Repository\UsersRepository; | ||
use Nette\Http\Request; | ||
use Nette\Http\Response; | ||
use Nette\Utils\Validators; | ||
|
||
class EmailValidationApiHandler extends ApiHandler | ||
{ | ||
private $request; | ||
|
||
private $usersRepository; | ||
|
||
private $action = 'validate'; | ||
|
||
public function __construct( | ||
Request $request, | ||
UsersRepository $usersRepository | ||
) { | ||
$this->request = $request; | ||
$this->usersRepository = $usersRepository; | ||
} | ||
|
||
public function params() | ||
{ | ||
return [ | ||
new InputParam(InputParam::TYPE_POST, 'email', InputParam::REQUIRED), | ||
]; | ||
} | ||
|
||
public function setAction(string $action) | ||
{ | ||
$this->action = $action; | ||
} | ||
|
||
/** | ||
* @param ApiAuthorizationInterface $authorization | ||
* @return \Nette\Application\IResponse | ||
*/ | ||
public function handle(ApiAuthorizationInterface $authorization) | ||
{ | ||
$paramsProcessor = new ParamsProcessor($this->params()); | ||
|
||
$error = $paramsProcessor->isError(); | ||
if ($error) { | ||
$response = new JsonResponse([ | ||
'status' => 'error', | ||
'message' => $error, | ||
'code' => 'invalid_request', | ||
]); | ||
$response->setHttpCode(Response::S400_BAD_REQUEST); | ||
return $response; | ||
} | ||
|
||
$params = $paramsProcessor->getValues(); | ||
if (!Validators::isEmail($params['email'])) { | ||
$response = new JsonResponse([ | ||
'status' => 'error', | ||
'message' => 'Email is not valid', | ||
'code' => 'invalid_param', | ||
]); | ||
$response->setHttpCode(Response::S400_BAD_REQUEST); | ||
return $response; | ||
} | ||
|
||
$user = $this->usersRepository->getByEmail($params['email']); | ||
if (!$user) { | ||
$result = [ | ||
'status' => 'error', | ||
'message' => 'Email isn\'t assigned to any user', | ||
'code' => 'email_not_found', | ||
]; | ||
$response = new JsonResponse($result); | ||
$response->setHttpCode(Response::S404_NOT_FOUND); | ||
return $response; | ||
} | ||
|
||
$action = $this->getAction(); | ||
if ($action === 'validate') { | ||
$this->usersRepository->setEmailValidated($user, new \DateTime()); | ||
$message = 'Email has been validated'; | ||
} elseif ($action === 'invalidate') { | ||
$this->usersRepository->setEmailInvalidated($user); | ||
$message = 'Email has been invalidated'; | ||
} else { | ||
throw new \Exception('invalid action resolved: ' . $action); | ||
} | ||
|
||
$result = [ | ||
'status' => 'ok', | ||
'message' => $message, | ||
'code' => 'success', | ||
]; | ||
|
||
$response = new JsonResponse($result); | ||
$response->setHttpCode(Response::S200_OK); | ||
|
||
return $response; | ||
} | ||
|
||
private function getAction(): string | ||
{ | ||
if (isset($this->action)) { | ||
return $this->action; | ||
} | ||
if (strpos($this->request->getUrl()->getPath(), "invalidate") !== false) { | ||
return 'invalidate'; | ||
} | ||
return 'validate'; | ||
} | ||
} |
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
17 changes: 17 additions & 0 deletions
17
src/migrations/20210224160115_add_email_validated_at_flag.php
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,17 @@ | ||
<?php | ||
|
||
use Phinx\Migration\AbstractMigration; | ||
|
||
class AddEmailValidatedAtFlag extends AbstractMigration | ||
{ | ||
public function change() | ||
{ | ||
$this->table('users') | ||
->addColumn('email_validated_at', 'datetime', [ 'null' => true, 'after' => 'confirmed_at' ]) | ||
->update(); | ||
|
||
// use current "confirmed_at" values as default values | ||
// any new user should get a null as default | ||
$this->execute('UPDATE users SET email_validated_at = confirmed_at'); | ||
} | ||
} |
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
Oops, something went wrong.