public UserSession createTestSession() { User user = createTestUser(); return new UserSession(UUID.randomUUID(), user, Collections.emptyList(), Locale.ENGLISH, false); }
@Override public synchronized UserSession getUserSession() { if (exceptionOnGetUserSession) { throw new NoUserSessionException(UUID.fromString(USER_ID)); } if (session == null) { User user = new User(); user.setId(UUID.fromString(USER_ID)); user.setLogin("test_admin"); user.setName("Test Administrator"); user.setPassword(DigestUtils.md5Hex("test_admin")); session = new UserSession(UUID.randomUUID(), user, Collections.<Role>emptyList(), Locale.forLanguageTag("en"), false); } return session; }
/** * Create a new session and fill it with security data. Must be called inside a transaction. * @param sessionId target session id * @param user user instance * @param locale user locale * @param system create system session * @return new session instance */ public UserSession createSession(UUID sessionId, User user, Locale locale, boolean system) { List<Role> roles = new ArrayList<>(); for (UserRole userRole : user.getUserRoles()) { if (userRole.getRole() != null) { roles.add(userRole.getRole()); } } UserSession session = new UserSession(sessionId, user, roles, locale, system); compilePermissions(session, roles); if (user.getGroup() == null) throw new IllegalStateException("User is not in a Group"); compileConstraints(session, user.getGroup()); compileSessionAttributes(session, user.getGroup()); return session; }
/** * Create a new session from existing for another user and fill it with security data for that new user. * Must be called inside a transaction. * @param src existing session * @param user another user instance * @return new session with the same ID as existing */ public UserSession createSession(UserSession src, User user) { List<Role> roles = new ArrayList<>(); for (UserRole userRole : user.getUserRoles()) { if (userRole.getRole() != null) { roles.add(userRole.getRole()); } } UserSession session = new UserSession(src, user, roles, src.getLocale()); compilePermissions(session, roles); if (user.getGroup() == null) throw new IllegalStateException("User is not in a Group"); compileConstraints(session, user.getGroup()); compileSessionAttributes(session, user.getGroup()); return session; }
public Integer getPermissionValue(User user, PermissionType permissionType, String target) { Integer result; List<Role> roles = new ArrayList<>(); Transaction tx = persistence.createTransaction(); try { EntityManager em = persistence.getEntityManager(); user = em.find(User.class, user.getId()); for (UserRole userRole : user.getUserRoles()) { if (userRole.getRole() != null) { roles.add(userRole.getRole()); } } UserSession session = new UserSession(uuidSource.createUuid(), user, roles, userSessionSource.getLocale(), false); compilePermissions(session, roles); result = session.getPermissionValue(permissionType, target); tx.commit(); } finally { tx.end(); } return result; }