use of javax.naming.directory.Attribute in project zeppelin by apache.
the class ActiveDirectoryGroupRealm method getRoleNamesForUser.
private Set<String> getRoleNamesForUser(String username, LdapContext ldapContext) throws NamingException {
Set<String> roleNames = new LinkedHashSet<>();
SearchControls searchCtls = new SearchControls();
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String userPrincipalName = username;
if (this.principalSuffix != null && userPrincipalName.indexOf('@') < 0) {
userPrincipalName += principalSuffix;
}
String searchFilter = "(&(objectClass=*)(userPrincipalName=" + userPrincipalName + "))";
Object[] searchArguments = new Object[] { userPrincipalName };
NamingEnumeration answer = ldapContext.search(searchBase, searchFilter, searchArguments, searchCtls);
while (answer.hasMoreElements()) {
SearchResult sr = (SearchResult) answer.next();
if (log.isDebugEnabled()) {
log.debug("Retrieving group names for user [" + sr.getName() + "]");
}
Attributes attrs = sr.getAttributes();
if (attrs != null) {
NamingEnumeration ae = attrs.getAll();
while (ae.hasMore()) {
Attribute attr = (Attribute) ae.next();
if (attr.getID().equals("memberOf")) {
Collection<String> groupNames = LdapUtils.getAllAttributeValues(attr);
if (log.isDebugEnabled()) {
log.debug("Groups found for user [" + username + "]: " + groupNames);
}
Collection<String> rolesForGroups = getRoleNamesForGroups(groupNames);
roleNames.addAll(rolesForGroups);
}
}
}
}
return roleNames;
}
use of javax.naming.directory.Attribute in project zeppelin by apache.
the class ActiveDirectoryGroupRealm method searchForUserName.
public List<String> searchForUserName(String containString, LdapContext ldapContext) throws NamingException {
List<String> userNameList = new ArrayList<>();
SearchControls searchCtls = new SearchControls();
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String searchFilter = "(&(objectClass=*)(userPrincipalName=*" + containString + "*))";
Object[] searchArguments = new Object[] { containString };
NamingEnumeration answer = ldapContext.search(searchBase, searchFilter, searchArguments, searchCtls);
while (answer.hasMoreElements()) {
SearchResult sr = (SearchResult) answer.next();
if (log.isDebugEnabled()) {
log.debug("Retrieving userprincipalname names for user [" + sr.getName() + "]");
}
Attributes attrs = sr.getAttributes();
if (attrs != null) {
NamingEnumeration ae = attrs.getAll();
while (ae.hasMore()) {
Attribute attr = (Attribute) ae.next();
if (attr.getID().toLowerCase().equals("cn")) {
userNameList.addAll(LdapUtils.getAllAttributeValues(attr));
}
}
}
}
return userNameList;
}
use of javax.naming.directory.Attribute 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.Attribute in project gocd by gocd.
the class LdapUserSearch method toUser.
private User toUser(BasicAttributes attributes) throws NamingException {
String fullName = attributes.get(COMMON_NAME).get().toString();
Attribute samAccName = attributes.get(SAM_ACCOUNT_NAME);
String loginName;
loginName = samAccName != null ? samAccName.get().toString() : attributes.get(UID).get().toString();
Attribute emailAttr = attributes.get(MAIL_ID);
String emailAddress = emailAttr != null ? emailAttr.get().toString() : "";
return new User(loginName, fullName, emailAddress);
}
use of javax.naming.directory.Attribute in project Smack by igniterealtime.
the class JavaxResolver method lookupSRVRecords0.
@Override
protected List<SRVRecord> lookupSRVRecords0(String name, List<HostAddress> failedAddresses, DnssecMode dnssecMode) {
List<SRVRecord> res = null;
Attribute srvAttribute;
try {
Attributes dnsLookup = dirContext.getAttributes(name, new String[] { "SRV" });
srvAttribute = dnsLookup.get("SRV");
if (srvAttribute == null)
return null;
} catch (NameNotFoundException e) {
LOGGER.log(Level.FINEST, "No DNS SRV RR found for " + name, e);
return null;
} catch (NamingException e) {
LOGGER.log(Level.WARNING, "Exception while resolving DNS SRV RR for " + name, e);
return null;
}
try {
@SuppressWarnings("unchecked") NamingEnumeration<String> srvRecords = (NamingEnumeration<String>) srvAttribute.getAll();
res = new ArrayList<>();
while (srvRecords.hasMore()) {
String srvRecordString = srvRecords.next();
String[] srvRecordEntries = srvRecordString.split(" ");
int priority = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 4]);
int port = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 2]);
int weight = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 3]);
String host = srvRecordEntries[srvRecordEntries.length - 1];
List<InetAddress> hostAddresses = lookupHostAddress0(host, failedAddresses, dnssecMode);
if (hostAddresses == null) {
continue;
}
SRVRecord srvRecord = new SRVRecord(host, port, priority, weight, hostAddresses);
res.add(srvRecord);
}
} catch (NamingException e) {
LOGGER.log(Level.SEVERE, "Exception while resolving DNS SRV RR for" + name, e);
}
return res;
}
Aggregations