Exemple #1
0
  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;
  }
Exemple #2
0
  // Update roles and protocolMappers to given consentEntity from the consentModel
  private void updateGrantedConsentEntity(
      UserConsentEntity consentEntity, UserConsentModel consentModel) {
    Collection<UserConsentProtocolMapperEntity> grantedProtocolMapperEntities =
        consentEntity.getGrantedProtocolMappers();
    Collection<UserConsentProtocolMapperEntity> mappersToRemove =
        new HashSet<UserConsentProtocolMapperEntity>(grantedProtocolMapperEntities);

    for (ProtocolMapperModel protocolMapper : consentModel.getGrantedProtocolMappers()) {
      UserConsentProtocolMapperEntity grantedProtocolMapperEntity =
          new UserConsentProtocolMapperEntity();
      grantedProtocolMapperEntity.setUserConsent(consentEntity);
      grantedProtocolMapperEntity.setProtocolMapperId(protocolMapper.getId());

      // Check if it's already there
      if (!grantedProtocolMapperEntities.contains(grantedProtocolMapperEntity)) {
        em.persist(grantedProtocolMapperEntity);
        em.flush();
        grantedProtocolMapperEntities.add(grantedProtocolMapperEntity);
      } else {
        mappersToRemove.remove(grantedProtocolMapperEntity);
      }
    }
    // Those mappers were no longer on consentModel and will be removed
    for (UserConsentProtocolMapperEntity toRemove : mappersToRemove) {
      grantedProtocolMapperEntities.remove(toRemove);
      em.remove(toRemove);
    }

    Collection<UserConsentRoleEntity> grantedRoleEntities = consentEntity.getGrantedRoles();
    Set<UserConsentRoleEntity> rolesToRemove =
        new HashSet<UserConsentRoleEntity>(grantedRoleEntities);
    for (RoleModel role : consentModel.getGrantedRoles()) {
      UserConsentRoleEntity consentRoleEntity = new UserConsentRoleEntity();
      consentRoleEntity.setUserConsent(consentEntity);
      consentRoleEntity.setRoleId(role.getId());

      // Check if it's already there
      if (!grantedRoleEntities.contains(consentRoleEntity)) {
        em.persist(consentRoleEntity);
        em.flush();
        grantedRoleEntities.add(consentRoleEntity);
      } else {
        rolesToRemove.remove(consentRoleEntity);
      }
    }
    // Those roles were no longer on consentModel and will be removed
    for (UserConsentRoleEntity toRemove : rolesToRemove) {
      grantedRoleEntities.remove(toRemove);
      em.remove(toRemove);
    }

    em.flush();
  }