frontend/modules/user/models/SignupForm.php
<?php
/*
* This file is part of the HomeFinanceV2 project.
*
* (c) Rafalsky.com <http://github.com/Rafalsky/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace frontend\modules\user\models;
use cheatsheet\Time;
use common\commands\SendEmailCommand;
use common\models\User;
use common\models\UserToken;
use frontend\modules\user\Module;
use yii\base\Exception;
use yii\base\Model;
use yii\helpers\Url;
/**
* Signup form
*/
class SignupForm extends Model
{
/**
* @var
*/
public $username;
/**
* @var
*/
public $email;
/**
* @var
*/
public $password;
/**
* @inheritdoc
*/
public function rules()
{
return [
['username', 'filter', 'filter' => 'trim'],
['username', 'required'],
['username', 'unique',
'targetClass' => '\common\models\User',
'message' => \Yii::t('frontend', 'This username has already been taken.')
],
['username', 'string', 'min' => 2, 'max' => 255],
['email', 'filter', 'filter' => 'trim'],
['email', 'required'],
['email', 'email'],
['email', 'unique',
'targetClass'=> '\common\models\User',
'message' => \Yii::t('frontend', 'This email address has already been taken.')
],
['password', 'required'],
['password', 'string', 'min' => 6],
];
}
/**
* @return array
*/
public function attributeLabels()
{
return [
'username' => \Yii::t('frontend', 'Username'),
'email' => \Yii::t('frontend', 'E-mail'),
'password' => \Yii::t('frontend', 'Password'),
];
}
/**
* Signs user up.
* @return User|null the saved model or null if saving fails
* @throws \yii\base\InvalidParamException
* @throws Exception
*/
public function signup()
{
if ($this->validate()) {
$shouldBeActivated = $this->shouldBeActivated();
$user = new User();
$user->username = $this->username;
$user->email = $this->email;
$user->status = $shouldBeActivated ? User::STATUS_NOT_ACTIVE : User::STATUS_ACTIVE;
$user->setPassword($this->password);
if (!$user->save()) {
throw new Exception("User couldn't be saved");
}
$user->afterSignup();
if ($shouldBeActivated) {
$token = UserToken::create(
$user->id,
UserToken::TYPE_ACTIVATION,
Time::SECONDS_IN_A_DAY
);
\Yii::$app->commandBus->handle(new SendEmailCommand([
'subject' => \Yii::t('frontend', 'Activation email'),
'view' => 'activation',
'to' => $this->email,
'params' => [
'url' => Url::to(['/user/sign-in/activation', 'token' => $token->token], true)
]
]));
}
return $user;
}
return null;
}
/**
* @return bool
*/
public function shouldBeActivated()
{
/** @var Module $userModule */
$userModule = \Yii::$app->getModule('user');
if (!$userModule) {
return false;
} elseif ($userModule->shouldBeActivated) {
return true;
} else {
return false;
}
}
}