1 7 package org.jboss.security.auth.spi; 8 9 import java.util.Iterator ; 10 import java.util.HashMap ; 11 import java.util.ArrayList ; 12 import java.util.Enumeration ; 13 import java.security.acl.Group ; 14 import java.security.Principal ; 15 16 import org.jboss.security.SimpleGroup; 17 import org.jboss.security.SimplePrincipal; 18 19 23 public class Users 24 { 25 private HashMap users = new HashMap (); 26 27 public static class User implements Comparable 28 { 29 private String name; 30 private String password; 31 private String encoding; 32 private HashMap roleGroups = new HashMap (); 33 34 public User() 35 { 36 } 37 public User(String name) 38 { 39 this.name = name; 40 } 41 public String getName() 42 { 43 return name; 44 } 45 public String getPassword() 46 { 47 return password; 48 } 49 public void setPassword(String password) 50 { 51 this.password = password; 52 } 53 54 public String getEncoding() 55 { 56 return encoding; 57 } 58 public void setEncoding(String encoding) 59 { 60 this.encoding = encoding; 61 } 62 63 public Group [] getRoleSets() 64 { 65 Group [] roleSets = new Group [roleGroups.size()]; 66 roleGroups.values().toArray(roleSets); 67 return roleSets; 68 } 69 public String [] getRoleNames() 70 { 71 return getRoleNames("Roles"); 72 } 73 public String [] getRoleNames(String roleGroup) 74 { 75 Group group = (Group ) roleGroups.get(roleGroup); 76 String [] names = {}; 77 if( group != null ) 78 { 79 ArrayList tmp = new ArrayList (); 80 Enumeration iter = group.members(); 81 while( iter.hasMoreElements() ) 82 { 83 Principal p = (Principal ) iter.nextElement(); 84 tmp.add(p.getName()); 85 } 86 names = new String [tmp.size()]; 87 tmp.toArray(names); 88 } 89 return names; 90 } 91 public void addRole(String roleName, String roleGroup) 92 { 93 Group group = (Group ) roleGroups.get(roleGroup); 94 if( group == null ) 95 { 96 group = new SimpleGroup(roleGroup); 97 roleGroups.put(roleGroup, group); 98 } 99 SimplePrincipal role = new SimplePrincipal(roleName); 100 group.addMember(role); 101 } 102 public int compareTo(Object obj) 103 { 104 User u = (User) obj; 105 return name.compareTo(u.name); 106 } 107 108 public String toString() 109 { 110 return "User{" + 111 "name='" + name + "'" + 112 ", password=*" + 113 ", encoding='" + encoding + "'" + 114 ", roleGroups=" + roleGroups + 115 "}"; 116 } 117 } 118 119 public void addUser(User user) 120 { 121 users.put(user.getName(), user); 122 } 123 public Iterator getUsers() 124 { 125 return users.values().iterator(); 126 } 127 public User getUser(String name) 128 { 129 User find = (User) users.get(name); 130 return find; 131 } 132 133 public String toString() 134 { 135 return "Users("+System.identityHashCode(this)+"){" + 136 "users=" + users + 137 "}"; 138 } 139 } 140 | Popular Tags |