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);
}
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);
}
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);
}
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;
}
}
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);
}
}
Aggregations