Skip to content

Commit

Permalink
Merge pull request #1 from dmstr/feature/support-console-apps
Browse files Browse the repository at this point in the history
Feature/support console apps
  • Loading branch information
schmunk42 authored Sep 20, 2023
2 parents a4a28f3 + bb5439e commit 1e7431b
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/components/TokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use yii\di\Instance;
use yii\web\Session;
use yii\web\User;
use Yii;

/**
* @property UnencryptedToken $token
Expand Down Expand Up @@ -36,7 +37,7 @@ class TokenManager extends BaseTokenManager implements TokenManagerStorageInterf
/**
* session value identifier (key)
*/
protected const TOKEN_MANAGER_SESSION_KEY = __CLASS__;
public string $tokenManagerSessionKey = __CLASS__;

/**
* Static storage fallback if user session is disabled
Expand All @@ -50,8 +51,8 @@ class TokenManager extends BaseTokenManager implements TokenManagerStorageInterf
private Session $_session;

/**
* @throws InvalidConfigException
* @return void
* @throws InvalidConfigException
*/
public function init()
{
Expand Down Expand Up @@ -117,24 +118,24 @@ public function getToken(): UnencryptedToken
*/
public function persistTokenInStorage(): void
{
if ($this->getUser()->enableSession) {
$this->getSession()->set(static::TOKEN_MANAGER_SESSION_KEY, $this->_token);
if ($this->isStorageEnabled()) {
$this->getSession()->set($this->tokenManagerSessionKey, $this->_token);
} else {
static::$_storage['token'] = $this->_token;
static::$_storage['token'] = $this->_token;
}
}

/**
* Load saved token from (session) storage
*
* @throws LoadTokenException
* @return bool
* @throws LoadTokenException
*/
public function loadTokenFromStorage(): bool
{
/** @var UnencryptedToken|null $token */
if ($this->getUser()->enableSession) {
$token = $this->getSession()->get(static::TOKEN_MANAGER_SESSION_KEY);
if ($this->isStorageEnabled()) {
$token = $this->getSession()->get($this->tokenManagerSessionKey);
} else {
$token = static::$_storage['token'] ?? null;
}
Expand All @@ -153,14 +154,16 @@ public function loadTokenFromStorage(): bool
/**
* @return Session
*/
protected function getSession(): Session {
protected function getSession(): Session
{
return $this->_session;
}

/**
* @return User
*/
protected function getUser(): User {
protected function getUser(): User
{
return $this->_user;
}

Expand All @@ -171,10 +174,14 @@ protected function getUser(): User {
*/
public function isStorageEnabled(): bool
{
if (Yii::$app instanceof \yii\console\Application) {
return false;
}

if ($this->getUser()->enableSession) {
return $this->getSession()->getIsActive();
}
// use temporary static property for cache
return true;
return false;
}
}

0 comments on commit 1e7431b

Please sign in to comment.