1 16 package org.jmanage.core.auth; 17 18 import org.jmanage.core.crypto.Crypto; 19 20 import java.util.List ; 21 import java.util.Iterator ; 22 import java.security.Principal ; 23 24 28 public class User implements Principal , java.io.Serializable { 29 30 public final static String STATUS_ACTIVE = "A"; 31 public final static String STATUS_LOCKED = "L"; 32 34 private String username; 35 private String plaintextPassword; 36 private String password; private List roles; 38 private String status; 39 private int lockCount; 40 41 private boolean externalUser = false; 42 43 44 47 public User(){} 48 49 56 public User(String username, String password, List roles, String status, 57 int lockCount) { 58 this.username = username; 59 this.password = password; 60 this.roles = roles; 61 this.status = status; 62 this.lockCount = lockCount; 63 } 64 65 public String getUsername() { 66 return username; 67 } 68 69 public void setUsername(String username) { 70 this.username = username; 71 } 72 73 77 public String getPassword() { 78 if(password == null){ 79 assert plaintextPassword != null; 80 password = Crypto.hash(plaintextPassword); 81 } 82 return password; 83 } 84 85 92 public void setPlaintextPassword(String plaintext){ 93 this.plaintextPassword = plaintext; 94 } 95 96 public void setPassword(String password) { 97 this.password = password; 98 } 99 100 public List getRoles() { 101 return roles; 102 } 103 104 public String getRolesAsString(){ 105 StringBuffer strRoles = new StringBuffer (); 106 for(Iterator it=roles.iterator(); it.hasNext();){ 107 Role role = (Role)it.next(); 108 strRoles.append(role.getName()); 109 if(it.hasNext()) 110 strRoles.append(", "); 111 } 112 return strRoles.toString(); 113 } 114 115 public void setRoles(List roles) { 116 this.roles = roles; 117 } 118 119 public String getStatus() { 120 return status; 121 } 122 123 public void setStatus(String status) { 124 this.status = status; 125 } 126 127 public int getLockCount() { 128 return lockCount; 129 } 130 131 public void setLockCount(int lockCount) { 132 this.lockCount = lockCount; 133 } 134 135 public String getName() { 136 return getUsername(); 137 } 138 139 public boolean isExternalUser() { 140 return externalUser; 141 } 142 143 public void setExternalUser(boolean externalUser) { 144 this.externalUser = externalUser; 145 } 146 147 public boolean hasRole(String role) { 148 for(Iterator it=getRoles().iterator(); it.hasNext(); ){ 149 Role roleObj = (Role)it.next(); 150 if(role.equals(roleObj.getName())){ 151 return true; 152 } 153 } 154 return false; 155 } 156 157 public boolean equals(Object o){ 158 if(o == null) 159 return false; 160 161 if(this == o) 162 return true; 163 164 if(!(o instanceof Principal )) 165 return false; 166 Principal that = (Principal )o; 167 168 if(this.getName().equals(that.getName())) 169 return true; 170 return false; 171 } 172 173 public String toString(){ 174 return username; 175 } 176 } | Popular Tags |