PasswordAgeEnforceFilter::beforeAction()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 0
cts 17
cp 0
rs 9.552
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 20
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/yii2-usuario project.
5
 *
6
 * (c) 2amigOS! <http://2amigos.us/>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Da\User\Filter;
13
14
use Da\User\Model\User;
15
use Da\User\Module;
16
use Yii;
17
use yii\base\ActionFilter;
18
19
class PasswordAgeEnforceFilter extends ActionFilter
20
{
21
    public function beforeAction($action)
22
    {
23
        /** @var Module $module */
24
        $module = Yii::$app->getModule('user');
25
        $maxPasswordAge = $module->maxPasswordAge;
26
        // If feature is not set do nothing (or raise a configuration error?)
27
        if (is_null($maxPasswordAge)) {
28
            return parent::beforeAction($action);
29
        }
30
        if (Yii::$app->user->isGuest) {
31
            // Not our business
32
            return parent::beforeAction($action);
33
        }
34
        /** @var User $identity */
35
        $identity = Yii::$app->user->identity;
36
        if ($identity->password_age >= $maxPasswordAge) {
37
            // Force password change
38
            Yii::$app->getSession()->setFlash('warning', Yii::t('usuario', 'Your password has expired, you must change it now'));
0 ignored issues
show
Bug introduced by
The method getSession 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...
39
            return Yii::$app->response->redirect(['/user/settings/account'])->send();
40
        }
41
42
        return parent::beforeAction($action);
43
    }
44
}
45