Search in sources :

Example 1 with CustomAttribute

use of io.jans.orm.model.base.CustomAttribute in project jans by JanssenProject.

the class ClientService method updateAccessTime.

public void updateAccessTime(Client client, boolean isUpdateLogonTime) {
    if (isFalse(appConfiguration.getUpdateClientAccessTime())) {
        return;
    }
    String clientDn = client.getDn();
    CustomEntry customEntry = new CustomEntry();
    customEntry.setDn(clientDn);
    customEntry.setCustomObjectClasses(CLIENT_OBJECT_CLASSES);
    Date now = new GregorianCalendar(TimeZone.getTimeZone("UTC")).getTime();
    String nowDateString = ldapEntryManager.encodeTime(customEntry.getDn(), now);
    CustomAttribute customAttributeLastAccessTime = new CustomAttribute("jansLastAccessTime", nowDateString);
    customEntry.getCustomAttributes().add(customAttributeLastAccessTime);
    if (isUpdateLogonTime) {
        CustomAttribute customAttributeLastLogonTime = new CustomAttribute("jansLastLogonTime", nowDateString);
        customEntry.getCustomAttributes().add(customAttributeLastLogonTime);
    }
    try {
        ldapEntryManager.merge(customEntry);
    } catch (EntryPersistenceException epe) {
        log.error("Failed to update jansLastAccessTime and jansLastLogonTime of client '{}'", clientDn);
        log.trace("Failed to update user:", epe);
    }
    removeFromCache(client);
}
Also used : CustomEntry(io.jans.orm.model.base.CustomEntry) CustomAttribute(io.jans.orm.model.base.CustomAttribute) GregorianCalendar(java.util.GregorianCalendar) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) Date(java.util.Date)

Example 2 with CustomAttribute

use of io.jans.orm.model.base.CustomAttribute in project jans by JanssenProject.

the class ClientService method getAttribute.

public Object getAttribute(Client client, String clientAttribute) {
    Object attribute = null;
    if (clientAttribute != null) {
        if (clientAttribute.equals("displayName")) {
            attribute = client.getClientName();
        } else if (clientAttribute.equals("inum")) {
            attribute = client.getClientId();
        } else if (clientAttribute.equals("jansAppTyp")) {
            attribute = client.getApplicationType();
        } else if (clientAttribute.equals("jansIdTknSignedRespAlg")) {
            attribute = client.getIdTokenSignedResponseAlg();
        } else if (clientAttribute.equals("jansRedirectURI") && client.getRedirectUris() != null) {
            JSONArray array = new JSONArray();
            for (String redirectUri : client.getRedirectUris()) {
                array.put(redirectUri);
            }
            attribute = array;
        } else if (clientAttribute.equals("jansScope") && client.getScopes() != null) {
            JSONArray array = new JSONArray();
            for (String scopeDN : client.getScopes()) {
                Scope s = scopeService.getScopeByDn(scopeDN);
                if (s != null) {
                    String scopeName = s.getId();
                    array.put(scopeName);
                }
            }
            attribute = array;
        } else {
            for (CustomAttribute customAttribute : client.getCustomAttributes()) {
                if (customAttribute.getName().equals(clientAttribute)) {
                    List<String> values = customAttribute.getValues();
                    if (values != null) {
                        if (values.size() == 1) {
                            attribute = values.get(0);
                        } else {
                            JSONArray array = new JSONArray();
                            for (String v : values) {
                                array.put(v);
                            }
                            attribute = array;
                        }
                    }
                    break;
                }
            }
        }
    }
    return attribute;
}
Also used : Scope(io.jans.as.persistence.model.Scope) CustomAttribute(io.jans.orm.model.base.CustomAttribute) JSONArray(org.json.JSONArray) List(java.util.List)

Example 3 with CustomAttribute

use of io.jans.orm.model.base.CustomAttribute in project jans by JanssenProject.

the class CouchbaseBatchJobSample method main.

public static void main(String[] args) {
    // Prepare sample connection details
    CouchbaseEntryManagerSample couchbaseEntryManagerSample = new CouchbaseEntryManagerSample();
    // Create Couchbase entry manager
    final CouchbaseEntryManager couchbaseEntryManager = couchbaseEntryManagerSample.createCouchbaseEntryManager();
    BatchOperation<SimpleTokenCouchbase> tokenCouchbaseBatchOperation = new ProcessBatchOperation<SimpleTokenCouchbase>() {

        private int processedCount = 0;

        @Override
        public void performAction(List<SimpleTokenCouchbase> objects) {
            for (SimpleTokenCouchbase simpleTokenCouchbase : objects) {
                try {
                    CustomAttribute customAttribute = getUpdatedAttribute(couchbaseEntryManager, simpleTokenCouchbase.getDn(), "exp", simpleTokenCouchbase.getAttribute("exp"));
                    simpleTokenCouchbase.setCustomAttributes(Arrays.asList(new CustomAttribute[] { customAttribute }));
                    couchbaseEntryManager.merge(simpleTokenCouchbase);
                    processedCount++;
                } catch (EntryPersistenceException ex) {
                    LOG.error("Failed to update entry", ex);
                }
            }
            LOG.info("Total processed: " + processedCount);
        }
    };
    final Filter filter1 = Filter.createPresenceFilter("exp");
    couchbaseEntryManager.findEntries("o=jans", SimpleTokenCouchbase.class, filter1, SearchScope.SUB, new String[] { "exp" }, tokenCouchbaseBatchOperation, 0, 0, 100);
    BatchOperation<SimpleSession> sessionBatchOperation = new ProcessBatchOperation<SimpleSession>() {

        private int processedCount = 0;

        @Override
        public void performAction(List<SimpleSession> objects) {
            for (SimpleSession simpleSession : objects) {
                try {
                    CustomAttribute customAttribute = getUpdatedAttribute(couchbaseEntryManager, simpleSession.getDn(), "jansLastAccessTime", simpleSession.getAttribute("jansLastAccessTime"));
                    simpleSession.setCustomAttributes(Arrays.asList(new CustomAttribute[] { customAttribute }));
                    couchbaseEntryManager.merge(simpleSession);
                    processedCount++;
                } catch (EntryPersistenceException ex) {
                    LOG.error("Failed to update entry", ex);
                }
            }
            LOG.info("Total processed: " + processedCount);
        }
    };
    final Filter filter2 = Filter.createPresenceFilter("jansLastAccessTime");
    couchbaseEntryManager.findEntries("o=jans", SimpleSession.class, filter2, SearchScope.SUB, new String[] { "jansLastAccessTime" }, sessionBatchOperation, 0, 0, 100);
    BatchOperation<SimpleClient> clientBatchOperation = new ProcessBatchOperation<SimpleClient>() {

        private int processedCount = 0;

        @Override
        public void performAction(List<SimpleClient> objects) {
            for (SimpleClient simpleClient : objects) {
                processedCount++;
            }
            LOG.info("Total processed: " + processedCount);
        }
    };
    final Filter filter3 = Filter.createPresenceFilter("exp");
    List<SimpleClient> result3 = couchbaseEntryManager.findEntries("o=jans", SimpleClient.class, filter3, SearchScope.SUB, new String[] { "exp" }, clientBatchOperation, 0, 0, 1000);
    LOG.info("Result count (without collecting results): " + result3.size());
    BatchOperation<SimpleClient> clientBatchOperation2 = new DefaultBatchOperation<SimpleClient>() {

        private int processedCount = 0;

        @Override
        public void performAction(List<SimpleClient> objects) {
            for (SimpleClient simpleClient : objects) {
                processedCount++;
            }
            LOG.info("Total processed: " + processedCount);
        }
    };
    final Filter filter4 = Filter.createPresenceFilter("exp");
    List<SimpleClient> result4 = couchbaseEntryManager.findEntries("o=jans", SimpleClient.class, filter4, SearchScope.SUB, new String[] { "exp" }, clientBatchOperation2, 0, 0, 1000);
    LOG.info("Result count (with collecting results): " + result4.size());
}
Also used : SimpleTokenCouchbase(io.jans.orm.couchbase.model.SimpleTokenCouchbase) CustomAttribute(io.jans.orm.model.base.CustomAttribute) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) DefaultBatchOperation(io.jans.orm.model.DefaultBatchOperation) Filter(io.jans.orm.search.filter.Filter) ProcessBatchOperation(io.jans.orm.model.ProcessBatchOperation) CouchbaseEntryManager(io.jans.orm.couchbase.impl.CouchbaseEntryManager) List(java.util.List) SimpleClient(io.jans.orm.couchbase.model.SimpleClient) SimpleSession(io.jans.orm.couchbase.model.SimpleSession)

Example 4 with CustomAttribute

use of io.jans.orm.model.base.CustomAttribute in project jans by JanssenProject.

the class CouchbaseCustomStringAttributesSample method main.

public static void main(String[] args) {
    // Prepare sample connection details
    CouchbaseEntryManagerSample couchbaseEntryManagerSample = new CouchbaseEntryManagerSample();
    // Create Couchbase entry manager
    CouchbaseEntryManager couchbaseEntryManager = couchbaseEntryManagerSample.createCouchbaseEntryManager();
    String randomExternalUid = "otp:" + System.currentTimeMillis();
    // Add dummy user
    SimpleCustomStringUser newUser = new SimpleCustomStringUser();
    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 CustomAttribute("streetAddress", Arrays.asList("London", "Texas", "Kiev")));
    newUser.getCustomAttributes().add((new CustomAttribute("jansExternalUid", randomExternalUid)).setMultiValued(true));
    newUser.setUserRole(UserRole.ADMIN);
    newUser.setNotes(Arrays.asList("note 1", "note 2", "note 3"));
    couchbaseEntryManager.persist(newUser);
    LOG.info("Added User '{}' with uid '{}' and key '{}'", newUser, newUser.getUserId(), newUser.getDn());
    // Find added dummy user but use custom class with String values
    SimpleCustomStringUser foundUser = couchbaseEntryManager.find(SimpleCustomStringUser.class, newUser.getDn());
    LOG.info("Found User '{}' with uid '{}' and key '{}'", foundUser, foundUser.getUserId(), foundUser.getDn());
    LOG.info("Custom attributes '{}'", foundUser.getCustomAttributes());
    for (CustomAttribute customAttribute : foundUser.getCustomAttributes()) {
        LOG.info("Found custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
    }
    // Find by jsExternalUid
    Filter jsExternalUidFilter = Filter.createEqualityFilter("jansExternalUid", randomExternalUid).multiValued();
    List<SimpleCustomStringUser> foundUsers = couchbaseEntryManager.findEntries("ou=people,o=jans", SimpleCustomStringUser.class, jsExternalUidFilter);
    for (SimpleCustomStringUser foundUser2 : foundUsers) {
        LOG.info("Found User '{}' by jsExternalUid with uid '{}' and key '{}'", foundUser2, foundUser2.getUserId(), foundUser2.getDn());
    }
}
Also used : Filter(io.jans.orm.search.filter.Filter) CustomAttribute(io.jans.orm.model.base.CustomAttribute) CouchbaseEntryManager(io.jans.orm.couchbase.impl.CouchbaseEntryManager) SimpleCustomStringUser(io.jans.orm.couchbase.model.SimpleCustomStringUser)

Example 5 with CustomAttribute

use of io.jans.orm.model.base.CustomAttribute in project jans by JanssenProject.

the class CouchbaseUpdateAttributeSample method main.

public static void main(String[] args) {
    // Prepare sample connection details
    CouchbaseEntryManagerSample couchbaseEntryManagerSample = new CouchbaseEntryManagerSample();
    // Create Couchbase entry manager
    CouchbaseEntryManager couchbaseEntryManager = couchbaseEntryManagerSample.createCouchbaseEntryManager();
    String uid = "sample_user_" + System.currentTimeMillis();
    String dn = String.format("inum=%s,ou=people,o=jans", System.currentTimeMillis());
    SimpleUser newUser = new SimpleUser();
    newUser.setDn(dn);
    newUser.setUserId(uid);
    newUser.setUserPassword("test");
    couchbaseEntryManager.persist(newUser);
    SimpleUser user = couchbaseEntryManager.find(SimpleUser.class, dn);
    LOG.info("Found user '{}'", user);
    CustomEntry customEntry = new CustomEntry();
    customEntry.setDn(user.getDn());
    customEntry.setCustomObjectClasses(new String[] { "jansPerson" });
    Date now = new GregorianCalendar(TimeZone.getTimeZone("UTC")).getTime();
    String nowDateString = couchbaseEntryManager.encodeTime(customEntry.getDn(), now);
    CustomAttribute customAttribute = new CustomAttribute("jansLastLogonTime", nowDateString);
    customEntry.getCustomAttributes().add(customAttribute);
    couchbaseEntryManager.merge(customEntry);
    SimpleUser userAfterUpdate = couchbaseEntryManager.find(SimpleUser.class, dn);
    LOG.info("Found user after update '{}'", userAfterUpdate);
}
Also used : SimpleUser(io.jans.orm.couchbase.model.SimpleUser) CustomEntry(io.jans.orm.model.base.CustomEntry) CustomAttribute(io.jans.orm.model.base.CustomAttribute) GregorianCalendar(java.util.GregorianCalendar) CouchbaseEntryManager(io.jans.orm.couchbase.impl.CouchbaseEntryManager) Date(java.util.Date)

Aggregations

CustomAttribute (io.jans.orm.model.base.CustomAttribute)23 EntryPersistenceException (io.jans.orm.exception.EntryPersistenceException)9 Filter (io.jans.orm.search.filter.Filter)9 Date (java.util.Date)7 List (java.util.List)5 DefaultBatchOperation (io.jans.orm.model.DefaultBatchOperation)4 ProcessBatchOperation (io.jans.orm.model.ProcessBatchOperation)4 Calendar (java.util.Calendar)4 User (io.jans.as.common.model.common.User)3 CouchbaseEntryManager (io.jans.orm.couchbase.impl.CouchbaseEntryManager)3 CustomEntry (io.jans.orm.model.base.CustomEntry)3 GregorianCalendar (java.util.GregorianCalendar)3 SpannerEntryManager (io.jans.orm.cloud.spanner.impl.SpannerEntryManager)2 SpannerEntryManagerSample (io.jans.orm.cloud.spanner.persistence.SpannerEntryManagerSample)2 LdapEntryManager (io.jans.orm.ldap.impl.LdapEntryManager)2 SimpleSession (io.jans.orm.ldap.model.SimpleSession)2 SqlEntryManager (io.jans.orm.sql.impl.SqlEntryManager)2 SqlEntryManagerSample (io.jans.orm.sql.persistence.SqlEntryManagerSample)2 JSONArray (org.json.JSONArray)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2