Passed
Push — master ( e6d9c1...d3f2d9 )
by Alexey
02:34
created

DefaultController::actionRequestPasswordReset()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 12
c 0
b 0
f 0
rs 10
cc 4
nc 3
nop 0
1
<?php
2
3
namespace modules\users\controllers\frontend;
4
5
use Yii;
6
use yii\web\Controller;
7
use modules\users\models\SignupForm;
8
use modules\users\models\LoginForm;
9
use modules\users\models\EmailConfirmForm;
10
use modules\users\models\ResetPasswordForm;
11
use modules\users\models\PasswordResetRequestForm;
12
use yii\filters\AccessControl;
13
use yii\base\InvalidArgumentException;
14
use yii\web\BadRequestHttpException;
15
use yii\filters\VerbFilter;
16
use modules\users\Module;
17
18
/**
19
 * Class DefaultController
20
 * @package modules\users\controllers\frontend
21
 */
22
class DefaultController extends Controller
23
{
24
    /**
25
     * @inheritdoc
26
     * @return array
27
     */
28
    public function behaviors()
29
    {
30
        return [
31
            'access' => [
32
                'class' => AccessControl::class,
33
                'rules' => [
34
                    [
35
                        'allow' => true,
36
                        'roles' => ['?']
37
                    ],
38
                    [
39
                        'actions' => ['logout'],
40
                        'allow' => true,
41
                        'roles' => ['@']
42
                    ],
43
                ],
44
            ],
45
            'verbs' => [
46
                'class' => VerbFilter::class,
47
                'actions' => [
48
                    'logout' => ['post'],
49
                ],
50
            ],
51
        ];
52
    }
53
54
    /**
55
     * Logs in a user.
56
     *
57
     * @return string|\yii\web\Response
58
     */
59
    public function actionLogin()
60
    {
61
        $model = new LoginForm();
62
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
63
            return $this->goBack();
64
        } else {
65
            return $this->render('login', [
66
                'model' => $model,
67
            ]);
68
        }
69
    }
70
71
    /**
72
     * Logs out the current user.
73
     *
74
     * @return \yii\web\Response
75
     */
76
    public function actionLogout()
77
    {
78
        Yii::$app->user->logout();
0 ignored issues
show
Bug introduced by
The method logout() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
        Yii::$app->user->/** @scrutinizer ignore-call */ 
79
                         logout();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
79
        return $this->processGoHome();
80
    }
81
82
    /**
83
     * @return string|\yii\web\Response
84
     * @throws \yii\base\Exception
85
     */
86
    public function actionSignup()
87
    {
88
        $model = new SignupForm();
89
        if ($model->load(Yii::$app->request->post())) {
90
            if ($model->signup()) {
91
                return $this->processGoHome(Module::t('module', 'It remains to activate the account.'));
92
            }
93
        }
94
        return $this->render('signup', [
95
            'model' => $model,
96
        ]);
97
    }
98
99
    /**
100
     * @param mixed $token
101
     * @return \yii\web\Response
102
     * @throws BadRequestHttpException
103
     * @throws \Exception
104
     */
105
    public function actionEmailConfirm($token)
106
    {
107
        try {
108
            $model = new EmailConfirmForm($token);
109
        } catch (InvalidArgumentException $e) {
110
            throw new BadRequestHttpException($e->getMessage());
111
        }
112
113
        if ($model->confirmEmail()) {
114
            return $this->processGoHome(Module::t('module', 'Thank you for registering!'));
115
        }
116
        return $this->processGoHome(Module::t('module', 'Error sending message!'), 'error');
117
    }
118
119
    /**
120
     * Requests password reset.
121
     *
122
     * @return string|\yii\web\Response
123
     */
124
    public function actionRequestPasswordReset()
125
    {
126
        $model = new PasswordResetRequestForm();
127
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
128
            if ($model->sendEmail()) {
129
                return $this->processGoHome(Module::t('module', 'Check your email for further instructions.'));
130
            } else {
131
                Yii::$app->session->setFlash('error', Module::t('module', 'Sorry, we are unable to reset password.'));
0 ignored issues
show
Bug introduced by
The method setFlash() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

131
                Yii::$app->session->/** @scrutinizer ignore-call */ 
132
                                    setFlash('error', Module::t('module', 'Sorry, we are unable to reset password.'));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
132
            }
133
        }
134
        return $this->render('requestPasswordResetToken', [
135
            'model' => $model,
136
        ]);
137
    }
138
139
    /**
140
     * @param mixed $token
141
     * @return string|\yii\web\Response
142
     * @throws BadRequestHttpException
143
     * @throws \yii\base\Exception
144
     */
145
    public function actionResetPassword($token)
146
    {
147
        try {
148
            $model = new ResetPasswordForm($token);
149
        } catch (InvalidArgumentException $e) {
150
            throw new BadRequestHttpException($e->getMessage());
151
        }
152
153
        if ($model->load(Yii::$app->request->post()) && $this->processResetPassword($model)) {
154
            return $this->processGoHome(Module::t('module', 'Password changed successfully.'));
155
        }
156
157
        return $this->render('resetPassword', [
158
            'model' => $model,
159
        ]);
160
    }
161
162
    /**
163
     * @param $model ResetPasswordForm|\yii\base\Model
164
     * @return bool
165
     * @throws \yii\base\Exception
166
     */
167
    protected function processResetPassword($model)
168
    {
169
        if ($model->validate() && $model->resetPassword()) {
170
            return true;
171
        }
172
        return false;
173
    }
174
175
    /**
176
     * @param string $message
177
     * @param string $type
178
     * @return \yii\web\Response
179
     */
180
    public function processGoHome($message = '', $type = 'success')
181
    {
182
        if (!empty($message)) {
183
            Yii::$app->session->setFlash($type, $message);
184
        }
185
        return $this->goHome();
186
    }
187
}
188