/**
* 魏朝东 清华同方 Oct. 26 2001 *
* permission Package Version 1.0 *
**/
package permission;
import java.sql.* ;
import java.util.*;
public class Pass {
public HashSet hsAuth=new HashSet();
//散列表hsAuth为用户拥有的权限模块名
private String Realname=""; //真实姓名
private String Username=""; //用户名
private String Password=""; //密码
private int Department_id,Personnel_id; //用户角色id,个人id
int auth=0; //密码正确,则Auth=1,否则为0
int num = 0; //登录次数 最多为三次
public void setPass (String pusername,String ppasswd,String IPAddr,String Sessionid) throws ClassNotFoundException,SQLException
// 参数 (用户名,密码,IP地址,session产生的ID)
{
PreparedStatement prepare; //创建PerparedStatement实例
ResultSet rs; //创建记录集实例
String sql; //要执行的sql语句
DBConnect dbc = new DBConnect();
//创建DBConnect实例,用于数据库连接
Connection conn=dbc.getConn();//建立数据库连接
sql = "select * from manager where username = ?";
//在操作员信息表查询该用户是否为系统操作员
prepare = conn.prepareStatement(sql);
prepare.setString(1,pusername);
rs = prepare.executeQuery();//执行SQL语句
if(rs.next())
{
if(ppasswd.equals(rs.getString("passwd")))
//如果密码与用户输入的密码相等
{
Username = pusername;
Password = ppasswd;
Department_id = rs.getInt("department_id"); // 角色ID
Personnel_id = rs.getInt("personnel_id"); // 帐号ID
Realname = rs.getString("realname"); //真实姓名
num = 0;
auth=1; //密码正确,则Auth=1,成功登录
sql = "insert into log (username,ipaddr,regtime,success) VALUES (?,?,now(),1)";//在日志信息表中添加信息
prepare = conn.prepareStatement(sql);
prepare.setString(1,Username);
prepare.setString(2,IPAddr);
}
else//如果密码错误
{
sql = "insert into log (username,ipaddr,regtime,success,sessionid,errPass) values (?,?,now(),0,?,?)";
//在日志信息表中添加信息
prepare = conn.prepareStatement(sql);
prepare.setString(1,pusername);
prepare.setString(2,IPAddr);
prepare.setString(3,Sessionid);
prepare.setString(4,ppasswd);
num ++;
auth = 2; //密码错误 auth = 2;
}
prepare.executeUpdate(); //执行日志update语句
}
else
{
num ++;
auth=0;//用户名及密码错误auth=0
}
if(auth == 1)//读取操作权限,加到HASHTABLE中
{
sql = "select * from authority where department = ?";
//在权限信息表中读取相应角色的权限值
prepare = conn.prepareStatement(sql);
prepare.setInt(1,Department_id);
rs = prepare.executeQuery();
hsAuth.clear();//先清空散列表
for(int i=0;rs.next();i++)
{
hsAuth.add(rs.getString("spec"));//spec为操作权限关键字
}
}
if(num >=3){ //是否连续三次输错密码
return;}
rs.close();
}
public HashSet getAuth()//返回权限散列表
{
return (hsAuth);
}
public int getNum()//得到密码错误次数
{
return (num);
}
public String getDepartment_id()//返回角色类型
{
return(String.valueOf(Department_id));
}
public int getAuth(String authname)//判断用户是否有authname模块权限
{
if (hsAuth.contains(authname))
{
return(1);
}
else
return(0);
}
public int getPass(){//密码是否正确
return(auth);
}
public String getUsername()//返回用户名
{
return(Username);
}
public String getPassword()//返回密码
{
return(Password);
}
public void logout()//退出系统时的数据处理
{
//以下是清空登录数据
Realname=null;
Username=null;
Password=null;
Department_id = 0 ;
num = 0;
auth=0;
}
}