mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Исправлено поведение User\Component::getIdentity(), если в контроллере не было accessFilter
This commit is contained in:
@@ -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];
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user