⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ldapdao.java

📁 发泄网! 发泄网! 发泄网! 发泄网! 发泄网! 发泄网! 发泄网! 发泄网!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	/* CRMS, customer relationship management system	Copyright (C) 2003  Service To Youth Council	This program is free software; you can redistribute it and/or modify	it under the terms of the GNU General Public License as published by	the Free Software Foundation; either version 2 of the License, or	(at your option) any later version.	This program is distributed in the hope that it will be useful,	but WITHOUT ANY WARRANTY; without even the implied warranty of	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the	GNU General Public License for more details.	You should have received a copy of the GNU General Public License	along with this program; if not, write to the Free Software	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA	For further information contact the SYC ICT department on GPL@syc.net.au	98 Kermode Street	North Adelaide	South Australia	SA 5006 	+61 (0)8 8367 0755	*//* * LDAPDAO.java * * Created on 17 April 2003, 09:29 */package crms.dao;import javax.naming.*;import javax.naming.directory.*;import java.util.*;import crms.vo.*;import java.io.*;import java.beans.*;import org.apache.log4j.Logger;import crms.util.*;/** * * @author  dmurphy */public class LDAPDAO {		Logger logger = Logger.getLogger(LDAPDAO.class);		Hashtable env = new Hashtable(11);	private String LDAPURL = null;	private String LDAPBASEDN = null;	private String LDAPUSEROU = null;		//public static String LDAP_URL = "ldap://ldap.syc.net.au:389/o=SYC,c=au";		/** Creates a new instance of LDAPDAO */	protected LDAPDAO(String url, String basedn, String userou) {		this.LDAPURL = url;	this.LDAPBASEDN = basedn;	this.LDAPUSEROU = userou;				// 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");	}			private void setLDAPURL(String url) {		this.LDAPURL = url;		}		public String getLDAPURL() {		return LDAPURL;   	}	/** 	 * Remember to call ctx.close() to return the connection to the pool	 */		public DirContext getDirContext() throws NamingException {		// Create one initial context (Get connection from pool)		DirContext ctx = new InitialDirContext(env);		return ctx;	}	/**	 * Search the LDAP tree and find a user.	 *	 * @param userId	The user id to search for.	 * @return			An initialised StaffMember object if found or null if not.	 */		public StaffMember getUser(String userId) {				StaffMember sm = null;				/*if (userId.equals("everyone")) {			return StaffMember.ALL_STAFF;		} */				try {						DirContext ctx = getDirContext();			String search="uid=" + userId + ", ou=Staff";						logger.debug( "Performing LDAP lookup for '" + search + "'" );						Attributes attr= ctx.getAttributes(search);			sm = loadDetailsFromAttributes(attr);			ctx.close();			return sm;		}		catch (NamingException ex) {			//ex.printStackTrace();			return null;		}			}			public Group getGroup(String groupID) {				Group group = null;				try {						DirContext ctx = getDirContext();			String search="cn=" + groupID + ", ou=Groups";						logger.debug( "Performing LDAP lookup for '" + search + "'" );						Attributes attr= ctx.getAttributes(search);			group = new Group();			group.setNumericID(Integer.parseInt((String)attr.get("gidNumber").get()));			group.setName((String) attr.get("description").get());			group.setID((String) attr.get("cn").get());						ctx.close();			return group;		}		catch (NamingException ex) {			ex.printStackTrace();			throw new RuntimeException(ex);		}			}	public List searchStaff(StaffMember criteria) {		ArrayList staff = new ArrayList();		logger.debug( "Entering staffSearch()" );				try {			DirContext ctx = getDirContext();//			Attributes matchAttrs = new BasicAttributes(true); // ignore attribute name case//			matchAttrs.put(new BasicAttribute("profilePath",true));			//			matchAttrs.put(new BasicAttribute("departmentNumber",true));//			matchAttrs.put(new BasicAttribute("uid"));			//			if (criteria.getFirstName() != null) {//				matchAttrs.put(new BasicAttribute("givenName", criteria.getFirstName(),false));//			}//			//			if (criteria.getLastName() != null) {//				matchAttrs.put(new BasicAttribute("sn", criteria.getLastName(),false));//			}//			StringBuffer search = new StringBuffer();			// limit to posixAccount staff			search.append("(& (objectClass=posixAccount) ");			//search.append("(departmentNumber=*) ");			if (criteria.getUID() != null) {				search.append("(uid=" + criteria.getUID() + "*) ");			}			if (criteria.getFirstName() != null) {				search.append("(givenName=" + criteria.getFirstName() + "*) ");			}			if (criteria.getLastName() != null) {				search.append("(sn="+criteria.getLastName() + "*)");			}			if (criteria.getDepartment() != null) {				search.append("(departmentNumber=" + criteria.getDepartment() + ") ");			}			if (criteria.getLocation() != null) {				search.append("(physicalDeliveryOfficeName=" + criteria.getLocation() + ") ");			}			search.append(")");						// Specify the ids of the attributes to return			String[] attrIDs = {"cn", "uid", "departmentNumber","physicalDeliveryOfficeName", "sn", "givenName","title", "mobile", "mail" };						SearchControls ctls = new SearchControls();			ctls.setReturningAttributes(attrIDs);// Specify the ids of the attributes to return			logger.debug( "Performing staffSearch() LDAP lookup for '" + search.toString() + "'" );			NamingEnumeration answer = ctx.search("ou=Staff", search.toString(), ctls);			while (answer.hasMore()) {				SearchResult sr = (SearchResult) answer.next();				//System.out.println(sr.getName());				//printAttributes(sr.getAttributes());								Attributes attr = sr.getAttributes();								StaffMember sm = loadDetailsFromAttributes(attr);								staff.add(sm);			}			ctx.close();						Collections.sort(staff, StaffMember.USERS_SORT_ORDER);			return staff;		}		catch (NamingException ex) {			ex.printStackTrace();			throw new RuntimeException(ex);		}			}		public String getPrimaryGroup(String user) {	 		try {			DirContext ctx = getDirContext();						String search="uid=" + user + ", ou=Staff";						logger.debug( "Performing LDAP lookup for '" + search + "'" );						Attributes attr= ctx.getAttributes(search);						String groupID = (String) attr.get("gidNumber").get();						search="gidNumber=" + groupID;						// Specify the ids of the attributes to return			String[] attrIDs = {"cn"};						SearchControls ctls = new SearchControls();			ctls.setReturningAttributes(attrIDs);// Specify the ids of the attributes to return						NamingEnumeration answer = ctx.search("ou=Groups", search.toString(), ctls);			String groupName = null;						// User can have only one primary group (I'm assuming!)						if (answer.hasMore()) {				SearchResult sr = (SearchResult) answer.next();				//System.out.println(sr.getName());				//printAttributes(sr.getAttributes());				attr = sr.getAttributes();				groupName = (String) attr.get("cn").get();							}			ctx.close();						return groupName;		}		catch (NamingException ex) {			ex.printStackTrace();			throw new RuntimeException(ex);		}			}		public boolean isUserInGroup(String user, String groupID) {				ArrayList groups = (ArrayList) getGroupMembership(user);   				for (int i=0; i < groups.size(); i++) {			String gp = (String) groups.get(i);   			// TN: gp may be null if the group definitions are wrong			// for instance the posixUser's gid may be set to a non-existant posixGroup			if (gp != null && gp.equals(groupID)) {				return true;   			}		}		return false;	}		public List getGroupMembership(String user) {		ArrayList groups = new ArrayList();		groups.add(getPrimaryGroup(user));				try {			DirContext ctx = getDirContext();			StringBuffer search = new StringBuffer();			search.append("(memberUid=" + user + ") ");			// Specify the ids of the attributes to return			String[] attrIDs = {"cn" };						SearchControls ctls = new SearchControls();			ctls.setReturningAttributes(attrIDs);// Specify the ids of the attributes to return			NamingEnumeration answer = ctx.search("ou=Groups", search.toString(), ctls);			while (answer.hasMore()) {				SearchResult sr = (SearchResult) answer.next();				//System.out.println(sr.getName());				//printAttributes(sr.getAttributes());				Attributes attr = sr.getAttributes();				String groupName = (String) attr.get("cn").get();				if (!groups.contains(groupName)) {					groups.add(groupName);				}							}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -