1 17 18 19 package org.apache.catalina.realm; 20 21 22 import java.security.Principal ; 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 import javax.naming.Context ; 28 29 import org.apache.catalina.Group; 30 import org.apache.catalina.LifecycleException; 31 import org.apache.catalina.Role; 32 import org.apache.catalina.ServerFactory; 33 import org.apache.catalina.User; 34 import org.apache.catalina.UserDatabase; 35 import org.apache.catalina.core.StandardServer; 36 import org.apache.catalina.users.MemoryUser; 37 import org.apache.catalina.util.StringManager; 38 39 40 51 52 public class UserDatabaseRealm 53 extends RealmBase { 54 55 56 58 59 63 protected UserDatabase database = null; 64 65 66 69 protected final String info = 70 "org.apache.catalina.realm.UserDatabaseRealm/1.0"; 71 72 73 76 protected static final String name = "UserDatabaseRealm"; 77 78 79 83 protected String resourceName = "UserDatabase"; 84 85 86 89 private static StringManager sm = 90 StringManager.getManager(Constants.Package); 91 92 93 95 96 101 public String getInfo() { 102 103 return info; 104 105 } 106 107 108 112 public String getResourceName() { 113 114 return resourceName; 115 116 } 117 118 119 125 public void setResourceName(String resourceName) { 126 127 this.resourceName = resourceName; 128 129 } 130 131 132 134 135 145 public boolean hasRole(Principal principal, String role) { 146 if( principal instanceof GenericPrincipal) { 147 GenericPrincipal gp = (GenericPrincipal)principal; 148 if(gp.getUserPrincipal() instanceof User) { 149 principal = gp.getUserPrincipal(); 150 } 151 } 152 if(! (principal instanceof User) ) { 153 return super.hasRole(principal, role); 155 } 156 if("*".equals(role)) { 157 return true; 158 } else if(role == null) { 159 return false; 160 } 161 User user = (User)principal; 162 Role dbrole = database.findRole(role); 163 if(dbrole == null) { 164 return false; 165 } 166 if(user.isInRole(dbrole)) { 167 return true; 168 } 169 Iterator groups = user.getGroups(); 170 while(groups.hasNext()) { 171 Group group = (Group)groups.next(); 172 if(group.isInRole(dbrole)) { 173 return true; 174 } 175 } 176 return false; 177 } 178 179 181 182 185 protected String getName() { 186 187 return (name); 188 189 } 190 191 192 195 protected String getPassword(String username) { 196 197 User user = database.findUser(username); 198 199 if (user == null) { 200 return null; 201 } 202 203 return (user.getPassword()); 204 205 } 206 207 208 211 protected Principal getPrincipal(String username) { 212 213 User user = database.findUser(username); 214 if(user == null) { 215 return null; 216 } 217 218 List roles = new ArrayList (); 219 Iterator uroles = user.getRoles(); 220 while(uroles.hasNext()) { 221 Role role = (Role)uroles.next(); 222 roles.add(role.getName()); 223 } 224 Iterator groups = user.getGroups(); 225 while(groups.hasNext()) { 226 Group group = (Group)groups.next(); 227 uroles = group.getRoles(); 228 while(uroles.hasNext()) { 229 Role role = (Role)uroles.next(); 230 roles.add(role.getName()); 231 } 232 } 233 return new GenericPrincipal(this, username, user.getPassword(), roles, user); 234 } 235 236 237 239 240 246 public synchronized void start() throws LifecycleException { 247 248 super.start(); 250 251 try { 252 StandardServer server = (StandardServer) ServerFactory.getServer(); 253 Context context = server.getGlobalNamingContext(); 254 database = (UserDatabase) context.lookup(resourceName); 255 } catch (Throwable e) { 256 containerLog.error(sm.getString("userDatabaseRealm.lookup", 257 resourceName), 258 e); 259 database = null; 260 } 261 if (database == null) { 262 throw new LifecycleException 263 (sm.getString("userDatabaseRealm.noDatabase", resourceName)); 264 } 265 266 } 267 268 269 275 public synchronized void stop() throws LifecycleException { 276 277 super.stop(); 279 280 database = null; 282 283 } 284 285 286 } 287 | Popular Tags |