added registration process
This commit is contained in:
43
lib/User/Service/EmailConfirmationService.php
Normal file
43
lib/User/Service/EmailConfirmationService.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
namespace Da\User\Service;
|
||||
|
||||
|
||||
use Da\User\Contracts\ServiceInterface;
|
||||
use Da\User\Event\UserEvent;
|
||||
use Da\User\Model\Token;
|
||||
use Da\User\Model\User;
|
||||
use Da\User\Query\TokenQuery;
|
||||
|
||||
class EmailConfirmationService implements ServiceInterface
|
||||
{
|
||||
protected $model;
|
||||
protected $code;
|
||||
protected $tokenQuery;
|
||||
protected $userConfirmationService;
|
||||
|
||||
public function __construct(
|
||||
$code,
|
||||
User $model,
|
||||
TokenQuery $tokenQuery,
|
||||
UserConfirmationService $userConfirmationService
|
||||
) {
|
||||
$this->code = $code;
|
||||
$this->model = $model;
|
||||
$this->tokenQuery = $tokenQuery;
|
||||
$this->userConfirmationService = $userConfirmationService;
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
$token = $this->tokenQuery->whereIsConfirmationType($this->model->id, $this->code)->one();
|
||||
|
||||
if ($token instanceof Token && !$token->getIsExpired()) {
|
||||
$token->delete();
|
||||
|
||||
return $this->userConfirmationService->run();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@ -2,6 +2,7 @@
|
||||
namespace Da\User\Service;
|
||||
|
||||
use Da\User\Contracts\ServiceInterface;
|
||||
use Da\User\Factory\TokenFactory;
|
||||
use Da\User\Model\Token;
|
||||
use Da\User\Model\User;
|
||||
use Da\User\Query\UserQuery;
|
||||
@ -31,16 +32,10 @@ class PasswordRecoveryService implements ServiceInterface
|
||||
try {
|
||||
/** @var User $user */
|
||||
$user = $this->query->whereEmail($this->email)->one();
|
||||
/** @var Token $token */
|
||||
$token = Yii::createObject(
|
||||
[
|
||||
'class' => Token::class,
|
||||
'user_id' => $user->id,
|
||||
'type' => Token::TYPE_RECOVERY
|
||||
]
|
||||
);
|
||||
|
||||
if (!$token->save(false)) {
|
||||
$token = TokenFactory::makeRecoveryToken($user->id);
|
||||
|
||||
if (!$token) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@ -4,10 +4,7 @@ namespace Da\User\Service;
|
||||
|
||||
use Da\User\Contracts\ServiceInterface;
|
||||
use Da\User\Factory\TokenFactory;
|
||||
use Da\User\Helper\SecurityHelper;
|
||||
use Da\User\Model\Token;
|
||||
use Da\User\Model\User;
|
||||
use Da\User\Query\UserQuery;
|
||||
use yii\log\Logger;
|
||||
|
||||
class ResendConfirmationService implements ServiceInterface
|
||||
|
||||
@ -17,9 +17,11 @@ class UserConfirmationService implements ServiceInterface
|
||||
public function run()
|
||||
{
|
||||
$this->model->trigger(UserEvent::EVENT_BEFORE_CONFIRMATION);
|
||||
$result = (bool) $this->model->updateAttributes(['confirmed_at' => time()]);
|
||||
$this->model->trigger(UserEvent::EVENT_AFTER_CONFIRMATION);
|
||||
if ((bool)$this->model->updateAttributes(['confirmed_at' => time()])) {
|
||||
$this->model->trigger(UserEvent::EVENT_AFTER_CONFIRMATION);
|
||||
|
||||
return $result;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@ namespace Da\User\Service;
|
||||
|
||||
use Da\User\Contracts\ServiceInterface;
|
||||
use Da\User\Event\UserEvent;
|
||||
use Da\User\Factory\TokenFactory;
|
||||
use Da\User\Helper\SecurityHelper;
|
||||
use Da\User\Model\Token;
|
||||
use Da\User\Model\User;
|
||||
@ -50,10 +51,12 @@ class UserRegisterService implements ServiceInterface
|
||||
}
|
||||
|
||||
if($model->module->enableEmailConfirmation) {
|
||||
$token = $model->make(Token::class, ['type' => Token::TYPE_CONFIRMATION]);
|
||||
$token->link('user', $model);
|
||||
$token = TokenFactory::makeConfirmationToken($model->id);
|
||||
}
|
||||
|
||||
if(isset($token)) {
|
||||
$this->mailService->setViewParam('token', $token);
|
||||
}
|
||||
$this->mailService->run();
|
||||
|
||||
$model->trigger(UserEvent::EVENT_AFTER_REGISTER);
|
||||
|
||||
Reference in New Issue
Block a user