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\Action; |
12
|
|
|
use yii\base\ActionFilter; |
13
|
|
|
use yii\web\UnauthorizedHttpException; |
14
|
|
|
use yii\web\User; |
15
|
|
|
use yii\web\Request; |
16
|
|
|
use yii\web\Response; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* AuthMethod is a base class implementing the [[AuthInterface]] interface. |
20
|
|
|
* |
21
|
|
|
* @author Qiang Xue <[email protected]> |
22
|
|
|
* @since 2.0 |
23
|
|
|
*/ |
24
|
|
|
abstract class AuthMethod extends ActionFilter implements AuthInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var User the user object representing the user authentication status. If not set, the `user` application component will be used. |
28
|
|
|
*/ |
29
|
|
|
public $user; |
30
|
|
|
/** |
31
|
|
|
* @var Request the current request. If not set, the `request` application component will be used. |
32
|
|
|
*/ |
33
|
|
|
public $request; |
34
|
|
|
/** |
35
|
|
|
* @var Response the response to be sent. If not set, the `response` application component will be used. |
36
|
|
|
*/ |
37
|
|
|
public $response; |
38
|
|
|
/** |
39
|
|
|
* @var array list of action IDs that this filter will be applied to, but auth failure will not lead to error. |
40
|
|
|
* It may be used for actions, that are allowed for public, but return some additional data for authenticated users. |
41
|
|
|
* Defaults to empty, meaning authentication is not optional for any action. |
42
|
|
|
* @see isOptional |
43
|
|
|
* @since 2.0.7 |
44
|
|
|
*/ |
45
|
|
|
public $optional = []; |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritdoc |
50
|
|
|
*/ |
51
|
23 |
|
public function beforeAction($action) |
52
|
|
|
{ |
53
|
23 |
|
$response = $this->response ? : Yii::$app->getResponse(); |
54
|
|
|
|
55
|
|
|
try { |
56
|
23 |
|
$identity = $this->authenticate( |
57
|
23 |
|
$this->user ? : Yii::$app->getUser(), |
|
|
|
|
58
|
23 |
|
$this->request ? : Yii::$app->getRequest(), |
59
|
|
|
$response |
60
|
23 |
|
); |
61
|
23 |
|
} catch (UnauthorizedHttpException $e) { |
62
|
6 |
|
if ($this->isOptional($action)) { |
63
|
6 |
|
return true; |
64
|
|
|
} |
65
|
|
|
|
66
|
6 |
|
throw $e; |
67
|
|
|
} |
68
|
|
|
|
69
|
17 |
|
if ($identity !== null || $this->isOptional($action)) { |
70
|
17 |
|
return true; |
71
|
|
|
} else { |
72
|
2 |
|
$this->challenge($response); |
73
|
2 |
|
$this->handleFailure($response); |
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritdoc |
80
|
|
|
*/ |
81
|
1 |
|
public function challenge($response) |
82
|
|
|
{ |
83
|
1 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @inheritdoc |
87
|
|
|
*/ |
88
|
8 |
|
public function handleFailure($response) |
89
|
|
|
{ |
90
|
8 |
|
throw new UnauthorizedHttpException('You are requesting with an invalid credential.'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Checks, whether authentication is optional for the given action. |
95
|
|
|
* |
96
|
|
|
* @param Action $action |
97
|
|
|
* @return boolean |
98
|
|
|
* @see optional |
99
|
|
|
* @since 2.0.7 |
100
|
|
|
*/ |
101
|
8 |
|
protected function isOptional($action) |
102
|
|
|
{ |
103
|
8 |
|
$id = $this->getActionId($action); |
104
|
8 |
|
return in_array($id, $this->optional, true); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
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:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: