📄 passwordsfile.java
字号:
package jp.co.sjts.gsession.main;
import java.io.IOException;
import java.io.File;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Collections;
import java.util.StringTokenizer;
import java.util.ArrayList;
import jp.co.sjts.gsession.tools.*;
public class PasswordsFile extends GSTextFile implements Serializable
{
public final static String FILENAME = "password";
private File fPassword = null;
private Map PassMap = null; // KEY丗UID(Integer) VALUE丗(Password)
private int uidmax = 0; // 儐乕僓ID惗惉梡
/**
* 僐儞僗僩儔僋僞
*/
public PasswordsFile(String DataDir) throws GSException
{
fPassword = new File(DataDir+FILENAME);
PassMap = Collections.synchronizedMap(new HashMap());
if(!fPassword.exists())
{// 僼傽僀儖偑側偄応崌偼丄僨僼僅儖僩抣偱嶌惉
// 僨僼僅儖僩偱娗棟幰嶌惉
uidmax = 0;
String mdpass = Password.encode("admin".getBytes());
GSDate date = new GSDate();
String datebuf = date.getstrYear()+date.getstrMonth()+date.getstrDay()
+date.getstrHour()+date.getstrMinute()+date.getstrSecond();
Password Pass = new Password("admin",mdpass,"0","0","娗棟幰",datebuf);
PassMap.put(new Integer(0), Pass);
try {
Save();
} catch(GSException e) {
throw new GSException("\"password\"僼傽僀儖偺嶌惉偵幐攕!!");
}
}
}
public synchronized IPassword.GroupAuth[] GetGroupAuthArray(int uid){
Password pass;
synchronized(PassMap) {
pass = (Password)PassMap.get(new Integer(uid));
}
return pass.GetGroupAuthArray();
}
protected File GetFile() { return fPassword; }
protected synchronized void Read(BufferedReader bf) throws GSException
{
synchronized(PassMap) {
try{
PassMap.clear();
uidmax = Integer.parseInt(bf.readLine());
String buf;
for(buf=bf.readLine();buf!=null;buf=bf.readLine())
{
StringTokenizer st = new StringTokenizer(buf,",",true);
String[] tokens=new String[6];
int i=0;
boolean flg=false;
while(st.hasMoreTokens()){
String token = st.nextToken();
if(token.equals(",")){
if(flg){
tokens[i]="";
flg=!flg;
i++;
}else{
flg=!flg;
}
}else{
tokens[i]=token;
flg=false;
i++;
}
}
String user = tokens[0];
String mdpass = tokens[1];
String uid = tokens[2];
String gids = tokens[3];
String name = GSTool.delimitDecorde(",",tokens[4]);
String lastlogintime = tokens[5];
Password pass = new Password(user,mdpass,uid,gids,name,lastlogintime);
PassMap.put(new Integer(uid),pass);
}
}catch(IOException e){
throw new GSException("撉崬傒僄儔乕",e);
}
}
}
protected synchronized void Write(PrintWriter pw) throws GSException
{
synchronized(PassMap){
try{
pw.println(Integer.toString(uidmax));
Iterator it = PassMap.values().iterator();
while(it.hasNext()){
Password pass = (Password)it.next();
pw.println(pass.toString());
}
}catch(Exception e){
throw new GSException("彂崬傒僄儔乕僄儔乕",e);
}
}
}
/**
* 儐乕僓幆暿偐傜Password僆僽僕僃僋僩傪庢摼
* @param User 儐乕僓幆暿
* @return 奩摉偡傞Password僆僽僕僃僋僩丄奩摉偟側偄応崌偼(null)傪曉偡
*/
public synchronized Password UserToPassword(String User) throws GSException
{
synchronized(PassMap) {
Password pass = null;
for(Iterator it = PassMap.values().iterator();it.hasNext();)
{
pass = (Password)it.next();
if(pass.GetUser().equals(User))
{
try {
return (Password)pass.clone();
} catch(CloneNotSupportedException e) {
throw new GSException(e.toString());
}
}
}
return null;
}
}
/**
* 儐乕僓ID偐傜Password僆僽僕僃僋僩傪庢摼
* @param uid 儐乕僓ID
* @return 奩摉偡傞Password僆僽僕僃僋僩丄奩摉偟側偄応崌偼(null)傪曉偡
*/
public synchronized Password UidToPassword(int uid) throws GSException
{
Password pass=null;
synchronized(PassMap) {
pass = (Password)PassMap.get(new Integer(uid));
}
return pass;
}
/**
* 儐乕僓ID偐傜僷僗儚乕僪傪庢摼
* @param uid 儐乕僓ID
* @return 奩摉偡傞僷僗儚乕僪
*/
public synchronized String UidToMDPass(int uid)
{
Password pass=null;
synchronized(PassMap) {
pass = (Password)PassMap.get(new Integer(uid));
}
if(pass!=null)
return pass.GetMDPass();
else
return "";
}
/**
* 儐乕僓ID偐傜儐乕僓柤傪曉偡
* @param uid 儐乕僓ID
* @return 儐乕僓柤
*/
public synchronized String UidToUserName(int uid)
{
synchronized(PassMap){
Password pass = (Password)PassMap.get(new Integer(uid));
if(pass==null)
return null;
return pass.GetName();
}
}
/**
* 儐乕僓ID偐傜嵟廔儘僌僀儞帪娫傪曉偡
* @param uid 儐乕僓ID
* @return YYYYMMDDHHmmss
*/
public synchronized String LastLoginTimeToUID(int uid)
{
synchronized(PassMap){
return ((Password)PassMap.get(new Integer(uid))).GetLastLoginTime();
}
}
/**
* 儐乕僓ID偺嵟廔儘僌僀儞帪娫傪愝掕偡傞
* @param uid 儐乕僓ID
* @param time YYYYMMDDHHmmss
*/
public synchronized void SetLastLoginTime(int uid,String time)
{
synchronized(PassMap){
((Password)PassMap.get(new Integer(uid))).SetLastLoginTime(time);
}
}
/**
* 儐乕僓偺捛壛
* @param logonID 儘僌僆儞偵巊梡偡傞儐乕僓ID
* @param exPasswd 埫崋嵪傒僷僗儚乕僪
* @param userName 搊榐偡傞儐乕僓偺柤慜
* @param gids 搊榐偡傞儐乕僓偑強懏偡傞僌儖乕僾偺GID攝楍
* @return 惗惉偝傟偨儐乕僓ID
*/
public synchronized int addUser(String logonID,String exPasswd,String userName,int[] gids)
throws GSException
{
GSDate date = new GSDate();
uidmax++;
Password passwd = new Password(logonID,exPasswd,uidmax,gids,userName,date.getYMDHMS());
synchronized(PassMap){
PassMap.put(new Integer(uidmax),passwd);
}
return uidmax;
}
/**
* 儐乕僓偺曇廤
* @param uid 儐乕僓ID
* @param logonID 儘僌僆儞偵巊梡偡傞儐乕僓ID
* @param exPasswd 埫崋嵪傒僷僗儚乕僪
* @param userName 搊榐偡傞儐乕僓偺柤慜
* @param gids 搊榐偡傞儐乕僓偑強懏偡傞僌儖乕僾偺GID攝楍
*/
public synchronized int[] editUser(int uid,String logonID,String exPasswd,String userName,int[] gids)
throws GSException
{
ArrayList dellist=null;
Password passwd=null;
synchronized(PassMap){
passwd=(Password)PassMap.get(new Integer(uid));
if(passwd==null)
return null;
passwd.SetUser(logonID);
passwd.SetMDPass(exPasswd);
passwd.SetName(userName);
int[] srcgids = passwd.getGroups();
if((srcgids!=null)&&(gids!=null))
{
dellist = new ArrayList(srcgids.length);
boolean flg=false;
for(int i=0;i<srcgids.length;i++){
for(int j=0;j<gids.length;j++){
if(srcgids[i]==gids[j])
flg=true;
}
if(!flg){
passwd.DelGroup(srcgids[i]);
dellist.add(new Integer(srcgids[i]));
}
flg = false;
}
for(int i=0;i<gids.length;i++){
if(!passwd.existsGroup(gids[i]))
passwd.AddGroup(gids[i],IPassword.AUTH_NORMAL);
}
}else if((srcgids!=null)&&(gids==null)){
dellist = new ArrayList(srcgids.length);
for(int i=0;i<srcgids.length;i++){
passwd.DelGroup(srcgids[i]);
dellist.add(new Integer(srcgids[i]));
}
}else if((srcgids==null)&&(gids!=null)){
for(int i=0;i<gids.length;i++)
passwd.AddGroup(gids[i],IPassword.AUTH_NORMAL);
}
}
if((dellist!=null)&&(dellist.size()>0)){
int[] ret = new int[dellist.size()];
Iterator it = dellist.iterator();
for(int i=0;it.hasNext();i++)
ret[i] = ((Integer)it.next()).intValue();
return ret;
}
return null;
}
/**
* 儐乕僓偺嶍彍
* @param uid 嶍彍偡傞儐乕僓偺儐乕僓ID
* @return 強懏偟偰偄偨僌儖乕僾偺僌儖乕僾ID攝楍
*/
public synchronized int[] delUser(int uid)
{
synchronized(PassMap){
Password pass = (Password)PassMap.remove(new Integer(uid));
if(pass==null)
return null;
return pass.getGroups();
}
}
/**
* 娗棟幰尃尷傪帩偭偰偄傞偐
* @param gid 懳徾偺僌儖乕僾ID
* @param uid 懳徾偺儐乕僓ID
* @return 娗棟幰尃尷傪帩偭偰偄傞帪丗true丂偦傟埲奜:false
*/
public synchronized boolean isAdmin(int gid,int uid)
{
Password pass;
synchronized(PassMap){
pass = (Password)PassMap.get(new Integer(uid));
if(pass==null)
return false;
IPassword.GroupAuth auth = pass.GetGroupAuth(gid);
if(auth==null)
return false;
if(auth.Auth==IPassword.AUTH_ADMIN)
return true;
}
return false;
}
/**
* 娗棟幰(僗乕僷乕儐乕僓)尃尷傪帩偭偰偄傞偐
* @param uid 懳徾偺儐乕僓ID
* @return 娗棟幰尃尷傪帩偭偰偄傞帪丗true丂偦傟埲奜丗false
*/
public synchronized boolean isSU(int uid)
{
Password pass;
synchronized(PassMap){
pass = (Password)PassMap.get(new Integer(uid));
if(pass==null)
return false;
if(pass.GetGroupAuth(0)!=null)
return true;
}
return false;
}
/**
* 娗棟幰尃尷傪梌偊傞
* @param gid 懳徾偺僌儖乕僾ID
* @param uid 懳徾偺儐乕僓ID
*/
public synchronized void setAdmin(int gid,int uid)
{
Password pass;
synchronized(PassMap){
pass = (Password)PassMap.get(new Integer(uid));
if(pass==null)
return;
IPassword.GroupAuth auth = pass.GetGroupAuth(gid);
auth.Auth = IPassword.AUTH_ADMIN;
}
}
/**
* 僲乕儅儖尃尷偵偡傞
* @param gid 懳徾偺僌儖乕僾ID
* @param uid 懳徾偺儐乕僓ID
*/
public synchronized void setNormal(int gid,int uid)
{
Password pass;
synchronized(PassMap){
pass = (Password)PassMap.get(new Integer(uid));
if(pass==null)
return;
IPassword.GroupAuth auth = pass.GetGroupAuth(gid);
auth.Auth = IPassword.AUTH_NORMAL;
}
}
/**
* 強懏僌儖乕僾偺嶍彍
* @param uid 懳徾偺儐乕僓ID
* @param gid 懳徾偺僌儖乕僾ID
*/
public synchronized void delGroup(int uid,int gid)
{
Password pass;
synchronized(PassMap){
pass = (Password)PassMap.get(new Integer(uid));
if(pass==null)
return;
pass.DelGroup(gid);
}
}
/**
* 巜掕儐乕僓ID偺儐乕僓柤傪曄峏偡傞
* @param uid 儐乕僓ID
* @param name 儐乕僓柤
*/
public synchronized void setUserName(int uid,String name)
{
Password pass;
synchronized(PassMap){
pass = (Password)PassMap.get(new Integer(uid));
if(pass==null)
return;
pass.SetName(name);
}
}
/**
* 儐乕僓ID偺僷僗儚乕僪傪愝掕偡傞
* @param uid 儐乕僓ID
* @param mdpass 僷僗儚乕僪
*/
public synchronized void setMDPass(int uid,String mdpass)
{
Password pass;
synchronized(PassMap){
pass = (Password)PassMap.get(new Integer(uid));
if(pass==null)
return;
pass.SetMDPass(mdpass);
}
}
/**
* 儐乕僓ID偺僷僗儚乕僪傪庢摼偡傞
* @param uid 儐乕僓ID
* @return 僷僗儚乕僪
*/
public synchronized String getMDPass(int uid)
{
Password pass;
synchronized(PassMap){
pass = (Password)PassMap.get(new Integer(uid));
if(pass==null)
return null;
return pass.GetMDPass();
}
}
/**
* 儐乕僓ID偐傜強懏僌儖乕僾ID攝楍傪曉偡
* @param uid 儐乕僓ID
* @return 強懏僌儖乕僾ID攝楍丄傑偨偼null
*/
public synchronized int[] UidToGids(int uid)
{
Password pass;
synchronized(PassMap){
pass = (Password)PassMap.get(new Integer(uid));
if(pass==null)
return null;
return pass.getGroups();
}
}
/**
* 儐乕僓ID偐傜強懏僌儖乕僾偱娗棟幰尃尷傪帩偭偰偄傞僌儖乕僾ID攝楍傪曉偡
* @param uid 儐乕僓ID
* @return 娗棟幰尃尷傪帩偭偰偄傞強懏僌儖乕僾ID攝楍丄傑偨偼null
*/
public synchronized int[] UidToGroupAdminGids(int uid)
{
Password pass;
synchronized(PassMap){
pass = (Password)PassMap.get(new Integer(uid));
if(pass==null)
return null;
return pass.getGroupAdminGroups();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -