fix: DummySession, Session singleton 문제

- DummySession 호출 후 Session 호출시에도 DummySession이 나와야 함
- 함수 상속 관계 재 설정
This commit is contained in:
2023-02-28 00:43:56 +09:00
parent 904e26f56e
commit 4f0b18cc3c
2 changed files with 30 additions and 169 deletions
+6 -146
View File
@@ -17,19 +17,9 @@ namespace sammo;
*/
class DummySession extends Session
{
private $writeClosed = false;
private $sessionInfo = [];
protected $sessionInfo = [];
public static function getInstance(): Session
{
static $inst = null;
if ($inst === null) {
$inst = new DummySession();
}
return $inst;
}
public function restart(): Session
public function restart(): static
{
$this->sessionInfo = [];
return $this;
@@ -49,13 +39,13 @@ class DummySession extends Session
}
public function setReadOnly(): Session
public function setReadOnly(): static
{
$this->writeClosed = true;
return $this;
}
private function set(string $name, $value)
protected function set(string $name, $value)
{
$this->sessionInfo[$name] = $value;
}
@@ -77,52 +67,12 @@ class DummySession extends Session
return $this->get($name);
}
private function get(string $name)
protected function get(string $name)
{
return $this->sessionInfo[$name] ?? null;
}
public function login(int $userID, string $userName, int $grade, bool $reqOTP, ?string $tokenValidUntil, ?int $tokenID, array $acl): Session
{
$this->set('userID', $userID);
$this->set('userName', $userName);
$this->set('ip', Util::get_client_ip(true));
$this->set('time', time());
$this->set('userGrade', $grade);
$this->set('acl', $acl);
$this->set('reqOTP', $reqOTP);
$this->set('tokenValidUntil', $tokenValidUntil);
$this->set('tokenID', $tokenID);
return $this;
}
public function setReqOTP(bool $reqOTP=false, string $tokenValidUntil){
$this->set('reqOTP', $reqOTP);
$this->set('tokenValidUntil', $tokenValidUntil);
}
public function logout(): Session
{
if ($this->writeClosed) {
$this->restart();
}
if (class_exists('\\sammo\\UniqueConst')) {
$this->logoutGame();
}
$this->set('userID', null);
$this->set('userName', null);
$this->set('userGrade', null);
$this->set('acl', null);
$this->set('reqOTP', null);
$this->set('time', time());
$this->set('lastMsgGet', null);
$this->set('tokenID', null);
return $this;
}
public function loginGame(&$result = null): Session
public function loginGame(&$result = null): static
{
$userID = $this->userID;
if (!$userID) {
@@ -173,96 +123,6 @@ class DummySession extends Session
return $this;
}
public function logoutGame(): Session
{
if ($this->writeClosed) {
$this->restart();
}
$serverID = UniqueConst::$serverID;
$this->set($serverID.static::GAME_KEY_DATE, null);
$this->set($serverID.static::GAME_KEY_GENERAL_ID, null);
$this->set($serverID.static::GAME_KEY_GENERAL_NAME, null);
$this->set($serverID.static::GAME_KEY_EXPECTED_DEADTIME, null);
return $this;
}
/**
* 로그인 유저의 전역 grade를 받아옴
* @return int|null
*/
public static function getUserGrade(bool $requireLogin = false, string $exitPath = '..')
{
if ($requireLogin) {
$obj = self::requireLogin($exitPath);
} else {
$obj = self::getInstance();
}
return $obj->userGrade;
}
/**
* 로그인한 유저의 전역 id(숫자)를 받아옴
*
* @return int|null
*/
public static function getUserID(bool $requireLogin = false, string $exitPath = '..')
{
if ($requireLogin) {
$obj = self::requireLogin($exitPath);
} else {
$obj = self::getInstance();
}
return $obj->userID;
}
/**
*
*/
public static function getGeneralID(bool $requireLogin = false, string $exitPath = '..')
{
if ($requireLogin) {
$obj = self::requireLogin($exitPath);
} else {
$obj = self::getInstance();
}
return $obj->generalID??0;
}
public function isLoggedIn(bool $ignoreOTP = false): bool
{
if(!$ignoreOTP){
if($this->reqOTP){
return false;
}
if(!$this->tokenValidUntil){
return false;
}
$now = TimeUtil::now();
if($this->tokenValidUntil < $now){
return false;
}
}
if ($this->userID) {
return true;
} else {
return false;
}
}
public function isGameLoggedIn(): bool
{
if ($this->generalID) {
return true;
} else {
return false;
}
}
public function __destruct()
{
}