/** * @param attrs * @return * @throws javax.naming.NamingException */ private User newUser(Attributes attrs) throws NamingException { User user = new User(); if (attrs.get(source.getUsersIdKey()) != null) { user.setUid(attrs.get(source.getUsersIdKey()).get().toString()); } if (attrs.get(source.getUsersDescriptionKey()) != null) { user.setName(attrs.get(source.getUsersDescriptionKey()).get().toString()); } if (attrs.get(source.getUsersMailKey()) != null) { user.setMail(attrs.get(source.getUsersMailKey()).get().toString()); } user.setAuthenticationSourceName(source.getName()); return user; }
public Map<String, String> getAttributes(User user) throws DataSourceException, ConfigException { try { String s = "(&(objectClass=" + source.getUsersObjectClassValue() + ")(" + source.getUsersIdKey() + "=" + user.getUid() + "))"; List<SearchResult> r = this.search(s, SecurityEntityType.USER); if (!r.isEmpty()) { Attributes attrs = r.get(0).getAttributes(); Map<String, String> items = new HashMap<String, String>(); NamingEnumeration<? extends Attribute> lst = attrs.getAll(); while (lst.hasMoreElements()) { Attribute attr = lst.nextElement(); if (attr.get() != null) { items.put(attr.getID(), attr.get().toString()); } } return items; } else { return null; } } catch (javax.naming.AuthenticationException e) { throw new ConfigException(e, "LDAP connection failed, please check your settings"); } catch (NamingException e) { throw new DataSourceException(e, "LDAP Exception : " + e.getMessage()); } }
public Object getAttribute(User user, String attributeName) throws DataSourceException, ConfigException { try { String s = "(&(objectClass=" + source.getUsersObjectClassValue() + ")(" + source.getUsersIdKey() + "=" + user.getUid() + "))"; List<SearchResult> r = this.search(s, SecurityEntityType.USER); if (!r.isEmpty()) { Attributes attrs = r.get(0).getAttributes(); return attrs.get(attributeName).get(); } else { return null; } } catch (javax.naming.AuthenticationException e) { throw new ConfigException(e, "LDAP connection failed, please check your settings"); } catch (NamingException e) { throw new DataSourceException(e, "LDAP Exception : " + e.getMessage()); } }