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.

DbSessionManager is a singleton ? access it via DbSessionManager.getInstance(). It does not need to be injected as a Spring bean.

Methods

getSystemToken

Description:

MethodReturn valuesDescription

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.

There is a special super-user named system that OpenKM uses for background operations. The system token is typically used in cron tasks or automation actions that must bypass normal user permissions.

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)

Description:

MethodReturn valuesDescription

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

Description:

MethodReturn valuesDescription

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

Description:

MethodReturn valuesDescription

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

Description:

MethodReturn valuesDescription

getRoles(String token)

Set<String>

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

Description:

MethodReturn valuesDescription

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

Description:

MethodReturn valuesDescription

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

Description:

MethodReturn valuesDescription

getTokens()

List<String>

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();
        }
    }
}

add

Description:

MethodReturn valuesDescription

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

Description:

MethodReturn valuesDescription

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();
        }
    }
}