Problem/Motivation
I noticed while working on a contrib module that it would be good, if it would be possible to fetch the count of open sessions for a single user.
The module creates locks for content, so only a single user can edit it. The idea here now is that when user logs out it should remove open locks. As a user can log in to multiple browser and can have multiple sessions, it needs to make sure that this is the last open session.
As a workaround in content_lock following is used:
// Only do the database check if the original drupal session manager is used.
// Otherwise its not sure if sessions table has correct data. As it would be
// a common practice to extend the Class, instanceof is not used here!
if (get_class(Drupal::service('session_manager')) == \Drupal\Core\Session\SessionManager::class) {
$query = \Drupal::database()
->select('sessions');
$query->condition('uid', $account->id());
$query = $query->countQuery();
$session_count = (int) $query->execute()->fetchField();
}
See FR #2937733: Remove locks on user log out
Proposed resolution
Add new method for counting the open sessions to SessionManager
Remaining tasks
- Add new method
- Add tests
User interface changes
None
API changes
Add new countSession() method to SessionManager
Data model changes
None