Completed
Push — master ( d106c9...c78ba5 )
by Igor
05:11
created

IndexController::behaviors()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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

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...
147 1
                $user->authorize(true);
148 1
                return $this->goHome();
149
            }
150 1
            $session->setFlash('error', $user->getStatusDescription());
0 ignored issues
show
Bug introduced by
The method getStatusDescription cannot be called on $user (of type array|boolean).

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...
Bug introduced by
The method setFlash cannot be called on $session (of type array<string,null,{"provider":"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...
151 1
            return $this->goHome();
152
        }
153
154 8
        $model->prepareUser();
155
156 8
        if ($model->isVerifiedEmail() && $model->signup(false)) {
157 1
            $session['provider'] = null;
158 1
            return $this->goHome();
159
        }
160
161 7
        if ($model->load(Yii::$app->request->post()) && $model->signup()) {
162 5
            $session['provider'] = null;
163 5
            if ($model->sendEmail()) {
164 4
                $session->setFlash(
0 ignored issues
show
Bug introduced by
The method setFlash cannot be called on $session (of type array<string,null,{"provider":"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...
165 4
                    'success',
166 4
                    Yii::t(
167 4
                        'app.messages',
168 4
                        'Please activate your account'
169 4
                    ) . '. ' .
170 4
                    Yii::t(
171 4
                        'app.messages',
172 4
                        'A letter for activation was sent to {email}',
173 4
                        ['email' => $model->email]
174
                    )
175
                );
176 4
                return $this->goHome();
177
            }
178 1
            $session->setFlash(
0 ignored issues
show
Bug introduced by
The method setFlash cannot be called on $session (of type array<string,null,{"provider":"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 1
                'error',
180 1
                Yii::t(
181 1
                    'app.messages',
182 1
                    'An error occurred while sending a message to activate account'
183
                )
184
            );
185 1
            return $this->goHome();
186
        }
187
188 7
        return $this->render('signupProvider', [
189 7
            'model' => $model
190
        ]);
191
    }
192
193 3
    public function actionConfirmRequest()
194
    {
195 3
        $user = Yii::$app->user->identity;
196 3
        if ($user->isConfirmed()) {
197 1
            Http::exception(403);
198
        } // @codeCoverageIgnore
199
200 2
        $model = new ConfirmEmailForm();
201
202 2
        if ($model->sendEmail($user)) {
203 1
            Yii::$app->session->setFlash(
204 1
                'success',
205 1
                Yii::t(
206 1
                    'app.messages',
207 1
                    'A letter for activation was sent to {email}',
208 1
                    ['email' => $user->email]
209
                )
210
            );
211 1
            return $this->goHome();
212
        }
213 1
        Yii::$app->session->setFlash(
214 1
            'error',
215 1
            Yii::t(
216 1
                'app.messages',
217 1
                'An error occurred while sending a message to activate account'
218
            )
219
        );
220 1
        return $this->goHome();
221
    }
222
223 3
    public function actionConfirmEmail($token)
224
    {
225 3
        $model = new ConfirmEmailForm();
226
227 3
        if (!$model->validateToken($token)) {
228 2
            Yii::$app->session->setFlash(
229 2
                'error',
230 2
                Yii::t('app.messages', 'Invalid link for activate account')
231
            );
232 2
            return $this->goHome();
233
        }
234
235 1
        if ($model->confirmEmail()) {
236 1
            Yii::$app->session->setFlash(
237 1
                'success',
238 1
                Yii::t('app.messages', 'Your account is successfully activated')
239
            );
240
        }
241 1
        return $this->goHome();
242
    }
243
244 8
    public function actionRequestPasswordReset()
245
    {
246 8
        $model = new PasswordResetRequestForm();
247
248 8
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
249 2
            if ($model->sendEmail()) {
250 1
                Yii::$app->session->setFlash(
251 1
                    'success',
252 1
                    Yii::t(
253 1
                        'app.messages',
254 1
                        'We\'ve sent you an email with instructions to reset your password'
255
                    )
256
                );
257 1
                return $this->goHome();
258
            }
259 1
            Yii::$app->session->setFlash(
260 1
                'error',
261 1
                Yii::t(
262 1
                    'app.messages',
263 1
                    'An error occurred while sending a message to reset your password'
264
                )
265
            );
266 1
            return $this->goHome();
267
        }
268
269 8
        return $this->render('requestPasswordResetToken', [
270 8
            'model' => $model,
271
        ]);
272
    }
273
274 6
    public function actionResetPassword($token)
275
    {
276 6
        $model = new ResetPasswordForm();
277
278 6
        if (!$model->validateToken($token)) {
279 2
            Yii::$app->session->setFlash(
280 2
                'error',
281 2
                Yii::t('app.messages', 'Invalid link for reset password')
282
            );
283 2
            return $this->goHome();
284
        }
285
286 6
        if ($model->load(Yii::$app->request->post()) &&
287 6
            $model->validate() &&
288 6
            $model->resetPassword()
289
        ) {
290 1
            Yii::$app->session->setFlash(
291 1
                'success',
292 1
                Yii::t('app', 'New password was saved')
293
            );
294 1
            return $this->goHome();
295
        }
296
297 6
        return $this->render('resetPassword', [
298 6
            'model' => $model,
299
        ]);
300
    }
301
302 1
    public function actionLogout()
303
    {
304 1
        Yii::$app->user->logout();
305 1
        return $this->goHome();
306
    }
307
308
    /** @see commands/MaintenanceController **/
309 2
    public function actionMaintenance()
310
    {
311 2
        if (!Yii::$app->catchAll) {
312 1
            Http::exception(404);
313
        } // @codeCoverageIgnore
314
315 1
        $this->layout = 'maintenance';
316 1
        return $this->render('maintenance');
317
    }
318
}
319