Skip to content

Commit 0952520

Browse files
authored
feat: add Session::deleteUserSessionsInDatabase and Session::deleteAnonymousSessionsInDatabase (#337)
1 parent e5d5c27 commit 0952520

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ When flash data is restore, it will be delete in $_SESSION.
164164
* setUserIdForDatabase(userId: int): void
165165
* setLengthSessionID(length: int): void
166166
* getLengthSessionID(): int
167-
* deleteUserSessions(int $userID): void
168-
* deleteAnonymousSessions(): int
167+
* deleteUserSessionsInDatabase(int $userID): void
168+
* deleteAnonymousSessionsInDatabase(): int
169169

170170
#### Static Redis Driver
171171
* useNewRedisDriver(configuration: array|string): void

src/DriverManager.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,18 @@ public static function setPrefixForFile(string $prefix): void
198198
static::$driver->setPrefix($prefix);
199199
}
200200
}
201+
202+
public static function deleteUserSessionsInDatabase(int $userID): void
203+
{
204+
if (\method_exists(static::$driver, 'deleteUserSessions')) {
205+
static::$driver->deleteUserSessions($userID);
206+
}
207+
}
208+
209+
public static function deleteAnonymousSessionsInDatabase(): void
210+
{
211+
if (\method_exists(static::$driver, 'deleteAnonymousSessions')) {
212+
static::$driver->deleteAnonymousSessions();
213+
}
214+
}
201215
}

tests/SessionTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,24 @@ public function testUseCurrentDatabaseEncryptionDriver(): void
308308
$sessionId = Session::getId();
309309
Session::commit();
310310

311+
$sql = 'REPLACE INTO sessions VALUES(:id, null, UTC_TIMESTAMP(), :content)';
312+
$params = ['id' => 'session_123', 'content' => 'content'];
313+
$db->insert($sql, $params);
314+
315+
static::assertSame(1, $db->count('SELECT COUNT(id) FROM sessions WHERE id_user IS NULL'));
316+
static::assertSame(1, $db->count('SELECT COUNT(id) FROM sessions WHERE id_user IS NOT NULL'));
317+
311318
$userIdInTable = (int) $db->selectVar('SELECT id_user FROM sessions WHERE id = :id', ['id' => $sessionId]);
312319
static::assertSame($userId, $userIdInTable);
320+
321+
Session::deleteAnonymousSessionsInDatabase();
322+
323+
static::assertEmpty($db->count('SELECT COUNT(id) FROM sessions WHERE id_user IS NULL'));
324+
static::assertSame(1, $db->count('SELECT COUNT(id) FROM sessions WHERE id_user IS NOT NULL'));
325+
326+
Session::deleteUserSessionsInDatabase($userId);
327+
328+
static::assertEmpty($db->count('SELECT COUNT(id) FROM sessions WHERE id = :id', ['id' => $sessionId]));
313329
}
314330

315331
/** @throws \Exception */

0 commit comments

Comments
 (0)