PasswordResetRequestForm.php 2.24 KB
<?php
    
    namespace artbox\order\models;
    
    use Yii;
    use yii\base\Model;
    
    /**
     * Password reset request form
     */
    class PasswordResetRequestForm extends Model
    {
        public $email;
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    'email',
                    'trim',
                ],
                [
                    'email',
                    'required',
                ],
                [
                    'email',
                    'email',
                ],
                [
                    'email',
                    'exist',
                    'targetClass' => '\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();
        }
    }