Completed
Push — master ( a505a0...5d1dca )
by Fèvre
20s
created

TfaController   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 358
Duplicated Lines 6.98 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 8
dl 25
loc 358
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 3 1
C enable() 0 70 7
A disable() 5 51 3
B recoveryCode() 5 29 3
B generateRecoveryCode() 5 65 4
A _generateNewRecoveryCode() 0 12 1
A intro() 5 21 2
A configure() 5 52 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace App\Controller;
3
4
use App\Event\Logs;
5
use Cake\Core\Configure;
6
use Cake\Event\Event;
7
use Endroid\QrCode\QrCode;
8
use RobThree\Auth\TwoFactorAuth;
9
10
class TfaController extends AppController
11
{
12
    /**
13
     * Display the index action.
14
     *
15
     * @return void
16
     */
17
    public function index()
18
    {
19
    }
20
21
    /**
22
     * Display an intro to the Two-factor Authentication.
23
     *
24
     * @return \Cake\Network\Response|void
25
     */
26
    public function intro()
27
    {
28
        $this->loadModel('Users');
29
30
        $user = $this->Users
0 ignored issues
show
Documentation introduced by
The property Users does not exist on object<App\Controller\TfaController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
31
            ->find()
32
            ->where([
33
                'Users.id' => $this->Auth->user('id')
34
            ])
35
            ->select([
36
                'id',
37
                'two_factor_auth_enabled'
38
            ])
39
            ->first();
40
41 View Code Duplication
        if ($user->two_factor_auth_enabled == true) {
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...
42
            $this->Flash->error(__('You have already set-up the Two-factor authentication.'));
43
44
            return $this->redirect(['controller' => 'users', 'action' => 'security']);
45
        }
46
    }
47
48
    /**
49
     * Configure the Two-factor Authentication.
50
     *
51
     * @return \Cake\Network\Response|void
52
     */
53
    public function configure()
54
    {
55
        $this->loadModel('Users');
56
57
        $user = $this->Users
0 ignored issues
show
Documentation introduced by
The property Users does not exist on object<App\Controller\TfaController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
58
            ->find()
59
            ->contain([
60
                'UsersTwoFactorAuth'
61
            ])
62
            ->where([
63
                'Users.id' => $this->Auth->user('id')
64
            ])
65
            ->select([
66
                'Users.id',
67
                'Users.username',
68
                'Users.two_factor_auth_enabled',
69
                'UsersTwoFactorAuth.id',
70
                'UsersTwoFactorAuth.user_id',
71
                'UsersTwoFactorAuth.secret',
72
                'UsersTwoFactorAuth.username'
73
            ])
74
            ->first();
75
76 View Code Duplication
        if ($user->two_factor_auth_enabled == true) {
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...
77
            $this->Flash->error(__('You have already set-up the Two-factor authentication.'));
78
79
            return $this->redirect(['controller' => 'users', 'action' => 'security']);
80
        }
81
82
        $tfa = new TwoFactorAuth('Xeta');
83
84
        if (!is_null($user->users_two_factor_auth)) {
85
            $secret = $user->users_two_factor_auth->secret;
86
        } else {
87
            $this->loadModel('UsersTwoFactorAuth');
88
            $secret = $tfa->createSecret();
89
90
            $data = [
91
                'user_id' => $this->Auth->user('id'),
92
                'secret' => $secret,
93
                'username' => $user->username
94
            ];
95
96
            $entity = $this->UsersTwoFactorAuth->newEntity($data);
0 ignored issues
show
Documentation introduced by
The property UsersTwoFactorAuth does not exist on object<App\Controller\TfaController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
97
            $this->UsersTwoFactorAuth->save($entity);
0 ignored issues
show
Documentation introduced by
The property UsersTwoFactorAuth does not exist on object<App\Controller\TfaController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
98
        }
99
100
        $imgSrc = $tfa->getQRCodeImageAsDataUri($user->username, $secret);
101
        $secretCode = chunk_split($secret, 4, ' ');
102
103
        $this->set(compact('imgSrc', 'secretCode'));
104
    }
105
106
    /**
107
     * Enable the Two-factor Authentication.
108
     *
109
     * @return \Cake\Network\Response|void
110
     */
111
    public function enable()
112
    {
113
        if (!$this->request->is('post')) {
114
            return $this->redirect(['action' => 'configure']);
115
        }
116
117
        $this->loadModel('UsersTwoFactorAuth');
118
119
        $userTfa = $this->UsersTwoFactorAuth
0 ignored issues
show
Documentation introduced by
The property UsersTwoFactorAuth does not exist on object<App\Controller\TfaController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
120
            ->find()
121
            ->where([
122
                'UsersTwoFactorAuth.user_id' => $this->Auth->user('id')
123
            ])
124
            ->first();
125
126
        if (is_null($userTfa) || empty($userTfa->secret) || !isset($this->request->data['code'])) {
127
            $this->Flash->error(__('Two-factor secret verification failed. Please verify your secret and try again.'));
128
129
            return $this->redirect(['action' => 'configure']);
130
        }
131
132
        $tfa = new TwoFactorAuth('Xeta');
133
134
        if ($tfa->verifyCode($userTfa->secret, $this->request->data['code']) === true && $this->request->data['code'] != $userTfa->current_code) {
135
            $this->loadModel('Users');
136
137
            $user = $this->Users
0 ignored issues
show
Documentation introduced by
The property Users does not exist on object<App\Controller\TfaController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
138
                ->find()
139
                ->where([
140
                    'Users.id' => $this->Auth->user('id')
141
                ])
142
                ->select([
143
                    'id',
144
                    'username',
145
                    'two_factor_auth_enabled'
146
                ])
147
                ->first();
148
149
            $user->two_factor_auth_enabled = true;
150
            $this->Users->save($user);
0 ignored issues
show
Documentation introduced by
The property Users does not exist on object<App\Controller\TfaController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
151
152
            $data = [
153
                'session' => $this->request->clientIp() . $this->request->header('User-Agent') . gethostbyaddr($this->request->clientIp()),
154
                'current_code' => $this->request->data['code'],
155
                'recovery_code' => $this->_generateNewRecoveryCode($userTfa->username)
156
            ];
157
158
            $this->UsersTwoFactorAuth->patchEntity($userTfa, $data);
0 ignored issues
show
Documentation introduced by
The property UsersTwoFactorAuth does not exist on object<App\Controller\TfaController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
159
            $this->UsersTwoFactorAuth->save($userTfa);
0 ignored issues
show
Documentation introduced by
The property UsersTwoFactorAuth does not exist on object<App\Controller\TfaController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
160
161
            //Logs Event.
162
            $this->eventManager()->attach(new Logs());
0 ignored issues
show
Documentation introduced by
new \App\Event\Logs() is of type object<App\Event\Logs>, 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...
Deprecated Code introduced by
The method Cake\Event\EventManager::attach() has been deprecated with message: 3.0.0 Use on() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
163
            $event = new Event('Log.User', $this, [
164
                'user_id' => $user->id,
165
                'username' => $user->username,
166
                'user_ip' => $this->request->clientIp(),
167
                'user_agent' => $this->request->header('User-Agent'),
168
                'action' => '2FA.enabled'
169
            ]);
170
            $this->eventManager()->dispatch($event);
171
172
            $this->Flash->success(__('Two-factor authentication successfully enabled !'));
173
174
            $this->set(compact('user', 'userTfa'));
175
        } else {
176
            $this->Flash->error(__('Two-factor secret verification failed. Please verify your secret and try again.'));
177
178
            return $this->redirect(['action' => 'configure']);
179
        }
180
    }
181
182
    /**
183
     * Disable the Two-factor Authentication.
184
     *
185
     * @return \Cake\Network\Response
186
     */
187
    public function disable()
188
    {
189
        $this->loadModel('Users');
190
191
        $user = $this->Users
0 ignored issues
show
Documentation introduced by
The property Users does not exist on object<App\Controller\TfaController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
192
            ->find()
193
            ->where([
194
                'Users.id' => $this->Auth->user('id')
195
            ])
196
            ->select([
197
                'id',
198
                'username',
199
                'two_factor_auth_enabled'
200
            ])
201
            ->first();
202
203 View Code Duplication
        if (is_null($user) || $user->two_factor_auth_enabled == false) {
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...
204
            $this->Flash->error(__('The Two-factor authentication is not enabled !'));
205
206
            return $this->redirect(['controller' => 'users', 'action' => 'security']);
207
        }
208
209
        $user->two_factor_auth_enabled = false;
210
        $this->Users->save($user);
0 ignored issues
show
Documentation introduced by
The property Users does not exist on object<App\Controller\TfaController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
211
212
        $this->loadModel('UsersTwoFactorAuth');
213
214
        $userTfa = $this->UsersTwoFactorAuth
0 ignored issues
show
Documentation introduced by
The property UsersTwoFactorAuth does not exist on object<App\Controller\TfaController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
215
            ->find()
216
            ->where([
217
                'UsersTwoFactorAuth.user_id' => $this->Auth->user('id')
218
            ])
219
            ->first();
220
221
        $this->UsersTwoFactorAuth->delete($userTfa);
0 ignored issues
show
Documentation introduced by
The property UsersTwoFactorAuth does not exist on object<App\Controller\TfaController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
222
223
        //Logs Event.
224
        $this->eventManager()->attach(new Logs());
0 ignored issues
show
Documentation introduced by
new \App\Event\Logs() is of type object<App\Event\Logs>, 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...
Deprecated Code introduced by
The method Cake\Event\EventManager::attach() has been deprecated with message: 3.0.0 Use on() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
225
        $event = new Event('Log.User', $this, [
226
            'user_id' => $user->id,
227
            'username' => $user->username,
228
            'user_ip' => $this->request->clientIp(),
229
            'user_agent' => $this->request->header('User-Agent'),
230
            'action' => '2FA.disabled'
231
        ]);
232
        $this->eventManager()->dispatch($event);
233
234
        $this->Flash->success(__('The Two-factor authentication has been disabled !'));
235
236
        return $this->redirect(['controller' => 'users', 'action' => 'security']);
237
    }
238
239
    /**
240
     * Display the recovery code and the status of the code.
241
     *
242
     * @return \Cake\Network\Response|void
243
     */
244
    public function recoveryCode()
245
    {
246
        $this->loadModel('Users');
247
248
        $user = $this->Users
0 ignored issues
show
Documentation introduced by
The property Users does not exist on object<App\Controller\TfaController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
249
            ->find()
250
            ->contain([
251
                'UsersTwoFactorAuth'
252
            ])
253
            ->where([
254
                'Users.id' => $this->Auth->user('id')
255
            ])
256
            ->select([
257
                'Users.id',
258
                'Users.two_factor_auth_enabled',
259
                'UsersTwoFactorAuth.user_id',
260
                'UsersTwoFactorAuth.recovery_code',
261
                'UsersTwoFactorAuth.recovery_code_used'
262
            ])
263
            ->first();
264
265 View Code Duplication
        if (is_null($user) || $user->two_factor_auth_enabled == false) {
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...
266
            $this->Flash->error(__('The Two-factor authentication is not enabled !'));
267
268
            return $this->redirect(['controller' => 'users', 'action' => 'security']);
269
        }
270
271
        $this->set(compact('user'));
272
    }
273
274
    /**
275
     * Generate a new recovery code.
276
     *
277
     * @return \Cake\Network\Response
278
     */
279
    public function generateRecoveryCode()
280
    {
281
        $this->loadModel('Users');
282
        $this->loadModel('UsersTwoFactorAuth');
283
284
        $user = $this->Users
0 ignored issues
show
Documentation introduced by
The property Users does not exist on object<App\Controller\TfaController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
285
            ->find()
286
            ->where([
287
                'Users.id' => $this->Auth->user('id')
288
            ])
289
            ->select([
290
                'Users.id',
291
                'Users.username',
292
                'Users.two_factor_auth_enabled'
293
            ])
294
            ->first();
295
296 View Code Duplication
        if (is_null($user) || $user->two_factor_auth_enabled == false) {
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...
297
            $this->Flash->error(__('The Two-factor authentication is not enabled !'));
298
299
            return $this->redirect(['controller' => 'users', 'action' => 'security']);
300
        }
301
302
        $tfa = $this->UsersTwoFactorAuth
0 ignored issues
show
Documentation introduced by
The property UsersTwoFactorAuth does not exist on object<App\Controller\TfaController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
303
            ->find()
304
            ->where([
305
                'UsersTwoFactorAuth.user_id' => $this->Auth->user('id')
306
            ])
307
            ->select([
308
                'UsersTwoFactorAuth.id',
309
                'UsersTwoFactorAuth.user_id',
310
                'UsersTwoFactorAuth.username',
311
                'UsersTwoFactorAuth.recovery_code',
312
                'UsersTwoFactorAuth.recovery_code_used'
313
            ])
314
            ->first();
315
316
        $data = [
317
            'recovery_code' => $this->_generateNewRecoveryCode($tfa->username),
318
            'recovery_code_used' => false
319
        ];
320
321
        $this->UsersTwoFactorAuth->patchEntity($tfa, $data);
0 ignored issues
show
Documentation introduced by
The property UsersTwoFactorAuth does not exist on object<App\Controller\TfaController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
322
323
        if ($this->UsersTwoFactorAuth->save($tfa)) {
0 ignored issues
show
Documentation introduced by
The property UsersTwoFactorAuth does not exist on object<App\Controller\TfaController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
324
            //Logs Event.
325
            $this->eventManager()->attach(new Logs());
0 ignored issues
show
Documentation introduced by
new \App\Event\Logs() is of type object<App\Event\Logs>, 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...
Deprecated Code introduced by
The method Cake\Event\EventManager::attach() has been deprecated with message: 3.0.0 Use on() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
326
            $event = new Event('Log.User', $this, [
327
                'user_id' => $user->id,
328
                'username' => $user->username,
329
                'user_ip' => $this->request->clientIp(),
330
                'user_agent' => $this->request->header('User-Agent'),
331
                'action' => '2FA.recovery_code.regenerate'
332
            ]);
333
            $this->eventManager()->dispatch($event);
334
335
            $this->Flash->success(__('New two-factor recovery code successfully generated.'));
336
337
            return $this->redirect(['action' => 'recoveryCode']);
338
        } else {
339
            $this->Flash->error(__('Error to generate two-factor recovery code.'));
340
341
            return $this->redirect(['action' => 'recoveryCode']);
342
        }
343
    }
344
345
    /**
346
     * Function to generate a recovery code.
347
     *
348
     * Generate a code into this format :
349
     * XXXX-XXXX-XXXX-XXXX
350
     *
351
     * @param string $user The username of the user.
352
     *
353
     * @return string
354
     */
355
    protected function _generateNewRecoveryCode($user)
356
    {
357
        $code = md5(rand() . uniqid() . time() . $user);
358
359
        //Split the code into smaller chunks
360
        $code = chunk_split(substr($code, 0, 16), 4, '-');
361
362
        //Remove the last string added by chunk_split().
363
        $code = substr($code, 0, -1);
364
365
        return $code;
366
    }
367
}
368