use of javax.naming.directory.BasicAttributes in project fess by codelibs.
the class LdapManager method insert.
public void insert(final User user) {
if (!fessConfig.isLdapAdminEnabled(user.getName())) {
return;
}
final Supplier<Hashtable<String, String>> adminEnv = this::createAdminEnv;
final String userDN = fessConfig.getLdapAdminUserSecurityPrincipal(user.getName());
// attributes
search(fessConfig.getLdapAdminUserBaseDn(), fessConfig.getLdapAdminUserFilter(user.getName()), null, adminEnv, result -> {
if (!result.isEmpty()) {
modifyUserAttributes(user, adminEnv, userDN, result);
} else {
final BasicAttributes entry = new BasicAttributes();
addUserAttributes(entry, user);
final Attribute oc = fessConfig.getLdapAdminUserObjectClassAttribute();
entry.put(oc);
insert(userDN, entry, adminEnv);
}
});
// groups and roles
search(fessConfig.getLdapAdminUserBaseDn(), fessConfig.getLdapAdminUserFilter(user.getName()), new String[] { fessConfig.getLdapMemberofAttribute() }, adminEnv, result -> {
if (!result.isEmpty()) {
final List<String> oldGroupList = new ArrayList<>();
final List<String> oldRoleList = new ArrayList<>();
final String lowerGroupDn = fessConfig.getLdapAdminGroupBaseDn().toLowerCase(Locale.ROOT);
final String lowerRoleDn = fessConfig.getLdapAdminRoleBaseDn().toLowerCase(Locale.ROOT);
processSearchRoles(result, (entryDn, name) -> {
final String lowerEntryDn = entryDn.toLowerCase(Locale.ROOT);
if (lowerEntryDn.indexOf(lowerGroupDn) != -1) {
oldGroupList.add(name);
} else if (lowerEntryDn.indexOf(lowerRoleDn) != -1) {
oldRoleList.add(name);
}
});
final List<String> newGroupList = stream(user.getGroupNames()).get(stream -> stream.collect(Collectors.toList()));
stream(user.getGroupNames()).of(stream -> stream.forEach(name -> {
if (oldGroupList.contains(name)) {
oldGroupList.remove(name);
newGroupList.remove(name);
}
}));
oldGroupList.stream().forEach(name -> {
search(fessConfig.getLdapAdminGroupBaseDn(), fessConfig.getLdapAdminGroupFilter(name), null, adminEnv, subResult -> {
if (!subResult.isEmpty()) {
final List<ModificationItem> modifyList = new ArrayList<>();
modifyDeleteEntry(modifyList, "member", userDN);
modify(fessConfig.getLdapAdminGroupSecurityPrincipal(name), modifyList, adminEnv);
}
});
});
newGroupList.stream().forEach(name -> {
search(fessConfig.getLdapAdminGroupBaseDn(), fessConfig.getLdapAdminGroupFilter(name), null, adminEnv, subResult -> {
if (subResult.isEmpty()) {
final Group group = new Group();
group.setName(name);
insert(group);
}
final List<ModificationItem> modifyList = new ArrayList<>();
modifyAddEntry(modifyList, "member", userDN);
modify(fessConfig.getLdapAdminGroupSecurityPrincipal(name), modifyList, adminEnv);
});
});
final List<String> newRoleList = stream(user.getRoleNames()).get(stream -> stream.collect(Collectors.toList()));
stream(user.getRoleNames()).of(stream -> stream.forEach(name -> {
if (oldRoleList.contains(name)) {
oldRoleList.remove(name);
newRoleList.remove(name);
}
}));
oldRoleList.stream().forEach(name -> {
search(fessConfig.getLdapAdminRoleBaseDn(), fessConfig.getLdapAdminRoleFilter(name), null, adminEnv, subResult -> {
if (!subResult.isEmpty()) {
final List<ModificationItem> modifyList = new ArrayList<>();
modifyDeleteEntry(modifyList, "member", userDN);
modify(fessConfig.getLdapAdminRoleSecurityPrincipal(name), modifyList, adminEnv);
}
});
});
newRoleList.stream().forEach(name -> {
search(fessConfig.getLdapAdminRoleBaseDn(), fessConfig.getLdapAdminRoleFilter(name), null, adminEnv, subResult -> {
if (subResult.isEmpty()) {
final Role role = new Role();
role.setName(name);
insert(role);
}
final List<ModificationItem> modifyList = new ArrayList<>();
modifyAddEntry(modifyList, "member", userDN);
modify(fessConfig.getLdapAdminRoleSecurityPrincipal(name), modifyList, adminEnv);
});
});
} else {
stream(user.getGroupNames()).of(stream -> stream.forEach(name -> {
search(fessConfig.getLdapAdminGroupBaseDn(), fessConfig.getLdapAdminGroupFilter(name), null, adminEnv, subResult -> {
if (subResult.isEmpty()) {
final Group group = new Group();
group.setName(name);
insert(group);
}
final List<ModificationItem> modifyList = new ArrayList<>();
modifyAddEntry(modifyList, "member", userDN);
modify(fessConfig.getLdapAdminGroupSecurityPrincipal(name), modifyList, adminEnv);
});
}));
stream(user.getRoleNames()).of(stream -> stream.forEach(name -> {
search(fessConfig.getLdapAdminRoleBaseDn(), fessConfig.getLdapAdminRoleFilter(name), null, adminEnv, subResult -> {
if (subResult.isEmpty()) {
final Role role = new Role();
role.setName(name);
insert(role);
}
final List<ModificationItem> modifyList = new ArrayList<>();
modifyAddEntry(modifyList, "member", userDN);
modify(fessConfig.getLdapAdminRoleSecurityPrincipal(name), modifyList, adminEnv);
});
}));
}
});
}
use of javax.naming.directory.BasicAttributes in project fess by codelibs.
the class LdapManager method insert.
public void insert(final Group group) {
if (!fessConfig.isLdapAdminEnabled()) {
return;
}
final Supplier<Hashtable<String, String>> adminEnv = this::createAdminEnv;
final String entryDN = fessConfig.getLdapAdminGroupSecurityPrincipal(group.getName());
search(fessConfig.getLdapAdminGroupBaseDn(), fessConfig.getLdapAdminGroupFilter(group.getName()), null, adminEnv, result -> {
if (!result.isEmpty()) {
logger.info("{} exists in LDAP server.", group.getName());
modifyGroupAttributes(group, adminEnv, entryDN, result);
} else {
final BasicAttributes entry = new BasicAttributes();
addGroupAttributes(entry, group);
final Attribute oc = fessConfig.getLdapAdminGroupObjectClassAttribute();
entry.put(oc);
insert(entryDN, entry, adminEnv);
}
});
}
use of javax.naming.directory.BasicAttributes in project perun by CESNET.
the class ExtSourceEGISSOTest method getUsersSubjectsTest.
@Test
public void getUsersSubjectsTest() throws Exception {
System.out.println("getUsersSubjectsTest");
// define needed attributes
String usersQuery = "firstName=josef";
Map<String, String> mapOfAttributes = new HashMap<>();
mapOfAttributes.put("usersQuery", usersQuery);
doReturn(mapOfAttributes).when(extSourceEGISSO).getAttributes();
// mock connection and define received attributes
DirContext dirContext = mock(DirContext.class);
doReturn(dirContext).when(extSourceEGISSO).getContext();
Attributes attributes = new BasicAttributes();
attributes.put(new BasicAttribute("cn", "josef"));
NamingEnumeration<SearchResult> namingEnumeration = mock(NamingEnumeration.class);
doReturn(namingEnumeration).when(dirContext).search(anyString(), anyString(), any());
doReturn(true, false).when(namingEnumeration).hasMore();
SearchResult searchResult = new SearchResult("name", namingEnumeration, attributes);
doReturn(searchResult).when(namingEnumeration).next();
extSourceEGISSO.mapping = new HashMap<>();
extSourceEGISSO.mapping.put("cn", "cn");
// create expected subject to get
List<Map<String, String>> expectedSubjects = new ArrayList<>();
Map<String, String> subject = new HashMap<>();
subject.put("cn", "josef");
expectedSubjects.add(subject);
// test the method
List<Map<String, String>> actualSubjects = extSourceEGISSO.getUsersSubjects();
assertEquals("subjects should be same", expectedSubjects, actualSubjects);
}
use of javax.naming.directory.BasicAttributes in project yyl_example by Relucent.
the class LdapDaoHelper method create.
/**
* 创建新条目
* @param entry 新条目
* @param ctx LDAP上下文连接
* @deprecated 该方法未经过严谨测试
*/
public static void create(LdapEntry entry, LdapContext ctx) throws NamingException {
try {
String dn = entry.getDn();
Attributes attrs = new BasicAttributes(true);
if (entry != null && !entry.isEmpty()) {
Iterator<String> iterator = entry.keySet().iterator();
while (iterator.hasNext()) {
String id = iterator.next();
Attribute attr = new BasicAttribute(id);
List<?> values = entry.getAll(id);
if (values != null) {
for (Object value : values) {
attr.add(value);
}
}
attrs.put(attr);
}
}
ctx.createSubcontext(dn, attrs);
} catch (NamingException e) {
throw e;
}
}
use of javax.naming.directory.BasicAttributes in project jmeter by apache.
the class LDAPSampler method getBasicAttributes.
/**
* This will create the Basic Attributes for the In build TestCase for Add
* Test.
*
* @return the BasicAttributes
*/
private BasicAttributes getBasicAttributes() {
BasicAttributes basicattributes = new BasicAttributes();
// $NON-NLS-1$
BasicAttribute basicattribute = new BasicAttribute("objectclass");
// $NON-NLS-1$
basicattribute.add("top");
// $NON-NLS-1$
basicattribute.add("person");
// $NON-NLS-1$
basicattribute.add("organizationalPerson");
// $NON-NLS-1$
basicattribute.add("inetOrgPerson");
basicattributes.put(basicattribute);
// $NON-NLS-1$
String s1 = "User";
// $NON-NLS-1$
String s3 = "Test";
// $NON-NLS-1$
String s5 = "user";
// $NON-NLS-1$
String s6 = "test";
COUNTER.incrementAndGet();
// $NON-NLS-1$
basicattributes.put(new BasicAttribute("givenname", s1));
// $NON-NLS-1$
basicattributes.put(new BasicAttribute("sn", s3));
// $NON-NLS-1$ //$NON-NLS-2$
basicattributes.put(new BasicAttribute("cn", "TestUser" + COUNTER.get()));
// $NON-NLS-1$
basicattributes.put(new BasicAttribute("uid", s5));
// $NON-NLS-1$
basicattributes.put(new BasicAttribute("userpassword", s6));
// $NON-NLS-1$
setProperty(new StringProperty(ADD, "cn=TestUser" + COUNTER.get()));
return basicattributes;
}
Aggregations