diff --git a/common/models/LoginForm.php b/common/models/LoginForm.php new file mode 100644 index 0000000..da6de13 --- /dev/null +++ b/common/models/LoginForm.php @@ -0,0 +1,90 @@ +hasErrors()) { + $user = $this->getUser(); + if (!$user || !$user->validatePassword($this->password)) { + $this->addError($attribute, 'Incorrect username or password.'); + } + } + } + + /** + * Logs in a user using the provided username and password. + * + * @return bool whether the user is logged in successfully + */ + public function login() + { + if ($this->validate()) { + return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0); + } else { + return false; + } + } + + /** + * Finds user by [[username]] + * + * @return User|null + */ + protected function getUser() + { + if ($this->_user === null) { + $this->_user = User::findByUsername($this->username); + } + + return $this->_user; + } + } diff --git a/frontend/models/ContactForm.php b/frontend/models/ContactForm.php new file mode 100644 index 0000000..d50b316 --- /dev/null +++ b/frontend/models/ContactForm.php @@ -0,0 +1,74 @@ + 'Verification Code', + ]; + } + + /** + * Sends an email to the specified email address using the information collected by this model. + * + * @param string $email the target email address + * + * @return bool whether the email was sent + */ + public function sendEmail($email) + { + return Yii::$app->mailer->compose() + ->setTo($email) + ->setFrom([ $this->email => $this->name ]) + ->setSubject($this->subject) + ->setTextBody($this->body) + ->send(); + } + } diff --git a/frontend/models/PasswordResetRequestForm.php b/frontend/models/PasswordResetRequestForm.php new file mode 100644 index 0000000..cf37a12 --- /dev/null +++ b/frontend/models/PasswordResetRequestForm.php @@ -0,0 +1,81 @@ + '\common\models\User', + 'filter' => [ 'status' => User::STATUS_ACTIVE ], + 'message' => 'There is no user with this email address.', + ], + ]; + } + + /** + * Sends an email with a link, for resetting the password. + * + * @return bool whether the email was send + */ + public function sendEmail() + { + /* @var $user User */ + $user = User::findOne( + [ + 'status' => User::STATUS_ACTIVE, + 'email' => $this->email, + ] + ); + + if (!$user) { + return false; + } + + if (!User::isPasswordResetTokenValid($user->password_reset_token)) { + $user->generatePasswordResetToken(); + if (!$user->save()) { + return false; + } + } + + return Yii::$app->mailer->compose( + [ + 'html' => 'passwordResetToken-html', + 'text' => 'passwordResetToken-text', + ], + [ 'user' => $user ] + ) + ->setFrom([ Yii::$app->params[ 'supportEmail' ] => Yii::$app->name . ' robot' ]) + ->setTo($this->email) + ->setSubject('Password reset for ' . Yii::$app->name) + ->send(); + } + } diff --git a/frontend/models/ResetPasswordForm.php b/frontend/models/ResetPasswordForm.php new file mode 100644 index 0000000..5811460 --- /dev/null +++ b/frontend/models/ResetPasswordForm.php @@ -0,0 +1,71 @@ +_user = User::findByPasswordResetToken($token); + if (!$this->_user) { + throw new InvalidParamException('Wrong password reset token.'); + } + parent::__construct($config); + } + + /** + * @inheritdoc + */ + public function rules() + { + return [ + [ + 'password', + 'required', + ], + [ + 'password', + 'string', + 'min' => 6, + ], + ]; + } + + /** + * Resets password. + * + * @return bool if password was reset. + */ + public function resetPassword() + { + $user = $this->_user; + $user->setPassword($this->password); + $user->removePasswordResetToken(); + + return $user->save(false); + } + } diff --git a/frontend/models/SignupForm.php b/frontend/models/SignupForm.php new file mode 100644 index 0000000..f7cb2e3 --- /dev/null +++ b/frontend/models/SignupForm.php @@ -0,0 +1,98 @@ + '\common\models\User', + 'message' => 'This username has already been taken.', + ], + [ + 'username', + 'string', + 'min' => 2, + 'max' => 255, + ], + + [ + 'email', + 'trim', + ], + [ + 'email', + 'required', + ], + [ + 'email', + 'email', + ], + [ + 'email', + 'string', + 'max' => 255, + ], + [ + 'email', + 'unique', + 'targetClass' => '\common\models\User', + 'message' => 'This email address has already been taken.', + ], + + [ + 'password', + 'required', + ], + [ + 'password', + 'string', + 'min' => 6, + ], + ]; + } + + /** + * 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->generateAuthKey(); + + return $user->save() ? $user : null; + } + } -- libgit2 0.21.4