Completed
Push — master ( b61081...bf01ff )
by Pavel
04:03
created

AccountController::actionPassword()   D

Complexity

Conditions 9
Paths 9

Size

Total Lines 41
Code Lines 23

Duplication

Lines 6
Ratio 14.63 %

Importance

Changes 0
Metric Value
dl 6
loc 41
c 0
b 0
f 0
rs 4.909
cc 9
eloc 23
nc 9
nop 1
1
<?php
2
3
namespace inblank\activeuser\controllers;
4
5
use inblank\activeuser\models\forms\LoginForm;
6
use inblank\activeuser\models\forms\RegisterForm;
7
use inblank\activeuser\models\forms\ResendForm;
8
use inblank\activeuser\models\forms\RestoreForm;
9
use inblank\activeuser\traits\CommonTrait;
10
use yii;
11
use yii\web\Controller;
12
13
class AccountController extends Controller
14
{
15
16
    use CommonTrait;
17
18
    /**
19
     * User registering action
20
     */
21
    public function actionRegister()
22
    {
23
        if (!$this->module->isRegistrationEnabled()) {
24
            return $this->render('registerDisable');
25
        }
26
        $flashMessageId = 'activeuser_register';
27
28
        $email = Yii::$app->session->getFlash($flashMessageId);
29 View Code Duplication
        if (!empty($email)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
            return $this->render('registerAfter', [
31
                'user' => Yii::createObject(self::di('User'))->findOne(['email' => $email]),
0 ignored issues
show
Documentation introduced by
self::di('User') is of type *, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
32
            ]);
33
        }
34
        /** @var RegisterForm $model */
35
        $model = Yii::createObject(RegisterForm::className());
36 View Code Duplication
        if ($model->load(Yii::$app->getRequest()->post()) && $model->register()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
            // congratulation and instruction
38
            Yii::$app->session->setFlash($flashMessageId, $model->email);
39
            return $this->redirect(['/activeuser/account/register']);
40
        }
41
        return $this->render('register', [
42
            'model' => $model,
43
        ]);
44
    }
45
46
    /**
47
     * User login action
48
     */
49
    public function actionLogin()
50
    {
51
        // TODO filter too many login request
52
        /** @var LoginForm $model */
53
        $model = Yii::createObject(LoginForm::className());
54
        if ($model->load(Yii::$app->getRequest()->post()) && $model->login()) {
55
            return $this->goBack();
56
        }
57
        return $this->render('login', [
58
            'model' => $model,
59
        ]);
60
    }
61
62
    /**
63
     * User logout action
64
     */
65
    public function actionLogout()
66
    {
67
68
    }
69
70
    /**
71
     * Request password change
72
     * @return string|yii\web\Response
73
     * @throws yii\base\InvalidConfigException
74
     * @throws yii\web\NotFoundHttpException
75
     */
76
    public function actionRestore()
77
    {
78
        // TODO filter too many restore request and set in Module period for restore
79
        if (!$this->getModule()->enablePasswordRestore) {
80
            throw new yii\web\NotFoundHttpException();
81
        }
82
        $flashMessageId = 'activeuser_restore_sent';
83
        $email = Yii::$app->session->getFlash($flashMessageId);
84
        if ($email !== null) {
85
            // message with instructions sent
86
            return $this->render('restoreSent', [
87
                'email' => $email,
88
            ]);
89
        }
90
91
        /** @var RestoreForm $model */
92
        $model = Yii::createObject(RestoreForm::className());
93
        $model->setScenario($model::SCENARIO_EMAIL);
94 View Code Duplication
        if ($model->load(Yii::$app->getRequest()->post()) && $model->restore()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
            Yii::$app->session->setFlash($flashMessageId, $model->email);
96
            return $this->redirect(['/activeuser/account/restore']);
97
        }
98
        $error = Yii::$app->session->getFlash('activeuser_error');
99
        if (!empty($error) && !empty($error['token'])) {
100
            $error = $error['token'][0];
101
        } else {
102
            $error = null;
103
        }
104
        return $this->render('restore', [
105
            'model' => $model,
106
            'error' => $error,
107
        ]);
108
    }
109
110
    /**
111
     * Change user password
112
     * @param string $token user token for restore
113
     * @return string|yii\web\Response
114
     * @throws yii\base\InvalidConfigException
115
     * @throws yii\web\NotFoundHttpException
116
     */
117
    public function actionPassword($token = null)
118
    {
119
        $flashMessageId = 'activeuser_restore';
120
121
        $email = Yii::$app->session->getFlash($flashMessageId);
122 View Code Duplication
        if (!empty($email)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
123
            // congratulation message
124
            return $this->render('restoreComplete', [
125
                'user' => Yii::createObject(self::di('User'))->findOne(['email' => $email]),
0 ignored issues
show
Documentation introduced by
self::di('User') is of type *, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
126
            ]);
127
        }
128
129
        /** @var \inblank\activeuser\models\User $user */
130
        if (!$this->getModule()->enablePasswordRestore || $token === null || !($user = Yii::createObject(self::di('User'))->findByToken($token))) {
0 ignored issues
show
Documentation introduced by
self::di('User') is of type *, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
131
            throw new yii\web\NotFoundHttpException();
132
        }
133
134
        if ($user->isRestoreTokenExpired()) {
135
            Yii::$app->session->setFlash('activeuser_error', ['token' => Yii::t('activeuser_general', 'You token was expired')]);
136
            return $this->redirect(['/activeuser/account/restore']);
137
        }
138
139
        /** @var RestoreForm $model */
140
        $model = Yii::createObject(RestoreForm::className());
141
        $model->setScenario($model::SCENARIO_PASSWORD);
142
        if ($this->getModule()->generatePassOnRestore) {
143
            // password auto generation
144
            $changed = $user->changePassword();
145
        } else {
146
            // change to user entered
147
            $model->email = $user->email;
148
            $changed = $model->load(Yii::$app->getRequest()->post()) && $model->changePassword();
149
        }
150
        if ($changed) {
151
            Yii::$app->session->setFlash($flashMessageId, $user->email);
152
            return $this->redirect(['/activeuser/account/password']);
153
        }
154
        return $this->render('restorePass', [
155
            'model' => $model
156
        ]);
157
    }
158
159
    /**
160
     * User resend confirmation message
161
     */
162
    public function actionResend()
163
    {
164
        // TODO filter too many resend request and set in Module period for resend
165
        if(!$this->getModule()->enableConfirmation){
166
            throw new yii\web\NotFoundHttpException();
167
        }
168
169
        $email = Yii::$app->session->getFlash('resend');
170
        if($email!==null){
171
            // already sent
172
            return $this->render('resendComplete', [
173
                'email' => $email,
174
            ]);
175
        }
176
177
        // new resend
178
        /** @var ResendForm $model */
179
        $model = Yii::createObject(ResendForm::className());
180 View Code Duplication
        if ($model->load(Yii::$app->getRequest()->post()) && $model->resend()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
181
            // resend complete, even if the user is not found
182
            Yii::$app->session->setFlash('resend', $model->email);
183
            return $this->redirect(['/activeuser/account/resend']);
184
        }
185
        return $this->render('resend', [
186
            'model' => $model,
187
        ]);
188
    }
189
190
    /**
191
     * Confirm email page
192
     * @param string $token user token for confirm email
193
     * @return string
194
     * @throws yii\base\InvalidConfigException
195
     */
196
    public function actionConfirm($token = '')
197
    {
198
        /** @var \inblank\activeuser\models\User $user */
199
        $user = Yii::createObject(self::di('User'))->findByToken($token);
0 ignored issues
show
Documentation introduced by
self::di('User') is of type *, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
200
        if ($user !== null && $user->confirm()) {
201
            return $this->render('confirm', ['user' => $user]);
202
        }
203
        return $this->render('confirmWrong');
204
    }
205
206
}
207