Completed
Push — master ( 18dfa2...e7a8c5 )
by Vuong
01:47
created

VerifyAction   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 120
ccs 26
cts 28
cp 0.9286
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 7 1
A beforeRun() 0 12 2
B run() 0 30 8
1
<?php
2
/**
3
 * @link https://github.com/vuongxuongminh/yii2-mfa
4
 * @copyright Copyright (c) 2019 Vuong Xuong Minh
5
 * @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
6
 */
7
8
namespace vxm\mfa;
9
10
use Yii;
11
12
use yii\base\Action;
13
use yii\base\InvalidConfigException;
14
15
/**
16
 * Class VerifyAction provide an action verify mfa otp. For use, add it to actions method of your controller
17
 * Example:
18
 * ```php
19
 *       public function actions()
20
 *       {
21
 *           return [
22
 *               'verify' => [
23
 *                   'class' => 'vxm\mfa\VerifyAction',
24
 *                   'viewFile' => 'verify', // the name of view file use to render view
25
 *                   'formVar' => 'model', // the name of variable use to parse [[\vxm\mfa\OtpForm]] object to view file.
26
 *                   'retry' => true, // allow user retry when type wrong otp
27
 *                   'successCallback' => [$this, 'mfaPassed'], // callable call when user type valid otp if not set [[yii\web\Controller::goBack()]] will be call.
28
 *                   'invalidCallback' => [$this, 'mfaOtpInvalid'], // callable call when user type wrong otp if not set and property `retry` is false [[yii\web\User::loginRequired()]] will be call, it should be use for set flash notice to user.
29
 *                   'retry' => true, // allow user retry when type wrong otp
30
 *               ]
31
 *           ];
32
 *       }
33
 * ```
34
 *
35
 * @author Vuong Minh <[email protected]>
36
 * @since 1.0.0
37
 */
38
class VerifyAction extends Action
39
{
40
41
    use EnsureUserBehaviorAttachedTrait;
42
43
    /**
44
     * @var string the name of view file if not set an id of this action will be use.
45
     */
46
    public $viewFile;
47
48
    /**
49
     * @var string the name of variable in view refer to an object of `vxm\mfa\OtpForm`.
50
     */
51
    public $formVar = 'model';
52
53
    /**
54
     * @var callable|null when an identity had been verified it will be call. If not set, [[\yii\web\Controller::goBack()]] will be call.
55
     * This action will be parse at first param and `vxm\mfa\OtpForm` is second param
56
     * Example:
57
     *
58
     * ```php
59
     * 'successCallback' => function(\vxm\mfa\VerifyAction $action, \vxm\mfa\OtpForm $otp) {
60
     *
61
     *      return $action->controller->redirect(['site/dash-board']);
62
     * }
63
     *
64
     * ```
65
     */
66
    public $successCallback;
67
68
    /**
69
     * @var callable|null when an user submit wrong otp it will be call, if not set, [[yii\web\User::loginRequired()]] will be call.
70
     * This action will be parse at first param and `vxm\mfa\OtpForm` is second param
71
     * Example:
72
     *
73
     * ```php
74
     * 'invalidCallback' => function(\vxm\mfa\VerifyAction $action, \vxm\mfa\OtpForm $otp) {
75
     *      Yii::$app->session->setFlash('Otp is not valid');
76
     *
77
     *      return $action->controller->redirect(['site/login']);
78
     * }
79
     *
80
     * ```
81
     */
82
    public $invalidCallback;
83
84
    /**
85
     * @var bool weather allow user can retry when type wrong or not.
86
     */
87
    public $retry = false;
88
89
    /**
90
     * @var string the form class handle end-user data
91
     */
92
    public $formClass = OtpForm::class;
93
94
    /**
95
     * @inheritDoc
96
     * @throws InvalidConfigException
97
     */
98 4
    public function init()
99
    {
100 4
        $this->ensureUserBehaviorAttached();
101 4
        $this->viewFile = $this->viewFile ?? $this->id;
102
103 4
        parent::init();
104 4
    }
105
106
    /**
107
     * @inheritDoc
108
     */
109 4
    public function beforeRun()
110
    {
111 4
        $data = $this->user->getIdentityLoggedIn();
112
113 4
        if ($data === null) {
114 1
            $this->user->loginRequired();
115
116 1
            return false;
117
        }
118
119 3
        return parent::beforeRun();
120
    }
121
122
    /**
123
     * @return mixed|string|\yii\web\Response
124
     * @throws \yii\web\ForbiddenHttpException
125
     */
126 3
    public function run()
127
    {
128 3
        $formClass = $this->formClass;
129 3
        $form = new $formClass(['user' => $this->user]);
130
131 3
        if ($form->load(Yii::$app->request->post()) && $form->validate()) {
132 2
            if ($form->verify()) {
133 1
                $this->user->switchIdentityLoggedIn();
134 1
                $this->user->removeIdentityLoggedIn();
135
136 1
                if ($this->successCallback) {
137
                    return call_user_func($this->successCallback, $this, $form);
138
                } else {
139 1
                    return $this->controller->goBack();
0 ignored issues
show
Bug introduced by
The method goBack does only exist in yii\web\Controller, but not in yii\base\Controller and yii\console\Controller.

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...
140
                }
141
            } else {
142 1
                if (!$this->retry) {
143 1
                    $this->user->removeIdentityLoggedIn();
144
                }
145
146 1
                if ($this->invalidCallback) {
147
                    return call_user_func($this->invalidCallback, $this, $form);
148 1
                } elseif (!$this->retry) {
149 1
                    return $this->user->loginRequired();
150
                }
151
            }
152
        }
153
154 2
        return $this->controller->render($this->viewFile, [$this->formVar => $form]);
155
    }
156
157
}
158