Исправлено поведение User\Component::getIdentity(), если в контроллере не было accessFilter

This commit is contained in:
ErickSkrauch
2016-12-11 17:58:08 +03:00
parent 9fe42e4623
commit b00c4ae4fc
2 changed files with 89 additions and 33 deletions

View File

@@ -23,7 +23,7 @@ use yii\web\User as YiiUserComponent;
* @property AccountSession|null $activeSession
* @property AccountIdentity|null $identity
*
* @method AccountIdentity|null getIdentity($autoRenew = true)
* @method AccountIdentity|null loginByAccessToken($token, $type = null)
*/
class Component extends YiiUserComponent {
@@ -39,6 +39,8 @@ class Component extends YiiUserComponent {
public $sessionTimeout = 'P7D';
private $_identity;
public function init() {
parent::init();
if (!$this->secret) {
@@ -46,6 +48,24 @@ class Component extends YiiUserComponent {
}
}
/**
* @param bool $autoRenew
* @return null|AccountIdentity
*/
public function getIdentity($autoRenew = true) {
$result = parent::getIdentity($autoRenew);
if ($result === null && $this->_identity !== false) {
$bearer = $this->getBearerToken();
if ($bearer !== null) {
$result = $this->loginByAccessToken($bearer);
}
$this->_identity = $result ?: false;
}
return $result;
}
/**
* @param IdentityInterface $identity
* @param bool $rememberMe
@@ -149,14 +169,9 @@ class Component extends YiiUserComponent {
return null;
}
$authHeader = Yii::$app->request->getHeaders()->get('Authorization');
if ($authHeader === null || !preg_match('/^Bearer\s+(.*?)$/', $authHeader, $matches)) {
return null;
}
$token = $matches[1];
$bearer = $this->getBearerToken();
try {
$token = $this->parseToken($token);
$token = $this->parseToken($bearer);
} catch (VerificationException $e) {
return null;
}
@@ -203,4 +218,16 @@ class Component extends YiiUserComponent {
];
}
/**
* @return ?string
*/
private function getBearerToken() {
$authHeader = Yii::$app->request->getHeaders()->get('Authorization');
if ($authHeader === null || !preg_match('/^Bearer\s+(.*?)$/', $authHeader, $matches)) {
return null;
}
return $matches[1];
}
}