Completed
Push — master ( d8e37a...ffe2ad )
by Igor
11:28 queued 06:52
created

IndexController::actionSignup()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.1574

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 23
ccs 11
cts 14
cp 0.7856
rs 8.7972
cc 4
eloc 15
nc 3
nop 0
crap 4.1574
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 52
    public function behaviors()
25
    {
26
        return [
27
            'access' => [
28 52
                '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 52
            ],
43
            'verbs' => [
44 52
                'class' => VerbFilter::className(),
45
                'actions' => [
46
                    'logout' => ['post'],
47
                ],
48
            ],
49
        ];
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55 52
    public function actions()
56
    {
57
        return [
58
            'error' => [
59
                'class' => 'yii\web\ErrorAction',
60 52
            ],
61
            'auth' => [
62 52
                'class' => 'yii\authclient\AuthAction',
63 52
                'successCallback' => [$this, 'successCallback'],
64 52
                'successUrl' => 'signup-provider'
65
            ],
66
        ];
67
    }
68
69 7
    public function successCallback($provider)
70
    {
71 7
        Yii::$app->session['provider'] = null;
72 7
        Yii::$app->session['blocked'] = false;
73
74 7
        $type = UserProvider::getTypeByName($provider->id);
75 7
        $profile = $provider->getUserAttributes();
76 7
        $token = $provider->getAccessToken()->getParams();
77
        $data = [
78 7
            'type' => $type,
79 7
            'profile' => $profile,
80 7
            'token' => $token
81
        ];
82
83 7
        if ($user = User::findByProvider($type, $profile['id'])) {
84 2
            if ($user->isActive()) {
85 1
                $user->updateProvider(UserProvider::parseProvider($type, $data));
86 1
                $user->authorize(true);
87
            } else {
88 1
                Yii::$app->session['blocked'] = true;
89 2
                Yii::$app->session['message'] = $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...
90
            }
91
        } else {
92 7
            Yii::$app->session['provider'] = $data;
93
        }
94 7
    }
95
96 20
    public function actionIndex()
97
    {
98 20
        return $this->render('index');
99
    }
100
101 16
    public function actionLogin()
102
    {
103 16
        if (!Yii::$app->user->isGuest) {
104 1
            return $this->goHome();
105
        }
106
107 16
        $model = new LoginForm();
108 16
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
109 3
            return $this->goBack();
110
        } else {
111 16
            return $this->render('login', [
112 16
                'model' => $model,
113
            ]);
114
        }
115
    }
116
117 13
    public function actionSignup()
118
    {
119 13
        $model = new SignupForm();
120 13
        if ($model->load(Yii::$app->request->post()) && $model->signup()) {
121 2
            if ($model->sendEmail()) {
122 2
                Yii::$app->session->setFlash(
123 2
                    'success',
124 2
                    Yii::t('app.messages', 'Please activate your account') . '. ' .
125 2
                    Yii::t('app.messages', 'A letter for activation was sent to {email}', ['email' => $model->email])
126
                );
127
            } else {
128
                Yii::$app->session->setFlash(
129
                    'error',
130
                    Yii::t('app.messages', 'An error occurred while sending a message to activate account')
131
                );
132
            }
133 2
            return $this->goHome();
134
        }
135
136 13
        return $this->render('signup', [
137 13
            'model' => $model,
138
        ]);
139
    }
140
141 7
    public function actionSignupProvider()
142
    {
143 7
        if (Yii::$app->session['blocked']) {
144 1
            Yii::$app->session->setFlash('error', Yii::$app->session['message']);
145 1
            return $this->goHome();
146
        }
147
148 7
        if (!Yii::$app->user->isGuest || Yii::$app->session['provider'] === null) {
149 1
            return $this->goHome();
150
        }
151
152 7
        $model = new SignupProviderForm(Yii::$app->session['provider']);
153
154 7
        if ($model->isVerified() && $model->signup(false)) {
155 1
            Yii::$app->session['provider'] = null;
156 1
            return $this->goHome();
157
        }
158
159 6
        if ($model->load(Yii::$app->request->post()) && $model->signup()) {
160 4
            Yii::$app->session['provider'] = null;
161 4
            if ($model->sendEmail()) {
162 4
                Yii::$app->session->setFlash(
0 ignored issues
show
Bug introduced by
The method setFlash cannot be called on \Yii::$app->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...
163 4
                    'success',
164 4
                    Yii::t('app.messages', 'Please activate your account') . '. ' .
165 4
                    Yii::t('app.messages', 'A letter for activation was sent to {email}', ['email' => $model->email])
166
                );
167
            } else {
168
                Yii::$app->session->setFlash(
0 ignored issues
show
Bug introduced by
The method setFlash cannot be called on \Yii::$app->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...
169
                    'error',
170
                    Yii::t('app.messages', 'An error occurred while sending a message to activate account')
171
                );
172
            }
173 4
            return $this->goHome();
174
        }
175
176 6
        return $this->render('signupProvider', [
177 6
            'model' => $model
178
        ]);
179
    }
180
181 2
    public function actionConfirmRequest()
182
    {
183 2
        $user = Yii::$app->user->identity;
184 2
        if ($user->isConfirmed()) {
185 1
            Http::exception(403);
186
        } // @codeCoverageIgnore
187
188 1
        $model = new ConfirmEmailForm();
189
190 1
        if ($model->sendEmail($user)) {
191 1
            Yii::$app->session->setFlash(
192 1
                'success',
193 1
                Yii::t('app.messages', 'A letter for activation was sent to {email}', [
194 1
                    'email' => $user->email
195
                ])
196
            );
197
        } else {
198
            Yii::$app->session->setFlash(
199
                'error',
200
                Yii::t('app.messages', 'An error occurred while sending a message to activate account')
201
            );
202
        }
203 1
        return $this->goHome();
204
    }
205
206 3
    public function actionConfirmEmail($token)
207
    {
208 3
        $model = new ConfirmEmailForm();
209
210 3
        if (!$model->validateToken($token)) {
211 2
            Yii::$app->session->setFlash(
212 2
                'error',
213 2
                Yii::t('app.messages', 'Invalid link for activate account')
214
            );
215 2
            return $this->goHome();
216
        }
217
218 1
        if ($model->confirmEmail()) {
219 1
            Yii::$app->session->setFlash(
220 1
                'success',
221 1
                Yii::t('app.messages', 'Your account is successfully activated')
222
            );
223
        } else {
224
            Yii::$app->session->setFlash(
225
                'error',
226
                Yii::t('app.messages', 'An error occurred while activating account')
227
            );
228
        }
229 1
        return $this->goHome();
230
    }
231
232 7
    public function actionRequestPasswordReset()
233
    {
234 7
        $model = new PasswordResetRequestForm();
235
236 7
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
237 1
            if ($model->sendEmail()) {
238 1
                Yii::$app->session->setFlash(
239 1
                    'success',
240 1
                    Yii::t('app.messages', 'We\'ve sent you an email with instructions to reset your password')
241
                );
242
            } else {
243
                Yii::$app->session->setFlash(
244
                    'error',
245
                    Yii::t('app.messages', 'An error occurred while sending a message to reset your password')
246
                );
247
            }
248 1
            return $this->goHome();
249
        }
250
251 7
        return $this->render('requestPasswordResetToken', [
252 7
            'model' => $model,
253
        ]);
254
    }
255
256 6
    public function actionResetPassword($token)
257
    {
258 6
        $model = new ResetPasswordForm();
259
260 6
        if (!$model->validateToken($token)) {
261 2
            Yii::$app->session->setFlash(
262 2
                'error',
263 2
                Yii::t('app.messages', 'Invalid link for reset password')
264
            );
265 2
            return $this->goHome();
266
        }
267
268 6
        if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
269 1
            Yii::$app->session->setFlash(
270 1
                'success',
271 1
                Yii::t('app', 'New password was saved')
272
            );
273 1
            return $this->goHome();
274
        }
275
276 6
        return $this->render('resetPassword', [
277 6
            'model' => $model,
278
        ]);
279
    }
280
281 1
    public function actionLogout()
282
    {
283 1
        Yii::$app->user->logout();
284 1
        return $this->goHome();
285
    }
286
287
    /** @see commands/MaintenanceController **/
288 2
    public function actionMaintenance()
289
    {
290 2
        if (!Yii::$app->catchAll) {
291 1
            Http::exception(404);
292
        } // @codeCoverageIgnore
293
294 1
        $this->layout = 'maintenance';
295 1
        return $this->render('maintenance');
296
    }
297
}
298