DbSessionManager
DbSessionManager is a singleton that manages the in-memory map of active user sessions. Each session is represented by a string token that maps to a Spring Security Authentication object. The manager is the central point for resolving who a token belongs to, obtaining elevated system-level access, and inspecting active sessions.
Methods
Section titled “Methods”getSystemToken
Section titled “getSystemToken”Description:
| Method | Return values | Description |
|---|---|---|
| getSystemToken() | String | Returns the administrator token for the tenant of the currently logged-in user. Pass this token to any API method to execute the operation as the system super-user instead of the current user. |
Example:
package com.openkm;
import com.openkm.api.OKMDocument;import com.openkm.module.db.stuff.DbSessionManager;import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) { try { OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class); String systemToken = DbSessionManager.getInstance().getSystemToken(); // Delete is executed as the system super-user okmDocument.delete(systemToken, "d50133e3-dbfa-4d01-a109-28785cd48f40"); } catch (Exception e) { e.printStackTrace(); } }}getSystemToken (by tenant)
Section titled “getSystemToken (by tenant)”Description:
| Method | Return values | Description |
|---|---|---|
| getSystemToken(long tenantId) | String | Returns the administrator token for the given tenant ID. Use this overload when the tenant cannot be resolved from the current security context (e.g. in background threads). |
Example:
package com.openkm;
import com.openkm.module.db.stuff.DbSessionManager;
public class Test {
public static void main(String[] args) { try { long tenantId = 1L; String systemToken = DbSessionManager.getInstance().getSystemToken(tenantId); System.out.println("System token for tenant " + tenantId + ": " + systemToken); } catch (Exception e) { e.printStackTrace(); } }}getAuthentication
Section titled “getAuthentication”Description:
| Method | Return values | Description |
|---|---|---|
| getAuthentication(String token) | Authentication | Returns the Spring Security Authentication object associated with the given token, or null if the token does not exist. Also updates the session’s last-access timestamp. |
Example:
package com.openkm;
import com.openkm.module.db.stuff.DbSessionManager;import org.springframework.security.core.Authentication;
public class Test {
public static void main(String[] args) { try { String token = "some-token"; Authentication auth = DbSessionManager.getInstance().getAuthentication(token); if (auth != null) { System.out.println("User: " + auth.getName()); } } catch (Exception e) { e.printStackTrace(); } }}getUser
Section titled “getUser”Description:
| Method | Return values | Description |
|---|---|---|
| getUser(String token) | String | Returns the username associated with the given token, or null if the token does not exist. |
Example:
package com.openkm;
import com.openkm.module.db.stuff.DbSessionManager;
public class Test {
public static void main(String[] args) { try { String token = "some-token"; String user = DbSessionManager.getInstance().getUser(token); System.out.println("Token belongs to user: " + user); } catch (Exception e) { e.printStackTrace(); } }}getRoles
Section titled “getRoles”Description:
| Method | Return values | Description |
|---|---|---|
| getRoles(String token) | Set |
Returns the set of role names granted to the user associated with the given token. Returns an empty set if the token does not exist. |
Example:
package com.openkm;
import com.openkm.module.db.stuff.DbSessionManager;
import java.util.Set;
public class Test {
public static void main(String[] args) { try { String token = "some-token"; Set<String> roles = DbSessionManager.getInstance().getRoles(token); System.out.println("Roles: " + roles); } catch (Exception e) { e.printStackTrace(); } }}getToken
Section titled “getToken”Description:
| Method | Return values | Description |
|---|---|---|
| getToken(Authentication auth) | String | Returns the token string associated with the given Authentication object, or null if no matching session is found. |
Example:
package com.openkm;
import com.openkm.module.db.stuff.DbSessionManager;import org.springframework.security.core.Authentication;import org.springframework.security.core.context.SecurityContextHolder;
public class Test {
public static void main(String[] args) { try { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); String token = DbSessionManager.getInstance().getToken(auth); System.out.println("Current user token: " + token); } catch (Exception e) { e.printStackTrace(); } }}getInfo
Section titled “getInfo”Description:
| Method | Return values | Description |
|---|---|---|
| getInfo(String token) | DbSessionInfo | Returns the session information object for the given token. DbSessionInfo contains the Authentication object, the session creation time, and the last-access time. |
Example:
package com.openkm;
import com.openkm.bean.DbSessionInfo;import com.openkm.module.db.stuff.DbSessionManager;
public class Test {
public static void main(String[] args) { try { String token = "some-token"; DbSessionInfo info = DbSessionManager.getInstance().getInfo(token); if (info != null) { System.out.println("Created: " + info.getCreation().getTime()); System.out.println("Last access: " + info.getLastAccess().getTime()); } } catch (Exception e) { e.printStackTrace(); } }}getTokens
Section titled “getTokens”Description:
| Method | Return values | Description |
|---|---|---|
| getTokens() | List |
Returns a list of all active session tokens currently registered in the session manager. |
Example:
package com.openkm;
import com.openkm.module.db.stuff.DbSessionManager;
public class Test {
public static void main(String[] args) { try { for (String token : DbSessionManager.getInstance().getTokens()) { String user = DbSessionManager.getInstance().getUser(token); System.out.println("Token: " + token + " -> User: " + user); } } catch (Exception e) { e.printStackTrace(); } }}Description:
| Method | Return values | Description |
|---|---|---|
| add(String token, Authentication auth) | void | Registers a new session by associating the given token with an Authentication object. The session creation and last-access timestamps are set to the current time. |
Example:
package com.openkm;
import com.openkm.module.db.stuff.DbSessionManager;import org.springframework.security.core.Authentication;import org.springframework.security.core.context.SecurityContextHolder;
import java.util.UUID;
public class Test {
public static void main(String[] args) { try { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); String token = UUID.randomUUID().toString(); DbSessionManager.getInstance().add(token, auth); System.out.println("Session registered with token: " + token); } catch (Exception e) { e.printStackTrace(); } }}remove
Section titled “remove”Description:
| Method | Return values | Description |
|---|---|---|
| remove(String token) | void | Removes the session associated with the given token from the session manager. |
Example:
package com.openkm;
import com.openkm.module.db.stuff.DbSessionManager;
public class Test {
public static void main(String[] args) { try { String token = "some-token"; DbSessionManager.getInstance().remove(token); System.out.println("Session removed"); } catch (Exception e) { e.printStackTrace(); } }}