Search in sources :

Example 16 with Filter

use of io.jans.orm.search.filter.Filter in project jans by JanssenProject.

the class DeviceRegistrationService method findAllRegisteredByUsername.

public List<DeviceRegistration> findAllRegisteredByUsername(String username, String domain, String... returnAttributes) {
    String userInum = userService.getUserInum(username);
    if (userInum == null) {
        return Collections.emptyList();
    }
    String baseDn = getBaseDnForU2fUserDevices(userInum);
    if (persistenceEntryManager.hasBranchesSupport(baseDn)) {
        if (!containsBranch(baseDn)) {
            return Collections.emptyList();
        }
    }
    Filter resultFilter = Filter.createEqualityFilter("jansStatus", DeviceRegistrationStatus.ACTIVE.getValue());
    List<DeviceRegistration> fidoRegistrations = persistenceEntryManager.findEntries(baseDn, DeviceRegistration.class, resultFilter, returnAttributes);
    fidoRegistrations = fidoRegistrations.parallelStream().filter(f -> StringHelper.equals(domain, networkService.getHost(f.getApplication()))).filter(f -> (f.getDeviceData() == null)).collect(Collectors.toList());
    return fidoRegistrations;
}
Also used : Fido2RegistrationStatus(io.jans.fido2.model.entry.Fido2RegistrationStatus) Base64Service(io.jans.fido2.service.Base64Service) Fido2RegistrationData(io.jans.fido2.model.entry.Fido2RegistrationData) Date(java.util.Date) CoseEC2Algorithm(io.jans.fido2.ctap.CoseEC2Algorithm) Filter(io.jans.orm.search.filter.Filter) Fido2RegistrationEntry(io.jans.fido2.model.entry.Fido2RegistrationEntry) Inject(javax.inject.Inject) CoseService(io.jans.fido2.service.CoseService) DataMapperService(io.jans.fido2.service.DataMapperService) DeviceRegistration(io.jans.entry.DeviceRegistration) JsonNode(com.fasterxml.jackson.databind.JsonNode) StringHelper(io.jans.util.StringHelper) AttestationFormat(io.jans.fido2.ctap.AttestationFormat) SimpleBranch(io.jans.orm.model.base.SimpleBranch) Logger(org.slf4j.Logger) DeviceRegistrationStatus(io.jans.entry.DeviceRegistrationStatus) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) PersistenceEntryManager(io.jans.orm.PersistenceEntryManager) List(java.util.List) StaticConfiguration(io.jans.as.model.config.StaticConfiguration) RegistrationPersistenceService(io.jans.fido2.service.persist.RegistrationPersistenceService) UserService(io.jans.as.common.service.common.UserService) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Collections(java.util.Collections) NetworkService(io.jans.service.net.NetworkService) Filter(io.jans.orm.search.filter.Filter) DeviceRegistration(io.jans.entry.DeviceRegistration)

Example 17 with Filter

use of io.jans.orm.search.filter.Filter in project jans by JanssenProject.

the class SqlCustomObjectAttributesSample method main.

public static void main(String[] args) {
    // Prepare sample connection details
    SqlEntryManagerSample sqlEntryManagerSample = new SqlEntryManagerSample();
    // Create SQL entry manager
    SqlEntryManager sqlEntryManager = sqlEntryManagerSample.createSqlEntryManager();
    // Add dummy user
    SimpleUser newUser = new SimpleUser();
    newUser.setDn(String.format("inum=%s,ou=people,o=jans", System.currentTimeMillis()));
    newUser.setUserId("sample_user_" + System.currentTimeMillis());
    newUser.setUserPassword("test");
    newUser.getCustomAttributes().add(new CustomObjectAttribute("address", Arrays.asList("London", "Texas", "Kiev")));
    newUser.getCustomAttributes().add(new CustomObjectAttribute("jansGuid", "test_value"));
    newUser.getCustomAttributes().add(new CustomObjectAttribute("birthdate", new Date()));
    newUser.getCustomAttributes().add(new CustomObjectAttribute("jansActive", false));
    // Require cusom attribute in table with age: INT type
    newUser.getCustomAttributes().add(new CustomObjectAttribute("scimCustomThird", 18));
    newUser.setUserRole(UserRole.ADMIN);
    newUser.setMemberOf(Arrays.asList("group_1", "group_2", "group_3"));
    sqlEntryManager.persist(newUser);
    LOG.info("Added User '{}' with uid '{}' and key '{}'", newUser, newUser.getUserId(), newUser.getDn());
    // Find added dummy user
    SimpleUser foundUser = sqlEntryManager.find(SimpleUser.class, newUser.getDn());
    LOG.info("Found User '{}' with uid '{}' and key '{}'", foundUser, foundUser.getUserId(), foundUser.getDn());
    LOG.info("Custom attributes '{}'", foundUser.getCustomAttributes());
    for (CustomObjectAttribute customAttribute : foundUser.getCustomAttributes()) {
        if (customAttribute.getValue() instanceof Date) {
            LOG.info("Found date custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
        } else if (customAttribute.getValue() instanceof Integer) {
            LOG.info("Found integer custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
        } else if (customAttribute.getValue() instanceof Boolean) {
            LOG.info("Found boolean custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
        } else if (customAttribute.getValues().size() > 1) {
            LOG.info("Found list custom attribute '{}' with value '{}', multiValued: {}", customAttribute.getName(), customAttribute.getValues(), customAttribute.isMultiValued());
        }
    }
    for (Iterator<CustomObjectAttribute> it = foundUser.getCustomAttributes().iterator(); it.hasNext(); ) {
        CustomObjectAttribute attr = (CustomObjectAttribute) it.next();
        if (StringHelper.equalsIgnoreCase(attr.getName(), "jansGuid")) {
            attr.setValue("");
            break;
        }
    }
    sqlEntryManager.merge(foundUser);
    // Find updated dummy user
    SimpleUser foundUser2 = sqlEntryManager.find(SimpleUser.class, newUser.getDn());
    LOG.info("Found User '{}' with uid '{}' and key '{}'", foundUser2, foundUser2.getUserId(), foundUser2.getDn());
    LOG.info("Custom attributes after merge '{}'", foundUser2.getCustomAttributes());
    for (CustomObjectAttribute customAttribute : foundUser2.getCustomAttributes()) {
        if (customAttribute.getValue() instanceof Date) {
            LOG.info("Found date custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
        } else if (customAttribute.getValue() instanceof Integer) {
            LOG.info("Found integer custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
        } else if (customAttribute.getValue() instanceof Boolean) {
            LOG.info("Found boolean custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
        } else if (customAttribute.getValues().size() > 1) {
            LOG.info("Found list custom attribute '{}' with value '{}', multiValued: {}", customAttribute.getName(), customAttribute.getValues(), customAttribute.isMultiValued());
        }
    }
    // Find added dummy user by numeric attribute
    Filter filter = Filter.createGreaterOrEqualFilter("scimCustomThird", 16);
    List<SimpleUser> foundUsers = sqlEntryManager.findEntries("ou=people,o=jans", SimpleUser.class, filter);
    if (foundUsers.size() > 0) {
        foundUser = foundUsers.get(0);
        LOG.info("Found User '{}' by filter '{}' with uid '{}' and key '{}'", foundUser, filter, foundUser, foundUser);
    } else {
        LOG.error("Can't find User by filter '{}'", filter);
    }
}
Also used : CustomObjectAttribute(io.jans.orm.model.base.CustomObjectAttribute) SimpleUser(io.jans.orm.sql.model.SimpleUser) Filter(io.jans.orm.search.filter.Filter) SqlEntryManagerSample(io.jans.orm.sql.persistence.SqlEntryManagerSample) SqlEntryManager(io.jans.orm.sql.impl.SqlEntryManager) Date(java.util.Date)

Example 18 with Filter

use of io.jans.orm.search.filter.Filter in project jans by JanssenProject.

the class SpannerEntryManager method findEntriesImpl.

protected <T> PagedResult<EntryData> findEntriesImpl(String baseDN, Class<T> entryClass, Filter filter, SearchScope scope, String[] ldapReturnAttributes, String sortBy, SortOrder sortOrder, BatchOperation<T> batchOperation, SearchReturnDataType returnDataType, int start, int count, int chunkSize) {
    // Check entry class
    checkEntryClass(entryClass, false);
    String[] objectClasses = getTypeObjectClasses(entryClass);
    List<PropertyAnnotation> propertiesAnnotations = getEntryPropertyAnnotations(entryClass);
    String[] currentLdapReturnAttributes = ldapReturnAttributes;
    if (ArrayHelper.isEmpty(currentLdapReturnAttributes)) {
        currentLdapReturnAttributes = getAttributes(null, propertiesAnnotations, false);
    }
    Filter searchFilter;
    if (objectClasses.length > 0) {
        LOG.trace("Filter: {}", filter);
        searchFilter = addObjectClassFilter(filter, objectClasses);
    } else {
        searchFilter = filter;
    }
    // Find entries
    LOG.trace("-------------------------------------------------------");
    LOG.trace("Filter: {}", filter);
    LOG.trace("objectClasses count: {} ", objectClasses.length);
    LOG.trace("objectClasses: {}", ArrayHelper.toString(objectClasses));
    LOG.trace("Search filter: {}", searchFilter);
    // Prepare default sort
    Sort[] defaultSort = getDefaultSort(entryClass);
    if (StringHelper.isNotEmpty(sortBy)) {
        Sort requestedSort = buildSort(sortBy, sortOrder);
        if (ArrayHelper.isEmpty(defaultSort)) {
            defaultSort = new Sort[] { requestedSort };
        } else {
            defaultSort = ArrayHelper.arrayMerge(new Sort[] { requestedSort }, defaultSort);
        }
    }
    // Prepare properties types to allow build filter properly
    Map<String, PropertyAnnotation> propertiesAnnotationsMap = prepareEntryPropertiesTypes(entryClass, propertiesAnnotations);
    String key = toSQLKey(baseDN).getKey();
    ConvertedExpression convertedExpression;
    try {
        convertedExpression = toSqlFilter(key, objectClasses[0], searchFilter, propertiesAnnotationsMap);
    } catch (SearchException ex) {
        throw new EntryPersistenceException(String.format("Failed to convert filter '%s' to expression", searchFilter), ex);
    }
    PagedResult<EntryData> searchResult = null;
    try {
        SpannerBatchOperationWraper<T> batchOperationWraper = null;
        if (batchOperation != null) {
            batchOperationWraper = new SpannerBatchOperationWraper<T>(batchOperation, this, entryClass, propertiesAnnotations);
        }
        searchResult = searchImpl(key, objectClasses[0], convertedExpression, scope, currentLdapReturnAttributes, defaultSort, batchOperationWraper, returnDataType, start, count, chunkSize);
        if (searchResult == null) {
            throw new EntryPersistenceException(String.format("Failed to find entries with key: '%s', expression: '%s'", key, convertedExpression));
        }
        return searchResult;
    } catch (SearchException ex) {
        throw new EntryPersistenceException(String.format("Failed to find entries with key: '%s'", key), ex);
    } catch (Exception ex) {
        throw new EntryPersistenceException(String.format("Failed to find entries with key: '%s', expression: '%s'", key, convertedExpression), ex);
    }
}
Also used : EntryData(io.jans.orm.model.EntryData) SearchException(io.jans.orm.exception.operation.SearchException) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) MappingException(io.jans.orm.exception.MappingException) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) EntryDeleteException(io.jans.orm.exception.EntryDeleteException) SearchException(io.jans.orm.exception.operation.SearchException) AuthenticationException(io.jans.orm.exception.AuthenticationException) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation) Filter(io.jans.orm.search.filter.Filter) ConvertedExpression(io.jans.orm.cloud.spanner.model.ConvertedExpression) Sort(io.jans.orm.model.Sort)

Example 19 with Filter

use of io.jans.orm.search.filter.Filter in project jans by JanssenProject.

the class SpannerEntryManager method contains.

@Override
protected <T> boolean contains(String baseDN, String[] objectClasses, Class<T> entryClass, List<PropertyAnnotation> propertiesAnnotations, Filter filter, String[] ldapReturnAttributes) {
    if (StringHelper.isEmptyString(baseDN)) {
        throw new MappingException("Base DN to check contain entries is null");
    }
    // Create filter
    Filter searchFilter;
    if (objectClasses.length > 0) {
        searchFilter = addObjectClassFilter(filter, objectClasses);
    } else {
        searchFilter = filter;
    }
    // Prepare properties types to allow build filter properly
    Map<String, PropertyAnnotation> propertiesAnnotationsMap = prepareEntryPropertiesTypes(entryClass, propertiesAnnotations);
    String key = toSQLKey(baseDN).getKey();
    ConvertedExpression convertedExpression;
    try {
        convertedExpression = toSqlFilter(key, objectClasses[0], searchFilter, propertiesAnnotationsMap);
    } catch (SearchException ex) {
        throw new EntryPersistenceException(String.format("Failed to convert filter '%s' to expression", searchFilter));
    }
    PagedResult<EntryData> searchResult = null;
    try {
        searchResult = searchImpl(key, objectClasses[0], convertedExpression, SearchScope.SUB, ldapReturnAttributes, null, null, SearchReturnDataType.SEARCH, 0, 1, 0);
        if (searchResult == null) {
            throw new EntryPersistenceException(String.format("Failed to find entry with baseDN: '%s', filter: '%s'", baseDN, searchFilter));
        }
    } catch (Exception ex) {
        throw new EntryPersistenceException(String.format("Failed to find entry with baseDN: '%s', filter: '%s'", baseDN, searchFilter), ex);
    }
    return (searchResult != null) && (searchResult.getEntriesCount() > 0);
}
Also used : EntryData(io.jans.orm.model.EntryData) Filter(io.jans.orm.search.filter.Filter) ConvertedExpression(io.jans.orm.cloud.spanner.model.ConvertedExpression) SearchException(io.jans.orm.exception.operation.SearchException) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) MappingException(io.jans.orm.exception.MappingException) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) EntryDeleteException(io.jans.orm.exception.EntryDeleteException) SearchException(io.jans.orm.exception.operation.SearchException) AuthenticationException(io.jans.orm.exception.AuthenticationException) MappingException(io.jans.orm.exception.MappingException) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation)

Example 20 with Filter

use of io.jans.orm.search.filter.Filter in project jans by JanssenProject.

the class SpannerEntryManager method countEntries.

@Override
public <T> int countEntries(String baseDN, Class<T> entryClass, Filter filter, SearchScope scope) {
    if (StringHelper.isEmptyString(baseDN)) {
        throw new MappingException("Base DN to find entries is null");
    }
    // Check entry class
    checkEntryClass(entryClass, false);
    String[] objectClasses = getTypeObjectClasses(entryClass);
    List<PropertyAnnotation> propertiesAnnotations = getEntryPropertyAnnotations(entryClass);
    // Find entries
    Filter searchFilter;
    if (objectClasses.length > 0) {
        searchFilter = addObjectClassFilter(filter, objectClasses);
    } else {
        searchFilter = filter;
    }
    // Prepare properties types to allow build filter properly
    Map<String, PropertyAnnotation> propertiesAnnotationsMap = prepareEntryPropertiesTypes(entryClass, propertiesAnnotations);
    String key = toSQLKey(baseDN).getKey();
    ConvertedExpression convertedExpression;
    try {
        convertedExpression = toSqlFilter(key, objectClasses[0], searchFilter, propertiesAnnotationsMap);
    } catch (SearchException ex) {
        throw new EntryPersistenceException(String.format("Failed to convert filter '%s' to expression", searchFilter));
    }
    PagedResult<EntryData> searchResult;
    try {
        searchResult = searchImpl(toSQLKey(baseDN).getKey(), objectClasses[0], convertedExpression, scope, null, null, null, SearchReturnDataType.COUNT, 0, 0, 0);
    } catch (Exception ex) {
        throw new EntryPersistenceException(String.format("Failed to calculate the number of entries with baseDN: '%s', filter: '%s'", baseDN, searchFilter), ex);
    }
    return searchResult.getTotalEntriesCount();
}
Also used : EntryData(io.jans.orm.model.EntryData) SearchException(io.jans.orm.exception.operation.SearchException) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) MappingException(io.jans.orm.exception.MappingException) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) EntryDeleteException(io.jans.orm.exception.EntryDeleteException) SearchException(io.jans.orm.exception.operation.SearchException) AuthenticationException(io.jans.orm.exception.AuthenticationException) MappingException(io.jans.orm.exception.MappingException) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation) Filter(io.jans.orm.search.filter.Filter) ConvertedExpression(io.jans.orm.cloud.spanner.model.ConvertedExpression)

Aggregations

Filter (io.jans.orm.search.filter.Filter)188 Test (org.testng.annotations.Test)50 EntryPersistenceException (io.jans.orm.exception.EntryPersistenceException)32 ConvertedExpression (io.jans.orm.couchbase.model.ConvertedExpression)28 SearchException (io.jans.orm.exception.operation.SearchException)25 ConvertedExpression (io.jans.orm.sql.model.ConvertedExpression)24 MappingException (io.jans.orm.exception.MappingException)22 ArrayList (java.util.ArrayList)21 AuthenticationException (io.jans.orm.exception.AuthenticationException)20 PropertyAnnotation (io.jans.orm.reflect.property.PropertyAnnotation)19 EntryDeleteException (io.jans.orm.exception.EntryDeleteException)18 Date (java.util.Date)14 List (java.util.List)14 SqlEntryManager (io.jans.orm.sql.impl.SqlEntryManager)11 SqlEntryManagerSample (io.jans.orm.sql.persistence.SqlEntryManagerSample)11 CustomAttribute (io.jans.orm.model.base.CustomAttribute)9 CustomObjectAttribute (io.jans.orm.model.base.CustomObjectAttribute)9 DateTimeParseException (java.time.format.DateTimeParseException)9 SpannerEntryManager (io.jans.orm.cloud.spanner.impl.SpannerEntryManager)8 SpannerEntryManagerSample (io.jans.orm.cloud.spanner.persistence.SpannerEntryManagerSample)8