/** * Find an User by the id specified * * @param id * @return User */ @Override public User findById(long id) throws RuntimeException { IdentityQuery<User> query = identityManager.createIdentityQuery(User.class); query.setParameter(User.ID, id); return query.getResultList().get(0); }
@Override public boolean deleteGroup(String id) { verifyIdentityManager(); IdentityQuery<Group> query = identityManager.<Group> createIdentityQuery(Group.class); query.setParameter(AttributedType.ID, id); List<Group> result = query.getResultList(); Group group = null; if(result.size() == 1){ group = result.get(0); } else if(result.size() == 0){ log.error("No group instances with id:" + id); } else { log.error("Multiple group instances with id:" + id); } if (group != null) { identityManager.remove(group); return true; } return false; }
@Override public boolean deleteUser(String id) { verifyIdentityManager(); IdentityQuery<User> query = identityManager.<User> createIdentityQuery(User.class); query.setParameter(AttributedType.ID, id); List<User> result = query.getResultList(); User user = null; if(result.size() == 1){ user = result.get(0); } else if(result.size() == 0) { log.error("No user instances with id:" + id); } else { log.error("Multiple user instances with id:" + id); } if(user != null){ identityManager.remove(user); return true; } return false; }
/** * Validate the access token * * @param passedAccessToken * @param identityManager * @return */ public static boolean validateAccessToken(String passedAccessToken, IdentityManager identityManager) { IdentityQuery<Agent> agentQuery = identityManager.createIdentityQuery(Agent.class); agentQuery.setParameter(AttributedType.QUERY_ATTRIBUTE.byName("accessToken"), passedAccessToken); List<Agent> agents = agentQuery.getResultList(); int size = agents.size(); if (size == 0 || size != 1) { return false; } return true; }
private List<Group> getGroups(User storedUser) { IdentityQuery<Group> groupQuery = getIdentityManager().createIdentityQuery(Group.class); groupQuery.setParameter(Role.HAS_MEMBER, storedUser); return groupQuery.getResultList(); }
private List<Role> getRoles(User userFromIDM) { IdentityQuery<Role> query = getIdentityManager().createIdentityQuery(Role.class); query.setParameter(Role.ROLE_OF, userFromIDM); return query.getResultList(); }
/** * Get All the users * * @param name Role name * @return Users by roles */ @Override public List<User> findAllByRole(String name) { Role role = BasicModel.getRole(identityManager, name); IdentityQuery<User> query = identityManager.createIdentityQuery(User.class); query.setParameter(GroupRole.ROLE, role); return query.getResultList(); }
@Override public SCIMGroups getGroups(String id) { verifyIdentityManager(); SCIMGroups scimGroup = new SCIMGroups(); IdentityQuery<Group> query = identityManager.<Group> createIdentityQuery(Group.class); query.setParameter(AttributedType.ID, id); List<Group> result = query.getResultList(); Group group = null; if(result.size() == 1){ group = result.get(0); } else if(result.size() == 0){ log.error("No group instances with id:" + id); } else { log.error("Multiple group instances with id:" + id); } if (group != null) { scimGroup.setDisplayName(group.getName()); scimGroup.setId(id); } return scimGroup; }
/** * <p> * Find an {@link User} by id. * </p> * * @throws Exception */ @Test public void testFindById() throws Exception { loadOrCreateUser("admin", true); IdentityManager identityManager = getIdentityManager(); IdentityQuery<User> query = identityManager.<User> createQuery(User.class); query.setParameter(User.ID, "admin"); List<User> result = query.getResultList(); assertFalse(result.isEmpty()); assertTrue(result.size() == 1); assertEquals("admin", result.get(0).getId()); }
/** * <p> * Find an {@link Agent} by id. * </p> * * @throws Exception */ @Test public void testFindById() throws Exception { loadOrCreateAgent("someAgent", true); IdentityManager identityManager = getIdentityManager(); IdentityQuery<Agent> query = identityManager.<Agent> createQuery(Agent.class); query.setParameter(Agent.ID, "someAgent"); List<Agent> result = query.getResultList(); assertFalse(result.isEmpty()); assertTrue(result.size() == 1); assertEquals("someAgent", result.get(0).getId()); }
/** * <p> * Find an {@link Group} by id. * </p> * * @throws Exception */ @Test public void testFindByName() throws Exception { loadOrCreateGroup("admin", null, true); IdentityManager identityManager = getIdentityManager(); IdentityQuery<Group> query = identityManager.<Group> createQuery(Group.class); query.setParameter(Group.NAME, "admin"); List<Group> result = query.getResultList(); assertFalse(result.isEmpty()); assertTrue(result.size() == 1); assertEquals("admin", result.get(0).getName()); }
/** * <p> * Find an {@link Role} by id. * </p> * * @throws Exception */ @Test public void testFindByName() throws Exception { loadOrCreateRole("admin", true); IdentityManager identityManager = getIdentityManager(); IdentityQuery<Role> query = identityManager.<Role> createQuery(Role.class); query.setParameter(Role.NAME, "admin"); List<Role> result = query.getResultList(); assertFalse(result.isEmpty()); assertTrue(result.size() == 1); assertEquals("admin", result.get(0).getName()); }
/** * <p> * Finds groups by the creation date. * </p> * * @throws Exception */ @Test public void testFindCreationDate() throws Exception { Group group = loadOrCreateGroup("someGroup", null, true); IdentityManager identityManager = getIdentityManager(); IdentityQuery<Group> query = identityManager.<Group> createQuery(Group.class); query.setParameter(Group.CREATED_DATE, group.getCreatedDate()); List<Group> result = query.getResultList(); assertFalse(result.isEmpty()); assertTrue(result.size() == 1); assertEquals("someGroup", result.get(0).getName()); query = identityManager.<Group> createQuery(Group.class); Thread.sleep(500); query.setParameter(Group.CREATED_DATE, new Date()); result = query.getResultList(); assertTrue(result.isEmpty()); }
/** * <p> * Finds roles by the creation date. * </p> * * @throws Exception */ @Test public void testFindCreationDate() throws Exception { Role role = loadOrCreateRole("someRole", false); IdentityManager identityManager = getIdentityManager(); IdentityQuery<Role> query = identityManager.<Role> createQuery(Role.class); query.setParameter(Role.CREATED_DATE, role.getCreatedDate()); Thread.sleep(500); // only the previously created role List<Role> result = query.getResultList(); assertFalse(result.isEmpty()); assertTrue(result.size() == 1); assertEquals("someRole", result.get(0).getName()); query = identityManager.<Role> createQuery(Role.class); query.setParameter(Role.CREATED_DATE, new Date()); // no roles result = query.getResultList(); assertTrue(result.isEmpty()); }
/** * <p> * Find an {@link User} by his associated {@link Group}. * </p> * * @throws Exception */ @Test public void testFindBySingleGroup() throws Exception { User user = loadOrCreateUser("admin", true); Group administratorGroup = loadOrCreateGroup("Administrators", null, true); IdentityManager identityManager = getIdentityManager(); IdentityQuery<User> query = identityManager.createQuery(User.class); query.setParameter(User.MEMBER_OF, new String[] { "Administrators" }); List<User> result = query.getResultList(); assertTrue(result.isEmpty()); identityManager.addToGroup(user, administratorGroup); query = identityManager.createQuery(User.class); query.setParameter(User.MEMBER_OF, new String[] { "Administrators" }); result = query.getResultList(); assertFalse(result.isEmpty()); assertEquals(user.getId(), result.get(0).getId()); }
/** * <p> * Find an {@link Agent} by his associated {@link Role}. * </p> * * @throws Exception */ @Test public void testFindBySingleRole() throws Exception { Agent user = loadOrCreateAgent("admin", true); Role administratorRole = loadOrCreateRole("Administrators", true); IdentityManager identityManager = getIdentityManager(); IdentityQuery<Agent> query = identityManager.createQuery(Agent.class); query.setParameter(Agent.HAS_ROLE, new String[] { "Administrators" }); List<Agent> result = query.getResultList(); assertTrue(result.isEmpty()); identityManager.grantRole(user, administratorRole); query = identityManager.createQuery(Agent.class); query.setParameter(Agent.HAS_ROLE, new String[] { "Administrators" }); result = query.getResultList(); assertFalse(result.isEmpty()); assertEquals(user.getId(), result.get(0).getId()); }
/** * <p> * Find an {@link Agent} by his associated {@link Group}. * </p> * * @throws Exception */ @Test public void testFindBySingleGroup() throws Exception { Agent user = loadOrCreateAgent("admin", true); Group administratorGroup = loadOrCreateGroup("Administrators", null, true); IdentityManager identityManager = getIdentityManager(); IdentityQuery<Agent> query = identityManager.createQuery(Agent.class); query.setParameter(Agent.MEMBER_OF, new String[] { "Administrators" }); List<Agent> result = query.getResultList(); assertTrue(result.isEmpty()); identityManager.addToGroup(user, administratorGroup); query = identityManager.createQuery(Agent.class); query.setParameter(Agent.MEMBER_OF, new String[] { "Administrators" }); result = query.getResultList(); assertFalse(result.isEmpty()); assertEquals(user.getId(), result.get(0).getId()); }
/** * <p> * Find an {@link User} by his associated {@link Role}. * </p> * * @throws Exception */ @Test public void testFindBySingleRole() throws Exception { User user = loadOrCreateUser("admin", true); Role administratorRole = loadOrCreateRole("Administrators", true); IdentityManager identityManager = getIdentityManager(); IdentityQuery<User> query = identityManager.createQuery(User.class); query.setParameter(User.HAS_ROLE, new String[] { "Administrators" }); List<User> result = query.getResultList(); assertTrue(result.isEmpty()); identityManager.grantRole(user, administratorRole); query = identityManager.createQuery(User.class); query.setParameter(User.HAS_ROLE, new String[] { "Administrators" }); result = query.getResultList(); assertFalse(result.isEmpty()); assertEquals(user.getId(), result.get(0).getId()); }
/** * <p> * Finds groups by the creation date. * </p> * * @throws Exception */ @Test public void testFindWithParent() throws Exception { Group group = loadOrCreateGroup("someGroup", "Parent Group", true); IdentityManager identityManager = getIdentityManager(); IdentityQuery<Group> query = identityManager.<Group> createQuery(Group.class); query.setParameter(Group.PARENT, group.getParentGroup().getName()); List<Group> result = query.getResultList(); assertFalse(result.isEmpty()); assertTrue(result.size() == 1); assertEquals(group.getName(), result.get(0).getName()); assertEquals(group.getParentGroup().getName(), result.get(0).getParentGroup().getName()); }
/** * <p> * Find an {@link Agent} by his associated {@link Group} and {@link Role}. * </p> * * @throws Exception */ @Test public void testFindBySingleGroupRole() throws Exception { Agent user = loadOrCreateAgent("someAgent", true); Group salesGroup = loadOrCreateGroup("Sales", null, true); Role managerRole = loadOrCreateRole("Manager", true); IdentityManager identityManager = getIdentityManager(); IdentityQuery<Agent> query = identityManager.createQuery(Agent.class); query.setParameter(Agent.HAS_GROUP_ROLE, new GroupRole[] { new SimpleGroupRole(user, managerRole, salesGroup) }); List<Agent> result = query.getResultList(); assertTrue(result.isEmpty()); identityManager.grantGroupRole(user, managerRole, salesGroup); query = identityManager.createQuery(Agent.class); query.setParameter(Agent.HAS_GROUP_ROLE, new GroupRole[] { new SimpleGroupRole(user, managerRole, salesGroup) }); result = query.getResultList(); assertFalse(result.isEmpty()); assertEquals(user.getId(), result.get(0).getId()); }