Passed
Push — master ( f2b2d5...f72310 )
by Paweł
17:14 queued 07:33
created

CompositeAuth::authenticate()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 7.0368

Importance

Changes 0
Metric Value
cc 7
eloc 10
nc 8
nop 3
dl 0
loc 19
ccs 10
cts 11
cp 0.9091
crap 7.0368
rs 8.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\filters\auth;
9
10
use Yii;
11
use yii\base\InvalidConfigException;
12
13
/**
14
 * CompositeAuth is an action filter that supports multiple authentication methods at the same time.
15
 *
16
 * The authentication methods contained by CompositeAuth are configured via [[authMethods]],
17
 * which is a list of supported authentication class configurations.
18
 *
19
 * The following example shows how to support three authentication methods:
20
 *
21
 * ```php
22
 * public function behaviors()
23
 * {
24
 *     return [
25
 *         'compositeAuth' => [
26
 *             'class' => \yii\filters\auth\CompositeAuth::class,
27
 *             'authMethods' => [
28
 *                 \yii\filters\auth\HttpBasicAuth::class,
29
 *                 \yii\filters\auth\QueryParamAuth::class,
30
 *             ],
31
 *         ],
32
 *     ];
33
 * }
34
 * ```
35
 *
36
 * @author Qiang Xue <[email protected]>
37
 * @since 2.0
38
 */
39
class CompositeAuth extends AuthMethod
40
{
41
    /**
42
     * @var array the supported authentication methods. This property should take a list of supported
43
     * authentication methods, each represented by an authentication class or configuration.
44
     *
45
     * If this property is empty, no authentication will be performed.
46
     *
47
     * Note that an auth method class must implement the [[\yii\filters\auth\AuthInterface]] interface.
48
     */
49
    public $authMethods = [];
50
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 13
    public function beforeAction($action)
56
    {
57 13
        return empty($this->authMethods) ? true : parent::beforeAction($action);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 12
    public function authenticate($user, $request, $response)
64
    {
65 12
        foreach ($this->authMethods as $i => $auth) {
66 12
            if (!$auth instanceof AuthInterface) {
67 12
                $this->authMethods[$i] = $auth = Yii::createObject($auth);
68 12
                if (!$auth instanceof AuthInterface) {
69
                    throw new InvalidConfigException(get_class($auth) . ' must implement yii\filters\auth\AuthInterface');
70
                }
71
            }
72
73 12
            if (isset($this->owner->action) && $auth->isActive($this->owner->action)) {
0 ignored issues
show
Bug introduced by
The method isActive() does not exist on yii\filters\auth\AuthInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to yii\filters\auth\AuthInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
            if (isset($this->owner->action) && $auth->/** @scrutinizer ignore-call */ isActive($this->owner->action)) {
Loading history...
Bug Best Practice introduced by
The property action does not exist on yii\base\Component. Since you implemented __get, consider adding a @property annotation.
Loading history...
74 12
                $identity = $auth->authenticate($user, $request, $response);
75 11
                if ($identity !== null) {
76 7
                    return $identity;
77
                }
78
            }
79
        }
80
81 4
        return null;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 3
    public function challenge($response)
88
    {
89 3
        foreach ($this->authMethods as $method) {
90
            /* @var $method AuthInterface */
91 3
            $method->challenge($response);
92
        }
93 3
    }
94
}
95