Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ When flash data is restore, it will be delete in $_SESSION.
* setUserIdForDatabase(userId: int): void
* setLengthSessionID(length: int): void
* getLengthSessionID(): int
* deleteUserSessions(int $userID): void
* deleteAnonymousSessions(): int
* deleteUserSessionsInDatabase(int $userID): void
* deleteAnonymousSessionsInDatabase(): int

#### Static Redis Driver
* useNewRedisDriver(configuration: array|string): void
Expand Down
14 changes: 14 additions & 0 deletions src/DriverManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,18 @@ public static function setPrefixForFile(string $prefix): void
static::$driver->setPrefix($prefix);
}
}

public static function deleteUserSessionsInDatabase(int $userID): void
{
if (\method_exists(static::$driver, 'deleteUserSessions')) {
static::$driver->deleteUserSessions($userID);
}
}

public static function deleteAnonymousSessionsInDatabase(): void
{
if (\method_exists(static::$driver, 'deleteAnonymousSessions')) {
static::$driver->deleteAnonymousSessions();
}
}
}
16 changes: 16 additions & 0 deletions tests/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,24 @@ public function testUseCurrentDatabaseEncryptionDriver(): void
$sessionId = Session::getId();
Session::commit();

$sql = 'REPLACE INTO sessions VALUES(:id, null, UTC_TIMESTAMP(), :content)';
$params = ['id' => 'session_123', 'content' => 'content'];
$db->insert($sql, $params);

static::assertSame(1, $db->count('SELECT COUNT(id) FROM sessions WHERE id_user IS NULL'));
static::assertSame(1, $db->count('SELECT COUNT(id) FROM sessions WHERE id_user IS NOT NULL'));

$userIdInTable = (int) $db->selectVar('SELECT id_user FROM sessions WHERE id = :id', ['id' => $sessionId]);
static::assertSame($userId, $userIdInTable);

Session::deleteAnonymousSessionsInDatabase();

static::assertEmpty($db->count('SELECT COUNT(id) FROM sessions WHERE id_user IS NULL'));
static::assertSame(1, $db->count('SELECT COUNT(id) FROM sessions WHERE id_user IS NOT NULL'));

Session::deleteUserSessionsInDatabase($userId);

static::assertEmpty($db->count('SELECT COUNT(id) FROM sessions WHERE id = :id', ['id' => $sessionId]));
}

/** @throws \Exception */
Expand Down
Loading