Search in sources :

Example 1 with CustomAttribute

use of org.xdi.ldap.model.CustomAttribute in project oxCore by GluuFederation.

the class LdapSampleBatchJob method getUpdatedAttribute.

private static CustomAttribute getUpdatedAttribute(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.setDate(calendar.getTime());
        return customAttribute;
    } catch (ParseException e) {
        log.error("Can't parse attribute", e);
    }
    return null;
}
Also used : CustomAttribute(org.xdi.ldap.model.CustomAttribute) Calendar(java.util.Calendar) ParseException(java.text.ParseException) Date(java.util.Date)

Example 2 with CustomAttribute

use of org.xdi.ldap.model.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.xdi.ldap.model.CustomAttribute) JSONArray(org.codehaus.jettison.json.JSONArray) WebApplicationException(javax.ws.rs.WebApplicationException) JSONException(org.codehaus.jettison.json.JSONException)

Example 3 with CustomAttribute

use of org.xdi.ldap.model.CustomAttribute in project oxAuth by GluuFederation.

the class UserService method getUserByAttribute.

public User getUserByAttribute(String attributeName, String attributeValue) {
    log.debug("Getting user information from LDAP: attributeName = '{}', attributeValue = '{}'", attributeName, attributeValue);
    User user = new User();
    user.setDn(staticConfiguration.getBaseDn().getPeople());
    List<CustomAttribute> customAttributes = new ArrayList<CustomAttribute>();
    customAttributes.add(new CustomAttribute(attributeName, attributeValue));
    user.setCustomAttributes(customAttributes);
    List<User> entries = ldapEntryManager.findEntries(user);
    log.debug("Found '{}' entries", entries.size());
    if (entries.size() > 0) {
        return entries.get(0);
    } else {
        return null;
    }
}
Also used : User(org.xdi.oxauth.model.common.User) CustomAttribute(org.xdi.ldap.model.CustomAttribute) ArrayList(java.util.ArrayList)

Example 4 with CustomAttribute

use of org.xdi.ldap.model.CustomAttribute in project oxAuth by GluuFederation.

the class UserService method addUserAttribute.

public boolean addUserAttribute(User user, String attributeName, String attributeValue) {
    CustomAttribute customAttribute = getCustomAttribute(user, attributeName);
    if (customAttribute == null) {
        customAttribute = new CustomAttribute(attributeName, attributeValue);
        user.getCustomAttributes().add(customAttribute);
    } else {
        List<String> currentAttributeValues = customAttribute.getValues();
        List<String> newAttributeValues = new ArrayList<String>();
        newAttributeValues.addAll(currentAttributeValues);
        if (newAttributeValues.contains(attributeValue)) {
            return false;
        } else {
            newAttributeValues.add(attributeValue);
        }
        customAttribute.setValues(newAttributeValues);
    }
    return true;
}
Also used : CustomAttribute(org.xdi.ldap.model.CustomAttribute) ArrayList(java.util.ArrayList)

Example 5 with CustomAttribute

use of org.xdi.ldap.model.CustomAttribute in project oxAuth by GluuFederation.

the class UserService method removeUserAttribute.

public User removeUserAttribute(String userId, String attributeName, String attributeValue) {
    log.debug("Remove user attribute from LDAP: attributeName = '{}', attributeValue = '{}'", attributeName, attributeValue);
    User user = getUser(userId);
    if (user == null) {
        return null;
    }
    CustomAttribute customAttribute = getCustomAttribute(user, attributeName);
    if (customAttribute != null) {
        List<String> currentAttributeValues = customAttribute.getValues();
        if (currentAttributeValues.contains(attributeValue)) {
            List<String> newAttributeValues = new ArrayList<String>();
            newAttributeValues.addAll(currentAttributeValues);
            if (currentAttributeValues.contains(attributeValue)) {
                newAttributeValues.remove(attributeValue);
            } else {
                return null;
            }
            customAttribute.setValues(newAttributeValues);
        }
    }
    return updateUser(user);
}
Also used : User(org.xdi.oxauth.model.common.User) CustomAttribute(org.xdi.ldap.model.CustomAttribute) ArrayList(java.util.ArrayList)

Aggregations

CustomAttribute (org.xdi.ldap.model.CustomAttribute)17 User (org.xdi.oxauth.model.common.User)5 ArrayList (java.util.ArrayList)4 JSONArray (org.codehaus.jettison.json.JSONArray)2 EntryPersistenceException (org.gluu.site.ldap.persistence.exception.EntryPersistenceException)2 CustomEntry (org.xdi.ldap.model.CustomEntry)2 ParseException (java.text.ParseException)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 JSONException (org.codehaus.jettison.json.JSONException)1 SearchScope (org.xdi.ldap.model.SearchScope)1 Scope (org.xdi.oxauth.model.common.Scope)1 SimpleUser (org.xdi.oxauth.model.common.SimpleUser)1