@Override public void removeCredential(IdentityContext context, Account account, Class<? extends CredentialStorage> storageClass) { List<?> credentials = findCredentials(context, account, storageClass); EntityManager entityManager = getEntityManager(context); for (Object credential : credentials) { entityManager.remove(credential); } entityManager.flush(); }
@Override public void removeCredential(IdentityContext context, Account account, Class<? extends CredentialStorage> storageClass) { List<?> credentials = findCredentials(context, account, storageClass); EntityManager entityManager = getEntityManager(context); for (Object credential : credentials) { entityManager.remove(credential); } entityManager.flush(); }
private List<?> findAttributes(Object object) { Class<?> attributeClass = getConfig().getAttributeClass(); String identityProperty = getConfig().getAttributeIdentityProperty().getName(); EntityManager em = getEntityManager(); CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<?> criteria = builder.createQuery(attributeClass); Root<?> root = criteria.from(attributeClass); List<Predicate> predicates = new ArrayList<Predicate>(); predicates.add(builder.equal(root.get(identityProperty), object)); criteria.where(predicates.toArray(new Predicate[predicates.size()])); return em.createQuery(criteria).getResultList(); }
public JPACriteriaQueryBuilder(JPAIdentityStore identityStore, IdentityQuery<?> identityQuery) { this.identityStore = identityStore; this.identityQuery = identityQuery; this.config = identityStore.getConfig(); this.entityManager = identityStore.getEntityManager(); if(entityManager == null){ throw new IllegalStateException("Entity Manager is null"); } this.builder = this.entityManager.getCriteriaBuilder(); if(builder == null){ throw new IllegalStateException("Criteria Builder is null"); } Class<?> identityClass = this.config.getIdentityClass(); this.criteria = builder.createQuery(identityClass); this.root = criteria.from(identityClass); }
@Override public void updateAttributedType(IdentityContext context, AttributedType attributedType) { EntityManager entityManager = getEntityManager(context); for (EntityMapper entityMapper : getMapperFor(attributedType.getClass())) { entityMapper.updateEntity(attributedType, entityManager); } }
@Override public void updateAttributedType(IdentityContext context, AttributedType attributedType) { EntityManager entityManager = getEntityManager(context); for (EntityMapper entityMapper : getMapperFor(attributedType.getClass())) { entityMapper.updateEntity(attributedType, entityManager); } }
@Override public void removeAttributedType(IdentityContext context, AttributedType attributedType) { EntityManager entityManager = getEntityManager(context); EntityMapper rootMapper = getRootMapper(attributedType.getClass()); if (Relationship.class.isAssignableFrom(attributedType.getClass())) { removeChildRelationships(context, (Relationship) attributedType, entityManager); } removeAssociatedEntities(attributedType, entityManager, rootMapper); entityManager.remove(getRootEntity(attributedType, entityManager)); }
@Override public void removeAttributedType(IdentityContext context, AttributedType attributedType) { EntityManager entityManager = getEntityManager(context); EntityMapper rootMapper = getRootMapper(attributedType.getClass()); if (Relationship.class.isAssignableFrom(attributedType.getClass())) { removeChildRelationships(context, (Relationship) attributedType, entityManager); } removeAssociatedEntities(attributedType, entityManager, rootMapper); entityManager.remove(getRootEntity(attributedType, entityManager)); }
private void removeAttributes(Object object) { EntityManager em = getEntityManager(); if (getConfig().getAttributeClass() != null) { List<?> results = findAttributes(object); for (Object result : results) { em.remove(result); } } }
@Override public void add(IdentityContext identityContext, Partition partition, String configurationName) { add(identityContext, partition); // now that the partition entity is created, let`s populate the configuration name. // the configuration name is not part of the Model API, so we need to do this manually. EntityMapper entityMapper = getRootMapper(partition.getClass()); EntityManager entityManager = getEntityManager(identityContext); Object partitionEntity = getRootEntity(partition, entityManager); Property configurationNameProperty = entityMapper.getProperty(partition.getClass(), ConfigurationName.class).getValue(); configurationNameProperty.setValue(partitionEntity, configurationName); entityManager.merge(partitionEntity); }
@Override public void add(IdentityContext identityContext, Partition partition, String configurationName) { add(identityContext, partition); // now that the partition entity is created, let`s populate the configuration name. // the configuration name is not part of the Model API, so we need to do this manually. EntityMapper entityMapper = getRootMapper(partition.getClass()); EntityManager entityManager = getEntityManager(identityContext); Object partitionEntity = getRootEntity(partition, entityManager); Property configurationNameProperty = entityMapper.getProperty(partition.getClass(), ConfigurationName.class).getValue(); configurationNameProperty.setValue(partitionEntity, configurationName); entityManager.merge(partitionEntity); }
private List<?> findChildRelationships(IdentityContext context, Relationship relationship) { EntityManager em = getEntityManager(context); EntityMapper relationshipMemberMapper = getEntityMapperForProperty(relationship.getClass(), RelationshipMember.class); CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<?> criteria = builder.createQuery(relationshipMemberMapper.getEntityType()); Root<?> root = criteria.from(relationshipMemberMapper.getEntityType()); List<Predicate> predicates = new ArrayList<Predicate>(); Property ownerProperty = relationshipMemberMapper.getProperty(OwnerReference.class).getValue(); Join<?, ?> join = root.join(ownerProperty.getName()); EntityMapper relationshipMapper = getRootMapper(relationship.getClass()); Property identifierProperty = relationshipMapper.getProperty(Identifier.class).getValue(); predicates.add(builder.equal(join.get(identifierProperty.getName()), relationship.getId())); criteria.where(predicates.toArray(new Predicate[predicates.size()])); return em.createQuery(criteria).getResultList(); }
@Override public void addAttributedType(IdentityContext context, AttributedType attributedType) { EntityManager entityManager = getEntityManager(context); for (EntityMapper entityMapper : getMapperFor(attributedType.getClass())) { entityMapper.persist(attributedType, entityManager); if (Relationship.class.isInstance(attributedType)) { if (entityMapper.isRoot()) { storeRelationshipMembers((Relationship) attributedType, entityManager); } } } }
@Override public void addAttributedType(IdentityContext context, AttributedType attributedType) { EntityManager entityManager = getEntityManager(context); for (EntityMapper entityMapper : getMapperFor(attributedType.getClass())) { entityMapper.persist(attributedType, entityManager); if (Relationship.class.isInstance(attributedType)) { if (entityMapper.isRoot()) { storeRelationshipMembers((Relationship) attributedType, entityManager); } } } }
private List<?> findChildRelationships(IdentityContext context, Relationship relationship) { EntityManager em = getEntityManager(context); EntityMapper relationshipMemberMapper = getEntityMapperForProperty(relationship.getClass(), RelationshipMember.class); CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<?> criteria = builder.createQuery(relationshipMemberMapper.getEntityType()); Root<?> root = criteria.from(relationshipMemberMapper.getEntityType()); List<Predicate> predicates = new ArrayList<Predicate>(); Property ownerProperty = relationshipMemberMapper.getProperty(OwnerReference.class).getValue(); Join<?, ?> join = root.join(ownerProperty.getName()); EntityMapper relationshipMapper = getRootMapper(relationship.getClass()); Property identifierProperty = relationshipMapper.getProperty(Identifier.class).getValue(); predicates.add(builder.equal(join.get(identifierProperty.getName()), relationship.getId())); criteria.where(predicates.toArray(new Predicate[predicates.size()])); return em.createQuery(criteria).getResultList(); }
@Override public String getConfigurationName(IdentityContext identityContext, Partition partition) { EntityMapper entityMapper = getRootMapper(partition.getClass()); EntityManager entityManager = getEntityManager(identityContext); Object partitionEntity = entityManager.find(entityMapper.getEntityType(), partition.getId()); Property configurationNameProperty = entityMapper.getProperty(partition.getClass(), ConfigurationName.class).getValue(); String configurationName = configurationNameProperty.getValue(partitionEntity).toString(); if (isNullOrEmpty(configurationName)) { throw MESSAGES.partitionWithNoConfigurationName(partition); } return configurationName; }
@Override public String getConfigurationName(IdentityContext identityContext, Partition partition) { EntityMapper entityMapper = getRootMapper(partition.getClass()); EntityManager entityManager = getEntityManager(identityContext); Object partitionEntity = entityManager.find(entityMapper.getEntityType(), partition.getId()); Property configurationNameProperty = entityMapper.getProperty(partition.getClass(), ConfigurationName.class).getValue(); String configurationName = configurationNameProperty.getValue(partitionEntity).toString(); if (isNullOrEmpty(configurationName)) { throw MESSAGES.partitionWithNoConfigurationName(partition); } return configurationName; }
private List<?> findAttributes(IdentityType identityType, String idValue, Attribute<? extends Serializable> userAttribute) { Property<Object> attributeIdentityProperty = getConfig().getAttributeIdentityProperty(); EntityManager em = getEntityManager(); CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<?> criteria = builder.createQuery(getConfig().getAttributeClass()); Root<?> root = criteria.from(getConfig().getAttributeClass()); List<Predicate> predicates = new ArrayList<Predicate>(); Join<?, ?> join = root.join(attributeIdentityProperty.getName()); if (IDMUtil.isAgentType(identityType.getClass())) { predicates.add(builder.equal(join.get(getConfig().getIdentityIdProperty().getName()), idValue)); } else { predicates.add(builder.equal(join.get(getConfig().getModelProperty(PROPERTY_IDENTITY_NAME).getName()), idValue)); } predicates.add(builder.equal(root.get(getConfig().getAttributeNameProperty().getName()), userAttribute.getName())); criteria.where(predicates.toArray(new Predicate[predicates.size()])); return em.createQuery(criteria).getResultList(); }
@Override public void storeCredential(IdentityContext context, Account account, CredentialStorage storage) { EntityMapper credentialMapper = getCredentialAttributeMapper(storage.getClass()); Object newCredential = credentialMapper.createEntity(); EntityManager entityManager = getEntityManager(context); for (EntityMapping entityMapping : credentialMapper.getEntityMappings()) { for (Property property : entityMapping.getProperties().keySet()) { Property mappedProperty = entityMapping.getProperties().get(property); if (mappedProperty.getAnnotatedElement().isAnnotationPresent(OwnerReference.class)) { mappedProperty.setValue(newCredential, getOwnerEntity(account, mappedProperty, entityManager)); } else { mappedProperty.setValue(newCredential, property.getValue(storage)); } } } entityManager.persist(newCredential); entityManager.flush(); }
@Override public void update(IdentityType identityType) { checkInvalidIdentityType(identityType); IdentityTypeHandler<IdentityType> identityTypeManager = getConfig().getIdentityTypeManager(identityType.getClass()); Object identity = getIdentityObject(identityType); identityTypeManager.populateIdentityInstance(getContext().getRealm(), identity, identityType, this); updateAttributes(identityType, identity); EntityManager em = getEntityManager(); em.merge(identity); em.flush(); AbstractBaseEvent event = identityTypeManager.raiseUpdatedEvent(identityType, this); event.getContext().setValue(EVENT_CONTEXT_USER_ENTITY, identity); getContext().getEventBridge().raiseEvent(event); }