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)) { |
|
|
|
|
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
|
|
|
|