added registration process

This commit is contained in:
Antonio Ramirez
2016-12-09 09:36:18 +01:00
parent c3aad1b47d
commit 6457fbe2c2
17 changed files with 208 additions and 123 deletions

View File

@ -112,10 +112,10 @@ class AdminController extends Controller
$this->make(AjaxRequestModelValidator::class, [$user])->validate(); $this->make(AjaxRequestModelValidator::class, [$user])->validate();
$this->trigger(UserEvent::EVENT_BEFORE_CREATE, $event);
if ($user->load(Yii::$app->request->post())) { if ($user->load(Yii::$app->request->post())) {
$this->trigger(UserEvent::EVENT_BEFORE_CREATE, $event);
$mailService = MailFactory::makeWelcomeMailerService($user); $mailService = MailFactory::makeWelcomeMailerService($user);
if ($this->make(UserCreateService::class, [$user, $mailService])->run()) { if ($this->make(UserCreateService::class, [$user, $mailService])->run()) {
@ -139,14 +139,16 @@ class AdminController extends Controller
$this->make(AjaxRequestModelValidator::class, [$user])->validate(); $this->make(AjaxRequestModelValidator::class, [$user])->validate();
if ($user->load(Yii::$app->request->post())) {
$this->trigger(ActiveRecord::EVENT_BEFORE_UPDATE, $event); $this->trigger(ActiveRecord::EVENT_BEFORE_UPDATE, $event);
if ($user->load(Yii::$app->request->post()) && $user->save()) { if ($user->save()) {
Yii::$app->getSession()->setFlash('success', Yii::t('user', 'Account details have been updated')); Yii::$app->getSession()->setFlash('success', Yii::t('user', 'Account details have been updated'));
$this->trigger(ActiveRecord::EVENT_AFTER_UPDATE, $event); $this->trigger(ActiveRecord::EVENT_AFTER_UPDATE, $event);
return $this->refresh(); return $this->refresh();
} }
}
return $this->render('_account', ['user' => $user]); return $this->render('_account', ['user' => $user]);
} }
@ -162,16 +164,18 @@ class AdminController extends Controller
} }
/** @var UserEvent $event */ /** @var UserEvent $event */
$event = $this->make(UserEvent::class, [$user]); $event = $this->make(UserEvent::class, [$user]);
$this->make(AjaxRequestModelValidator::class, [$user])->validate(); $this->make(AjaxRequestModelValidator::class, [$user])->validate();
if ($profile->load(Yii::$app->request->post())) {
if($profile->save()) {
$this->trigger(UserEvent::EVENT_BEFORE_PROFILE_UPDATE, $event); $this->trigger(UserEvent::EVENT_BEFORE_PROFILE_UPDATE, $event);
if ($profile->load(Yii::$app->request->post()) && $profile->save()) {
Yii::$app->getSession()->setFlash('success', Yii::t('user', 'Profile details have been updated')); Yii::$app->getSession()->setFlash('success', Yii::t('user', 'Profile details have been updated'));
$this->trigger(UserEvent::EVENT_AFTER_PROFILE_UPDATE, $event); $this->trigger(UserEvent::EVENT_AFTER_PROFILE_UPDATE, $event);
return $this->refresh(); return $this->refresh();
} }
}
return $this->render( return $this->render(
'_profile', '_profile',
@ -216,9 +220,11 @@ class AdminController extends Controller
$event = $this->make(UserEvent::class, [$user]); $event = $this->make(UserEvent::class, [$user]);
$this->trigger(UserEvent::EVENT_BEFORE_CONFIRMATION, $event); $this->trigger(UserEvent::EVENT_BEFORE_CONFIRMATION, $event);
if ($this->make(UserConfirmationService::class, [$user])->run()) { if ($this->make(UserConfirmationService::class, [$user])->run()) {
Yii::$app->getSession()->setFlash('success', Yii::t('user', 'User has been confirmed')); Yii::$app->getSession()->setFlash('success', Yii::t('user', 'User has been confirmed'));
$this->trigger(UserEvent::EVENT_AFTER_CONFIRMATION, $event); $this->trigger(UserEvent::EVENT_AFTER_CONFIRMATION, $event);
} else { } else {
Yii::$app->getSession()->setFlash('warning', Yii::t('user', 'Unable to confirm user. Please, try again.')); Yii::$app->getSession()->setFlash('warning', Yii::t('user', 'Unable to confirm user. Please, try again.'));
} }
@ -236,9 +242,11 @@ class AdminController extends Controller
/** @var UserEvent $event */ /** @var UserEvent $event */
$event = $this->make(UserEvent::class, [$user]); $event = $this->make(UserEvent::class, [$user]);
$this->trigger(ActiveRecord::EVENT_BEFORE_DELETE, $event); $this->trigger(ActiveRecord::EVENT_BEFORE_DELETE, $event);
if ($user->delete()) { if ($user->delete()) {
Yii::$app->getSession()->setFlash('success', \Yii::t('user', 'User has been deleted')); Yii::$app->getSession()->setFlash('success', \Yii::t('user', 'User has been deleted'));
$this->trigger(ActiveRecord::EVENT_AFTER_DELETE, $event); $this->trigger(ActiveRecord::EVENT_AFTER_DELETE, $event);
} else { } else {
Yii::$app->getSession()->setFlash( Yii::$app->getSession()->setFlash(
'warning', 'warning',

View File

@ -80,9 +80,10 @@ class RecoveryController extends Controller
$this->make(AjaxRequestModelValidator::class, $form)->validate(); $this->make(AjaxRequestModelValidator::class, $form)->validate();
if ($form->load(Yii::$app->request->post())) {
$this->trigger(FormEvent::EVENT_BEFORE_REQUEST, $event); $this->trigger(FormEvent::EVENT_BEFORE_REQUEST, $event);
if ($form->load(Yii::$app->request->post())) {
$mailService = MailFactory::makeRecoveryMailerService($form->email); $mailService = MailFactory::makeRecoveryMailerService($form->email);
if ($this->make(PasswordRecoveryService::class, [$form->email, $mailService])->run()) { if ($this->make(PasswordRecoveryService::class, [$form->email, $mailService])->run()) {

View File

@ -5,13 +5,17 @@ use Da\User\Event\FormEvent;
use Da\User\Event\SocialNetworkConnectEvent; use Da\User\Event\SocialNetworkConnectEvent;
use Da\User\Event\UserEvent; use Da\User\Event\UserEvent;
use Da\User\Factory\MailFactory; use Da\User\Factory\MailFactory;
use Da\User\Form\RegistrationForm;
use Da\User\Form\ResendForm; use Da\User\Form\ResendForm;
use Da\User\Model\SocialNetworkAccount; use Da\User\Model\SocialNetworkAccount;
use Da\User\Model\User; use Da\User\Model\User;
use Da\User\Query\SocialNetworkAccountQuery; use Da\User\Query\SocialNetworkAccountQuery;
use Da\User\Query\UserQuery; use Da\User\Query\UserQuery;
use Da\User\Service\EmailConfirmationService;
use Da\User\Service\ResendConfirmationService; use Da\User\Service\ResendConfirmationService;
use Da\User\Service\UserConfirmationService;
use Da\User\Service\UserCreateService; use Da\User\Service\UserCreateService;
use Da\User\Service\UserRegisterService;
use Da\User\Traits\ContainerTrait; use Da\User\Traits\ContainerTrait;
use Da\User\Traits\ModuleTrait; use Da\User\Traits\ModuleTrait;
use Da\User\Validator\AjaxRequestModelValidator; use Da\User\Validator\AjaxRequestModelValidator;
@ -75,6 +79,45 @@ class RegistrationController extends Controller
]; ];
} }
public function actionRegister()
{
if(!$this->module->enableRegistration) {
throw new NotFoundHttpException();
}
/** @var RegistrationForm $form */
$form = $this->make(RegistrationForm::class);
/** @var FormEvent $event */
$event = $this->make(FormEvent::class, [$form]);
$this->make(AjaxRequestModelValidator::class, [$form])->validate();
if($form->load(Yii::$app->request->post()) && $form->validate()) {
$this->trigger(UserEvent::EVENT_BEFORE_REGISTER, $event);
$user = $this->make(User::class, [$form->attributes]);
$user->setScenario('register');
$mailService = MailFactory::makeWelcomeMailerService($user);
if($this->make(UserRegisterService::class, [$user, $mailService])->run()) {
Yii::$app->session->setFlash(
'info',
Yii::t(
'user',
'Your account has been created and a message with further instructions has been sent to your email'
)
);
return $this->render('message', [
'title' => Yii::t('user', 'Your account has been created')
]);
}
return $this->render('register', [
'model' => $form,
'module' => $this->module,
]);
}
}
public function actionConnect($code) public function actionConnect($code)
{ {
/** @var SocialNetworkAccount $account */ /** @var SocialNetworkAccount $account */
@ -92,9 +135,10 @@ class RegistrationController extends Controller
$this->make(AjaxRequestModelValidator::class, [$user])->validate(); $this->make(AjaxRequestModelValidator::class, [$user])->validate();
if ($user->load(Yii::$app->request->post())) {
$this->trigger(SocialNetworkConnectEvent::EVENT_BEFORE_CONNECT, $event); $this->trigger(SocialNetworkConnectEvent::EVENT_BEFORE_CONNECT, $event);
if ($user->load(Yii::$app->request->post())) {
$mailService = MailFactory::makeWelcomeMailerService($user); $mailService = MailFactory::makeWelcomeMailerService($user);
if ($this->make(UserCreateService::class, [$user, $mailService])->run()) { if ($this->make(UserCreateService::class, [$user, $mailService])->run()) {
$account->connect($user); $account->connect($user);
@ -125,12 +169,21 @@ class RegistrationController extends Controller
/** @var UserEvent $event */ /** @var UserEvent $event */
$event = $this->make(UserEvent::class, [$user]); $event = $this->make(UserEvent::class, [$user]);
$userConfirmationService = $this->make(UserConfirmationService::class, [$user]);
$this->trigger(UserEvent::EVENT_BEFORE_CONFIRMATION, $event); $this->trigger(UserEvent::EVENT_BEFORE_CONFIRMATION, $event);
// TODO ATTEMPT CONFIRMATION if ($this->make(EmailConfirmationService::class, [$code, $user, $userConfirmationService])->run()) {
Yii::$app->user->login($user, $this->module->rememberLoginLifespan);
Yii::$app->session->setFlash('success', Yii::t('user', 'Thank you, registration is now complete.'));
$this->trigger(UserEvent::EVENT_AFTER_CONFIRMATION, $event); $this->trigger(UserEvent::EVENT_AFTER_CONFIRMATION, $event);
} else {
Yii::$app->session->setFlash(
'danger',
Yii::t('user', 'The confirmation link is invalid or expired. Please try requesting a new one.')
);
}
return $this->render( return $this->render(
'message', 'message',
@ -152,8 +205,8 @@ class RegistrationController extends Controller
$this->make(AjaxRequestModelValidator::class, [$form])->validate(); $this->make(AjaxRequestModelValidator::class, [$form])->validate();
$this->trigger(FormEvent::EVENT_BEFORE_RESEND, $event);
if ($form->load(Yii::$app->request->post()) && $form->validate()) { if ($form->load(Yii::$app->request->post()) && $form->validate()) {
$this->trigger(FormEvent::EVENT_BEFORE_RESEND, $event);
/** @var User $user */ /** @var User $user */
$user = $this->userQuery->whereEmail($form->email)->one(); $user = $this->userQuery->whereEmail($form->email)->one();
$mailService = MailFactory::makeConfirmationMailerService($user); $mailService = MailFactory::makeConfirmationMailerService($user);

View File

@ -17,7 +17,7 @@ class TokenFactory
*/ */
public static function makeConfirmationToken($userId) public static function makeConfirmationToken($userId)
{ {
$token = self::make(Token::class, ['user_id' => $userId, 'type' => Token::TYPE_CONFIRMATION]); $token = self::make($userId, Token::TYPE_CONFIRMATION);
$token->save(false); $token->save(false);
@ -25,9 +25,43 @@ class TokenFactory
} }
protected static function make($class, $params = []) /**
* @param $userId
*
* @return Token
*/
public static function makeConfirmNewMailToken($userId)
{ {
return Yii::$container->get($class, $params); $token = self::make($userId, Token::TYPE_CONFIRM_NEW_EMAIL);
$token->save(false);
return $token;
}
/**
* @param $userId
*
* @return Token
*/
public static function makeRecoveryToken($userId)
{
$token = self::make($userId, Token::TYPE_RECOVERY);
$token->save(false);
return $token;
}
/**
* @param $userId
* @param $type
*
* @return Token
*/
protected static function make($userId, $type)
{
return Yii::$container->get(Token::class, ['user_id' => $userId, 'type' => $type]);
} }
} }

View File

@ -1,38 +1,27 @@
<?php <?php
/* namespace Da\User\Form;
* This file is part of the Dektrium project.
*
* (c) Dektrium project <http://github.com/dektrium/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace dektrium\user\models;
use Da\User\Model\User;
use Da\User\Traits\ContainerTrait;
use dektrium\user\traits\ModuleTrait; use dektrium\user\traits\ModuleTrait;
use Yii; use Yii;
use yii\base\Model; use yii\base\Model;
/**
* Registration form collects user input on registration process, validates it and creates new User model.
*
* @author Dmitry Erofeev <dmeroff@gmail.com>
*/
class RegistrationForm extends Model class RegistrationForm extends Model
{ {
use ModuleTrait; use ModuleTrait;
use ContainerTrait;
/** /**
* @var string User email address * @var string User email address
*/ */
public $email; public $email;
/** /**
* @var string Username * @var string Username
*/ */
public $username; public $username;
/** /**
* @var string Password * @var string Password
*/ */
@ -43,13 +32,14 @@ class RegistrationForm extends Model
*/ */
public function rules() public function rules()
{ {
$user = $this->module->modelMap['User']; /** @var User $user */
$user = $this->getClassMap()->get(User::class);
return [ return [
// username rules // username rules
'usernameLength' => ['username', 'string', 'min' => 3, 'max' => 255], 'usernameLength' => ['username', 'string', 'min' => 3, 'max' => 255],
'usernameTrim' => ['username', 'filter', 'filter' => 'trim'], 'usernameTrim' => ['username', 'filter', 'filter' => 'trim'],
'usernamePattern' => ['username', 'match', 'pattern' => $user::$usernameRegexp], 'usernamePattern' => ['username', 'match', 'pattern' => $user->usernameRegex],
'usernameRequired' => ['username', 'required'], 'usernameRequired' => ['username', 'required'],
'usernameUnique' => [ 'usernameUnique' => [
'username', 'username',
@ -85,14 +75,6 @@ class RegistrationForm extends Model
]; ];
} }
/**
* @inheritdoc
*/
public function formName()
{
return 'register-form';
}
/** /**
* Registers a new user account. If registration was successful it will set flash message. * Registers a new user account. If registration was successful it will set flash message.
* *

View File

@ -46,41 +46,4 @@ class ResendForm extends Model
'email' => Yii::t('user', 'Email'), 'email' => Yii::t('user', 'Email'),
]; ];
} }
/**
* Creates new confirmation token and sends it to the user.
*
* @return bool
*/
public function resend()
{
if (!$this->validate()) {
return false;
}
$user = $this->userQuery->whereEmail($this->email)->one();
$user = $this->finder->findUserByEmail($this->email);
if ($user instanceof User && !$user->isConfirmed) {
/** @var Token $token */
$token = \Yii::createObject([
'class' => Token::className(),
'user_id' => $user->id,
'type' => Token::TYPE_CONFIRMATION,
]);
$token->save(false);
$this->mailer->sendConfirmationMessage($user, $token);
}
\Yii::$app->session->setFlash(
'info',
\Yii::t(
'user',
'A message has been sent to your email address. It contains a confirmation link that you must click to complete registration.'
)
);
return true;
}
} }

View File

@ -11,6 +11,7 @@
namespace dektrium\user\models; namespace dektrium\user\models;
use Da\User\Factory\TokenFactory;
use dektrium\user\helpers\Password; use dektrium\user\helpers\Password;
use dektrium\user\Mailer; use dektrium\user\Mailer;
use dektrium\user\Module; use dektrium\user\Module;
@ -160,12 +161,8 @@ class SettingsForm extends Model
{ {
$this->user->unconfirmed_email = $this->email; $this->user->unconfirmed_email = $this->email;
/** @var Token $token */ /** @var Token $token */
$token = Yii::createObject([ $token = TokenFactory::makeConfirmNewMailToken($this->user->id);
'class' => Token::className(),
'user_id' => $this->user->id,
'type' => Token::TYPE_CONFIRM_NEW_EMAIL,
]);
$token->save(false);
$this->mailer->sendReconfirmationMessage($this->user, $token); $this->mailer->sendReconfirmationMessage($this->user, $token);
Yii::$app->session->setFlash( Yii::$app->session->setFlash(
'info', 'info',

View File

@ -138,7 +138,7 @@ class Profile extends ActiveRecord
*/ */
public function getUser() public function getUser()
{ {
return $this->hasOne($this->getClassMap()->get('User'), ['id' => 'user_id']); return $this->hasOne($this->getClassMap()->get(User::class), ['id' => 'user_id']);
} }
/** /**

View File

@ -96,7 +96,7 @@ class SocialNetworkAccount extends ActiveRecord
*/ */
public function getUser() public function getUser()
{ {
return $this->hasOne($this->getClassMap()->get('User'), ['id' => 'user_id']); return $this->hasOne($this->getClassMap()->get(User::class), ['id' => 'user_id']);
} }
/** /**

View File

@ -74,7 +74,7 @@ class Token extends ActiveRecord
*/ */
public function getUser() public function getUser()
{ {
return $this->hasOne($this->getClassMap()->get('User'), ['id' => 'user_id']); return $this->hasOne($this->getClassMap()->get(User::class), ['id' => 'user_id']);
} }
/** /**

View File

@ -236,7 +236,7 @@ class User extends ActiveRecord implements IdentityInterface
*/ */
public function getProfile() public function getProfile()
{ {
return $this->hasOne($this->getClassMap()->get('Profile'), ['user_id' => 'id']); return $this->hasOne($this->getClassMap()->get(Profile::class), ['user_id' => 'id']);
} }
/** /**
@ -246,7 +246,9 @@ class User extends ActiveRecord implements IdentityInterface
{ {
if ($this->connectedAccounts == null) { if ($this->connectedAccounts == null) {
/** @var SocialNetworkAccount[] $accounts */ /** @var SocialNetworkAccount[] $accounts */
$accounts = $this->hasMany($this->getClassMap()->get('Account'), ['user_id' => 'id'])->all(); $accounts = $this->hasMany($this->getClassMap()
->get(SocialNetworkAccount::class), ['user_id' => 'id'])
->all();
foreach ($accounts as $account) { foreach ($accounts as $account) {
$this->connectedAccounts[$account->provider] = $account; $this->connectedAccounts[$account->provider] = $account;

View File

@ -10,4 +10,9 @@ class TokenQuery extends ActiveQuery
{ {
return $this->andWhere(['user_id' => $userId, 'code' => $code, 'type' => Token::TYPE_RECOVERY]); return $this->andWhere(['user_id' => $userId, 'code' => $code, 'type' => Token::TYPE_RECOVERY]);
} }
public function whereIsConfirmationType($userId, $code)
{
return $this->andWhere(['user_id' => $userId, 'code' => $code, 'type' => Token::TYPE_CONFIRM_NEW_EMAIL]);
}
} }

View 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;
}
}

View File

@ -2,6 +2,7 @@
namespace Da\User\Service; namespace Da\User\Service;
use Da\User\Contracts\ServiceInterface; use Da\User\Contracts\ServiceInterface;
use Da\User\Factory\TokenFactory;
use Da\User\Model\Token; use Da\User\Model\Token;
use Da\User\Model\User; use Da\User\Model\User;
use Da\User\Query\UserQuery; use Da\User\Query\UserQuery;
@ -31,16 +32,10 @@ class PasswordRecoveryService implements ServiceInterface
try { try {
/** @var User $user */ /** @var User $user */
$user = $this->query->whereEmail($this->email)->one(); $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; return false;
} }

View File

@ -4,10 +4,7 @@ namespace Da\User\Service;
use Da\User\Contracts\ServiceInterface; use Da\User\Contracts\ServiceInterface;
use Da\User\Factory\TokenFactory; use Da\User\Factory\TokenFactory;
use Da\User\Helper\SecurityHelper;
use Da\User\Model\Token;
use Da\User\Model\User; use Da\User\Model\User;
use Da\User\Query\UserQuery;
use yii\log\Logger; use yii\log\Logger;
class ResendConfirmationService implements ServiceInterface class ResendConfirmationService implements ServiceInterface

View File

@ -17,9 +17,11 @@ class UserConfirmationService implements ServiceInterface
public function run() public function run()
{ {
$this->model->trigger(UserEvent::EVENT_BEFORE_CONFIRMATION); $this->model->trigger(UserEvent::EVENT_BEFORE_CONFIRMATION);
$result = (bool) $this->model->updateAttributes(['confirmed_at' => time()]); if ((bool)$this->model->updateAttributes(['confirmed_at' => time()])) {
$this->model->trigger(UserEvent::EVENT_AFTER_CONFIRMATION); $this->model->trigger(UserEvent::EVENT_AFTER_CONFIRMATION);
return $result; return true;
}
return false;
} }
} }

View File

@ -3,6 +3,7 @@ namespace Da\User\Service;
use Da\User\Contracts\ServiceInterface; use Da\User\Contracts\ServiceInterface;
use Da\User\Event\UserEvent; use Da\User\Event\UserEvent;
use Da\User\Factory\TokenFactory;
use Da\User\Helper\SecurityHelper; use Da\User\Helper\SecurityHelper;
use Da\User\Model\Token; use Da\User\Model\Token;
use Da\User\Model\User; use Da\User\Model\User;
@ -50,10 +51,12 @@ class UserRegisterService implements ServiceInterface
} }
if($model->module->enableEmailConfirmation) { if($model->module->enableEmailConfirmation) {
$token = $model->make(Token::class, ['type' => Token::TYPE_CONFIRMATION]); $token = TokenFactory::makeConfirmationToken($model->id);
$token->link('user', $model);
} }
if(isset($token)) {
$this->mailService->setViewParam('token', $token);
}
$this->mailService->run(); $this->mailService->run();
$model->trigger(UserEvent::EVENT_AFTER_REGISTER); $model->trigger(UserEvent::EVENT_AFTER_REGISTER);