Search in sources :

Example 1 with SimpleUser

use of io.jans.orm.cloud.spanner.model.SimpleUser in project jans by JanssenProject.

the class SpannerSample method main.

public static void main(String[] args) {
    // Prepare sample connection details
    SpannerEntryManagerSample sqlEntryManagerSample = new SpannerEntryManagerSample();
    // Create SQL entry manager
    SpannerEntryManager sqlEntryManager = sqlEntryManagerSample.createSpannerEntryManager();
    SimpleUser newUser = new SimpleUser();
    newUser.setDn(String.format("inum=%s,ou=people,o=jans", System.currentTimeMillis()));
    newUser.setUserId("sample_user_" + System.currentTimeMillis());
    newUser.setUserPassword("pwd");
    newUser.getCustomAttributes().add(new CustomObjectAttribute("address", Arrays.asList("London", "Texas", "Kiev")));
    newUser.getCustomAttributes().add(new CustomObjectAttribute("transientId", "transientId"));
    newUser.getCustomAttributes().add(new CustomObjectAttribute("memberOf", Arrays.asList("1", "2", "3", "3")));
    sqlEntryManager.persist(newUser);
    SimpleUser dummyUser = sqlEntryManager.find(SimpleUser.class, newUser.getDn());
    LOG.info("Dummy User '{}'", dummyUser);
    String[] values = new String[] { "Somewhere: " + System.currentTimeMillis(), "Somewhere2: " + System.currentTimeMillis() };
    dummyUser.getCustomAttributes().add(new CustomObjectAttribute("address", Arrays.asList(values)));
    dummyUser.getCustomAttributes().add(new CustomObjectAttribute("transientId", "new_transientId"));
    dummyUser.getCustomAttributes().add(new CustomObjectAttribute("jansGuid", "test_guid"));
    dummyUser.getCustomAttributes().add(new CustomObjectAttribute("memberOf", Arrays.asList("1", "9")));
    dummyUser.setUserId("user1");
    dummyUser.setUserPassword("test_pwd");
    sqlEntryManager.merge(dummyUser);
    /*
        Filter filterX1 = Filter.createEqualityFilter("transientId", "new_transientId");
        List<SimpleUser> usersss = sqlEntryManager.findEntries("ou=people,o=jans", SimpleUser.class, filterX1);
        System.out.println(usersss.size());
*/
    Filter filterX = Filter.createEqualityFilter("memberOf", "1");
    List<SimpleUser> userss = sqlEntryManager.findEntries("ou=people,o=jans", SimpleUser.class, filterX, new String[] { "memberOf", "uid" }, 10);
    System.out.println(userss.size());
    Filter filterR = Filter.createEqualityFilter("jansGuid", "test_guid");
    int removed = sqlEntryManager.remove("ou=people,o=jans", SimpleUser.class, filterR, 10);
    System.out.println(removed);
    // sqlEntryManager.remove(dummyUser);
    sqlEntryManager.destroy();
    System.exit(0);
    // Find all users which have specified object classes defined in SimpleUser
    List<SimpleUser> users = sqlEntryManager.findEntries("ou=people,o=jans", SimpleUser.class, null);
    for (SimpleUser user : users) {
        LOG.info("User with uid: '{}' with DN: '{}'", user.getUserId(), user.getDn());
    }
    /*
        if (users.size() > 0) {
            // Add attribute "address" to first user
            SimpleUser user = users.get(0);
            LOG.info("Updating: " + user.getUserId());
 
            String[] values = new String[] { "Somewhere: " + System.currentTimeMillis(), "Somewhere2: " + System.currentTimeMillis() };
            user.getCustomAttributes().add(new CustomObjectAttribute("address", Arrays.asList(values)));
            user.getCustomAttributes().add(new CustomObjectAttribute("transientId", "new_transientId"));
            user.getCustomAttributes().add(new CustomObjectAttribute("jansGuid", "test_guid"));
            user.setUserId("user1");
            user.setUserPassword("test_pwd");

            sqlEntryManager.merge(user);
        }
*/
    for (SimpleUser user : users) {
        boolean result1 = sqlEntryManager.authenticate(user.getDn(), SimpleUser.class, "test_pwd");
        boolean result2 = sqlEntryManager.authenticate("ou=people,o=jans", SimpleUser.class, user.getUserId(), "test");
        System.out.println("authetication result: " + result1 + ", " + result2);
    }
    Filter filter = Filter.createEqualityFilter("jansStatus", "active");
    List<SimpleAttribute> attributes = sqlEntryManager.findEntries("o=jans", SimpleAttribute.class, filter, SearchScope.SUB, null, null, 10, 0, 0);
    for (SimpleAttribute attribute : attributes) {
        LOG.info("Attribute with displayName: " + attribute.getCustomAttributes().get(1));
    }
    Filter filter2 = Filter.createEqualityFilter("jansState", "authenticated");
    List<SimpleSession> sessions = sqlEntryManager.findEntries("o=jans", SimpleSession.class, filter2, SearchScope.SUB, null, null, 10, 0, 0);
    LOG.info("Found sessions: " + sessions.size());
    List<SimpleGrant> grants = sqlEntryManager.findEntries("o=jans", SimpleGrant.class, null, SearchScope.SUB, new String[] { "grtId" }, null, 1, 0, 0);
    LOG.info("Found grants: " + grants.size());
    try {
        PagedResult<SimpleUser> listViewResponse = sqlEntryManager.findPagedEntries("o=jans", SimpleUser.class, null, new String[] { "uid", "displayName", "jansStatus" }, "uid", SortOrder.ASCENDING, 0, 6, 4);
        LOG.info("Found persons: " + listViewResponse.getEntriesCount() + ", total persons: " + listViewResponse.getTotalEntriesCount());
        for (SimpleUser user : listViewResponse.getEntries()) {
            System.out.println(user.getUserId());
        }
    } catch (Exception ex) {
        LOG.info("Failed to search", ex);
    }
    try {
        PagedResult<SimpleUser> listViewResponse = sqlEntryManager.findPagedEntries("o=jans", SimpleUser.class, null, new String[] { "uid", "displayName", "jansStatus" }, "uid", SortOrder.DESCENDING, 0, 6, 4);
        LOG.info("Found persons: " + listViewResponse.getEntriesCount() + ", total persons: " + listViewResponse.getTotalEntriesCount());
        for (SimpleUser user : listViewResponse.getEntries()) {
            System.out.println(user.getUserId());
        }
    } catch (Exception ex) {
        LOG.info("Failed to search", ex);
    }
}
Also used : CustomObjectAttribute(io.jans.orm.model.base.CustomObjectAttribute) SimpleUser(io.jans.orm.cloud.spanner.model.SimpleUser) SimpleAttribute(io.jans.orm.cloud.spanner.model.SimpleAttribute) SimpleGrant(io.jans.orm.cloud.spanner.model.SimpleGrant) Filter(io.jans.orm.search.filter.Filter) SpannerEntryManagerSample(io.jans.orm.cloud.spanner.persistence.SpannerEntryManagerSample) SimpleSession(io.jans.orm.cloud.spanner.model.SimpleSession) SpannerEntryManager(io.jans.orm.cloud.spanner.impl.SpannerEntryManager)

Example 2 with SimpleUser

use of io.jans.orm.cloud.spanner.model.SimpleUser in project jans by JanssenProject.

the class SpannerUserSearchSample method addTestUsers.

private static void addTestUsers(SpannerEntryManager sqlEntryManager, int countUsers) {
    long startTime = System.currentTimeMillis();
    for (int j = 137; j <= countUsers; j++) {
        String uid = "user" + j;
        /*String.format("user%06d", userUid);*/
        SimpleUser newUser = new SimpleUser();
        newUser.setDn(String.format("inum=%s,ou=people,o=jans", System.currentTimeMillis()));
        newUser.setUserId(uid);
        newUser.setUserPassword("topsecret" + uid);
        newUser.setUserRole(j % 2 == 0 ? UserRole.ADMIN : UserRole.USER);
        newUser.setMemberOf(Arrays.asList("group_1", "group_2", "group_3"));
        newUser.setAttributeValue("givenName", "Agent Smith");
        newUser.getCustomAttributes().add(new CustomObjectAttribute("address", Arrays.asList("London", "Texas", "Kiev")));
        newUser.getCustomAttributes().add(new CustomObjectAttribute("transientId", "transientId"));
        List<Object> jansExtUid = Arrays.asList(1, 11);
        if (j % 2 == 0) {
            jansExtUid = Arrays.asList(1, 11, 2, 22);
        } else if (j % 3 == 0) {
            jansExtUid = Arrays.asList(2, 22, 3, 33);
        } else if (j % 5 == 0) {
            jansExtUid = Arrays.asList(1, 11, 2, 22, 3, 33, 4, 44);
        }
        newUser.getCustomAttributes().add(new CustomObjectAttribute("jansExtUid", jansExtUid));
        newUser.getCustomAttributes().add(new CustomObjectAttribute("birthdate", new Date()));
        newUser.getCustomAttributes().add(new CustomObjectAttribute("jansActive", false));
        sqlEntryManager.persist(newUser);
        if (j % 1000 == 0) {
            LOG.info("Added: '{}'", j);
        }
    }
    long endTime = System.currentTimeMillis();
    long duration = (endTime - startTime) / 1000L;
    LOG.info("Duration: '{}'", duration);
}
Also used : CustomObjectAttribute(io.jans.orm.model.base.CustomObjectAttribute) SimpleUser(io.jans.orm.cloud.spanner.model.SimpleUser) Date(java.util.Date)

Example 3 with SimpleUser

use of io.jans.orm.cloud.spanner.model.SimpleUser in project jans by JanssenProject.

the class SpannerCustomMultiValuedTypesSample method main.

public static void main(String[] args) {
    // Prepare sample connection details
    SpannerEntryManagerSample sqlEntryManagerSample = new SpannerEntryManagerSample();
    // Create SQL entry manager
    SpannerEntryManager sqlEntryManager = sqlEntryManagerSample.createSpannerEntryManager();
    // 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.cloud.spanner.model.SimpleUser) Filter(io.jans.orm.search.filter.Filter) SpannerEntryManagerSample(io.jans.orm.cloud.spanner.persistence.SpannerEntryManagerSample) SpannerEntryManager(io.jans.orm.cloud.spanner.impl.SpannerEntryManager)

Example 4 with SimpleUser

use of io.jans.orm.cloud.spanner.model.SimpleUser in project jans by JanssenProject.

the class SpannerUserSearchSample method main.

public static void main(String[] args) throws InterruptedException {
    // Prepare sample connection details
    final SpannerEntryManagerSample sqlEntryManagerSample = new SpannerEntryManagerSample();
    final SpannerEntryManager sqlEntryManager = sqlEntryManagerSample.createSpannerEntryManager();
    int countUsers = 2000000;
    int threadCount = 200;
    int threadIterationCount = 200;
    Filter filter = Filter.createEqualityFilter(Filter.createLowercaseFilter("uid"), String.format("user%06d", countUsers));
    boolean foundUser = sqlEntryManager.contains("ou=people,o=jans", SimpleUser.class, filter);
    if (!foundUser) {
        addTestUsers(sqlEntryManager, countUsers);
    }
    long totalStart = System.currentTimeMillis();
    try {
        ExecutorService executorService = Executors.newFixedThreadPool(threadCount, daemonThreadFactory());
        for (int i = 0; i < threadCount; i++) {
            activeCount.incrementAndGet();
            final int count = i;
            executorService.execute(new Runnable() {

                @Override
                public void run() {
                    long start = System.currentTimeMillis();
                    for (int j = 0; j < threadIterationCount; j++) {
                        long userUid = Math.round(Math.random() * countUsers);
                        String uid = "user" + userUid;
                        /*String.format("user%06d", userUid);*/
                        try {
                            Filter filter = Filter.createEqualityFilter(Filter.createLowercaseFilter("uid"), StringHelper.toLowerCase(uid));
                            // Filter filter = Filter.createEqualityFilter("uid", uid);
                            List<SimpleUser> foundUsers = sqlEntryManager.findEntries("ou=people,o=jans", SimpleUser.class, filter);
                            if (foundUsers.size() > 0) {
                                successResult.incrementAndGet();
                            } else {
                                LOG.warn("Failed to find user: " + uid);
                                failedResult.incrementAndGet();
                            }
                        } catch (Throwable e) {
                            errorResult.incrementAndGet();
                            System.out.println("ERROR !!!, thread: " + count + ", uid: " + uid + ", error:" + e.getMessage());
                            e.printStackTrace();
                        }
                    }
                    long end = System.currentTimeMillis();
                    long duration = end - start;
                    LOG.info("Thread " + count + " execution time: " + duration);
                    totalTime.addAndGet(duration);
                    activeCount.decrementAndGet();
                }
            });
        }
        while (activeCount.get() != 0) {
            Thread.sleep(1000L);
        }
    } finally {
        sqlEntryManager.destroy();
    }
    long totalEnd = System.currentTimeMillis();
    long duration = totalEnd - totalStart;
    LOG.info("Total execution time: " + duration + " after execution: " + (threadCount * threadIterationCount));
    System.out.println(String.format("successResult: '%d', failedResult: '%d', errorResult: '%d'", successResult.get(), failedResult.get(), errorResult.get()));
}
Also used : SimpleUser(io.jans.orm.cloud.spanner.model.SimpleUser) Filter(io.jans.orm.search.filter.Filter) SpannerEntryManagerSample(io.jans.orm.cloud.spanner.persistence.SpannerEntryManagerSample) ExecutorService(java.util.concurrent.ExecutorService) List(java.util.List) SpannerEntryManager(io.jans.orm.cloud.spanner.impl.SpannerEntryManager)

Example 5 with SimpleUser

use of io.jans.orm.cloud.spanner.model.SimpleUser in project jans by JanssenProject.

the class SpannerCustomObjectAttributesSample method main.

public static void main(String[] args) {
    // Prepare sample connection details
    SpannerEntryManagerSample sqlEntryManagerSample = new SpannerEntryManagerSample();
    // Create SQL entry manager
    SpannerEntryManager sqlEntryManager = sqlEntryManagerSample.createSpannerEntryManager();
    // 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.cloud.spanner.model.SimpleUser) Filter(io.jans.orm.search.filter.Filter) SpannerEntryManagerSample(io.jans.orm.cloud.spanner.persistence.SpannerEntryManagerSample) Date(java.util.Date) SpannerEntryManager(io.jans.orm.cloud.spanner.impl.SpannerEntryManager)

Aggregations

SimpleUser (io.jans.orm.cloud.spanner.model.SimpleUser)5 SpannerEntryManager (io.jans.orm.cloud.spanner.impl.SpannerEntryManager)4 SpannerEntryManagerSample (io.jans.orm.cloud.spanner.persistence.SpannerEntryManagerSample)4 CustomObjectAttribute (io.jans.orm.model.base.CustomObjectAttribute)4 Filter (io.jans.orm.search.filter.Filter)4 Date (java.util.Date)2 SimpleAttribute (io.jans.orm.cloud.spanner.model.SimpleAttribute)1 SimpleGrant (io.jans.orm.cloud.spanner.model.SimpleGrant)1 SimpleSession (io.jans.orm.cloud.spanner.model.SimpleSession)1 List (java.util.List)1 ExecutorService (java.util.concurrent.ExecutorService)1