Completed
Push — master ( f45cc5...2edd5b )
by Igor
05:32
created

IndexController::behaviors()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 42
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 42
ccs 4
cts 4
cp 1
rs 8.8571
c 1
b 0
f 0
cc 1
eloc 28
nc 1
nop 0
crap 1
1
<?php
2
3
namespace app\controllers;
4
5
use Yii;
6
use yii\filters\VerbFilter;
7
use yii\filters\AccessControl;
8
use app\components\BaseController;
9
use app\components\auth\AuthHandler;
10
use app\helpers\Http;
11
use app\models\forms\LoginForm;
12
use app\models\forms\SignupForm;
13
use app\models\forms\SignupProviderForm;
14
use app\models\forms\PasswordResetRequestForm;
15
use app\models\forms\ResetPasswordForm;
16
use app\models\forms\ConfirmEmailForm;
17
18
class IndexController extends BaseController
19
{
20
    /**
21
     * @inheritdoc
22
     */
23 58
    public function behaviors()
24
    {
25
        return [
26
            'access' => [
27 58
                'class' => AccessControl::className(),
28
                'only' => [
29
                    'auth',
30
                    'logout',
31
                    'signup',
32
                    'signup-provider',
33
                    'confirm-request',
34
                    'request-password-reset',
35
                ],
36
                'rules' => [
37
                    [
38
                        'actions' => [
39
                            'auth',
40
                            'signup',
41
                            'signup-provider',
42
                            'request-password-reset',
43
                        ],
44
                        'allow' => true,
45
                        'roles' => ['?'],
46
                    ],
47
                    [
48
                        'actions' => [
49
                            'logout',
50
                            'confirm-request'
51
                        ],
52
                        'allow' => true,
53
                        'roles' => ['@'],
54
                    ],
55
                ],
56 58
            ],
57
            'verbs' => [
58 58
                'class' => VerbFilter::className(),
59
                'actions' => [
60
                    'logout' => ['post'],
61
                ],
62
            ],
63
        ];
64
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69 58
    public function actions()
70
    {
71
        return [
72
            'error' => [
73
                'class' => 'yii\web\ErrorAction',
74 58
            ],
75
            'auth' => [
76 58
                'class' => 'yii\authclient\AuthAction',
77 58
                'successCallback' => [$this, 'successCallback'],
78 58
                'successUrl' => 'signup-provider'
79
            ],
80
        ];
81
    }
82
83 8
    public function successCallback($client)
84
    {
85 8
        Yii::$app->session['authClient'] = $client;
86 8
    }
87
88 19
    public function actionIndex()
89
    {
90 19
        return $this->render('index');
91
    }
92
93 16
    public function actionLogin()
94
    {
95 16
        if (!Yii::$app->user->isGuest) {
96 1
            return $this->goHome();
97
        }
98
99 16
        $model = new LoginForm();
100 16
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
101 3
            return $this->goBack();
102
        }
103 16
        return $this->render('login', [
104 16
            'model' => $model,
105
        ]);
106
    }
107
108 15
    public function actionSignup()
109
    {
110 15
        $model = new SignupForm();
111 15
        if ($model->load(Yii::$app->request->post()) && $model->signup()) {
112 3
            if ($model->sendEmail()) {
113 1
                Yii::$app->session->setFlash(
114 1
                    'success',
115 1
                    Yii::t(
116 1
                        'app.messages',
117 1
                        'Please activate your account'
118 1
                    ) . '. ' .
119 1
                    Yii::t(
120 1
                        'app.messages',
121 1
                        'A letter for activation was sent to {email}',
122 1
                        ['email' => $model->email]
123
                    )
124
                );
125 1
                return $this->goHome();
126
            }
127 2
            Yii::$app->session->setFlash(
128 2
                'error',
129 2
                Yii::t(
130 2
                    'app.messages',
131 2
                    'An error occurred while sending a message to activate account'
132
                )
133
            );
134 2
            return $this->goHome();
135
        }
136
137 15
        return $this->render('signup', [
138 15
            'model' => $model,
139
        ]);
140
    }
141
142 9
    public function actionSignupProvider()
143
    {
144 9
        $session = Yii::$app->session;
145 9
        $authClient = $session['authClient'];
146
147 9
        if ($authClient === null) {
148 1
            return $this->goHome();
149
        }
150
151 8
        $authHandler = (new AuthHandler($authClient))->handle();
152
153 8
        $user = $authHandler->getUser();
154
155 8
        if ($authHandler->isExist()) {
156
            $session['authClient'] = null;
157
            if ($user->isActive()) {
158
                $user->authorize(true);
159
                return $this->goHome();
160
            }
161
            $session->setFlash('error', $user->getStatusDescription());
0 ignored issues
show
Bug introduced by
The method setFlash cannot be called on $session (of type array<string,null,{"authClient":"null"}>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
162
            return $this->goHome();
163
        }
164
165 8
        $model = new SignupProviderForm($user);
166 8
        $model->email = $authHandler->getEmail();
167
168 8
        if ($authHandler->isVerified() && $model->signup(false)) {
169
            $user->setConfirmed();
170
            $user->save();
171
            $session['authClient'] = null;
172
            return $this->goHome();
173
        }
174
175 7
        if ($model->load(Yii::$app->request->post()) && $model->signup()) {
176
            $session['authClient'] = null;
177
            if ($model->sendEmail()) {
178
                $session->setFlash(
0 ignored issues
show
Bug introduced by
The method setFlash cannot be called on $session (of type array<string,null,{"authClient":"null"}>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
179
                    'success',
180
                    Yii::t(
181
                        'app.messages',
182
                        'Please activate your account'
183
                    ) . '. ' .
184
                    Yii::t(
185
                        'app.messages',
186
                        'A letter for activation was sent to {email}',
187
                        ['email' => $model->email]
188
                    )
189
                );
190
                return $this->goHome();
191
            }
192
            $session->setFlash(
0 ignored issues
show
Bug introduced by
The method setFlash cannot be called on $session (of type array<string,null,{"authClient":"null"}>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
193
                'error',
194
                Yii::t(
195
                    'app.messages',
196
                    'An error occurred while sending a message to activate account'
197
                )
198
            );
199
            return $this->goHome();
200
        }
201
202 7
        return $this->render('signupProvider', [
203 7
            'model' => $model
204
        ]);
205
    }
206
207 3
    public function actionConfirmRequest()
208
    {
209 3
        $user = Yii::$app->user->identity;
210 3
        if ($user->isConfirmed()) {
211 1
            Http::exception(403);
212
        } // @codeCoverageIgnore
213
214 2
        $model = new ConfirmEmailForm();
215
216 2
        if ($model->sendEmail($user)) {
217 1
            Yii::$app->session->setFlash(
218 1
                'success',
219 1
                Yii::t(
220 1
                    'app.messages',
221 1
                    'A letter for activation was sent to {email}',
222 1
                    ['email' => $user->email]
223
                )
224
            );
225 1
            return $this->goHome();
226
        }
227 1
        Yii::$app->session->setFlash(
228 1
            'error',
229 1
            Yii::t(
230 1
                'app.messages',
231 1
                'An error occurred while sending a message to activate account'
232
            )
233
        );
234 1
        return $this->goHome();
235
    }
236
237 3
    public function actionConfirmEmail($token)
238
    {
239 3
        $model = new ConfirmEmailForm();
240
241 3
        if (!$model->validateToken($token)) {
242 2
            Yii::$app->session->setFlash(
243 2
                'error',
244 2
                Yii::t('app.messages', 'Invalid link for activate account')
245
            );
246 2
            return $this->goHome();
247
        }
248
249 1
        if ($model->confirmEmail()) {
250 1
            Yii::$app->session->setFlash(
251 1
                'success',
252 1
                Yii::t('app.messages', 'Your account is successfully activated')
253
            );
254
        }
255 1
        return $this->goHome();
256
    }
257
258 8
    public function actionRequestPasswordReset()
259
    {
260 8
        $model = new PasswordResetRequestForm();
261
262 8
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
263 2
            if ($model->sendEmail()) {
264 1
                Yii::$app->session->setFlash(
265 1
                    'success',
266 1
                    Yii::t(
267 1
                        'app.messages',
268 1
                        'We\'ve sent you an email with instructions to reset your password'
269
                    )
270
                );
271 1
                return $this->goHome();
272
            }
273 1
            Yii::$app->session->setFlash(
274 1
                'error',
275 1
                Yii::t(
276 1
                    'app.messages',
277 1
                    'An error occurred while sending a message to reset your password'
278
                )
279
            );
280 1
            return $this->goHome();
281
        }
282
283 8
        return $this->render('requestPasswordResetToken', [
284 8
            'model' => $model,
285
        ]);
286
    }
287
288 6
    public function actionResetPassword($token)
289
    {
290 6
        $model = new ResetPasswordForm();
291
292 6
        if (!$model->validateToken($token)) {
293 2
            Yii::$app->session->setFlash(
294 2
                'error',
295 2
                Yii::t('app.messages', 'Invalid link for reset password')
296
            );
297 2
            return $this->goHome();
298
        }
299
300 6
        if ($model->load(Yii::$app->request->post()) &&
301 6
            $model->validate() &&
302 6
            $model->resetPassword()
303
        ) {
304 1
            Yii::$app->session->setFlash(
305 1
                'success',
306 1
                Yii::t('app', 'New password was saved')
307
            );
308 1
            return $this->goHome();
309
        }
310
311 6
        return $this->render('resetPassword', [
312 6
            'model' => $model,
313
        ]);
314
    }
315
316 1
    public function actionLogout()
317
    {
318 1
        Yii::$app->user->logout();
319 1
        return $this->goHome();
320
    }
321
322
    /** @see commands/MaintenanceController **/
323 2
    public function actionMaintenance()
324
    {
325 2
        if (!Yii::$app->catchAll) {
326 1
            Http::exception(404);
327
        } // @codeCoverageIgnore
328
329 1
        $this->layout = 'maintenance';
330 1
        return $this->render('maintenance');
331
    }
332
}
333