Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
66.67% |
2 / 3 |
CRAP | |
96.43% |
27 / 28 |
| SignupForm | |
0.00% |
0 / 1 |
|
66.67% |
2 / 3 |
6 | |
96.43% |
27 / 28 |
| rules | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
| signup | |
0.00% |
0 / 1 |
3.00 | |
93.75% |
15 / 16 |
|||
| sendConfirmationEmail | |
100.00% |
1 / 1 |
2 | |
100.00% |
9 / 9 |
|||
| <?php | |
| namespace frontend\models; | |
| use Yii; | |
| use yii\base\Model; | |
| use common\models\User; | |
| /** | |
| * Signup form | |
| */ | |
| class SignupForm extends Model | |
| { | |
| public $firstname; | |
| public $lastname; | |
| public $username; | |
| public $email; | |
| public $password; | |
| public $repeatPassword; | |
| /** | |
| * @inheritdoc | |
| */ | |
| public function rules() | |
| { | |
| return [ | |
| ['firstname', 'trim'], | |
| ['firstname', 'required'], | |
| ['lastname', 'trim'], | |
| ['lastname', 'required'], | |
| ['username', 'trim'], | |
| ['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'], | |
| ['username', 'string', 'min' => 2, 'max' => 255], | |
| ['email', 'trim'], | |
| ['email', 'required'], | |
| ['email', 'email'], | |
| ['email', 'unique', 'targetClass' => '\common\models\User', 'message' => Yii::t('app', 'This email address has already been taken.')], | |
| ['email', 'string', 'max' => 255], | |
| ['password', 'required'], | |
| ['password', 'string', 'min' => 6], | |
| ['repeatPassword', 'required'], | |
| ['repeatPassword', 'compare', 'compareAttribute' => 'password', 'skipOnEmpty' => false, 'message' => Yii::t('app', 'Repeat password should be exacly the same as password.')], | |
| ]; | |
| } | |
| /** | |
| * Signs user up. | |
| * | |
| * @return User|null the saved model or null if saving fails | |
| */ | |
| public function signup() | |
| { | |
| if (!$this->validate()) { | |
| return null; | |
| } | |
| $user = new User(); | |
| $user->username = $this->username; | |
| $user->email = $this->email; | |
| $user->setPassword($this->password); | |
| $user->firstname = $this->firstname; | |
| $user->lastname = $this->lastname; | |
| $user->generateAuthKey(); | |
| $user->generateAccessToken(); | |
| $user->generateEmailConfirmToken(); | |
| if(!$user->save()){ | |
| return null; | |
| } | |
| self::sendConfirmationEmail($user); | |
| Yii::$app->getSession()->setFlash('info', Yii::t('app', 'Confirm your email to activate your account.')); | |
| return $user; | |
| } | |
| /** | |
| * Sends an email to the specified email address using the information collected by this model. | |
| * | |
| * @param string $email the target email address | |
| * @return boolean whether the email was sent | |
| */ | |
| private static function sendConfirmationEmail($user) | |
| { | |
| $message = Yii::$app->mailer->compose('emailConfirmToken', ['user' => $user]); | |
| $fromEmail = \Yii::$app->params['supportEmail']; | |
| $sourceEmail = $message->getFrom(); | |
| if (!empty($sourceEmail)) $fromEmail = key($sourceEmail); | |
| return $message->setFrom([$fromEmail => \Yii::$app->name . ' robot']) | |
| ->setReplyTo([\Yii::$app->params['supportEmail'] => \Yii::$app->name . ' robot']) | |
| ->setTo([$user->email => $user->fullname]) | |
| ->setSubject(Yii::t('app', 'Email confirmation')) | |
| ->send(); | |
| } | |
| } |