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.
Current user
Section titled “Current user”getUser
Section titled “getUser”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"isUser
Section titled “isUser”Description:
| Method | Return values | Description |
|---|---|---|
| isUser(String userId) | boolean | Returns true if the currently authenticated user has the given username. |
getUserByToken
Section titled “getUserByToken”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. |
Tenant
Section titled “Tenant”getTenantId
Section titled “getTenantId”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);getTenantIdByToken
Section titled “getTenantIdByToken”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. |
getRoles
Section titled “getRoles”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. |
hasRole
Section titled “hasRole”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);User type checks
Section titled “User type checks”isRegularUser
Section titled “isRegularUser”Description:
| Method | Return values | Description |
|---|---|---|
| isRegularUser() | boolean | Returns true if the current user does not have the default admin role. |
isAdminUser
Section titled “isAdminUser”Description:
| Method | Return values | Description |
|---|---|---|
| isAdminUser() | boolean | Returns true if the current user has the default admin role. |
isSystemUser
Section titled “isSystemUser”Description:
| Method | Return values | Description |
|---|---|---|
| isSystemUser() | boolean | Returns true if the current user is the internal system user used for background operations. |
isSuperUser
Section titled “isSuperUser”Description:
| Method | Return values | Description |
|---|---|---|
| isSuperUser() | boolean | Returns true if the current user is the default administrator user (okmAdmin). |
hasFullAccess
Section titled “hasFullAccess”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(); } }}Authentication access
Section titled “Authentication access”getAuthentication
Section titled “getAuthentication”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. |
getAuthenticationByToken
Section titled “getAuthenticationByToken”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. |
getRemoteAddress
Section titled “getRemoteAddress”Description:
| Method | Return values | Description |
|---|---|---|
| getRemoteAddress() | String | Returns the remote IP address of the currently authenticated user, or null if not available. |