Completed
Push — master ( c064eb...85769a )
by Igor
03:38
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 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
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 yii\web\ForbiddenHttpException;
9
use yii\web\NotFoundHttpException;
10
use app\services\SocialAuth;
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 \yii\web\Controller
19
{
20
    private $socialAuth;
21
22 45
    public function __construct($id, $module, SocialAuth $socialAuth, $config = [])
23
    {
24 45
        $this->socialAuth = $socialAuth;
25 45
        parent::__construct($id, $module, $config);
26 45
    }
27
28
    /**
29
     * @inheritdoc
30
     */
31 45
    public function behaviors()
32
    {
33
        return [
34 45
            'access' => [
35
                'class' => AccessControl::class,
36
                'only' => [
37
                    'auth',
38
                    'logout',
39
                    'signup',
40
                    'signup-provider',
41
                    'confirm-request',
42
                    'request-password-reset',
43
                ],
44
                'rules' => [
45
                    [
46
                        'actions' => [
47
                            'auth',
48
                            'signup',
49
                            'signup-provider',
50
                            'request-password-reset',
51
                        ],
52
                        'allow' => true,
53
                        'roles' => ['?'],
54
                    ],
55
                    [
56
                        'actions' => [
57
                            'logout',
58
                            'confirm-request'
59
                        ],
60
                        'allow' => true,
61
                        'roles' => ['@'],
62
                    ],
63
                ],
64
            ],
65
            'verbs' => [
66
                'class' => VerbFilter::class,
67
                'actions' => [
68
                    'logout' => ['post'],
69
                ],
70
            ],
71
        ];
72
    }
73
74
    /**
75
     * @inheritdoc
76
     */
77 45
    public function actions()
78
    {
79
        return [
80 45
            'error' => [
81
                'class' => 'yii\web\ErrorAction',
82
            ],
83
            'auth' => [
84 45
                'class' => 'yii\authclient\AuthAction',
85 45
                'successCallback' => [$this, 'successCallback'],
86 45
                'successUrl' => 'signup-provider'
87
            ],
88
        ];
89
    }
90
91
    public function successCallback($client)
92
    {
93
        Yii::$app->session['authClient'] = $client;
94
    }
95
96 11
    public function goHome()
97
    {
98 11
        Yii::$app->session['authClient'] = null;
99 11
        return Yii::$app->getResponse()->redirect(Yii::$app->getHomeUrl());
0 ignored issues
show
Bug introduced by
The method getHomeUrl does only exist in yii\web\Application, but not in yii\console\Application.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
100
    }
101
102 15
    public function actionIndex()
103
    {
104 15
        return $this->render('index');
105
    }
106
107 16
    public function actionLogin()
108
    {
109 16
        if (!Yii::$app->user->isGuest) {
110 1
            return $this->goHome();
111
        }
112
113 16
        $model = new LoginForm();
114 16
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
115 3
            return $this->goBack();
116
        }
117 16
        return $this->render('login', [
118 16
            'model' => $model,
119
        ]);
120
    }
121
122 13
    public function actionSignup()
123
    {
124 13
        $model = new SignupForm();
125 13
        if ($model->load(Yii::$app->request->post()) && $model->signup()) {
126 2
            if ($model->sendEmail()) {
127 2
                Yii::$app->session->setFlash(
128 2
                    'success',
129 2
                    Yii::t(
130 2
                        'app.msg',
131 2
                        'Please activate your account. A letter for activation was sent to {email}',
132 2
                        ['email' => $model->email]
133
                    )
134
                );
135 2
                return $this->goHome();
136
            }
137
            Yii::$app->session->setFlash(
138
                'error',
139
                Yii::t(
140
                    'app.msg',
141
                    'An error occurred while sending a message to activate account'
142
                )
143
            );
144
            return $this->goHome();
145
        }
146
147 13
        return $this->render('signup', [
148 13
            'model' => $model,
149
        ]);
150
    }
151
152
    public function actionSignupProvider()
153
    {
154
        $session = Yii::$app->session;
155
        if ($session['authClient'] === null) {
156
            return $this->goHome();
157
        }
158
159
        $this->socialAuth->execute($session['authClient']);
160
        $user = $this->socialAuth->user();
161
162
        if ($user === null) {
163
            return $this->goHome();
164
        }
165
166
        $model = new SignupProviderForm($user, $this->socialAuth->email());
167
168
        if ($this->socialAuth->isExist() && $user->isActive() === false) {
169
            $session->setFlash('error', $user->getStatusDescription());
170
            return $this->goHome();
171
        }
172
173
        if ($this->socialAuth->isExist() && $user->isActive() && $model->login()) {
174
            return $this->goHome();
175
        }
176
177
        if ($this->socialAuth->isVerified() && $model->saveUser() && $model->login()) {
178
            return $this->goHome();
179
        }
180
181
        if ($model->load(Yii::$app->request->post()) && $model->signup()) {
182
            $model->login();
183
184
            if ($model->sendEmail()) {
185
                $session->setFlash(
186
                    'success',
187
                    Yii::t(
188
                        'app.msg',
189
                        'Please activate your account. A letter for activation was sent to {email}',
190
                        ['email' => $model->email]
191
                    )
192
                );
193
                return $this->goHome();
194
            }
195
            $session->setFlash(
196
                'error',
197
                Yii::t('app.msg', 'An error occurred while sending a message to activate account')
198
            );
199
            return $this->goHome();
200
        }
201
202
        return $this->render('signupProvider', [
203
            'model' => $model
204
        ]);
205
    }
206
207 2
    public function actionConfirmRequest()
208
    {
209 2
        $user = Yii::$app->user->identity;
210 2
        if ($user->isConfirmed()) {
211 1
            throw new ForbiddenHttpException(Yii::t('app.msg', 'Access Denied'));
212
        } // @codeCoverageIgnore
213
214 1
        $model = new ConfirmEmailForm();
215
216 1
        if ($model->sendEmail($user)) {
217 1
            Yii::$app->session->setFlash(
218 1
                'success',
219 1
                Yii::t('app.msg', 'A letter for activation was sent to {email}', ['email' => $user->email])
220
            );
221 1
            return $this->goHome();
222
        }
223
        Yii::$app->session->setFlash(
224
            'error',
225
            Yii::t('app.msg', 'An error occurred while sending a message to activate account')
226
        );
227
        return $this->goHome();
228
    }
229
230 3
    public function actionConfirmEmail($token)
231
    {
232 3
        $model = new ConfirmEmailForm();
233
234 3
        if (!$model->validateToken($token)) {
235 2
            Yii::$app->session->setFlash(
236 2
                'error',
237 2
                Yii::t('app.msg', 'Invalid link for activate account')
238
            );
239 2
            return $this->goHome();
240
        }
241
242 1
        if ($model->confirmEmail()) {
243 1
            Yii::$app->session->setFlash(
244 1
                'success',
245 1
                Yii::t('app.msg', 'Your account is successfully activated')
246
            );
247
        }
248 1
        return $this->goHome();
249
    }
250
251 7
    public function actionRequestPasswordReset()
252
    {
253 7
        $model = new PasswordResetRequestForm();
254
255 7
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
256 1
            if ($model->sendEmail()) {
257 1
                Yii::$app->session->setFlash(
258 1
                    'success',
259 1
                    Yii::t('app.msg', 'We\'ve sent you an email with instructions to reset your password')
260
                );
261 1
                return $this->goHome();
262
            }
263
            Yii::$app->session->setFlash(
264
                'error',
265
                Yii::t('app.msg', 'An error occurred while sending a message to reset your password')
266
            );
267
            return $this->goHome();
268
        }
269
270 7
        return $this->render('requestPasswordResetToken', [
271 7
            'model' => $model,
272
        ]);
273
    }
274
275 6
    public function actionResetPassword($token)
276
    {
277 6
        $model = new ResetPasswordForm();
278
279 6
        if (!$model->validateToken($token)) {
280 2
            Yii::$app->session->setFlash(
281 2
                'error',
282 2
                Yii::t('app.msg', 'Invalid link for reset password')
283
            );
284 2
            return $this->goHome();
285
        }
286
287 6
        if ($model->load(Yii::$app->request->post()) &&
288 3
            $model->validate() &&
289 1
            $model->resetPassword()
290
        ) {
291 1
            Yii::$app->session->setFlash(
292 1
                'success',
293 1
                Yii::t('app.msg', 'New password was saved')
294
            );
295 1
            return $this->goHome();
296
        }
297
298 6
        return $this->render('resetPassword', [
299 6
            'model' => $model,
300
        ]);
301
    }
302
303 1
    public function actionLogout()
304
    {
305 1
        Yii::$app->user->logout();
306 1
        return $this->goHome();
307
    }
308
309
    /** @see commands/MaintenanceController **/
310 2
    public function actionMaintenance()
311
    {
312 2
        if (!Yii::$app->catchAll) {
313 1
            throw new NotFoundHttpException(Yii::t('app.msg', 'Page not found'));
314
        } // @codeCoverageIgnore
315
316 1
        $this->layout = 'maintenance';
317 1
        return $this->render('maintenance');
318
    }
319
}
320