1 18 package net.sf.drftpd.master.usermanager.jsx; 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 JSX.ObjOut; 36 37 41 public class JSXUser 42 extends AbstractUser 43 implements PlainTextPasswordUser, UnixPassword { 44 private String unixPassword; 45 private String password; 46 transient JSXUserManager usermanager; 47 private transient boolean purged; 48 49 public JSXUser(JSXUserManager usermanager, String username) { 50 super(username); 51 created = System.currentTimeMillis(); 52 this.usermanager = usermanager; 53 } 54 55 public boolean checkPassword(String password) { 56 if (this.password == null) { 57 if (this.unixPassword == null) 58 throw new IllegalStateException ("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 password) { 74 this.unixPassword = null; 75 this.password = password; 76 } 77 78 public String getPassword() { 79 return this.password; 80 } 81 82 public void rename(String username) 83 throws UserExistsException, UserFileException { 84 usermanager.rename(this, username); usermanager.getUserFile(this.getUsername()).delete(); 86 this.username = username; 87 commit(); } 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 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 userfile = usermanager.getUserFile(this.getUsername()); 118 userfile.delete(); 119 } 120 121 protected void finalize() throws Throwable { 122 this.commit(); 123 } 124 125 public void update() { 126 } 129 public String getUnixPassword() { 130 return unixPassword; 131 } 132 public void setUnixPassword(String password) { 133 this.password = null; 134 this.unixPassword = password; 135 } 136 private void readObject(ObjectInputStream stream) 137 throws IOException , ClassNotFoundException { 138 stream.defaultReadObject(); 139 if(groups == null) groups = new ArrayList (); 140 if(ipMasks == null) ipMasks = new ArrayList (); 141 if(tagline == null) tagline = ""; 142 } 143 144 } 145 | Popular Tags |