private String extractEntityId(OrganizationalEntity entity) { if (!isEntityIdDn()) { return entity.getId(); } String entityAttrId = null; if (entity instanceof User) { entityAttrId = getConfigProperty(USER_ATTR_ID, DEFAULT_USER_ATTR_ID); } else if (entity instanceof Group) { entityAttrId = getConfigProperty(ROLE_ATTR_ID, DEFAULT_ROLE_ATTR_ID); } return extractAttribute(entity.getId(), entityAttrId); }
private String getAttributeValueForEntity(OrganizationalEntity entity, String attributeName, String defaultValue) { String context = getConfigPropertyByEntity(entity, USER_CTX, ROLE_CTX); String filter = getConfigPropertyByEntity(entity, USER_FILTER, ROLE_FILTER); String attrId = getConfigProperty(attributeName, defaultValue); String entityId = extractEntityId(entity); return ldapSearcher.search(context, filter, entityId).getSingleAttributeResult(attrId); }
@Override public String getLanguageForEntity(OrganizationalEntity entity) { String result = getAttributeValueForEntity(entity, LANG_ATTR_ID, DEFAULT_LANG_ATTR_ID); return result == null ? DEFAULT_LOCALE : result; }
@Override public Iterator<OrganizationalEntity> getMembersForGroup(Group group) { String roleContext = getConfigProperty(ROLE_CTX); String roleFilter = getConfigProperty(ROLE_MEMBERS_FILTER, getConfigProperty(ROLE_FILTER)); String roleAttrId = getConfigProperty(MEMBER_ATTR_ID, DEFAULT_MEMBER_ATTR_ID); String entityId = extractEntityId(group); List<String> memberIds = ldapSearcher.search(roleContext, roleFilter, entityId).getAttributeResults(roleAttrId); return memberIds.stream() .filter(memberId -> memberId != null) .map(memberId -> (OrganizationalEntity) TaskModelProvider.getFactory().newUser(memberId)) .collect(Collectors.toList()) .iterator(); }
private UserInfo createLdapUserInfoUid(Properties properties) { properties.setProperty(LDAPUserInfoImpl.NAME_ATTR_ID, "uid"); return new LDAPUserInfoImpl(properties); }
@Override public String getEntityForEmail(String email) { String context = getConfigProperty(USER_CTX); String filter = getConfigProperty(EMAIL_FILTER); String attributeId = getConfigProperty(USER_ATTR_ID, DEFAULT_USER_ATTR_ID); LdapSearcher searcher = ldapSearcher.search(context, filter, email); if (searcher.getSearchResults().isEmpty()) { return null; } if (isEntityIdDn()) { return searcher.getSingleSearchResult().getNameInNamespace(); } return searcher.getSingleAttributeResult(attributeId); }
private String getConfigPropertyByEntity(OrganizationalEntity entity, String userKey, String roleKey) { if (entity instanceof User) { return getConfigProperty(userKey); } else if (entity instanceof Group) { return getConfigProperty(roleKey); } else { throw new IllegalArgumentException("Unknown organizational entity: " + entity); } }
@Override public boolean hasEmail(Group group) { return getEmailForEntity(group) != null; }
private UserInfo createLdapUserInfoCn(Properties properties) { properties.setProperty(LDAPUserInfoImpl.NAME_ATTR_ID, "cn"); return new LDAPUserInfoImpl(properties); }
private boolean isEntityIdDn() { return Boolean.parseBoolean(getConfigProperty(IS_ENTITY_ID_DN, DEFAULT_ENTITY_ID_DN)); }
public static UserInfo getUserInfo() { UserInfo userInfo = new DefaultUserInfo(true); if ("ldap".equalsIgnoreCase(USER_INFO_IMPL)) { userInfo = new LDAPUserInfoImpl(true); } else if ("db".equalsIgnoreCase(USER_INFO_IMPL)) { userInfo = new DBUserInfoImpl(true); } else if ("props".equalsIgnoreCase(USER_INFO_IMPL)) { userInfo = new PropertyUserInfoImpl(true); } else if ("custom".equalsIgnoreCase(USER_INFO_IMPL)) { try { userInfo = (UserInfo) Class.forName(CUSTOM_USER_INFO_IMPL).newInstance(); } catch (Exception e) { throw new RuntimeException("Unable to create instance of custom user info impl", e); } } return userInfo; } }
@Override public String getDisplayName(OrganizationalEntity entity) { return getAttributeValueForEntity(entity, NAME_ATTR_ID, DEFAULT_NAME_ATTR_ID); }
private void testGetEntityForEmail(String email, String expected, boolean useDN) { Properties properties = createUserInfoProperties(); if (useDN) { properties.setProperty(LDAPUserInfoImpl.IS_ENTITY_ID_DN, "true"); } UserInfo ldapUserInfo = new LDAPUserInfoImpl(properties); Assertions.assertThat(ldapUserInfo.getEntityForEmail(email)).isEqualTo(expected); }
@Override public String getEmailForEntity(OrganizationalEntity entity) { return getAttributeValueForEntity(entity, EMAIL_ATTR_ID, DEFAULT_EMAIL_ATTR_ID); }
private void testHasEmail(Group group, boolean hasEmail, boolean customAttribute) { Properties properties = createUserInfoProperties(); if (customAttribute) { properties.setProperty(LDAPUserInfoImpl.EMAIL_ATTR_ID, "email"); } if (group.getId().startsWith("cn=")) { properties.setProperty(LDAPUserInfoImpl.IS_ENTITY_ID_DN, "true"); } UserInfo ldapUserInfo = new LDAPUserInfoImpl(properties); Assertions.assertThat(ldapUserInfo.hasEmail(group)).isEqualTo(hasEmail); }
private void testGetEmailForEntity(OrganizationalEntity entity, String email, boolean customAttribute) { Properties properties = createUserInfoProperties(); if (customAttribute) { properties.setProperty(LDAPUserInfoImpl.EMAIL_ATTR_ID, "email"); } if (entity.getId().startsWith("uid=") || entity.getId().startsWith("cn=")) { properties.setProperty(LDAPUserInfoImpl.IS_ENTITY_ID_DN, "true"); } UserInfo ldapUserInfo = new LDAPUserInfoImpl(properties); Assertions.assertThat(ldapUserInfo.getEmailForEntity(entity)).isEqualTo(email); }
private void testGetLanguageForEntity(OrganizationalEntity entity, String language, boolean customAttribute) { Properties properties = createUserInfoProperties(); if (customAttribute) { properties.setProperty(LDAPUserInfoImpl.LANG_ATTR_ID, "language"); } if (entity.getId().startsWith("uid=") || entity.getId().startsWith("cn=")) { properties.setProperty(LDAPUserInfoImpl.IS_ENTITY_ID_DN, "true"); } UserInfo ldapUserInfo = new LDAPUserInfoImpl(properties); Assertions.assertThat(ldapUserInfo.getLanguageForEntity(entity)).isEqualTo(language); }
private void testGetDisplayName(OrganizationalEntity entity, String expectedName, boolean customAttribute) { Properties properties = createUserInfoProperties(); if (customAttribute) { properties.setProperty(LDAPUserInfoImpl.NAME_ATTR_ID, "name"); } if (entity.getId().startsWith("uid=") || entity.getId().startsWith("cn=")) { properties.setProperty(LDAPUserInfoImpl.IS_ENTITY_ID_DN, "true"); } UserInfo ldapUserInfo = new LDAPUserInfoImpl(properties); String name = ldapUserInfo.getDisplayName(entity); Assertions.assertThat(name).isNotNull(); Assertions.assertThat(name).isEqualTo(expectedName); }
private void testGetMembersForGroup(boolean emptyGroup, boolean customAttribute, boolean distinguishedName) { Properties properties = createUserInfoProperties(); if (customAttribute) { properties.setProperty(LDAPUserInfoImpl.MEMBER_ATTR_ID, "representative"); } if (distinguishedName) { properties.setProperty(LDAPUserInfoImpl.IS_ENTITY_ID_DN, "true"); } UserInfo ldapUserInfo = new LDAPUserInfoImpl(properties); Group group; if (distinguishedName) { group = emptyGroup ? USER_DN : MANAGER_DN; } else { group = emptyGroup ? USER : MANAGER; } Iterator<OrganizationalEntity> iterator = ldapUserInfo.getMembersForGroup(group); if (emptyGroup) { Assertions.assertThat(iterator.hasNext()).isFalse(); return; } Assertions.assertThat(iterator.hasNext()).isTrue(); User user = (User) iterator.next(); if (customAttribute) { Assertions.assertThat(user.getId()).isEqualTo(MARY_DN.getId()); } else { Assertions.assertThat(user.getId()).isEqualTo(JOHN_DN.getId()); } Assertions.assertThat(iterator.hasNext()).isFalse(); }
public static UserInfo getUserInfo() { UserInfo userInfo = new DefaultUserInfo(true); if ("ldap".equalsIgnoreCase(USER_INFO_IMPL)) { userInfo = new LDAPUserInfoImpl(true); } else if ("db".equalsIgnoreCase(USER_INFO_IMPL)) { userInfo = new DBUserInfoImpl(true); } else if ("props".equalsIgnoreCase(USER_INFO_IMPL)) { userInfo = new PropertyUserInfoImpl(true); } else if ("custom".equalsIgnoreCase(USER_INFO_IMPL)) { try { userInfo = (UserInfo) Class.forName(CUSTOM_USER_INFO_IMPL).newInstance(); } catch (Exception e) { throw new RuntimeException("Unable to create instance of custom user info impl", e); } } return userInfo; } }