Skip to content

PrincipalUtils

Utility class with static helper methods for querying the current user’s identity, roles, tenant, and authentication context from the Spring Security context. All methods are static.

Description:

Method Return values Description
getUser() String Returns the username of the currently authenticated user from the Spring Security context.

Example:

System.out.println(PrincipalUtils.getUser()); // "jsmith"

Description:

Method Return values Description
isUser(String userId) boolean Returns true if the currently authenticated user has the given username.

Description:

Method Return values Description
getUserByToken(String token) String Returns the username associated with the given session token. Throws AccessDeniedException if the token is not found.

Description:

Method Return values Description
getTenantId() long Returns the tenant ID of the currently authenticated user. Falls back to Config.DEFAULT_TENANT_ID if the tenant cannot be resolved.

Example:

long tenantId = PrincipalUtils.getTenantId();
System.out.println("Tenant: " + tenantId);

Description:

Method Return values Description
getTenantIdByToken(String token) long Returns the tenant ID associated with the given session token. Throws AccessDeniedException if the token is not found.

Description:

Method Return values Description
getRoles() Set Returns the set of role names granted to the currently authenticated user. Returns an empty set if not authenticated.

Description:

Method Return values Description
hasRole(String role) boolean Returns true if the currently authenticated user has the given role.

Example:

if (PrincipalUtils.hasRole("ROLE_ADMIN")) {
System.out.println("User is admin");
}
Set<String> roles = PrincipalUtils.getRoles();
System.out.println("Roles: " + roles);

Description:

Method Return values Description
isRegularUser() boolean Returns true if the current user does not have the default admin role.

Description:

Method Return values Description
isAdminUser() boolean Returns true if the current user has the default admin role.

Description:

Method Return values Description
isSystemUser() boolean Returns true if the current user is the internal system user used for background operations.

Description:

Method Return values Description
isSuperUser() boolean Returns true if the current user is the default administrator user (okmAdmin).

Description:

Method Return values Description
hasFullAccess() boolean Returns true if the current user has unrestricted access to the repository — i.e. is the system user, the superuser, or has the admin role.

Example:

package com.openkm;
import com.openkm.principal.PrincipalUtils;
public class Test {
public static void main(String[] args) {
try {
System.out.println("User: " + PrincipalUtils.getUser());
System.out.println("TenantId: " + PrincipalUtils.getTenantId());
System.out.println("IsAdmin: " + PrincipalUtils.isAdminUser());
System.out.println("HasFullAccess: " + PrincipalUtils.hasFullAccess());
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
getAuthentication() Authentication Returns the Spring Security Authentication object for the current request. Tries the custom SecurityHolder, then SecurityContextHolder, then the HTTP request. Returns null if no authenticated user is found.

Description:

Method Return values Description
getAuthenticationByToken(String token) Authentication Returns the Spring Security Authentication object for the given session token. Throws AccessDeniedException if the token is not found in DbSessionManager.

Description:

Method Return values Description
getRemoteAddress() String Returns the remote IP address of the currently authenticated user, or null if not available.