/** * Signs user up. * @return User|null the saved model or null if saving fails */ public function signup() { if ($this->validate()) { $user = new User(); $user->username = $this->username; $user->email = $this->email; $user->setPassword($this->password); $user->generateAuthKey(); if ($user->save()) { $Verification = new UserVerified(); $Verification->user_id = $user->id; $Verification->key = sha1($user->username . time() . uniqid()); if ($Verification->save()) { $this->signupNotification($user->email, $Verification->key); return $user; } } } return null; }
/** * Logs in a user using the provided username and password. * @return boolean whether the user is logged in successfully */ public function login() { if ($this->validate()) { $user = $this->getUser(); $user->online = 1; $user->update(false); $Verified = \common\models\UserVerified::findOne(['user_id' => $user->id]); if (!$Verified || !$Verified->verificate_date) { Yii::$app->session->setFlash('verification', 'Для авторизации вам требуется подтвердить аккаунт. На указанный Вами email было выслано письмо содержащее ссылку для подтверждения аккаунта'); return false; } return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0); } else { return false; } }
/** * @param $key * Verification account */ public function actionVerificate($key) { $model = UserVerified::findOne(['key' => $key]); if ($model && !$model->verificate_date) { $model->verificate_date = time(); if ($model->save()) { Yii::$app->session->setFlash('verification', 'Аккаунт активирован. Используйте свой логин и пароль для входа на сайт. '); $this->redirect('/site/login'); } } }