use of javax.naming.directory.BasicAttributes in project fess by codelibs.
the class LdapManager method insert.
public void insert(final User user) {
final FessConfig fessConfig = ComponentUtil.getFessConfig();
if (!fessConfig.isLdapAdminEnabled(user.getName())) {
return;
}
final Supplier<Hashtable<String, String>> adminEnv = () -> 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, fessConfig);
} else {
final BasicAttributes entry = new BasicAttributes();
addUserAttributes(entry, user, fessConfig);
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 Role role) {
final FessConfig fessConfig = ComponentUtil.getFessConfig();
if (!fessConfig.isLdapAdminEnabled()) {
return;
}
final Supplier<Hashtable<String, String>> adminEnv = () -> createAdminEnv();
search(fessConfig.getLdapAdminRoleBaseDn(), fessConfig.getLdapAdminRoleFilter(role.getName()), null, adminEnv, result -> {
if (!result.isEmpty()) {
logger.info("{} exists in LDAP server.", role.getName());
} else {
final String entryDN = fessConfig.getLdapAdminRoleSecurityPrincipal(role.getName());
final BasicAttributes entry = new BasicAttributes();
addRoleAttributes(entry, role, fessConfig);
final Attribute oc = fessConfig.getLdapAdminRoleObjectClassAttribute();
entry.put(oc);
insert(entryDN, entry, adminEnv);
}
});
}
use of javax.naming.directory.BasicAttributes in project hive by apache.
the class LdapTestUtils method mockAttributes.
private static Attributes mockAttributes(NameValues... namedValues) throws NamingException {
Attributes attributes = new BasicAttributes();
for (NameValues namedValue : namedValues) {
Attribute attr = new BasicAttribute(namedValue.name);
for (String value : namedValue.values) {
attr.add(value);
}
attributes.put(attr);
}
return attributes;
}
use of javax.naming.directory.BasicAttributes in project hadoop by apache.
the class TestLdapGroupsMappingBase method setupMocksBase.
@Before
public void setupMocksBase() throws NamingException {
MockitoAnnotations.initMocks(this);
DirContext ctx = getContext();
doReturn(ctx).when(groupsMapping).getDirContext();
when(ctx.search(Mockito.anyString(), Mockito.anyString(), Mockito.any(Object[].class), Mockito.any(SearchControls.class))).thenReturn(userNames);
// We only ever call hasMoreElements once for the user NamingEnum, so
// we can just have one return value
when(userNames.hasMoreElements()).thenReturn(true);
SearchResult groupSearchResult = mock(SearchResult.class);
// We're going to have to define the loop here. We want two iterations,
// to get both the groups
when(groupNames.hasMoreElements()).thenReturn(true, true, false);
when(groupNames.nextElement()).thenReturn(groupSearchResult);
// Define the attribute for the name of the first group
Attribute group1Attr = new BasicAttribute("cn");
group1Attr.add(testGroups[0]);
Attributes group1Attrs = new BasicAttributes();
group1Attrs.put(group1Attr);
// Define the attribute for the name of the second group
Attribute group2Attr = new BasicAttribute("cn");
group2Attr.add(testGroups[1]);
Attributes group2Attrs = new BasicAttributes();
group2Attrs.put(group2Attr);
// This search result gets reused, so return group1, then group2
when(groupSearchResult.getAttributes()).thenReturn(group1Attrs, group2Attrs);
when(getUserNames().nextElement()).thenReturn(getUserSearchResult());
when(getUserSearchResult().getAttributes()).thenReturn(getAttributes());
// Define results for groups 1 level up
SearchResult parentGroupResult = mock(SearchResult.class);
// only one parent group
when(parentGroupNames.hasMoreElements()).thenReturn(true, false);
when(parentGroupNames.nextElement()).thenReturn(parentGroupResult);
// Define the attribute for the parent group
Attribute parentGroup1Attr = new BasicAttribute("cn");
parentGroup1Attr.add(testParentGroups[2]);
Attributes parentGroup1Attrs = new BasicAttributes();
parentGroup1Attrs.put(parentGroup1Attr);
// attach the attributes to the result
when(parentGroupResult.getAttributes()).thenReturn(parentGroup1Attrs);
when(parentGroupResult.getNameInNamespace()).thenReturn("CN=some_group,DC=test,DC=com");
}
use of javax.naming.directory.BasicAttributes in project camel by apache.
the class SpringLdapProducerTest method testBind.
@Test
public void testBind() throws Exception {
String dn = "some dn";
BasicAttributes attributes = new BasicAttributes();
Exchange exchange = new DefaultExchange(context);
Message in = new DefaultMessage();
Map<String, Object> body = new HashMap<String, Object>();
body.put(SpringLdapProducer.DN, dn);
body.put(SpringLdapProducer.ATTRIBUTES, attributes);
when(ldapEndpoint.getOperation()).thenReturn(LdapOperation.BIND);
processBody(exchange, in, body);
verify(ldapTemplate).bind(eq(dn), isNull(), eq(attributes));
}
Aggregations