Completed
Push — master ( 40239d...b4bd21 )
by Igor
07:24
created

IndexController::actionConfirmEmail()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 6
Bugs 0 Features 1
Metric Value
c 6
b 0
f 1
dl 0
loc 20
ccs 12
cts 12
cp 1
rs 9.4285
cc 3
eloc 12
nc 3
nop 1
crap 3
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', 'auth'],
30
                'rules' => [
31
                    [
32
                        'actions' => ['signup', '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 25
    public function actionIndex()
79
    {
80 25
        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
        } else {
93 16
            return $this->render('login', [
94 16
                'model' => $model,
95
            ]);
96
        }
97
    }
98
99 15
    public function actionSignup()
100
    {
101 15
        $model = new SignupForm();
102 15
        if ($model->load(Yii::$app->request->post()) && $model->signup()) {
103 3
            if ($model->sendEmail()) {
104 1
                Yii::$app->session->setFlash(
105 1
                    'success',
106 1
                    Yii::t(
107 1
                        'app.messages',
108 1
                        'Please activate your account'
109 1
                    ) . '. ' .
110 1
                    Yii::t(
111 1
                        'app.messages',
112 1
                        'A letter for activation was sent to {email}',
113 1
                        ['email' => $model->email]
114
                    )
115
                );
116
            } else {
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
            }
125 3
            return $this->goHome();
126
        }
127
128 15
        return $this->render('signup', [
129 15
            'model' => $model,
130
        ]);
131
    }
132
133 9
    public function actionSignupProvider()
134
    {
135 9
        $session = Yii::$app->session;
136 9
        $provider = $session['provider'];
137 9
        if (!Yii::$app->user->isGuest || $provider === null) {
138 1
            return $this->goHome();
139
        }
140
141 8
        $model = new SignupProviderForm($provider);
142
143
        // check exist user and provider
144 8
        if ($user = User::findByProvider($provider['type'], $provider['profile']['id'])) {
145 2
            $session['provider'] = null;
146 2
            if ($user->isActive()) {
147 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...
148 1
                $user->authorize(true);
149 1
                return $this->goHome();
150
            } else {
151 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...
152 1
                return $this->goHome();
153
            }
154
        }
155
156 8
        $model->prepareUser();
157
158 8
        if ($model->isVerifiedEmail() && $model->signup(false)) {
159 1
            $session['provider'] = null;
160 1
            return $this->goHome();
161
        }
162
163 7
        if ($model->load(Yii::$app->request->post()) && $model->signup()) {
164 5
            if ($model->sendEmail()) {
165 4
                $session->setFlash(
166 4
                    'success',
167 4
                    Yii::t(
168 4
                        'app.messages',
169 4
                        'Please activate your account'
170 4
                    ) . '. ' .
171 4
                    Yii::t(
172 4
                        'app.messages',
173 4
                        'A letter for activation was sent to {email}',
174 4
                        ['email' => $model->email]
175
                    )
176
                );
177
            } else {
178 1
                $session->setFlash(
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
            }
186 5
            $session['provider'] = null;
187 5
            return $this->goHome();
188
        }
189
190 7
        return $this->render('signupProvider', [
191 7
            'model' => $model
192
        ]);
193
    }
194
195 3
    public function actionConfirmRequest()
196
    {
197 3
        $user = Yii::$app->user->identity;
198 3
        if ($user->isConfirmed()) {
199 1
            Http::exception(403);
200
        } // @codeCoverageIgnore
201
202 2
        $model = new ConfirmEmailForm();
203
204 2
        if ($model->sendEmail($user)) {
205 1
            Yii::$app->session->setFlash(
206 1
                'success',
207 1
                Yii::t(
208 1
                    'app.messages',
209 1
                    'A letter for activation was sent to {email}',
210 1
                    ['email' => $user->email]
211
                )
212
            );
213
        } else {
214 1
            Yii::$app->session->setFlash(
215 1
                'error',
216 1
                Yii::t(
217 1
                    'app.messages',
218 1
                    'An error occurred while sending a message to activate account'
219
                )
220
            );
221
        }
222 2
        return $this->goHome();
223
    }
224
225 3
    public function actionConfirmEmail($token)
226
    {
227 3
        $model = new ConfirmEmailForm();
228
229 3
        if (!$model->validateToken($token)) {
230 2
            Yii::$app->session->setFlash(
231 2
                'error',
232 2
                Yii::t('app.messages', 'Invalid link for activate account')
233
            );
234 2
            return $this->goHome();
235
        }
236
237 1
        if ($model->confirmEmail()) {
238 1
            Yii::$app->session->setFlash(
239 1
                'success',
240 1
                Yii::t('app.messages', 'Your account is successfully activated')
241
            );
242
        }
243 1
        return $this->goHome();
244
    }
245
246 8
    public function actionRequestPasswordReset()
247
    {
248 8
        $model = new PasswordResetRequestForm();
249
250 8
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
251 2
            if ($model->sendEmail()) {
252 1
                Yii::$app->session->setFlash(
253 1
                    'success',
254 1
                    Yii::t(
255 1
                        'app.messages',
256 1
                        'We\'ve sent you an email with instructions to reset your password'
257
                    )
258
                );
259
            } else {
260 1
                Yii::$app->session->setFlash(
261 1
                    'error',
262 1
                    Yii::t(
263 1
                        'app.messages',
264 1
                        'An error occurred while sending a message to reset your password'
265
                    )
266
                );
267
            }
268 2
            return $this->goHome();
269
        }
270
271 8
        return $this->render('requestPasswordResetToken', [
272 8
            'model' => $model,
273
        ]);
274
    }
275
276 6
    public function actionResetPassword($token)
277
    {
278 6
        $model = new ResetPasswordForm();
279
280 6
        if (!$model->validateToken($token)) {
281 2
            Yii::$app->session->setFlash(
282 2
                'error',
283 2
                Yii::t('app.messages', 'Invalid link for reset password')
284
            );
285 2
            return $this->goHome();
286
        }
287
288 6
        if ($model->load(Yii::$app->request->post()) &&
289 6
            $model->validate() &&
290 6
            $model->resetPassword()
291
        ) {
292 1
            Yii::$app->session->setFlash(
293 1
                'success',
294 1
                Yii::t('app', 'New password was saved')
295
            );
296 1
            return $this->goHome();
297
        }
298
299 6
        return $this->render('resetPassword', [
300 6
            'model' => $model,
301
        ]);
302
    }
303
304 1
    public function actionLogout()
305
    {
306 1
        Yii::$app->user->logout();
307 1
        return $this->goHome();
308
    }
309
310
    /** @see commands/MaintenanceController **/
311 2
    public function actionMaintenance()
312
    {
313 2
        if (!Yii::$app->catchAll) {
314 1
            Http::exception(404);
315
        } // @codeCoverageIgnore
316
317 1
        $this->layout = 'maintenance';
318 1
        return $this->render('maintenance');
319
    }
320
}
321