1 26 27 package org.objectweb.jonas.security.realm.principals; 28 29 import java.io.Serializable ; 30 import java.security.Principal ; 31 import java.util.ArrayList ; 32 import java.util.Enumeration ; 33 import java.util.StringTokenizer ; 34 import java.util.Vector ; 35 36 import org.objectweb.jonas.security.realm.lib.HashHelper; 37 import org.objectweb.jonas.security.realm.lib.HashPassword; 38 import org.objectweb.jonas.security.realm.lib.XML; 39 40 47 public class User implements Principal , Serializable , UserMBean { 48 49 52 protected static final String SEPARATOR = ","; 53 54 57 private String name = null; 58 59 62 private String password = null; 63 64 67 private HashPassword hashPassword = null; 68 69 72 private Vector groups = new Vector (); 73 74 77 private Vector roles = new Vector (); 78 79 82 private ArrayList combinedRoles = new ArrayList (); 83 84 87 public User() { 88 } 89 90 95 public User(String name, String password) { 96 setName(name); 97 setPassword(password); 98 } 99 100 104 public void setName(String name) { 105 if (name.startsWith("##DN##")) { 106 this.name = name.replace('=', '#').replace(',', '%').replace(' ', '$'); 108 } else { 109 this.name = name; 110 } 111 } 112 113 117 public String getName() { 118 return name; 119 } 120 121 125 public String getPassword() { 126 return password; 127 } 128 129 133 public void setPassword(String password) { 134 this.password = password; 135 setHashPassword(HashHelper.getHashPassword(password)); 136 } 137 138 142 protected void setHashPassword(HashPassword hashPassword) { 143 this.hashPassword = hashPassword; 144 } 145 146 150 public HashPassword getHashPassword() { 151 return hashPassword; 152 } 153 154 158 public void setGroups(String groups) { 159 StringTokenizer st = new StringTokenizer (groups, SEPARATOR); 160 String group = null; 161 while (st.hasMoreTokens()) { 162 group = st.nextToken().trim(); 163 addGroup(group); 164 } 165 } 166 167 171 public String getGroups() { 172 String groupsList = ""; 173 Enumeration g = groups.elements(); 174 int nb = 0; 175 String group = null; 176 177 while (g.hasMoreElements()) { 178 if (nb > 0) { 179 groupsList += ", "; 180 } 181 group = (String ) g.nextElement(); 182 groupsList += group; 183 } 184 return groupsList; 185 186 } 187 188 192 public String [] getArrayGroups() { 193 return ((String []) groups.toArray(new String [groups.size()])); 194 } 195 196 200 public void setRoles(String roles) { 201 if (roles != null) { 202 StringTokenizer st = new StringTokenizer (roles, SEPARATOR); 203 String role = null; 204 while (st.hasMoreTokens()) { 205 role = st.nextToken().trim(); 206 addRole(role); 207 } 208 } 209 } 210 211 215 public void addGroup(String group) { 216 if (!groups.contains(group)) { 217 this.groups.addElement(group); 218 } 219 } 220 221 225 public void addRole(String role) { 226 if (!roles.contains(role)) { 227 this.roles.addElement(role); 228 } 229 } 230 231 235 public void removeGroup(String group) { 236 if (groups.contains(group)) { 237 this.groups.removeElement(group); 238 } 239 } 240 241 245 public void removeRole(String role) { 246 if (roles.contains(role)) { 247 this.roles.removeElement(role); 248 } 249 } 250 251 255 public String getRoles() { 256 String rolesList = ""; 257 Enumeration r = roles.elements(); 258 int nb = 0; 259 String role = null; 260 261 while (r.hasMoreElements()) { 262 if (nb > 0) { 263 rolesList += ", "; 264 } 265 role = (String ) r.nextElement(); 266 rolesList += role; 267 } 268 return rolesList; 269 } 270 271 275 public void setCombinedRoles(ArrayList combinedRoles) { 276 this.combinedRoles = combinedRoles; 277 } 278 279 283 public ArrayList getCombinedRoles() { 284 return combinedRoles; 285 } 286 287 291 public String [] getArrayRoles() { 292 return ((String []) roles.toArray(new String [roles.size()])); 293 } 294 295 299 public String toXML() { 300 StringBuffer xml = new StringBuffer ("<user name=\""); 301 xml.append(name); 302 xml.append("\" password=\""); 303 if (password != null) { 304 xml.append(password); 305 } 306 xml.append("\""); 307 XML.appendVectorToBuffer("groups=", xml, groups); 308 XML.appendVectorToBuffer("roles=", xml, roles); 309 xml.append(" />"); 310 return xml.toString(); 311 } 312 313 317 public String toString() { 318 return this.toXML(); 319 } 320 321 } | Popular Tags |