Completed
Push — master ( f733ed...c064eb )
by Igor
04:01
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.messages',
131 2
                        'Please activate your account'
132 2
                    ) . '. ' .
133 2
                    Yii::t(
134 2
                        'app.messages',
135 2
                        'A letter for activation was sent to {email}',
136 2
                        ['email' => $model->email]
137
                    )
138
                );
139 2
                return $this->goHome();
140
            }
141
            Yii::$app->session->setFlash(
142
                'error',
143
                Yii::t(
144
                    'app.messages',
145
                    'An error occurred while sending a message to activate account'
146
                )
147
            );
148
            return $this->goHome();
149
        }
150
151 13
        return $this->render('signup', [
152 13
            'model' => $model,
153
        ]);
154
    }
155
156
    public function actionSignupProvider()
157
    {
158
        $session = Yii::$app->session;
159
        if ($session['authClient'] === null) {
160
            return $this->goHome();
161
        }
162
163
        $this->socialAuth->execute($session['authClient']);
164
        $user = $this->socialAuth->user();
165
166
        if ($user === null) {
167
            return $this->goHome();
168
        }
169
170
        $model = new SignupProviderForm($user, $this->socialAuth->email());
171
172
        if ($this->socialAuth->isExist() && $user->isActive() === false) {
173
            $session->setFlash('error', $user->getStatusDescription());
174
            return $this->goHome();
175
        }
176
177
        if ($this->socialAuth->isExist() && $user->isActive() && $model->login()) {
178
            return $this->goHome();
179
        }
180
181
        if ($this->socialAuth->isVerified() && $model->saveUser() && $model->login()) {
182
            return $this->goHome();
183
        }
184
185
        if ($model->load(Yii::$app->request->post()) && $model->signup()) {
186
            $model->login();
187
188
            if ($model->sendEmail()) {
189
                $session->setFlash(
190
                    'success',
191
                    Yii::t('app.messages', 'Please activate your account') . '. ' .
192
                    Yii::t('app.messages', 'A letter for activation was sent to {email}', ['email' => $model->email])
193
                );
194
                return $this->goHome();
195
            }
196
            $session->setFlash(
197
                'error',
198
                Yii::t('app.messages', 'An error occurred while sending a message to activate account')
199
            );
200
            return $this->goHome();
201
        }
202
203
        return $this->render('signupProvider', [
204
            'model' => $model
205
        ]);
206
    }
207
208 2
    public function actionConfirmRequest()
209
    {
210 2
        $user = Yii::$app->user->identity;
211 2
        if ($user->isConfirmed()) {
212 1
            throw new ForbiddenHttpException(Yii::t('app', 'Access Denied'));
213
        } // @codeCoverageIgnore
214
215 1
        $model = new ConfirmEmailForm();
216
217 1
        if ($model->sendEmail($user)) {
218 1
            Yii::$app->session->setFlash(
219 1
                'success',
220 1
                Yii::t('app.messages', 'A letter for activation was sent to {email}', ['email' => $user->email])
221
            );
222 1
            return $this->goHome();
223
        }
224
        Yii::$app->session->setFlash(
225
            'error',
226
            Yii::t('app.messages', 'An error occurred while sending a message to activate account')
227
        );
228
        return $this->goHome();
229
    }
230
231 3
    public function actionConfirmEmail($token)
232
    {
233 3
        $model = new ConfirmEmailForm();
234
235 3
        if (!$model->validateToken($token)) {
236 2
            Yii::$app->session->setFlash(
237 2
                'error',
238 2
                Yii::t('app.messages', 'Invalid link for activate account')
239
            );
240 2
            return $this->goHome();
241
        }
242
243 1
        if ($model->confirmEmail()) {
244 1
            Yii::$app->session->setFlash(
245 1
                'success',
246 1
                Yii::t('app.messages', 'Your account is successfully activated')
247
            );
248
        }
249 1
        return $this->goHome();
250
    }
251
252 7
    public function actionRequestPasswordReset()
253
    {
254 7
        $model = new PasswordResetRequestForm();
255
256 7
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
257 1
            if ($model->sendEmail()) {
258 1
                Yii::$app->session->setFlash(
259 1
                    'success',
260 1
                    Yii::t('app.messages', 'We\'ve sent you an email with instructions to reset your password')
261
                );
262 1
                return $this->goHome();
263
            }
264
            Yii::$app->session->setFlash(
265
                'error',
266
                Yii::t('app.messages', 'An error occurred while sending a message to reset your password')
267
            );
268
            return $this->goHome();
269
        }
270
271 7
        return $this->render('requestPasswordResetToken', [
272 7
            '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 3
            $model->validate() &&
290 1
            $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
            throw new NotFoundHttpException(Yii::t('app', 'Page not found'));
315
        } // @codeCoverageIgnore
316
317 1
        $this->layout = 'maintenance';
318 1
        return $this->render('maintenance');
319
    }
320
}
321