Search in sources :

Example 26 with CustomObjectAttribute

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

the class UserService method removeUserAttributeValue.

public User removeUserAttributeValue(String userId, String attributeName, String attributeValue) {
    log.debug("Remove user attribute value from LDAP: attributeName = '{}', attributeValue = '{}'", attributeName, attributeValue);
    User user = getUser(userId);
    if (user == null) {
        return null;
    }
    CustomObjectAttribute customAttribute = getCustomAttribute(user, attributeName);
    if (customAttribute != null) {
        List<Object> currentAttributeValues = customAttribute.getValues();
        if (currentAttributeValues.contains(attributeValue)) {
            List<Object> newAttributeValues = new ArrayList<>();
            newAttributeValues.addAll(currentAttributeValues);
            if (currentAttributeValues.contains(attributeValue)) {
                newAttributeValues.remove(attributeValue);
            } else {
                return null;
            }
            customAttribute.setValues(newAttributeValues);
        }
    }
    return updateUser(user);
}
Also used : CustomObjectAttribute(io.jans.orm.model.base.CustomObjectAttribute) User(io.jans.as.common.model.common.User) ArrayList(java.util.ArrayList)

Example 27 with CustomObjectAttribute

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

the class UserService method setCustomAttribute.

public void setCustomAttribute(User user, String attributeName, String attributeValue) {
    CustomObjectAttribute customAttribute = getCustomAttribute(user, attributeName);
    if (customAttribute == null) {
        customAttribute = new CustomObjectAttribute(attributeName);
        user.getCustomAttributes().add(customAttribute);
    }
    customAttribute.setValue(attributeValue);
}
Also used : CustomObjectAttribute(io.jans.orm.model.base.CustomObjectAttribute)

Example 28 with CustomObjectAttribute

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

the class UserService method addDefaultUser.

public User addDefaultUser(String uid) {
    String peopleBaseDN = getPeopleBaseDn();
    String inum = inumService.generatePeopleInum();
    User user = new User();
    user.setDn("inum=" + inum + "," + peopleBaseDN);
    user.setCustomAttributes(Arrays.asList(new CustomObjectAttribute("inum", inum), new CustomObjectAttribute("jansStatus", GluuStatus.ACTIVE.getValue()), new CustomObjectAttribute("displayName", "User " + uid + " added via Jans Auth custom plugin")));
    user.setUserId(uid);
    List<String> personCustomObjectClassList = getPersonCustomObjectClassList();
    if ((personCustomObjectClassList != null) && !personCustomObjectClassList.isEmpty()) {
        user.setCustomObjectClasses(personCustomObjectClassList.toArray(new String[personCustomObjectClassList.size()]));
    }
    user.setCreatedAt(new Date());
    persistenceEntryManager.persist(user);
    return getUser(uid);
}
Also used : CustomObjectAttribute(io.jans.orm.model.base.CustomObjectAttribute) User(io.jans.as.common.model.common.User) Date(java.util.Date)

Example 29 with CustomObjectAttribute

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

the class AuthenticationService method getUserByAttribute.

private User getUserByAttribute(PersistenceEntryManager ldapAuthEntryManager, String baseDn, String attributeName, String attributeValue) {
    log.debug("Getting user information from LDAP: attributeName = '{}', attributeValue = '{}'", attributeName, attributeValue);
    if (StringHelper.isEmpty(attributeValue)) {
        return null;
    }
    SimpleUser sampleUser = new SimpleUser();
    sampleUser.setDn(baseDn);
    List<CustomObjectAttribute> customAttributes = new ArrayList<CustomObjectAttribute>();
    customAttributes.add(new CustomObjectAttribute(attributeName, attributeValue));
    sampleUser.setCustomAttributes(customAttributes);
    log.debug("Searching user by attributes: '{}', baseDn: '{}'", customAttributes, baseDn);
    List<User> entries = ldapAuthEntryManager.findEntries(sampleUser, 1);
    log.debug("Found '{}' entries", entries.size());
    if (entries.size() > 0) {
        SimpleUser foundUser = entries.get(0);
        return ldapAuthEntryManager.find(User.class, foundUser.getDn());
    } else {
        return null;
    }
}
Also used : CustomObjectAttribute(io.jans.orm.model.base.CustomObjectAttribute) SimpleUser(io.jans.as.common.model.common.SimpleUser) SimpleUser(io.jans.as.common.model.common.SimpleUser) User(io.jans.as.common.model.common.User) ArrayList(java.util.ArrayList)

Example 30 with CustomObjectAttribute

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

the class CouchbaseCustomObjectAttributesSample method main.

public static void main(String[] args) {
    // Prepare sample connection details
    CouchbaseEntryManagerSample couchbaseEntryManagerSample = new CouchbaseEntryManagerSample();
    // Create SQL entry manager
    CouchbaseEntryManager sqlEntryManager = couchbaseEntryManagerSample.createCouchbaseEntryManager();
    // 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.couchbase.model.SimpleUser) Filter(io.jans.orm.search.filter.Filter) CouchbaseEntryManager(io.jans.orm.couchbase.impl.CouchbaseEntryManager) Date(java.util.Date)

Aggregations

CustomObjectAttribute (io.jans.orm.model.base.CustomObjectAttribute)32 User (io.jans.as.common.model.common.User)12 Filter (io.jans.orm.search.filter.Filter)9 Test (org.testng.annotations.Test)7 Date (java.util.Date)6 ArrayList (java.util.ArrayList)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 SimpleUser (io.jans.orm.cloud.spanner.model.SimpleUser)4 SimpleUser (io.jans.orm.sql.model.SimpleUser)4 SpannerEntryManager (io.jans.orm.cloud.spanner.impl.SpannerEntryManager)3 SpannerEntryManagerSample (io.jans.orm.cloud.spanner.persistence.SpannerEntryManagerSample)3 CouchbaseEntryManager (io.jans.orm.couchbase.impl.CouchbaseEntryManager)3 SimpleUser (io.jans.orm.couchbase.model.SimpleUser)3 SqlEntryManager (io.jans.orm.sql.impl.SqlEntryManager)3 SqlEntryManagerSample (io.jans.orm.sql.persistence.SqlEntryManagerSample)3 SimpleUser (io.jans.as.common.model.common.SimpleUser)1 GluuAttribute (io.jans.model.GluuAttribute)1 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