diff --git a/models/LoginForm.php b/models/LoginForm.php new file mode 100755 index 0000000..88796b2 --- /dev/null +++ b/models/LoginForm.php @@ -0,0 +1,109 @@ +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 Customer|null + */ + protected function getUser() + { + if ($this->_user === null) { + $this->_user = Customer::findByUsername($this->username); + } + + return $this->_user; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'username' => Yii::t('app', 'Логин'), + 'password' => Yii::t('app', 'Пароль'), + 'rememberMe' => Yii::t('app', 'Запомнить'), + ]; + } + } diff --git a/models/PasswordResetRequestForm.php b/models/PasswordResetRequestForm.php new file mode 100755 index 0000000..7752175 --- /dev/null +++ b/models/PasswordResetRequestForm.php @@ -0,0 +1,77 @@ + '\artbox\order\models\Customer', + 'filter' => [ 'status' => Customer::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 Customer */ + $user = Customer::findOne( + [ + 'status' => Customer::STATUS_ACTIVE, + 'email' => $this->email, + ] + ); + if (!$user) { + return false; + } + + if (!Customer::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(); + } + } \ No newline at end of file diff --git a/models/ResetPasswordForm.php b/models/ResetPasswordForm.php new file mode 100755 index 0000000..9a60919 --- /dev/null +++ b/models/ResetPasswordForm.php @@ -0,0 +1,71 @@ +_user = Customer::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); + } + } \ No newline at end of file diff --git a/models/SignupForm.php b/models/SignupForm.php new file mode 100755 index 0000000..a9fe276 --- /dev/null +++ b/models/SignupForm.php @@ -0,0 +1,98 @@ + '\artbox\order\models\Customer', + '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' => '\artbox\order\models\Customer', + 'message' => 'This email address has already been taken.', + ], + + [ + 'password', + 'required', + ], + [ + 'password', + 'string', + 'min' => 6, + ], + ]; + } + + /** + * Signs user up. + * + * @return \artbox\order\models\Customer|null the saved model or null if saving fails + */ + public function signup() + { + if (!$this->validate()) { + return null; + } + + $user = new Customer(); + $user->username = $this->username; + $user->email = $this->email; + $user->setPassword($this->password); + $user->generateAuthKey(); + + return $user->save() ? $user : null; + } + } \ No newline at end of file -- libgit2 0.21.4