1 18 package net.sf.drftpd.master.usermanager.xstream; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.io.ObjectInputStream ; 23 import java.util.ArrayList ; 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 com.thoughtworks.xstream.XStream; 36 import com.thoughtworks.xstream.io.xml.DomDriver; 37 38 42 public class XStreamUser 43 extends AbstractUser 44 implements PlainTextPasswordUser, UnixPassword { 45 private String unixPassword; 46 private String password; 47 transient XStreamUserManager usermanager; 48 private transient boolean purged; 49 50 public XStreamUser(XStreamUserManager usermanager, String username) { 51 super(username); 52 created = System.currentTimeMillis(); 53 this.usermanager = usermanager; 54 } 55 56 public boolean checkPassword(String password) { 57 if (this.password == null) { 58 if (this.unixPassword == null) 59 throw new IllegalStateException ("no password set"); 60 if (this 61 .unixPassword 62 .equals( 63 Crypt.crypt( 64 this.unixPassword.substring(0, 2), 65 password))) { 66 setPassword(password); 67 return true; 68 } 69 return false; 70 } 71 return this.password.equals(password); 72 } 73 74 public void setPassword(String password) { 75 this.unixPassword = null; 76 this.password = password; 77 } 78 79 public String getPassword() { 80 return this.password; 81 } 82 83 public void rename(String username) 84 throws UserExistsException, UserFileException 85 { 86 usermanager.rename(this, username); usermanager.getUserFile(this.getUsername()).delete(); 88 this.username = username; 89 commit(); } 91 92 public void commit() throws UserFileException { 93 if (this.purged) 94 return; 95 96 try { 97 XStream xst = new XStream(new DomDriver()); 98 SafeFileWriter out = new SafeFileWriter( 99 usermanager.getUserFile(this.getUsername()) 100 ); 101 try { 102 out.write(xst.toXML(this)); 103 } finally { 104 out.close(); 105 } 106 Logger.getLogger(XStreamUser.class).debug("wrote "+getUsername()); 107 } catch (IOException ex) { 108 throw new UserFileException( 109 "Error writing userfile for " 110 + this.getUsername() 111 + ": " 112 + ex.getMessage(), ex); 113 } 114 } 115 116 public void purge() { 117 this.purged = true; 118 usermanager.remove(this); 119 File userfile = usermanager.getUserFile(this.getUsername()); 120 userfile.delete(); 121 } 122 123 protected void finalize() throws Throwable { 124 this.commit(); 125 } 126 127 public void update() { 128 } 131 public String getUnixPassword() { 132 return unixPassword; 133 } 134 public void setUnixPassword(String password) { 135 this.password = null; 136 this.unixPassword = password; 137 } 138 private void readObject(ObjectInputStream stream) 139 throws IOException , ClassNotFoundException { 140 stream.defaultReadObject(); 141 if(groups == null) groups = new ArrayList (); 142 if(ipMasks == null) ipMasks = new ArrayList (); 143 } 144 145 } 146 | Popular Tags |