Search in sources :

Example 1 with CustomAttribute

use of org.gluu.persist.model.base.CustomAttribute in project oxCore by GluuFederation.

the class LdapSampleBatchJob method main.

public static void main(String[] args) {
    // Prepare sample connection details
    LdapSampleEntryManager ldapSampleEntryManager = new LdapSampleEntryManager();
    // Create LDAP entry manager
    final LdapEntryManager ldapEntryManager = ldapSampleEntryManager.createLdapEntryManager();
    BatchOperation<SimpleTokenLdap> tokenLdapBatchOperation = new ProcessBatchOperation<SimpleTokenLdap>() {

        private int processedCount = 0;

        @Override
        public void performAction(List<SimpleTokenLdap> objects) {
            for (SimpleTokenLdap simpleTokenLdap : objects) {
                try {
                    CustomAttribute customAttribute = getUpdatedAttribute(ldapEntryManager, "oxAuthExpiration", simpleTokenLdap.getAttribute("oxAuthExpiration"));
                    simpleTokenLdap.setCustomAttributes(Arrays.asList(new CustomAttribute[] { customAttribute }));
                    ldapEntryManager.merge(simpleTokenLdap);
                    processedCount++;
                } catch (EntryPersistenceException ex) {
                    LOG.error("Failed to update entry", ex);
                }
            }
            LOG.info("Total processed: " + processedCount);
        }
    };
    final Filter filter1 = Filter.createPresenceFilter("oxAuthExpiration");
    ldapEntryManager.findEntries("o=gluu", SimpleTokenLdap.class, filter1, SearchScope.SUB, new String[] { "oxAuthExpiration" }, tokenLdapBatchOperation, 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(ldapEntryManager, "oxLastAccessTime", simpleSession.getAttribute("oxLastAccessTime"));
                    simpleSession.setCustomAttributes(Arrays.asList(new CustomAttribute[] { customAttribute }));
                    ldapEntryManager.merge(simpleSession);
                    processedCount++;
                } catch (EntryPersistenceException ex) {
                    LOG.error("Failed to update entry", ex);
                }
            }
            LOG.info("Total processed: " + processedCount);
        }
    };
    final Filter filter2 = Filter.createPresenceFilter("oxLastAccessTime");
    ldapEntryManager.findEntries("o=gluu", SimpleSession.class, filter2, SearchScope.SUB, new String[] { "oxLastAccessTime" }, 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("oxAuthClientSecretExpiresAt");
    List<SimpleClient> result3 = ldapEntryManager.findEntries("o=gluu", SimpleClient.class, filter3, SearchScope.SUB, new String[] { "oxAuthClientSecretExpiresAt" }, 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("oxAuthClientSecretExpiresAt");
    List<SimpleClient> result4 = ldapEntryManager.findEntries("o=gluu", SimpleClient.class, filter4, SearchScope.SUB, new String[] { "oxAuthClientSecretExpiresAt" }, clientBatchOperation2, 0, 0, 1000);
    LOG.info("Result count (with collecting results): " + result4.size());
}
Also used : SimpleTokenLdap(org.gluu.ldap.model.SimpleTokenLdap) CustomAttribute(org.gluu.persist.model.base.CustomAttribute) EntryPersistenceException(org.gluu.persist.exception.mapping.EntryPersistenceException) DefaultBatchOperation(org.gluu.persist.model.DefaultBatchOperation) LdapEntryManager(org.gluu.persist.ldap.impl.LdapEntryManager) Filter(org.gluu.search.filter.Filter) ProcessBatchOperation(org.gluu.persist.model.ProcessBatchOperation) List(java.util.List) SimpleClient(org.gluu.ldap.model.SimpleClient) SimpleSession(org.gluu.ldap.model.SimpleSession)

Example 2 with CustomAttribute

use of org.gluu.persist.model.base.CustomAttribute in project oxCore by GluuFederation.

the class LdapSampleBatchJob method getUpdatedAttribute.

private static CustomAttribute getUpdatedAttribute(LdapEntryManager ldapEntryManager, String attributeName, String attributeValue) {
    try {
        Calendar calendar = Calendar.getInstance();
        Date oxLastAccessTimeDate = StaticUtils.decodeGeneralizedTime(attributeValue);
        calendar.setTime(oxLastAccessTimeDate);
        calendar.add(Calendar.SECOND, -1);
        CustomAttribute customAttribute = new CustomAttribute();
        customAttribute.setName(attributeName);
        customAttribute.setValue(ldapEntryManager.encodeGeneralizedTime(calendar.getTime()));
        return customAttribute;
    } catch (ParseException e) {
        LOG.error("Can't parse attribute", e);
    }
    return null;
}
Also used : CustomAttribute(org.gluu.persist.model.base.CustomAttribute) Calendar(java.util.Calendar) ParseException(java.text.ParseException) Date(java.util.Date)

Example 3 with CustomAttribute

use of org.gluu.persist.model.base.CustomAttribute in project oxAuth by GluuFederation.

the class AuthenticationService method updateLastLogonUserTime.

private void updateLastLogonUserTime(User user) {
    if (!appConfiguration.getUpdateUserLastLogonTime()) {
        return;
    }
    CustomEntry customEntry = new CustomEntry();
    customEntry.setDn(user.getDn());
    List<String> personCustomObjectClassList = appConfiguration.getPersonCustomObjectClassList();
    if ((personCustomObjectClassList != null) && !personCustomObjectClassList.isEmpty()) {
        // Combine object classes from LDAP and configuration in one list
        Set<Object> customPersonCustomObjectClassList = new HashSet<Object>();
        customPersonCustomObjectClassList.add("gluuPerson");
        customPersonCustomObjectClassList.addAll(personCustomObjectClassList);
        if (user.getCustomObjectClasses() != null) {
            customPersonCustomObjectClassList.addAll(Arrays.asList(user.getCustomObjectClasses()));
        }
        customEntry.setCustomObjectClasses(customPersonCustomObjectClassList.toArray(new String[customPersonCustomObjectClassList.size()]));
    } else {
        customEntry.setCustomObjectClasses(UserService.USER_OBJECT_CLASSES);
    }
    Date now = new GregorianCalendar(TimeZone.getTimeZone("UTC")).getTime();
    String nowDateString = ldapEntryManager.encodeTime(customEntry.getDn(), now);
    CustomAttribute customAttribute = new CustomAttribute("oxLastLogonTime", nowDateString);
    customEntry.getCustomAttributes().add(customAttribute);
    try {
        ldapEntryManager.merge(customEntry);
    } catch (EntryPersistenceException epe) {
        log.error("Failed to update oxLastLogonTime of user '{}'", user.getUserId());
    }
}
Also used : CustomEntry(org.gluu.persist.model.base.CustomEntry) CustomAttribute(org.gluu.persist.model.base.CustomAttribute) EntryPersistenceException(org.gluu.persist.exception.EntryPersistenceException)

Example 4 with CustomAttribute

use of org.gluu.persist.model.base.CustomAttribute in project oxCore by GluuFederation.

the class LdapSample method main.

public static void main(String[] args) {
    // Prepare sample connection details
    LdapSampleEntryManager ldapSampleEntryManager = new LdapSampleEntryManager();
    // Create LDAP entry manager
    LdapEntryManager ldapEntryManager = ldapSampleEntryManager.createLdapEntryManager();
    // Find all users which have specified object classes defined in SimpleUser
    List<SimpleUser> users = ldapEntryManager.findEntries("o=gluu", SimpleUser.class, null);
    for (SimpleUser user : users) {
        LOG.debug("User with uid: " + user.getUserId());
    }
    if (users.size() > 0) {
        // Add attribute "streetAddress" to first user
        SimpleUser user = users.get(0);
        user.getCustomAttributes().add(new CustomAttribute("streetAddress", "Somewhere: " + System.currentTimeMillis()));
        ldapEntryManager.merge(user);
    }
    Filter filter = Filter.createEqualityFilter("gluuStatus", "active");
    List<SimpleAttribute> attributes = ldapEntryManager.findEntries("o=gluu", SimpleAttribute.class, filter, SearchScope.SUB, null, null, 10, 0, 0);
    for (SimpleAttribute attribute : attributes) {
        LOG.debug("Attribute with displayName: " + attribute.getCustomAttributes().get(1));
    }
    List<SimpleSession> sessions = ldapEntryManager.findEntries("o=gluu", SimpleSession.class, filter, SearchScope.SUB, null, null, 10, 0, 0);
    LOG.debug("Found sessions: " + sessions.size());
    List<SimpleGrant> grants = ldapEntryManager.findEntries("o=gluu", SimpleGrant.class, null, SearchScope.SUB, new String[] { "oxAuthGrantId" }, null, 10, 0, 0);
    LOG.debug("Found grants: " + grants.size());
    try {
        ListViewResponse<SimpleUser> vlvResponse = ldapEntryManager.findListViewResponse("o=gluu", SimpleUser.class, null, 10, 100000, 1000, "displayName", SortOrder.ASCENDING, new String[] { "uid", "displayName", "gluuStatus" });
        LOG.debug("Found persons: " + vlvResponse.getTotalResults());
        System.out.println(vlvResponse.getResult().size());
    } catch (Exception ex) {
        LOG.error("Failed to search", ex);
    }
}
Also used : SimpleUser(org.gluu.ldap.model.SimpleUser) CustomAttribute(org.gluu.persist.model.base.CustomAttribute) SimpleAttribute(org.gluu.ldap.model.SimpleAttribute) SimpleGrant(org.gluu.ldap.model.SimpleGrant) LdapEntryManager(org.gluu.persist.ldap.impl.LdapEntryManager) Filter(org.gluu.search.filter.Filter) SimpleSession(org.gluu.ldap.model.SimpleSession)

Example 5 with CustomAttribute

use of org.gluu.persist.model.base.CustomAttribute in project oxAuth by GluuFederation.

the class RegisterRestWebServiceImpl method putCustomStuffIntoObject.

/**
 * Puts custom object class and custom attributes in client object for persistence.
 *
 * @param p_client        client object
 * @param p_requestObject request object
 */
private void putCustomStuffIntoObject(Client p_client, JSONObject p_requestObject) throws JSONException {
    // custom object class
    final String customOC = appConfiguration.getDynamicRegistrationCustomObjectClass();
    if (StringUtils.isNotBlank(customOC)) {
        p_client.setCustomObjectClasses(new String[] { customOC });
    }
    // custom attributes (custom attributes must be in custom object class)
    final List<String> attrList = appConfiguration.getDynamicRegistrationCustomAttributes();
    if (attrList != null && !attrList.isEmpty()) {
        for (String attr : attrList) {
            if (p_requestObject.has(attr)) {
                final JSONArray parameterValuesJsonArray = p_requestObject.optJSONArray(attr);
                final List<String> parameterValues = parameterValuesJsonArray != null ? toList(parameterValuesJsonArray) : Arrays.asList(p_requestObject.getString(attr));
                if (parameterValues != null && !parameterValues.isEmpty()) {
                    try {
                        boolean processed = processApplicationAttributes(p_client, attr, parameterValues);
                        if (!processed) {
                            p_client.getCustomAttributes().add(new CustomAttribute(attr, parameterValues));
                        }
                    } catch (Exception e) {
                        log.debug(e.getMessage(), e);
                    }
                }
            }
        }
    }
}
Also used : CustomAttribute(org.gluu.persist.model.base.CustomAttribute) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) WebApplicationException(javax.ws.rs.WebApplicationException) InvalidJwtException(org.gluu.oxauth.model.exception.InvalidJwtException)

Aggregations

CustomAttribute (org.gluu.persist.model.base.CustomAttribute)9 SimpleSession (org.gluu.ldap.model.SimpleSession)2 EntryPersistenceException (org.gluu.persist.exception.EntryPersistenceException)2 LdapEntryManager (org.gluu.persist.ldap.impl.LdapEntryManager)2 CustomEntry (org.gluu.persist.model.base.CustomEntry)2 Filter (org.gluu.search.filter.Filter)2 JSONArray (org.json.JSONArray)2 ParseException (java.text.ParseException)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 List (java.util.List)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 SimpleAttribute (org.gluu.ldap.model.SimpleAttribute)1 SimpleClient (org.gluu.ldap.model.SimpleClient)1 SimpleGrant (org.gluu.ldap.model.SimpleGrant)1 SimpleTokenLdap (org.gluu.ldap.model.SimpleTokenLdap)1 SimpleUser (org.gluu.ldap.model.SimpleUser)1 InvalidJwtException (org.gluu.oxauth.model.exception.InvalidJwtException)1 EntryPersistenceException (org.gluu.persist.exception.mapping.EntryPersistenceException)1 DefaultBatchOperation (org.gluu.persist.model.DefaultBatchOperation)1