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

📄 authutil.java.svn-base

📁 一个timesheet程序,用来统计开发人员的度量衡web在线程序.用于软件行业
💻 SVN-BASE
字号:
package com.nsi.control.web.util;

import java.sql.Connection;
import java.sql.Statement;
import java.util.Date;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;

import javax.naming.Context;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.nsi.components.login.LoginUser;
import com.nsi.control.exceptions.NsiEventException;
import com.nsi.persistence.DataSrcUtil;
import com.nsi.persistence.IsqlDataSource;
import com.nsi.util.GetSeqsNumber;
import com.nsi.util.ValHelper;

/**
 * @author Chris Ye, created on Oct 7, 2008
 *
 * AuthUtil
 */
public final class AuthUtil
{
	private static Log log = LogFactory.getLog(AuthUtil.class);
	/**
	 * private constructor of AuthUtil, provent for instantiation
	 */
	private AuthUtil()
	{
	}
	private static class AuthUtilHolder
	{
		static final AuthUtil authUtil = new AuthUtil();
	}
	/**
	 * @return an instance of AuthUtil
	 */
	public static AuthUtil getInstance()
	{
		return AuthUtilHolder.authUtil;
	}
	public LoginUser autheticate(String userid, String pwd) throws NsiEventException
	{
		LoginUser user = new LoginUser();
		Map<String, String> result = new HashMap<String, String>();
		String sSql = 	"select a.loginname, a.password, a.userroleid, a.resourceid,a.usertype, b.firstname, b.lastname, a.bactive as actuser, b.bactive " + 
							"from t_user a, t_resource b " + 
							"where loginname ='" + userid + "' " + 
							"and a.resourceid = b.resourceid";
		IsqlDataSource src = DataSrcUtil.getInstance().getDataSource();
		Connection conn = null;
		try
		{
			conn = src.getConnection();
			result = src.retrieveSingleRow(conn, sSql);
		}
		catch(SQLException se)
		{
			log.error("autheticate() caught SQLException: " + se);
		}
		catch(Exception ex)
		{
			log.error("autheticate() caught Exception: " + ex);
		}
		finally
		{
			src.closeConn(conn);
		}
		if(!result.isEmpty())
		{
			String password = ValHelper.getInstance().getValue(result, "password");
			String userroleid = ValHelper.getInstance().getValue(result, "userroleid");
			String resourceid = ValHelper.getInstance().getValue(result, "resourceid");
			String firstname = ValHelper.getInstance().getValue(result, "firstname");
			String lastname = ValHelper.getInstance().getValue(result, "lastname");
			String bactive = ValHelper.getInstance().getValue(result, "bactive");
			String actuser = ValHelper.getInstance().getValue(result, "actuser");
			String usertype = ValHelper.getInstance().getValue(result, "usertype");
			if(password.trim().equals(pwd.trim()) && "T".equalsIgnoreCase(bactive.trim()) && "T".equalsIgnoreCase(actuser.trim())) 
			{
				user = new LoginUser(userid, password, resourceid, userroleid,usertype,lastname, firstname);
			}
		}
		return user;
	}
	public LoginUser searchUserId(String userid) throws NsiEventException
	{
		LoginUser user = new LoginUser();
		Map<String, String> result = new HashMap<String, String>();
		String sSql = 	"select a.loginname, a.password, a.userroleid, a.resourceid,a.usertype, b.firstname, b.lastname, a.bactive as actuser, b.bactive " + 
							"from t_user a, t_resource b " + 
							"where loginname ='" + userid + "' " + 
							"and a.resourceid = b.resourceid";
		IsqlDataSource src = DataSrcUtil.getInstance().getDataSource();
		Connection conn = null;
		try
		{
			conn = src.getConnection();
			result = src.retrieveSingleRow(conn, sSql);
		}
		catch(SQLException se)
		{
			log.error("autheticate() caught SQLException: " + se);
		}
		catch(Exception ex)
		{
			log.error("autheticate() caught Exception: " + ex);
		}
		finally
		{
			src.closeConn(conn);
		}
		if(!result.isEmpty())
		{
			String password = ValHelper.getInstance().getValue(result, "password");
			String userroleid = ValHelper.getInstance().getValue(result, "userroleid");
			String resourceid = ValHelper.getInstance().getValue(result, "resourceid");
			String firstname = ValHelper.getInstance().getValue(result, "firstname");
			String lastname = ValHelper.getInstance().getValue(result, "lastname");
			String bactive = ValHelper.getInstance().getValue(result, "bactive");
			String actuser = ValHelper.getInstance().getValue(result, "actuser");
			String usertype = ValHelper.getInstance().getValue(result, "usertype");			
				user = new LoginUser(userid, password, resourceid, userroleid,usertype,lastname, firstname);
		}
		return user;
	}
	public Map<String,String> getDeptrolemap(String resourceid) throws NsiEventException
	{
		Map<String,String> deptrolemap =  new HashMap<String,String>();
		List<Map<String,String>> result = new ArrayList<Map<String,String>>();
		String sSql = "select departmentid, deptroleid from t_res_position where resourceid =" + resourceid;
		IsqlDataSource src = DataSrcUtil.getInstance().getDataSource();
		Connection conn = null;
		try
		{
			conn = src.getConnection();
			result = src.executeRetrieve(conn, sSql);
		}
		catch(SQLException se)
		{
			log.error("getDeptrolemap() caught SQLException: " + se);
		}
		catch(Exception ex)
		{
			log.error("getDeptrolemap() caught Exception: " + ex);
		}
		finally
		{
			src.closeConn(conn);
		}
		if(!result.isEmpty())
		{
			int size = result.size();
			for (int i = 0; i < size; i++)
			{
				Map<String,String> resultmap = result.get(i);
				deptrolemap.put( ValHelper.getInstance().getValue(resultmap, "departmentid"), ValHelper.getInstance().getValue(resultmap, "deptroleid"));
			}
		}
		return deptrolemap;
	}
	public Map<String,String> getProjrolemap(String resourceid) throws NsiEventException
	{
		Map<String,String> projrolemap =  new HashMap<String,String>();
		List<Map<String,String>> result = new ArrayList<Map<String,String>>();
		String sSql = "select projectid, projroleid from t_proj_assign where resourceid =" + resourceid;
		IsqlDataSource src = DataSrcUtil.getInstance().getDataSource();
		Connection conn = null;
		try
		{
			conn = src.getConnection();
			result = src.executeRetrieve(conn, sSql);
		}
		catch(SQLException se)
		{
			log.error("getProjrolemap() caught SQLException: " + se);
		}
		catch(Exception ex)
		{
			log.error("getProjrolemap() caught Exception: " + ex);
		}
		finally
		{
			src.closeConn(conn);
		}
		if(!result.isEmpty())
		{
			int size = result.size();
			for (int i = 0; i < size; i++)
			{
				Map<String,String> resultmap = result.get(i);
				projrolemap.put( ValHelper.getInstance().getValue(resultmap, "projectid"), ValHelper.getInstance().getValue(resultmap, "projroleid"));
			}
		}
		return projrolemap;
	}
	public boolean domainAutheticate(String userid, String pwd)throws NsiEventException{
		boolean flag = false;		 
		String password = pwd; 	
	    String DN =userid+"@achievo.com";
	    Properties env = new Properties(); 
		env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory"); 
		env.put(Context.SECURITY_AUTHENTICATION, "simple"); 
		env.put(Context.SECURITY_PRINCIPAL,DN); 
		env.put(Context.SECURITY_CREDENTIALS, password); 
		env.put(Context.PROVIDER_URL, "LDAP://szsite.achievo.com:389/"); 
		DirContext ctx = null; 
		try {          
		ctx = new InitialDirContext(env);
		Attributes attr = ctx.getAttributes("CN=Configuration,DC=achievo,DC=com");
		ctx.close();
		flag=true;
		System.out.println("Autheticate Successful");
		} catch (javax.naming.AuthenticationException e) { 
		flag=false; 		
		System.out.println("Autheticate failure"); 
		} catch (Exception e) { 
		flag=false;
		System.out.println("Autheticate Error"); 
		} 
		
		return flag;
	}
	public void createUser(String userid) throws Exception{
		String username=firstToUpperCase(userid);
		StringTokenizer    t=new    StringTokenizer(username,".");
		String firstname=t.nextToken();
		String lastname = t.nextToken();
		String email = userid+"@achievo.com";
		String resourceid =GetSeqsNumber.getInstance().getSeqsNumber("resourceid_seq");
		String usertype="E";
		String bActive ="T";
		String moduserid="9999";
		String empltypeid="3";
		String userroleid="2";
		String agencyid="0";
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");   
		String createdate = format.format(new Date());
		 String sSql = 
		"insert into t_resource( resourceid,createdate,moduserid,lastname,firstname,email,empltypeid,bactive,startdate,agencyid) " +
		"values('"+resourceid+"','"+createdate+"','"+moduserid+"','"+lastname+"','"+firstname+"','"+email+"','"+empltypeid+"', " +
		"'"+bActive+"','"+createdate+"','"+agencyid+"') "+";"+	 
		"insert into t_user ( userid, loginname, usertype, createdate,bactive, resourceid, userroleid, moduserid ) " + 
		"values ( nextval('userid_seq'), '" + userid + "', " + "'" +usertype+ "', " + "'" +createdate+ "', " + 
		"'" + bActive + "', '" + resourceid + "', '" + userroleid + "', '" + moduserid + "' )"+";";
		IsqlDataSource src = DataSrcUtil.getInstance().getDataSource();
		Connection conn = null;
		try
		{   conn=src.getConnection();
			Statement stmt = conn.createStatement();
			int resultCount = src.executeUpdate(stmt, sSql);
			if(resultCount != 1)
			{
				throw new NsiEventException("ERROR insert resource into t_resource!!  resultCount = " + resultCount);

			}
		}
		catch(SQLException se)
		{
			log.error("createUser() caught SQLException: " + se);
		}
		catch(Exception ex)
		{
			log.error("createUser() caught Exception: " + ex);
		}
		finally
		{
			src.closeConn(conn);
		}
	}
	public String firstToUpperCase(String userid){
		StringTokenizer    t=new    StringTokenizer(userid,".");
		String firstname=t.nextToken();		
		String lastname = t.nextToken();		
		userid=toUpperCaseFirstLetter(firstname)+"."+toUpperCaseFirstLetter(lastname);
		return userid; 
	}
	public  String toUpperCaseFirstLetter(String s) {   
		  
        return isNullStr(s)   
  
            ? s   
  
            : s.substring(0, 1).toUpperCase() + s.substring(1);   
  
    }   
	public  boolean isNullStr(String s) {   
		  
        return (s == null || s.equals("null") || s.equals("")) ? true : false;   
  
    }
	
}

⌨️ 快捷键说明

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