@Test public void testGetById() { Assert.assertEquals(realm1, model.getRealm("id1")); Assert.assertEquals(realm1, model.getRealmByName("realm1")); Assert.assertEquals(realm2, model.getRealm("id2")); Assert.assertEquals(realm2, model.getRealmByName("realm2")); ClientModel r1app1 = realm1.getClientByClientId("app1"); ClientModel r1app2 = realm1.getClientByClientId("app2"); ClientModel r2app1 = realm2.getClientByClientId("app1"); ClientModel r2app2 = realm2.getClientByClientId("app2"); Assert.assertEquals(r1app1, realm1.getClientById(r1app1.getId())); Assert.assertNull(realm2.getClientById(r1app1.getId())); ClientModel r2cl1 = realm2.getClientByClientId("cl1"); Assert.assertEquals(r2cl1.getId(), realm2.getClientById(r2cl1.getId()).getId()); Assert.assertNull(realm1.getClientByClientId(r2cl1.getId())); RoleModel r1App1Role = r1app1.getRole("app1Role1"); Assert.assertEquals(r1App1Role, realm1.getRoleById(r1App1Role.getId())); Assert.assertNull(realm2.getRoleById(r1App1Role.getId())); RoleModel r2Role1 = realm2.getRole("role2"); Assert.assertNull(realm1.getRoleById(r2Role1.getId())); Assert.assertEquals(r2Role1, realm2.getRoleById(r2Role1.getId())); }
private UserConsentModel toConsentModel(UserConsentEntity entity) { if (entity == null) { return null; } ClientModel client = realm.getClientById(entity.getClientId()); if (client == null) { throw new ModelException("Client with id " + entity.getClientId() + " is not available"); } UserConsentModel model = new UserConsentModel(client); Collection<UserConsentRoleEntity> grantedRoleEntities = entity.getGrantedRoles(); if (grantedRoleEntities != null) { for (UserConsentRoleEntity grantedRole : grantedRoleEntities) { RoleModel grantedRoleModel = realm.getRoleById(grantedRole.getRoleId()); if (grantedRoleModel != null) { model.addGrantedRole(grantedRoleModel); } } } Collection<UserConsentProtocolMapperEntity> grantedProtocolMapperEntities = entity.getGrantedProtocolMappers(); if (grantedProtocolMapperEntities != null) { for (UserConsentProtocolMapperEntity grantedProtMapper : grantedProtocolMapperEntities) { ProtocolMapperModel protocolMapper = client.getProtocolMapperById(grantedProtMapper.getProtocolMapperId()); model.addGrantedProtocolMapper(protocolMapper); } } return model; }
protected void deleteComposites(List<RoleRepresentation> roles, RoleModel role) { for (RoleRepresentation rep : roles) { RoleModel composite = realm.getRoleById(rep.getId()); if (composite == null) { throw new NotFoundException("Could not find composite role: " + rep.getName()); } role.removeCompositeRole(composite); } }
@Override public Set<RoleModel> getRoleMappings() { // we query ids only as the role might be cached and following the @ManyToOne will result in a // load // even if we're getting just the id. TypedQuery<String> query = em.createNamedQuery("userRoleMappingIds", String.class); query.setParameter("user", getUser()); List<String> ids = query.getResultList(); Set<RoleModel> roles = new HashSet<RoleModel>(); for (String roleId : ids) { RoleModel roleById = realm.getRoleById(roleId); if (roleById == null) continue; roles.add(roleById); } return roles; }