Search in sources :

Example 26 with Filter

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

the class SqlCustomMultiValuedTypesSample 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("jansOptOuts", Arrays.asList("London", "Texas", "Kiev")));
    newUser.getCustomAttributes().add(new CustomObjectAttribute("jansExtUid", "test_value").multiValued());
    newUser.getCustomAttributes().add(new CustomObjectAttribute("jansPPID", "test_value").multiValued());
    newUser.setMemberOf(Arrays.asList("group_1", "group_2", "group_3"));
    newUser.setAttributeValue("givenName", "john");
    sqlEntryManager.persist(newUser);
    LOG.info("Added User '{}' with uid '{}' and key '{}'", newUser, newUser.getUserId(), newUser.getDn());
    LOG.info("Persisted custom attributes '{}'", newUser.getCustomAttributes());
    // 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());
    // Dump custom attributes
    for (CustomObjectAttribute attr : foundUser.getCustomAttributes()) {
        System.out.println(attr.getName() + " - " + attr.getValues());
    }
    // Update custom attributes
    foundUser.setAttributeValues("jansOptOuts", Arrays.asList("London", "Texas", "Kiev", "Dublin"));
    foundUser.setAttributeValues("jansExtUid", Arrays.asList("test_value_11", "test_value_22", "test_value_33", "test_value_44"));
    foundUser.setAttributeValues("jansExtUid", Arrays.asList(11, 22, 33, 44));
    foundUser.setAttributeValues("jansPPID", Arrays.asList("fuzzy_value_1", "fuzzy_value_2"));
    foundUser.setAttributeValue("jansGuid", "simple");
    CustomObjectAttribute multiValuedSingleValue = new CustomObjectAttribute("jansAssociatedClnt", "multivalued_single_valued");
    multiValuedSingleValue.setMultiValued(true);
    foundUser.getCustomAttributes().add(multiValuedSingleValue);
    sqlEntryManager.merge(foundUser);
    LOG.info("Updated custom attributes '{}'", foundUser.getCustomAttributes());
    // Find updated dummy user
    SimpleUser foundUpdatedUser = sqlEntryManager.find(SimpleUser.class, newUser.getDn());
    LOG.info("Found User '{}' with uid '{}' and key '{}'", foundUpdatedUser, foundUpdatedUser.getUserId(), foundUpdatedUser.getDn());
    LOG.info("Cusom attributes '{}'", foundUpdatedUser.getCustomAttributes());
    // Dump custom attributes
    for (CustomObjectAttribute attr : foundUser.getCustomAttributes()) {
        System.out.println(attr.getName() + " - " + attr.getValues());
    }
    Filter filter = Filter.createEqualityFilter(Filter.createLowercaseFilter("givenName"), StringHelper.toLowerCase("john"));
    List<SimpleUser> foundUpdatedUsers = sqlEntryManager.findEntries("o=jans", SimpleUser.class, filter);
    System.out.println(foundUpdatedUsers);
}
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)

Example 27 with Filter

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

the class SqlSimpleGroupSample method main.

public static void main(String[] args) {
    // Prepare sample connection details
    SqlEntryManagerSample sqlEntryManagerSample = new SqlEntryManagerSample();
    // Create SQL entry manager
    SqlEntryManager sqlEntryManager = sqlEntryManagerSample.createSqlEntryManager();
    String personDN = "inum=ae8c7ff9-def7-4b42-8147-de8380617a37,ou=people,o=gluu";
    Filter ownerFilter = Filter.createEqualityFilter("owner", personDN);
    Filter memberFilter = Filter.createEqualityFilter("member", personDN);
    Filter searchFilter = Filter.createORFilter(ownerFilter, memberFilter);
    List<SimpleGroup> result = sqlEntryManager.findEntries("ou=groups,o=gluu", SimpleGroup.class, searchFilter, 1);
    System.out.println(result);
}
Also used : Filter(io.jans.orm.search.filter.Filter) SimpleGroup(io.jans.orm.sql.model.SimpleGroup) SqlEntryManagerSample(io.jans.orm.sql.persistence.SqlEntryManagerSample) SqlEntryManager(io.jans.orm.sql.impl.SqlEntryManager)

Example 28 with Filter

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

the class SpannerEntryManager method removeImpl.

protected <T> int removeImpl(String dn, Class<T> entryClass, Filter filter, int count) {
    // Check entry class
    checkEntryClass(entryClass, false);
    String[] objectClasses = getTypeObjectClasses(entryClass);
    Filter searchFilter;
    if (objectClasses.length > 0) {
        LOG.trace("Filter: {}", filter);
        searchFilter = addObjectClassFilter(filter, objectClasses);
    } else {
        throw new EntryDeleteException(String.format("Failed to delete entries with DN: '%s', filter: '%s' because objectClass is not specified", dn, filter));
    }
    // Find entries
    LOG.trace("-------------------------------------------------------");
    LOG.trace("Filter: {}", filter);
    LOG.trace("objectClasses count: {} ", objectClasses.length);
    LOG.trace("objectClasses: {}", objectClasses.toString());
    LOG.trace("Search filter: {}", searchFilter);
    // Prepare properties types to allow build filter properly
    List<PropertyAnnotation> propertiesAnnotations = getEntryPropertyAnnotations(entryClass);
    Map<String, PropertyAnnotation> propertiesAnnotationsMap = prepareEntryPropertiesTypes(entryClass, propertiesAnnotations);
    String key = toSQLKey(dn).getKey();
    ConvertedExpression convertedExpression;
    try {
        convertedExpression = toSqlFilterWithEmptyAlias(key, objectClasses[0], searchFilter, propertiesAnnotationsMap);
    } catch (SearchException ex) {
        throw new EntryDeleteException(String.format("Failed to convert filter '%s' to expression", searchFilter));
    }
    try {
        int processed = (int) getOperationService().delete(key, objectClasses[0], convertedExpression, count);
        return processed;
    } catch (Exception ex) {
        throw new EntryDeleteException(String.format("Failed to delete entries with key: '%s', expression: '%s'", key, convertedExpression), ex);
    }
}
Also used : Filter(io.jans.orm.search.filter.Filter) ConvertedExpression(io.jans.orm.cloud.spanner.model.ConvertedExpression) SearchException(io.jans.orm.exception.operation.SearchException) EntryDeleteException(io.jans.orm.exception.EntryDeleteException) 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)

Example 29 with Filter

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

the class FidoDeviceWebService method searchDevices.

private PagedResult<BaseScimResource> searchDevices(String userId, String filter, String sortBy, SortOrder sortOrder, int startIndex, int count) throws Exception {
    Filter ldapFilter = scimFilterParserService.createFilter(filter, Filter.createPresenceFilter("jansId"), FidoDeviceResource.class);
    log.info("Executing search for fido devices using: ldapfilter '{}', sortBy '{}', sortOrder '{}', startIndex '{}', count '{}', userId '{}'", ldapFilter.toString(), sortBy, sortOrder.getValue(), startIndex, count, userId);
    // Currently, searching with SUB scope in Couchbase requires some help (beyond use of baseDN)
    if (StringUtils.isNotEmpty(userId)) {
        ldapFilter = Filter.createANDFilter(ldapFilter, Filter.createEqualityFilter("personInum", userId));
    }
    PagedResult<GluuCustomFidoDevice> list;
    try {
        list = entryManager.findPagedEntries(fidoDeviceService.getDnForFidoDevice(userId, null), GluuCustomFidoDevice.class, ldapFilter, null, sortBy, sortOrder, startIndex - 1, count, getMaxCount());
    } catch (Exception e) {
        log.info("Returning an empty listViewReponse");
        log.error(e.getMessage(), e);
        list = new PagedResult<>();
        list.setEntries(new ArrayList<>());
    }
    List<BaseScimResource> resources = new ArrayList<>();
    for (GluuCustomFidoDevice device : list.getEntries()) {
        FidoDeviceResource scimDev = new FidoDeviceResource();
        transferAttributesToFidoResource(device, scimDev, endpointUrl, userPersistenceHelper.getUserInumFromDN(device.getDn()));
        resources.add(scimDev);
    }
    log.info("Found {} matching entries - returning {}", list.getTotalEntriesCount(), list.getEntries().size());
    PagedResult<BaseScimResource> result = new PagedResult<>();
    result.setEntries(resources);
    result.setTotalEntriesCount(list.getTotalEntriesCount());
    return result;
}
Also used : GluuCustomFidoDevice(io.jans.scim.model.fido.GluuCustomFidoDevice) Filter(io.jans.orm.search.filter.Filter) FidoDeviceResource(io.jans.scim.model.scim2.fido.FidoDeviceResource) BaseScimResource(io.jans.scim.model.scim2.BaseScimResource) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) SCIMException(io.jans.scim.model.exception.SCIMException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) PagedResult(io.jans.orm.model.PagedResult)

Example 30 with Filter

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

the class Scim2GroupService method restoreMembersDisplay.

/**
 * Takes two GroupResource objects and attempts to fill the members' display names
 * in the second object when missing based on the data existing in the first object.
 * In practice the first object represents an already stored group while the
 * second is the result of modifications applied upon the first. In the course
 * of modifications some display names may have removed. This method tries to
 * recover some of this lost data
 * @param trusted Object containing valid group data
 * @param altered Modified object
 */
public void restoreMembersDisplay(GroupResource trusted, GroupResource altered) {
    int aSize = membersCount(altered);
    int tSize = membersCount(trusted);
    if (aSize > 0 && tSize > 0) {
        Map<String, String> map = trusted.getMembers().stream().filter(m -> m.getDisplay() != null).collect(Collectors.toMap(Member::getValue, Member::getDisplay));
        for (Member member : altered.getMembers()) {
            String inum = member.getValue();
            if (member.getDisplay() == null) {
                member.setDisplay(map.get(inum));
            }
        }
    }
}
Also used : GroupService(io.jans.scim.service.GroupService) Arrays(java.util.Arrays) Filter(io.jans.orm.search.filter.Filter) Meta(io.jans.scim.model.scim2.Meta) DateUtil(io.jans.scim.model.scim2.util.DateUtil) GluuStatus(io.jans.model.GluuStatus) ArrayList(java.util.ArrayList) ScimResourceUtil(io.jans.scim.model.scim2.util.ScimResourceUtil) HashSet(java.util.HashSet) Inject(javax.inject.Inject) BaseScimResource(io.jans.scim.model.scim2.BaseScimResource) PersonService(io.jans.scim.service.PersonService) Map(java.util.Map) GluuGroup(io.jans.scim.model.GluuGroup) Status(javax.ws.rs.core.Response.Status) SortOrder(io.jans.orm.model.SortOrder) Member(io.jans.scim.model.scim2.group.Member) Logger(org.slf4j.Logger) UserResource(io.jans.scim.model.scim2.user.UserResource) OrganizationService(io.jans.scim.service.OrganizationService) Set(java.util.Set) ScimFilterParserService(io.jans.scim.service.antlr.scimFilter.ScimFilterParserService) Collectors(java.util.stream.Collectors) PersistenceEntryManager(io.jans.orm.PersistenceEntryManager) Serializable(java.io.Serializable) GroupResource(io.jans.scim.model.scim2.group.GroupResource) List(java.util.List) Stream(java.util.stream.Stream) ExternalScimService(io.jans.scim.service.external.ExternalScimService) PagedResult(io.jans.orm.model.PagedResult) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) ApplicationScoped(javax.enterprise.context.ApplicationScoped) GluuCustomPerson(io.jans.scim.model.GluuCustomPerson) Collections(java.util.Collections) Member(io.jans.scim.model.scim2.group.Member)

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