Package net.sf.basedb.core
Class SessionControl
- java.lang.Object
-
- net.sf.basedb.core.SessionControl
-
- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
PluginSessionControl
,ServiceSessionControl
public class SessionControl extends Object implements AutoCloseable
This class handles user sessions. Each instance is thread-safe and can handle a single user at a time. This class is used to createDbControl
objects which are needed to access the database.- Version:
- 2.0
- Author:
- Nicklas
- See Also:
Application.newSessionControl(String, String, String)
,Application.getSessionControl(String, String, String, boolean)
- Last modified
- $Date: 2020-06-05 13:10:03 +0200 (Fri, 05 Jun 2020) $
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
SessionControl.LoginInfo
Internal class to hold information about the logged in user.private static class
SessionControl.SettingInfo
Internal class to hold information about a settingstatic class
SessionControl.UnverifiedDeviceInfo
Class for storing temporary device information for a user that has been authenticated but before a device has been verified.
-
Field Summary
Fields Modifier and Type Field Description private Map<String,Boolean>
allowedClients
private Map<String,SessionControl.SettingInfo>
clientDefaultSettings
Stores name and value fromClientDefaultSettingData
.private int
clientId
The id of theClientData
. 0 = no clientprivate boolean
closed
If the session control has been closed it can no longer be used.private Map<ContextKey,ItemContext>
currentContexts
Map for storing current contexts.private Map<DbControl,String>
dbControlCache
Holds all createdDbControl
object, so we can close them if the user logs out.private String
externalClientId
The external id of theClientData
. null = no clientprivate String
id
The id of the object.private int
jobId
private long
lastAccess
The time of the last access to this object.private GeoLocation
location
Location of the remote IP.private boolean
locationLoaded
private static org.slf4j.Logger
log
Log core events.private SessionControl.LoginInfo
loginInfo
Login information for the logged in user.private int
pluginId
private String
remoteId
The remote IP.private SessionControl.UnverifiedDeviceInfo
unverifiedDeviceInfo
Temporary login information before a device is verified.
-
Constructor Summary
Constructors Constructor Description SessionControl(String id, String externalClientId, boolean failIfNoClient, String remoteId)
Creates a newSessionControl
object.SessionControl(String id, SessionControl parent, PluginDefinition plugin, Job job)
Create a new session control object using another one as the template.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected void
clearDbControlCache()
void
close()
Close this session control so it can't be used again.private SessionControl.LoginInfo
createLoginInfo(Session session, UserData userData, String comment, boolean impersonated, AuthenticationMethod authenticationMethod, int deviceId)
Create a LoginInfo object and load all information that it needs.void
deleteContext(int contextId)
Delete a context.int
deleteCurrentContexts(Item itemType, boolean inMemory, boolean inDatabase)
Delete all default contexts for the logged in user.int
getActiveProjectId()
Get the id of the active project, or 0 if no project is active.String
getActiveProjectName()
Get the name of the active project, or null if no project is active.Set<Permission>
getActiveProjectPermissions()
Get the permissions that the logged in user has in the currently active project.Set<Integer>
getActiveRoles()
Get the id of all active roles where the logged in user is a member.List<ItemContext>
getAllCurrentContexts(boolean inMemory, boolean inDatabase)
Get a list with the current default contexts.(package private) int
getAllPermissions(SharedData sharedData)
(package private) int
getAllPermissions(Item itemType, UserData owner, ItemKeyData itemKey, ProjectKeyData projectKey)
Get the combined permissions for an item of the specified type, owned by the specified user and shared with the specified item and project keys.Set<Integer>
getAllRoles()
Get the id of all roles where the logged in user is a member.AuthenticationMethod
getAuthenticationMethod()
Get the method that was used to authenticate the currently logged in user.String
getClientDefaultSetting(String name)
Get the value of theClientDefaultSetting
with the specified name.int
getClientId()
Get the id of theClient
application in use.private ClientData
getClientWithExternalId(String externalId)
ItemContext
getContext(int contextId)
Load a context without making it the current context.Enumeration<Integer,String>
getContextNames(Item item)
Same as:getContextNames(item, "")
Enumeration<Integer,String>
getContextNames(Item item, String subContext)
Get a list with the names of all named contexts for a given item and subcontext.ItemContext
getCurrentContext(Item item)
Same as:getCurrentContext(item, "", null)
.ItemContext
getCurrentContext(Item item, String subContext)
Same as:getCurrentContext(item, subContext, null)
.ItemContext
getCurrentContext(Item item, String subContext, ItemContext defaultContext)
Get the current context.ItemContext
getCurrentContext(Item item, ItemContext defaultContext)
Same as:getCurrentContext(item, "", defaultContext)
.int
getCurrentSessionId()
Get the id of the current user session, or 0 if no user is logged in.int
getDeviceId()
Get the id of theUserDevice
in use.String
getExternalClientId()
Get the external id of theClient
application in use.Set<Integer>
getFriends()
Get the id of all friends to logged in user.Set<Integer>
getGroups()
Get the id of all groups where the logged in user is a member, directly or indirectly.String
getId()
Get the id of thisSessionControl
.Set<Integer>
getInactiveRoles()
Get the id of all inactive roles where the logged in user is a member.int
getItemKeyId()
Get the id of theItemKey
that new items should be shared to according to the settings in the currently active project.Set<Integer>
getItemKeys()
Get the id of all item keys where the logged in user has read permission.Set<Integer>
getItemKeys(Permission permission)
Get the id of all item keys where the logged in user has the requested permission.int
getJobId()
Get the id of the job that is currently running with this session control.PluginSessionControl
getJobSessionControl(Job job)
Create a new session control for executing a job.long
getLastAccess()
Get the time this object last was accessed.Locale
getLocale()
Get the locale that is curently assigned to this session control.(package private) GeoLocation
getLocation(AuthenticationMethod authMeth)
int
getLoggedInUserId()
Get the id of the logged in user, or 0 if no user is logged in.String
getLoggedInUserLogin()
Get the login name of the currently logged in user.String
getLoggedInUserName()
Get the name of the currently logged in user.int
getPluginId()
Get the id of the plug-in that is currently running with this session control.(package private) PluginPermission
getPluginPermissions(Item itemType)
Get all plugin-specific permissions for the specified item type.PluginSessionControl
getPluginSessionControl(PluginDefinition plugin, Job job)
Create a new session control for executing a plugin.int
getProjectKeyId()
Get the id of theProjectKey
that can be used to share an item to the active project with full permission, or 0 if no project is active or the logged in user only has read permission to the active project.Set<Integer>
getProjectKeys()
Get the id of all project keys where the logged in user has read permission.Set<Integer>
getProjectKeys(Permission permission)
Get the id of all project keys where the logged in user has the requested permission.(package private) int
getProjectPermission(Project project)
Set<Integer>
getProjects()
Get the id of all projects where the logged in user is a member, directly or indirectly.(package private) String
getRemoteId()
Get the remote id that was passed to theApplication.newSessionControl(String,String,String)
method when creating this object.(package private) int
getRolePermissions(Item itemType)
Get all role-based permissions for the specified type of items.Set<Integer>
getRoles()
Deprecated.In 3.11, usegetAllRoles()
orgetActiveRoles()
instead<T> T
getSessionSetting(String name)
Get the value of a session setting with the specified name.(package private) int
getSharedPermissions(ShareableData shareableData)
Get the shared permissions for aShareableData
object.SessionControl.UnverifiedDeviceInfo
getUnverifiedDeviceInfo()
Get information about a device that is currently waiting to be verified.String
getUserClientSetting(String name)
Get the value of theUserClientSetting
with the specified name.String
getUserDefaultSetting(String name)
Get the value of theUserDefaultSetting
with the specified name.boolean
hasPermission(Permission permission, Item itemType)
Check if the logged in user has the specfied permission for all item of the specified type.boolean
hasSystemPermission(Permission permission)
Check if the logged in user has the specified system permission.SessionControl
impersonateLogin(int userId, String comment)
Log in as another user or create a clone of the currently logged in user's session.SessionControl
impersonateLogin(Ownable item, String comment)
Log in as the owner of the specified item.boolean
isAllowedToUseClient(String externalClientId)
Checks if this session control can be used by the given client application.boolean
isClosed()
If this session control has been closed or not.boolean
isDenied(Permission permission, Item itemType)
Check if the logged in user has been denied the specified permission for items of the specified type.boolean
isFriendOf(User user)
Check if the logged in user is a friend of of the specified user.boolean
isImpersonated()
Check if the logged in user was impersonated by another user.boolean
isLoggedIn()
Checks if a user is logged in or not.boolean
isMemberOf(Group group)
Check if the logged in user is a member, directly or indirectly, of the specified group.boolean
isMemberOf(Project project)
Check if the logged in user is a member, directly or indirectly, of the specified project.boolean
isMemberOf(Role role)
Check if the logged in user is a member of the specified role.boolean
isRoleInactive(int roleId)
Is the given role inactive or not?private Map<String,SessionControl.SettingInfo>
listToMap(List<? extends SettingData> settings)
Copy settings from a List to a Map.private Map<String,SessionControl.SettingInfo>
loadClientDefaultSettings(Session session, int clientId)
Load client applications default settings.ItemContext
loadContext(int contextId)
Load a context and make it the current context.ItemContext
loadContext(int contextId, boolean copyTemporaryObjects)
Load a context and make it the current context, optionally copying temporary filters and objects from the current context to the loaded context.private ItemContext
loadContextInternal(int contextId)
Load a context with the specified ID.private ItemContext
loadContextInternal(Item item, String subContext, String name)
Load a named context.private UserDeviceData
loadDevice(Session session, LoginRequest loginRequest, AuthenticatedUser authUser)
Verify the device the user is using.private Map<String,SessionControl.SettingInfo>
loadUserClientSettings(Session session, int userId, int clientId)
Load users client-specific settings.private Map<String,SessionControl.SettingInfo>
loadUserDefaultSettings(Session session, int userId)
Load users default settings.void
login(LoginRequest loginRequest)
Log in to BASE.(package private) void
login(Keyring keyring)
void
logout()
Log out of BASE.DbControl
newDbControl()
Create a newDbControl
object for this session.void
reloadPermissions()
Tell the core to reload the logged in users permission the next time the client application needs to check the permissions.void
reloadSettings(boolean onlyClientDefaultSettings, boolean saveCurrentUserSettings)
Reloads user- and client-specific settings.private void
saveAllCurrentContexts(Session session, UserData userData, ClientData clientData)
private void
saveAllUserSettings(Session session)
Saves all user settings.int
saveCurrentContextAs(Item item, String name, boolean overwrite, boolean isPublic)
Same as:saveCurrentContextAs(item, "", name, overwrite, isPublic)
.int
saveCurrentContextAs(Item item, String subContext, String name, boolean overwrite, boolean isPublic)
Save the current context as a named context.private <I extends SettingData>
voidsaveSettings(Session session, Query<I> query, Map<String,SessionControl.SettingInfo> settings, Class<I> dataClass, UserData userData, ClientData clientData)
Save settings when a user logs out.void
setActiveProject(Project project)
Set the active project.boolean
setRoleInactive(int roleId, boolean inactive)
Set a role as active or inactive.<T> T
setSessionSetting(String name, Object value)
Set the value of a session setting.String
setUserClientSetting(String name, String value)
Set the value of aUserClientSetting
.String
setUserDefaultSetting(String name, String value)
Set the value of aUserDefaultSetting
.void
updateLastAccess()
Update the time this object last was accessed.void
verifyDevice(String code, boolean rememberDevice)
Call this method to verify a device.private AuthenticatedUser
verifyUserExternal(Session session, LoginRequest loginRequest, List<AuthenticationManager> authManagers)
Verify the user with external authentication.(package private) AuthenticatedUser
verifyUserInternal(Session session, LoginRequest loginRequest)
Verify the user with internal authentication.
-
-
-
Field Detail
-
log
private static final org.slf4j.Logger log
Log core events.
-
id
private final String id
The id of the object. Used byApplication.getSessionControl(String,String,String,boolean)
-
clientId
private final int clientId
The id of theClientData
. 0 = no client
-
externalClientId
private final String externalClientId
The external id of theClientData
. null = no client
-
remoteId
private final String remoteId
The remote IP.
-
location
private GeoLocation location
Location of the remote IP.- Since:
- 3.12
-
locationLoaded
private boolean locationLoaded
-
dbControlCache
private final Map<DbControl,String> dbControlCache
Holds all createdDbControl
object, so we can close them if the user logs out. We use only the keys of the map since there is noWeakHashSet
class.
-
lastAccess
private long lastAccess
The time of the last access to this object.
-
closed
private boolean closed
If the session control has been closed it can no longer be used.
-
clientDefaultSettings
private Map<String,SessionControl.SettingInfo> clientDefaultSettings
Stores name and value fromClientDefaultSettingData
.
-
loginInfo
private SessionControl.LoginInfo loginInfo
Login information for the logged in user.
-
unverifiedDeviceInfo
private SessionControl.UnverifiedDeviceInfo unverifiedDeviceInfo
Temporary login information before a device is verified.
-
currentContexts
private final Map<ContextKey,ItemContext> currentContexts
Map for storing current contexts.
-
pluginId
private int pluginId
-
jobId
private int jobId
-
-
Constructor Detail
-
SessionControl
SessionControl(String id, SessionControl parent, PluginDefinition plugin, Job job)
Create a new session control object using another one as the template. Permissions and settings are copied/referenced from the parent session control. If the plugin uses permissions those permissions are loaded and used for access control.- Parameters:
id
- The ID if the new session controlparent
- The parent session controlplugin
- The plugin that is executing
-
SessionControl
SessionControl(String id, String externalClientId, boolean failIfNoClient, String remoteId) throws BaseException
Creates a newSessionControl
object.- Parameters:
id
- The id used inApplication.newSessionControl(String,String,String)
externalClientId
- Optional external ID of the client application in usefailIfNoClient
- If TRUE, an exception is thrown if an external client id has been specified, but not foundremoteId
- For example, the IP-address- Throws:
BaseException
-
-
Method Detail
-
getLastAccess
public long getLastAccess()
Get the time this object last was accessed. Used by the cleanup timer inApplication.cleanSessionControlCache(boolean)
- Returns:
- A long object.
- Since:
- 2.4.5
-
updateLastAccess
public void updateLastAccess()
Update the time this object last was accessed. Used by the cleanup timer inApplication.cleanSessionControlCache(boolean)
-
isClosed
public boolean isClosed()
If this session control has been closed or not. A closed session control can't be used.- Returns:
- TRUE if the session control is closed
- Since:
- 2.4.5
-
close
public void close()
Close this session control so it can't be used again.- Specified by:
close
in interfaceAutoCloseable
- Since:
- 2.4.5
-
getId
public String getId()
Get the id of thisSessionControl
. Use this value for retrieving the object from theApplication
cache.- Returns:
- The id
- See Also:
Application.getSessionControl(String,String,String,boolean)
-
getClientId
public int getClientId()
Get the id of theClient
application in use. UseClient.getById(DbControl, int)
to get theClient
object.- Returns:
- Client id as an int.
-
getExternalClientId
public String getExternalClientId()
Get the external id of theClient
application in use.- Returns:
- A java.lang.String object.
- See Also:
getClientId()
-
getDeviceId
public int getDeviceId()
Get the id of theUserDevice
in use. UseUserDevice.getById(DbControl, int)
to get theUserDevice
object.- Returns:
- Device id as an int, 0 if the device is unknown
- Since:
- 3.12
-
getRemoteId
String getRemoteId()
Get the remote id that was passed to theApplication.newSessionControl(String,String,String)
method when creating this object. This value is checked against the value passed to theApplication.getSessionControl(String,String,String,boolean)
method.
-
getLocation
GeoLocation getLocation(AuthenticationMethod authMeth)
-
newDbControl
public DbControl newDbControl() throws BaseException
Create a newDbControl
object for this session.- Returns:
- A
DbControl
object - Throws:
BaseException
- If there is an error
-
login
void login(Keyring keyring)
-
login
public void login(LoginRequest loginRequest)
Log in to BASE. The method checks that the given login is valid, the password is correct and that the user has USE permission for the client application if one has been specified.- Parameters:
loginRequest
- Request containing login/password and other information- Throws:
ItemNotFoundException
- If a user with the specified username is not foundInvalidPasswordException
- If the specified password is incorrectPermissionDeniedException
- If the user doesn't havePermission.USE
permission for the current client application or if a user is already logged inBaseException
- If there is any other error- Since:
- 3.3
- See Also:
logout()
,isLoggedIn()
,getLoggedInUserId()
-
getUnverifiedDeviceInfo
public SessionControl.UnverifiedDeviceInfo getUnverifiedDeviceInfo()
Get information about a device that is currently waiting to be verified. If this method returns a non-null value a verification has been sent to the user via email. This code should be used as a parameter when callingverifyDevice(String, boolean)
.- Returns:
- Information about the unverified device, or null if device verification is not needed
- Since:
- 3.12
-
verifyDevice
public void verifyDevice(String code, boolean rememberDevice)
Call this method to verify a device. If device verification is not needed at this moment, an LoginException is thrown. If the verification fails the login process must be re-started from the beginning. There is no second try. If the verification is successful, the user will be logged in after this method completes.- Parameters:
code
- The verification code that was sent (by email) to the userrememberDevice
- A flag indicating if the device should be remembered. If not, the device needs to be verified again the next time it is used- Since:
- 3.12
- See Also:
getUnverifiedDeviceInfo()
-
verifyUserInternal
AuthenticatedUser verifyUserInternal(Session session, LoginRequest loginRequest)
Verify the user with internal authentication. If the LoginRequest has a user id this is used first, otherwise the login is used.
-
verifyUserExternal
private AuthenticatedUser verifyUserExternal(Session session, LoginRequest loginRequest, List<AuthenticationManager> authManagers)
Verify the user with external authentication.
-
loadDevice
private UserDeviceData loadDevice(Session session, LoginRequest loginRequest, AuthenticatedUser authUser)
Verify the device the user is using. The verification is done if the current client application supports it and if the user has enabled device verification. If the device is found to be unverified a UnverifiedLoginInfo instance is created and returned.- Returns:
- An
UserDeviceData
object if device verification is supported and needed, null to continue with normal login - Since:
- 3.12, 3.17
-
impersonateLogin
public SessionControl impersonateLogin(int userId, String comment) throws ItemNotFoundException, PermissionDeniedException, BaseException
Log in as another user or create a clone of the currently logged in user's session. If this call is successful, you will get a newSessionControl
object which is equivalent to aSessionControl
where that user logged in by normal means. This method requires that the logged in user hasPermission.ACT_AS_ANOTHER_USER
permission or that the userId is the same as the currently logged in user.- Parameters:
userId
- The id of the user to login ascomment
- A comment that will be placed in theSession
information- Returns:
- A new
SessionControl
object - Throws:
ItemNotFoundException
- If no user with the specified id existsPermissionDeniedException
- If the logged in user doesn't havePermission.ACT_AS_ANOTHER_USER
permission and the userId is a different user from the currently logged in userBaseException
- If there is another error
-
impersonateLogin
public SessionControl impersonateLogin(Ownable item, String comment)
Log in as the owner of the specified item.- Parameters:
item
- Login as the owner of this Item. Null is not allowedcomment
- A comment that will be placed in theSession
information- Returns:
- A
SessionControl
for the created session. - See Also:
impersonateLogin(int, String)
-
isAllowedToUseClient
public boolean isAllowedToUseClient(String externalClientId)
Checks if this session control can be used by the given client application. It is allowed if the given id is the same as when this session control was created, or if there is a logged in user that is allowed to use the given client.- Parameters:
externalClientId
- The external id of the client to check- Returns:
- TRUE if the client can be used, FALSE if not
- Since:
- 3.9
-
getClientWithExternalId
private ClientData getClientWithExternalId(String externalId)
-
createLoginInfo
private SessionControl.LoginInfo createLoginInfo(Session session, UserData userData, String comment, boolean impersonated, AuthenticationMethod authenticationMethod, int deviceId) throws BaseException
Create a LoginInfo object and load all information that it needs.- Throws:
BaseException
-
logout
public void logout() throws BaseException
Log out of BASE. If the logged in user was impersonated the system will revert to the original user. If no user is logged in this method does nothing. If there are anyDbControl
objects still open, they will be closed. Changes made to items managed by thoseDbControl
objects will be lost unless the items are connected to a newDbControl
object.- Throws:
BaseException
- If there is an error- See Also:
login(LoginRequest)
,isLoggedIn()
,impersonateLogin(int, String)
,isImpersonated()
-
isLoggedIn
public boolean isLoggedIn()
Checks if a user is logged in or not.- Returns:
- TRUE if a user is logged in, FALSE otherwise.
- See Also:
login(LoginRequest)
,logout()
,getLoggedInUserId()
-
getLoggedInUserId
public int getLoggedInUserId()
Get the id of the logged in user, or 0 if no user is logged in. UseUser.getById(DbControl, int)
to get theUser
object.- Returns:
- User id as an int.
- See Also:
login(LoginRequest)
,logout()
,isLoggedIn()
-
getLoggedInUserLogin
public String getLoggedInUserLogin()
Get the login name of the currently logged in user. Returns null if no user is logged in.- Since:
- 3.15.1
- See Also:
User.getLogin()
-
getLoggedInUserName
public String getLoggedInUserName()
Get the name of the currently logged in user. Returns null if no user is logged in.- Since:
- 3.15.1
- See Also:
User.getName()
-
getAuthenticationMethod
public AuthenticationMethod getAuthenticationMethod()
Get the method that was used to authenticate the currently logged in user. Internal authentication returnAuthenticationMethod.INTERNAL
.- Returns:
- An AuthenticationMethod object or null if no user is logged in
- Since:
- 3.3
-
getCurrentSessionId
public int getCurrentSessionId()
Get the id of the current user session, or 0 if no user is logged in. UseSession.getById(DbControl, int)
to theSession
object.- Returns:
- The session id, or 0 if no user is logged in
- Since:
- 2.13
-
getPluginId
public int getPluginId()
Get the id of the plug-in that is currently running with this session control.- Returns:
- The plug-in id, or 0 if not running by a plug-in
- Since:
- 2.13
-
getJobId
public int getJobId()
Get the id of the job that is currently running with this session control.- Returns:
- The job id, or 0 if not running by a job
- Since:
- 2.13
-
isImpersonated
public boolean isImpersonated()
Check if the logged in user was impersonated by another user.- Returns:
- TRUE or FALSE
- See Also:
impersonateLogin(int, String)
-
reloadPermissions
public void reloadPermissions()
Tell the core to reload the logged in users permission the next time the client application needs to check the permissions.
-
reloadSettings
public void reloadSettings(boolean onlyClientDefaultSettings, boolean saveCurrentUserSettings) throws BaseException
Reloads user- and client-specific settings.- Parameters:
onlyClientDefaultSettings
- Setting this to TRUE will only reloadClientDefaultSetting
:ssaveCurrentUserSettings
- Setting this to TRUE will save the current user-specific settings before reloading them. This parameter is ignored ifonlyClientDefaultSettings
is true. The settings are never changed if the logged in user is a multi-user account.- Throws:
BaseException
- If the settings cannot be reloaded
-
getActiveProjectId
public int getActiveProjectId()
Get the id of the active project, or 0 if no project is active. UseProject.getById(DbControl, int)
to get theProject
object.- Returns:
- An int.
- See Also:
setActiveProject(Project)
-
getActiveProjectName
public String getActiveProjectName()
Get the name of the active project, or null if no project is active.- Since:
- 3.15.1
- See Also:
setActiveProject(Project)
-
setActiveProject
public void setActiveProject(Project project) throws PermissionDeniedException, BaseException
Set the active project. When a project is active queries will by default only load items shared to the that project, and newShareable
items will automatically be shared to the active project with delete permission. The active project doesn't affect already existing items.- Parameters:
project
- The project to make active- Throws:
PermissionDeniedException
- If the logged in user doesn't havePermission.USE
permission for the projectBaseException
- If there is another error- See Also:
getActiveProjectId()
-
getActiveProjectPermissions
public Set<Permission> getActiveProjectPermissions()
Get the permissions that the logged in user has in the currently active project.- Returns:
- A Set with the permissions
- Since:
- 3.1
-
getPluginSessionControl
public PluginSessionControl getPluginSessionControl(PluginDefinition plugin, Job job)
Create a new session control for executing a plugin.- Parameters:
plugin
- The plugin to executejob
- The job that executes the plug-in, or null- Returns:
- A plugin session control
- Since:
- 2.15 (was package private before that)
-
getJobSessionControl
public PluginSessionControl getJobSessionControl(Job job)
Create a new session control for executing a job. The new session control will inherit all settings from this session control.- Since:
- 3.3
-
getProjectKeyId
public int getProjectKeyId()
Get the id of theProjectKey
that can be used to share an item to the active project with full permission, or 0 if no project is active or the logged in user only has read permission to the active project. UseProjectKey.getById(DbControl, int)
to get theProjectKey
object.- See Also:
getActiveProjectId()
,setActiveProject(Project)
-
getItemKeyId
public int getItemKeyId()
Get the id of theItemKey
that new items should be shared to according to the settings in the currently active project. If no project is active or if the projects doesn't share to any item key, 0 is returned. UseItemKey.getById(DbControl, int)
to get theItemKey
object.- Since:
- 2.16
- See Also:
getActiveProjectId()
,setActiveProject(Project)
,getProjectKeyId()
-
hasSystemPermission
public boolean hasSystemPermission(Permission permission)
Check if the logged in user has the specified system permission.- Parameters:
permission
- One of the system permission constants defined by thePermission
enumeration- Returns:
- TRUE if the logged in user has the permission, FALSE otherwise
-
hasPermission
public boolean hasPermission(Permission permission, Item itemType)
Check if the logged in user has the specfied permission for all item of the specified type. This method checks the permissions given to the roles the user is a member of and, if a plugin is active, the permissions given to the plugin.- Parameters:
permission
- One of the permissions constants defined by thePermission
enumerationitemType
- One of the item constants defined by theItem
enumeration- Returns:
- TRUE if the logged in user has the permission, FALSE otherwise
-
isDenied
public boolean isDenied(Permission permission, Item itemType)
Check if the logged in user has been denied the specified permission for items of the specified type. This method checks the permissions given to the roles the user is a member of and, if a plugin is active, the permissions given to the plugin. Note that this method is not the inverse of thehasPermission(Permission, Item)
method. Both methods may return false in which case the system also checks ownership and sharing information to determine if access is granted. If this method return TRUE thehasPermission
method always return false.- Parameters:
permission
- One of the permissions constants defined by thePermission
enumerationitemType
- One of the item constants defined by theItem
enumeration- Returns:
- TRUE if the logged in user is denied the permission, FALSE otherwise
-
getRolePermissions
int getRolePermissions(Item itemType)
Get all role-based permissions for the specified type of items.
-
getPluginPermissions
PluginPermission getPluginPermissions(Item itemType)
Get all plugin-specific permissions for the specified item type.
-
getSharedPermissions
int getSharedPermissions(ShareableData shareableData)
Get the shared permissions for aShareableData
object.
-
getAllPermissions
int getAllPermissions(Item itemType, UserData owner, ItemKeyData itemKey, ProjectKeyData projectKey)
Get the combined permissions for an item of the specified type, owned by the specified user and shared with the specified item and project keys.
-
getAllPermissions
int getAllPermissions(SharedData sharedData)
-
isMemberOf
public boolean isMemberOf(Group group)
Check if the logged in user is a member, directly or indirectly, of the specified group.- Parameters:
group
- The group to check- Returns:
- TRUE if the user is a member, FALSE otherwise
-
isMemberOf
public boolean isMemberOf(Role role)
Check if the logged in user is a member of the specified role.- Parameters:
role
- The role to check- Returns:
- TRUE if the user is a member, FALSE otherwise
-
isMemberOf
public boolean isMemberOf(Project project)
Check if the logged in user is a member, directly or indirectly, of the specified project.- Parameters:
project
- The project to check- Returns:
- TRUE if the user is a member, FALSE otherwise
-
getProjectPermission
int getProjectPermission(Project project)
-
isFriendOf
public boolean isFriendOf(User user)
Check if the logged in user is a friend of of the specified user. A users is a friend if the logged in user is a member, directly or indirectly, of a group where the other user is direct member.- Parameters:
user
- The user to check- Returns:
- TRUE if the user is a friend, FALSE otherwise
-
getRoles
@Deprecated public Set<Integer> getRoles()
Deprecated.In 3.11, usegetAllRoles()
orgetActiveRoles()
insteadGet the id of all roles where the logged in user is a member. For backwards compatibility reasons this method returngetAllRoles()
.- Returns:
- A
Set
containing role id:s
-
getAllRoles
public Set<Integer> getAllRoles()
Get the id of all roles where the logged in user is a member.- Returns:
- A
Set
containing role id:s, or null - Since:
- 3.11
-
getActiveRoles
public Set<Integer> getActiveRoles()
Get the id of all active roles where the logged in user is a member.- Returns:
- A
Set
containing role id:s or null - Since:
- 3.11
-
getInactiveRoles
public Set<Integer> getInactiveRoles()
Get the id of all inactive roles where the logged in user is a member.- Returns:
- A
Set
containing role id:s or null - Since:
- 3.11
-
isRoleInactive
public boolean isRoleInactive(int roleId)
Is the given role inactive or not? Note that if the user is not a member of the given role FALSE is returned.- Parameters:
roleId
- The id of the role to check- Returns:
- TRUE if the role is inactive, FALSE if not (or if the user is not a member)
- Since:
- 3.11
-
setRoleInactive
public boolean setRoleInactive(int roleId, boolean inactive)
Set a role as active or inactive.- Parameters:
roleId
- The id of the role to setinactive
- TRUE to set the role to inactive, FALSE to set it to active- Returns:
- TRUE if a change was made, FALSE if not (or if the user is not a member of the role)
- Since:
- 3.11
-
getGroups
public Set<Integer> getGroups()
Get the id of all groups where the logged in user is a member, directly or indirectly.- Returns:
- A
Set
containing group id:s
-
getFriends
public Set<Integer> getFriends()
Get the id of all friends to logged in user. A users is a friend if the logged in user is a member, directly or indirectly, of a group where the other user is direct member.- Returns:
- A
Set
containing user id:s
-
getProjects
public Set<Integer> getProjects()
Get the id of all projects where the logged in user is a member, directly or indirectly.- Returns:
- A
Set
containing project id:s
-
getItemKeys
public Set<Integer> getItemKeys()
Get the id of all item keys where the logged in user has read permission.- Returns:
- A
Set
containing item key id:s
-
getItemKeys
public Set<Integer> getItemKeys(Permission permission)
Get the id of all item keys where the logged in user has the requested permission.- Parameters:
permission
- Requested permissions.- Returns:
- A
Set
containing item key id:s
-
getProjectKeys
public Set<Integer> getProjectKeys()
Get the id of all project keys where the logged in user has read permission.- Returns:
- A
Set
containing project key id:s
-
getProjectKeys
public Set<Integer> getProjectKeys(Permission permission)
Get the id of all project keys where the logged in user has the requested permission.- Parameters:
permission
- The requested permissions- Returns:
- A
Set
containing project key id:s
-
getCurrentContext
public ItemContext getCurrentContext(Item item)
Same as:getCurrentContext(item, "", null)
.
-
getCurrentContext
public ItemContext getCurrentContext(Item item, String subContext)
Same as:getCurrentContext(item, subContext, null)
.
-
getCurrentContext
public ItemContext getCurrentContext(Item item, ItemContext defaultContext)
Same as:getCurrentContext(item, "", defaultContext)
.- Parameters:
item
- Type of item for which current context should be returned.defaultContext
- A default context to copy values from if a new context must be created- Returns:
- A
ItemContext
object. - See Also:
getCurrentContext(Item, String, ItemContext)
-
getCurrentContext
public ItemContext getCurrentContext(Item item, String subContext, ItemContext defaultContext)
Get the current context. If no current context exists for the item/subcontext and a user is logged in, the default context will be loaded from the database. If it doesn't exist in the database or no user is logged in a new current context will be created. If a default context is specified the settings will be copied to the new context. The current context is automatically saved in the database when the user logs out.- Parameters:
item
- The type of item to get the context forsubContext
- Current sub context.defaultContext
- A default context to copy values from if a new context must be created.- Returns:
- A
ItemContext
object. - Throws:
InvalidDataException
- If the item is nullBaseException
- If there is another error- See Also:
getCurrentContext(Item)
,loadContextInternal(Item, String, String)
,saveCurrentContextAs(Item, String, boolean, boolean)
-
loadContext
public ItemContext loadContext(int contextId) throws NotLoggedInException, BaseException
Load a context and make it the current context. All information in the current context is lost. If a saved context with the specified ID doesn't exists, or if the logged in user does't have permission to read it, null is returned and the current context is left unchanged.- Parameters:
contextId
- The ID of the context to load- Returns:
- An
ItemContext
object or null - Throws:
NotLoggedInException
- If no user is logged inBaseException
- If there is another error- See Also:
saveCurrentContextAs(Item, String, boolean, boolean)
,getContextNames(Item)
-
loadContext
public ItemContext loadContext(int contextId, boolean copyTemporaryObjects) throws NotLoggedInException, BaseException
Load a context and make it the current context, optionally copying temporary filters and objects from the current context to the loaded context. If a saved context with the specified ID doesn't exists, or if the logged in user does't have permission to read it, null is returned and the current context is left unchanged.- Parameters:
contextId
- The ID of the context to load- Returns:
- An
ItemContext
object or null - Throws:
NotLoggedInException
- If no user is logged inBaseException
- If there is another error- Since:
- 3.6
- See Also:
saveCurrentContextAs(Item, String, boolean, boolean)
,getContextNames(Item)
-
getContext
public ItemContext getContext(int contextId)
Load a context without making it the current context. If a saved context with the specified ID doesn't exists, or if the logged in user does't have permission to read it, null is returned.- Parameters:
contextId
- The ID of the context to load- Returns:
- An
ItemContext
object or null - Throws:
NotLoggedInException
- If no user is logged inBaseException
- If there is another error- See Also:
loadContext(int)
,getContextNames(Item)
-
saveCurrentContextAs
public int saveCurrentContextAs(Item item, String name, boolean overwrite, boolean isPublic) throws PermissionDeniedException, InvalidDataException, ItemAlreadyExistsException, BaseException
Same as:saveCurrentContextAs(item, "", name, overwrite, isPublic)
.
-
saveCurrentContextAs
public int saveCurrentContextAs(Item item, String subContext, String name, boolean overwrite, boolean isPublic) throws PermissionDeniedException, InvalidDataException, ItemAlreadyExistsException, BaseException
Save the current context as a named context. Named contexts are stored in the database and can be loaded at any time with theloadContext(int)
method. If no current context exists, this method does nothing.- Parameters:
item
- The type of item to save the current context forsubContext
- The name of the sub-contextname
- The name of the contextoverwrite
- If TRUE, an existing context with the same name is overwritten, otherwise an exception is thrownisPublic
- If the saved context should be visible to everyone or not (requiresPermission.SHARE_TO_EVERYONE
permission)- Returns:
- Saved context's id as an int.
- Throws:
PermissionDeniedException
- If no user is logged in, or if the logged in user is a multi-user account or if theisPublic
parameter is true but the logged in user doesn't haveSHARE_TO_EVERYONE
permissionInvalidDataException
- If the item or name parameter is nullItemAlreadyExistsException
- If another context with the same name exists and the overwrite parameter is FALSEBaseException
- If there is another error
-
deleteContext
public void deleteContext(int contextId) throws PermissionDeniedException, BaseException
Delete a context. If a saved context with the specified id doesn't exists, this method does nothing.- Parameters:
contextId
- The id of the context- Throws:
PermissionDeniedException
- If no user is logged in, or if he logged in user is a multi-user account or isn't the owner of the contextBaseException
- If there is another error- See Also:
getContextNames(Item, String)
-
getContextNames
public Enumeration<Integer,String> getContextNames(Item item) throws InvalidDataException, BaseException
Same as:getContextNames(item, "")
- Throws:
InvalidDataException
BaseException
- See Also:
getContextNames(Item, String)
-
getContextNames
public Enumeration<Integer,String> getContextNames(Item item, String subContext) throws InvalidDataException, BaseException
Get a list with the names of all named contexts for a given item and subcontext.- Parameters:
item
- Item for which named contexts should be returnedsubContext
- Certain sub context to return contexts for.- Returns:
- A enumeration containing the id and names, the list will be empty if no user is logged in or if no names contexts exists for the specified item type/subcontext
- Throws:
InvalidDataException
- If the item parameter is nullBaseException
- If there is another error
-
loadContextInternal
private ItemContext loadContextInternal(int contextId) throws BaseException
Load a context with the specified ID. If no context with that ID exists or if the context belongs to a different client application or user and not is public null is returned.- Throws:
BaseException
-
loadContextInternal
private ItemContext loadContextInternal(Item item, String subContext, String name) throws BaseException
Load a named context. Returns null if no context with the specified name exists for the specified item type/subcontext.- Throws:
BaseException
-
saveAllCurrentContexts
private void saveAllCurrentContexts(Session session, UserData userData, ClientData clientData)
-
getAllCurrentContexts
public List<ItemContext> getAllCurrentContexts(boolean inMemory, boolean inDatabase)
Get a list with the current default contexts. The list may include, only in-memory contexts, in-database contexts, or both. If both options are enabled and an in-memory context also has a database representation, both are included in the list. Eg. the list may include more than one context wereItemContext.getContextId()
returns the same value.- Parameters:
inMemory
- If TRUE, the in-memory contexts are included in the listinDatabase
- If TRUE, the in-database contexts are included in the list- Returns:
- A list with ItemContext objects
- Since:
- 2.15
-
deleteCurrentContexts
public int deleteCurrentContexts(Item itemType, boolean inMemory, boolean inDatabase)
Delete all default contexts for the logged in user.- Parameters:
itemType
- The item type to delete the contexts for, or null to delete all contextsinMemory
- TRUE to delete in-memory contextsinDatabase
- TRUE to delete in-database contexts- Returns:
- The number of contexts deleted
- Since:
- 2.8
-
getLocale
public Locale getLocale()
Get the locale that is curently assigned to this session control.- Returns:
- A Locale object
- Since:
- 2.16
-
getSessionSetting
public <T> T getSessionSetting(String name)
Get the value of a session setting with the specified name.- Parameters:
name
- The name of the setting- Returns:
- A object with the value of the setting, or null if no setting is found
-
setSessionSetting
public <T> T setSessionSetting(String name, Object value)
Set the value of a session setting.- Parameters:
name
- The name of the settingvalue
- The new value of the setting, or null to remove the setting- Returns:
- The old value of the setting, or null if it did not exist
-
getUserClientSetting
public String getUserClientSetting(String name)
Get the value of theUserClientSetting
with the specified name. If no user is logged in null is returned.- Parameters:
name
- The name of the setting- Returns:
- A string with the value of the setting, or null if no setting is found
-
setUserClientSetting
public String setUserClientSetting(String name, String value)
Set the value of aUserClientSetting
.- Parameters:
name
- The name of the settingvalue
- The new value of the setting, or null to remove the setting- Returns:
- The old value of the setting, or null if it did not exist or no user is logged in
-
getUserDefaultSetting
public String getUserDefaultSetting(String name)
Get the value of theUserDefaultSetting
with the specified name.- Parameters:
name
- The name of the setting- Returns:
- A string with the value of the setting, or null if no setting is found
-
setUserDefaultSetting
public String setUserDefaultSetting(String name, String value)
Set the value of aUserDefaultSetting
.- Parameters:
name
- The name of the settingvalue
- The new value of the setting, or null to remove the setting- Returns:
- The old value of the setting, or null if it did not exist or no user is logged in
-
getClientDefaultSetting
public String getClientDefaultSetting(String name)
Get the value of theClientDefaultSetting
with the specified name.- Parameters:
name
- The name of the setting- Returns:
- A string with the value of the setting, or null if no setting is found
-
listToMap
private Map<String,SessionControl.SettingInfo> listToMap(List<? extends SettingData> settings)
Copy settings from a List to a Map. The key is the name of the setting (SettingData.getName()
) and the value is aSessionControl.SettingInfo
object.
-
loadClientDefaultSettings
private Map<String,SessionControl.SettingInfo> loadClientDefaultSettings(Session session, int clientId)
Load client applications default settings.
-
loadUserDefaultSettings
private Map<String,SessionControl.SettingInfo> loadUserDefaultSettings(Session session, int userId)
Load users default settings.
-
loadUserClientSettings
private Map<String,SessionControl.SettingInfo> loadUserClientSettings(Session session, int userId, int clientId)
Load users client-specific settings.
-
saveAllUserSettings
private void saveAllUserSettings(Session session)
Saves all user settings.
-
saveSettings
private <I extends SettingData> void saveSettings(Session session, Query<I> query, Map<String,SessionControl.SettingInfo> settings, Class<I> dataClass, UserData userData, ClientData clientData) throws BaseException
Save settings when a user logs out. This method will delete settings that have been deleted, update changed settings and create new settings.- Parameters:
session
- The Hibernate sessionquery
- A Hibernate query to load the settings already in the databasesettings
- The settings to savedataClass
- TheClass
of the settings, should be one ofUserDefaultSettingData
orUserClientSettingData
userData
- TheUserData
object for the logged in userclientData
- TheClientData
object for the client application- Throws:
BaseException
-
clearDbControlCache
protected void clearDbControlCache()
-
-