@Override public void restartSession(RealmModel realm, UserModel user, String loginUsername, String ipAddress, String authMethod, boolean rememberMe, String brokerSessionId, String brokerUserId) { UserSessionUpdateTask task = new UserSessionUpdateTask() { @Override public void runUpdate(UserSessionEntity entity) { provider.updateSessionEntity(entity, realm, user, loginUsername, ipAddress, authMethod, rememberMe, brokerSessionId, brokerUserId); entity.setState(null); entity.getNotes().clear(); entity.getAuthenticatedClientSessions().clear(); } }; update(task); }
@Override public void removeNote(String name) { UserSessionUpdateTask task = new UserSessionUpdateTask() { @Override public void runUpdate(UserSessionEntity entity) { entity.getNotes().remove(name); } }; update(task); }
@Override public void setState(State state) { UserSessionUpdateTask task = new UserSessionUpdateTask() { @Override public void runUpdate(UserSessionEntity entity) { entity.setState(state); } }; update(task); }
@Override public void setNote(String name, String value) { UserSessionUpdateTask task = new UserSessionUpdateTask() { @Override public void runUpdate(UserSessionEntity entity) { if (value == null) { if (entity.getNotes().containsKey(name)) { removeNote(name); } return; } entity.getNotes().put(name, value); } }; update(task); }
public void setLastSessionRefresh(int lastSessionRefresh) { entity.setLastSessionRefresh(lastSessionRefresh); update(); }
@Override public void setState(State state) { entity.setState(state); update(); }
@Override public void removeNote(String name) { if (entity.getNotes() != null) { entity.getNotes().remove(name); update(); } }
@Override public void setNote(String name, String value) { if (entity.getNotes() == null) { entity.setNotes(new HashMap<String, String>()); } entity.getNotes().put(name, value); update(); }
@Override public void removeAuthenticatedClientSessions(Collection<String> removedClientUUIDS) { if (removedClientUUIDS == null || removedClientUUIDS.isEmpty()) { return; } // Performance: do not remove the clientUUIDs from the user session until there is enough of them; // an invalid session is handled as nonexistent in UserSessionAdapter.getAuthenticatedClientSessions() if (removedClientUUIDS.size() >= MINIMUM_INACTIVE_CLIENT_SESSIONS_TO_CLEANUP) { // Update user session UserSessionUpdateTask task = new UserSessionUpdateTask() { @Override public void runUpdate(UserSessionEntity entity) { removedClientUUIDS.forEach(entity.getAuthenticatedClientSessions()::remove); } }; update(task); } // do not iterate the removedClientUUIDS and remove the clientSession directly as the addTask can manipulate // the collection being iterated, and that can lead to unpredictable behaviour (e.g. NPE) List<UUID> clientSessionUuids = removedClientUUIDS.stream() .map(entity.getAuthenticatedClientSessions()::get) .filter(Objects::nonNull) .collect(Collectors.toList()); clientSessionUuids.forEach(clientSessionId -> this.clientSessionUpdateTx.addTask(clientSessionId, Tasks.removeSync())); }
void attachSession(UserSessionAdapter userSession, ClientSessionModel clientSession) { UserSessionEntity entity = userSession.getEntity(); String clientSessionId = clientSession.getId(); if (entity.getClientSessions() == null) { entity.setClientSessions(new HashSet<String>()); } if (!entity.getClientSessions().contains(clientSessionId)) { entity.getClientSessions().add(clientSessionId); userSession.update(); } }
void dettachSession(UserSessionAdapter userSession, ClientSessionModel clientSession) { UserSessionEntity entity = userSession.getEntity(); String clientSessionId = clientSession.getId(); if (entity.getClientSessions() != null && entity.getClientSessions().contains(clientSessionId)) { entity.getClientSessions().remove(clientSessionId); if (entity.getClientSessions().isEmpty()) { entity.setClientSessions(null); } userSession.update(); } }
public void setLastSessionRefresh(int lastSessionRefresh) { if (offline) { // Received the message from the other DC that we should update the lastSessionRefresh in local cluster. Don't update DB in that case. // The other DC already did. Boolean ignoreRemoteCacheUpdate = (Boolean) session.getAttribute(CrossDCLastSessionRefreshListener.IGNORE_REMOTE_CACHE_UPDATE); if (ignoreRemoteCacheUpdate == null || !ignoreRemoteCacheUpdate) { provider.getPersisterLastSessionRefreshStore().putLastSessionRefresh(session, entity.getId(), realm.getId(), lastSessionRefresh); } } UserSessionUpdateTask task = new UserSessionUpdateTask() { @Override public void runUpdate(UserSessionEntity entity) { entity.setLastSessionRefresh(lastSessionRefresh); } @Override public CrossDCMessageStatus getCrossDCMessageStatus(SessionEntityWrapper<UserSessionEntity> sessionWrapper) { return new CrossDCLastSessionRefreshChecker(provider.getLastSessionRefreshStore(), provider.getOfflineLastSessionRefreshStore()) .shouldSaveUserSessionToRemoteCache(UserSessionAdapter.this.session, UserSessionAdapter.this.realm, sessionWrapper, offline, lastSessionRefresh); } @Override public String toString() { return "setLastSessionRefresh(" + lastSessionRefresh + ')'; } }; update(task); }