KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > master > usermanager > jsx > JSXUser


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * DrFTPD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with DrFTPD; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package net.sf.drftpd.master.usermanager.jsx;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.ObjectInputStream JavaDoc;
23 import java.util.ArrayList JavaDoc;
24
25 import net.sf.drftpd.master.usermanager.AbstractUser;
26 import net.sf.drftpd.master.usermanager.PlainTextPasswordUser;
27 import net.sf.drftpd.master.usermanager.UnixPassword;
28 import net.sf.drftpd.master.usermanager.UserExistsException;
29 import net.sf.drftpd.master.usermanager.UserFileException;
30 import net.sf.drftpd.util.Crypt;
31 import net.sf.drftpd.util.SafeFileWriter;
32
33 import org.apache.log4j.Logger;
34
35 import JSX.ObjOut;
36
37 /**
38  * @author mog
39  * @version $Id: JSXUser.java,v 1.13 2004/04/20 04:11:49 mog Exp $
40  */

41 public class JSXUser
42     extends AbstractUser
43     implements PlainTextPasswordUser, UnixPassword {
44     private String JavaDoc unixPassword;
45     private String JavaDoc password;
46     transient JSXUserManager usermanager;
47     private transient boolean purged;
48
49     public JSXUser(JSXUserManager usermanager, String JavaDoc username) {
50         super(username);
51         created = System.currentTimeMillis();
52         this.usermanager = usermanager;
53     }
54
55     public boolean checkPassword(String JavaDoc password) {
56         if (this.password == null) {
57             if (this.unixPassword == null)
58                 throw new IllegalStateException JavaDoc("no password set");
59             if (this
60                 .unixPassword
61                 .equals(
62                     Crypt.crypt(
63                         this.unixPassword.substring(0, 2),
64                         password))) {
65                 setPassword(password);
66                 return true;
67             }
68             return false;
69         }
70         return this.password.equals(password);
71     }
72
73     public void setPassword(String JavaDoc password) {
74         this.unixPassword = null;
75         this.password = password;
76     }
77
78     public String JavaDoc getPassword() {
79         return this.password;
80     }
81
82     public void rename(String JavaDoc username)
83         throws UserExistsException, UserFileException {
84         usermanager.rename(this, username); // throws ObjectExistsException
85
usermanager.getUserFile(this.getUsername()).delete();
86         this.username = username;
87         commit(); // throws IOException
88
}
89
90     public void commit() throws UserFileException {
91         if (this.purged)
92             return;
93
94         try {
95             ObjOut out =
96                 new ObjOut(
97                     new SafeFileWriter(
98                         usermanager.getUserFile(this.getUsername())));
99             try {
100                 out.writeObject(this);
101             } finally {
102                 out.close();
103             }
104             Logger.getLogger(JSXUser.class).debug("wrote "+getUsername());
105         } catch (IOException JavaDoc ex) {
106             throw new UserFileException(
107                 "Error writing userfile for "
108                     + this.getUsername()
109                     + ": "
110                     + ex.getMessage(), ex);
111         }
112     }
113
114     public void purge() {
115         this.purged = true;
116         usermanager.remove(this);
117         File JavaDoc userfile = usermanager.getUserFile(this.getUsername());
118         userfile.delete();
119     }
120
121     protected void finalize() throws Throwable JavaDoc {
122         this.commit();
123     }
124
125     public void update() {
126         //an update was made, but commit() should be called from all places so we don't need to do anything.
127
//if we do, make sure it's implemented in all set and update methods in AbstractUser
128
}
129     public String JavaDoc getUnixPassword() {
130         return unixPassword;
131     }
132     public void setUnixPassword(String JavaDoc password) {
133         this.password = null;
134         this.unixPassword = password;
135     }
136     private void readObject(ObjectInputStream JavaDoc stream)
137         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
138         stream.defaultReadObject();
139         if(groups == null) groups = new ArrayList JavaDoc();
140         if(ipMasks == null) ipMasks = new ArrayList JavaDoc();
141         if(tagline == null) tagline = "";
142     }
143
144 }
145
Popular Tags