📄 ldapdao.java
字号:
ctx.close(); return groups; } catch (NamingException ex) { ex.printStackTrace(); throw new RuntimeException(ex); } } /** List the UID's in the system. This function is similar to listUsers() but is designed to have less overhead * by only returning the UID's as strings. * * @return A list of User ID's from LDAP */ public List listUIDs() { ArrayList users = new ArrayList(); try { DirContext ctx = getDirContext(); Attributes matchAttrs = new BasicAttributes(true); // ignore attribute name case matchAttrs.put(new BasicAttribute("uid")); String[] attrIDs = {"uid", "mail"}; NamingEnumeration answer = ctx.search("ou=Staff", matchAttrs, attrIDs); while (answer.hasMore()) { SearchResult sr = (SearchResult) answer.next(); Attributes attr = sr.getAttributes(); users.add(attr.get("uid").get()); // logger.debug("Found UID: " + attr.get("uid").get()); } ctx.close(); // Collections.sort(users, StaffMember.USERS_SORT_ORDER); return users; } catch (NamingException ex) { ex.printStackTrace(); throw new RuntimeException(ex); } } protected StaffMember loadDetailsFromAttributes(Attributes attr) throws NamingException { StaffMember sm = new StaffMember(); sm.setFirstName((String) attr.get("givenName").get()); sm.setLastName((String) attr.get("sn").get()); sm.setDepartment((String) attr.get("departmentNumber").get()); sm.setUID((String) attr.get("uid").get()); sm.setJobTitle((String) attr.get("title").get()); Attribute mobile = attr.get("mobile"); if (mobile != null && mobile.size() > 0) { sm.setMobile((String) mobile.get()); } Attribute email = attr.get("mail"); if (email != null && email.size() > 0) { sm.setEmail((String) email.get()); } //logger.debug("Can SMS - mobile: " + sm.getMobile() + " ingroup: " + isUserInGroup(sm.getUID(), "crmssms") ); sm.setCanSMS( (isUserInGroup(sm.getUID(), "crmssms") == true) && (sm.getMobile() != null) ); Attribute location = attr.get("physicalDeliveryOfficeName"); if (location != null) { sm.setLocation((String)location.get()); } // employeeNumber = power level of user within CRMS Attribute power = attr.get("employeeNumber"); if (power != null) { sm.setPower(new Integer( (String)power.get() ).intValue()); } return sm; } /** Search the LDAP database to enumerate all users. * * @return An ArrayList of StaffMember objects */ public List listUsers() { ArrayList users = new ArrayList(); try { DirContext ctx = getDirContext(); Attributes matchAttrs = new BasicAttributes(true); // ignore attribute name case matchAttrs.put(new BasicAttribute("departmentNumber",true)); matchAttrs.put(new BasicAttribute("uid")); // Specify the ids of the attributes to return String[] attrIDs = {"cn", "uid", "departmentNumber", "sn", "givenName","title", "mobile", "mail", "physicalDeliveryOfficeName" }; NamingEnumeration answer = ctx.search("ou=Staff", matchAttrs,attrIDs); while (answer.hasMore()) { SearchResult sr = (SearchResult) answer.next(); //System.out.println(sr.getName()); //printAttributes(sr.getAttributes()); Attributes attr = sr.getAttributes(); StaffMember sm = loadDetailsFromAttributes(attr); users.add(sm); } ctx.close(); Collections.sort(users, StaffMember.USERS_SORT_ORDER); return users; } catch (NamingException ex) { ex.printStackTrace(); throw new RuntimeException(ex); } } public List getLocations() { ArrayList locations = new ArrayList(); try { DirContext ctx = getDirContext(); Attributes matchAttrs = new BasicAttributes(true); // ignore attribute name case// matchAttrs.put(new BasicAttribute("scriptPath",true)); // matchAttrs.put(new BasicAttribute("departmentNumber",true));// matchAttrs.put(new BasicAttribute("uid")); // Specify the ids of the attributes to return String[] attrIDs = { "street", "l", "postalCode", "st", "telephoneNumber", "facsimileTelephoneNumber", "physicalDeliveryOfficeName", "ou" }; NamingEnumeration answer = ctx.search("ou=Sites, ou=Lookups", matchAttrs, attrIDs); while (answer.hasMore()) { SearchResult sr = (SearchResult) answer.next(); //System.out.println(sr.getName()); //printAttributes(sr.getAttributes()); Attributes attr = sr.getAttributes(); Site site = new Site(); site.setStreet((String) attr.get("street").get()); site.setSuburb((String) attr.get("l").get()); site.setPostCode((String) attr.get("postalCode").get()); site.setState((String) attr.get("st").get()); site.setPhone((String) attr.get("telephoneNumber").get()); site.setFax((String) attr.get("facsimileTelephoneNumber").get()); site.setSiteName((String) attr.get("physicalDeliveryOfficeName").get()); site.setSiteCode((String) attr.get("ou").get()); locations.add(site); } Collections.sort(locations, Site.SITE_SORT_ORDER); ctx.close(); return locations; } catch (NamingException ex) { ex.printStackTrace(); throw new RuntimeException(ex); } } public List getDepartments() { ArrayList departments = new ArrayList(); try { DirContext ctx = getDirContext(); Attributes matchAttrs = new BasicAttributes(true); // ignore attribute name case// matchAttrs.put(new BasicAttribute("scriptPath",true)); // matchAttrs.put(new BasicAttribute("departmentNumber",true));// matchAttrs.put(new BasicAttribute("uid")); // Specify the ids of the attributes to return String[] attrIDs = { "physicalDeliveryOfficeName", "ou", "seeAlso" }; NamingEnumeration answer = ctx.search("ou=Departments, ou=Lookups", matchAttrs, attrIDs); while (answer.hasMore()) { SearchResult sr = (SearchResult) answer.next(); //System.out.println(sr.getName()); //printAttributes(sr.getAttributes()); Attributes attr = sr.getAttributes(); Department dept = new Department(); dept.setCode((String)attr.get("ou").get()); dept.setName((String)attr.get("physicalDeliveryOfficeName").get()); if (attr.get("seeAlso") != null) { dept.setManager((String)attr.get("seeAlso").get()); } departments.add(dept); } Collections.sort(departments, Department.DEPARTMENT_SORT_ORDER); ctx.close(); return departments; } catch (NamingException ex) { ex.printStackTrace(); throw new RuntimeException(ex); } } public List getGroups() { ArrayList groups = new ArrayList(); try { DirContext ctx = getDirContext(); Attributes matchAttrs = new BasicAttributes(true); // ignore attribute name case// matchAttrs.put(new BasicAttribute("scriptPath",true)); // matchAttrs.put(new BasicAttribute("departmentNumber",true));// matchAttrs.put(new BasicAttribute("uid")); // Specify the ids of the attributes to return String[] attrIDs = { "cn", "gidNumber", "description" }; NamingEnumeration answer = ctx.search("ou=Groups", matchAttrs, attrIDs); while (answer.hasMore()) { SearchResult sr = (SearchResult) answer.next(); //System.out.println(sr.getName()); //printAttributes(sr.getAttributes()); Attributes attr = sr.getAttributes(); Group gp = new Group(); gp.setID((String) attr.get("cn").get()); gp.setName((String) attr.get("description").get()); gp.setNumericID(Integer.parseInt((String) attr.get("gidNumber").get())); groups.add(gp); } Collections.sort(groups, Group.GROUP_NAME_ORDER); ctx.close(); return groups; } catch (NamingException ex) { ex.printStackTrace(); throw new RuntimeException(ex); } } public static void printAttributes(Attributes answer) throws NamingException { for (NamingEnumeration ae = answer.getAll(); ae.hasMore();) { Attribute attr = (Attribute)ae.next(); System.out.println("attribute: " + attr.getID()); /* Print each value */ for (NamingEnumeration e = attr.getAll(); e.hasMore(); System.out.println("value: " + e.next())); } } /** * This method allows authentication of a CRMS user by attempting to * bind to the LDAP directory with the supplied username and password. * An attempt is made to create a Directory Context with the supplied * credentials. A successful context creation assures us that the user * has provided their valid login details. * * @param id User name for CRMS user. * @param password CRMS User's password * @return Boolean indicating success of login attempt. */ public boolean autenticateUser(String id, String password) { try { logger.debug("Authenticating user: " + id); Hashtable env = new Hashtable(); // Set up environment for creating initial context env = new Hashtable(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, LDAPURL); // Enable connection pooling env.put("com.sun.jndi.ldap.connect.pool", "true"); logger.debug("User dn:uid=" + id + "," + LDAPUSEROU + "," + LDAPBASEDN); env.put(Context.SECURITY_PRINCIPAL, "uid=" + id + "," + LDAPUSEROU + "," + LDAPBASEDN); env.put(Context.SECURITY_CREDENTIALS, password); DirContext ctx = new InitialDirContext(env); ctx.close(); } catch (NamingException ex) { logger.debug("Exception while authenticating user.",ex); logger.debug("User authentication failed!"); return false; } logger.debug("User authenticated."); return true; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -