Annotation of java/classes/org/w3c/jigsaw/acl/AclRealm.java, revision 1.4
1.1 bmahe 1: // AclRealm.java
1.4 ! bmahe 2: // $Id: AclRealm.java,v 1.3 1999/04/23 14:06:46 bmahe Exp $
1.1 bmahe 3: // (c) COPYRIGHT MIT, INRIA and Keio, 1999.
4: // Please first read the full copyright statement in file COPYRIGHT.html
5:
6: package org.w3c.jigsaw.acl;
7:
8: import java.security.Principal;
9: import java.security.acl.AclEntry;
10: import java.security.acl.LastOwnerException;
11: import java.security.acl.NotOwnerException;
12: import java.security.acl.Permission;
1.3 bmahe 13: import java.util.Enumeration;
14: import java.util.Vector;
1.1 bmahe 15:
1.3 bmahe 16: import org.w3c.jigsaw.auth.AuthRealm;
17: import org.w3c.jigsaw.auth.AuthUser;
18: import org.w3c.jigsaw.auth.IPMatcher;
19: import org.w3c.jigsaw.auth.RealmsCatalog;
20: import org.w3c.jigsaw.http.httpd;
21:
22: import org.w3c.tools.resources.FramedResource;
23: import org.w3c.tools.resources.InvalidResourceException;
24: import org.w3c.tools.resources.ResourceReference;
25: import org.w3c.tools.resources.Attribute;
26: import org.w3c.tools.resources.AttributeRegistry;
27: import org.w3c.tools.resources.StringAttribute;
28: import org.w3c.tools.resources.StringArrayAttribute;
1.1 bmahe 29:
30: /**
1.4 ! bmahe 31: * @version $Revision: 1.3 $
1.1 bmahe 32: * @author Benoît Mahé (bmahe@w3.org)
33: */
34: public class AclRealm extends JAcl {
1.3 bmahe 35: /**
36: * Attribute index - The methods protected by the filter.
37: */
38: protected static int ATTR_METHODS = -1 ;
1.2 bmahe 39: /**
40: * Attribute index - The realm name for this ACL.
41: */
42: protected static int ATTR_REALM = -1;
43:
1.3 bmahe 44: static {
45: Attribute a = null ;
46: Class c = null ;
47: try {
48: c = Class.forName("org.w3c.jigsaw.acl.AclRealm");
49: } catch (Exception ex) {
50: ex.printStackTrace() ;
51: System.exit(1) ;
52: }
53: // The protected methods
54: a = new StringArrayAttribute("methods"
55: , null
56: , Attribute.EDITABLE) ;
57: ATTR_METHODS = AttributeRegistry.registerAttribute(c, a) ;
58: // The realm name (to be resolved by the RealmFactory).
59: a = new StringAttribute("realm"
60: , null
61: , Attribute.EDITABLE|Attribute.MANDATORY);
62: ATTR_REALM = AttributeRegistry.registerAttribute(c, a) ;
63: }
64:
65: /**
66: * The IPMatcher to match IP templates to user records.
67: */
68: protected IPMatcher ipmatcher = null ;
69: /**
70: * The catalog of realms that make our scope.
71: */
72: protected RealmsCatalog catalog = null ;
73: /**
74: * Our associated realm.
75: */
76: protected ResourceReference rr_realm = null ;
77: /**
78: * The nam of the realm we cache in <code>realm</code>.
79: */
80: protected String loaded_realm = null ;
81:
1.4 ! bmahe 82: protected Vector entries = null;
1.3 bmahe 83:
84: /**
85: * Get the list of methods that this filter protect
86: * @return An array of String giving the name of the protected methods,
87: * or <strong>null</strong>, in wich case <em>all</em> methods are
88: * to be protected.
89: */
90: public String[] getMethods() {
91: return (String[]) getValue(ATTR_METHODS, null) ;
92: }
93:
94: /**
95: * Get the realm of this filter.
96: */
97: public String getRealm() {
98: return (String) getValue(ATTR_REALM, null) ;
99: }
100:
101: /**
102: * Get a pointer to our realm, and initialize our ipmatcher.
103: */
1.4 ! bmahe 104: protected synchronized void acquireRealm() {
! 105: entries = new Vector(10);
1.3 bmahe 106: // Get our catalog:
107: if ( catalog == null ) {
108: httpd server = (httpd)
109: ((FramedResource) getTargetResource()).getServer() ;
110: catalog = server.getRealmsCatalog() ;
111: }
112: // Check that our realm name is valid:
113: String name = getRealm() ;
114: if ( name == null )
115: return ;
116: if ((rr_realm != null) && name.equals(loaded_realm))
117: return ;
118: // Load the realm and create the ipmtacher object
119: rr_realm = catalog.loadRealm(name) ;
120: if (rr_realm != null) {
121: try {
122: AuthRealm realm = (AuthRealm) rr_realm.lock();
123: Enumeration enum = realm.enumerateUserNames() ;
124: while (enum.hasMoreElements()) {
125: String uname = (String) enum.nextElement() ;
126: ResourceReference rr_user = realm.loadUser(uname) ;
127: try {
128: AuthUser user = (AuthUser) rr_user.lock();
1.4 ! bmahe 129: createEntry(user);
1.3 bmahe 130: } catch (InvalidResourceException ex) {
131: System.out.println("Invalid user reference : "+uname);
132: } finally {
133: rr_user.unlock();
134: }
135: }
136: } catch (InvalidResourceException ex) {
137:
138: } finally {
139: rr_realm.unlock();
140: }
141: }
142: }
143:
1.4 ! bmahe 144: protected void createEntry(AuthUser user) {
1.3 bmahe 145: AclPrincipal aclp = new AclPrincipal(user);
1.4 ! bmahe 146: entries.addElement(aclp);
! 147: }
! 148:
! 149: protected boolean hasPrincipal(Principal p) {
! 150: //test with equals...
! 151: int idx = entries.indexOf(p);
! 152: return (idx != -1);
1.3 bmahe 153: }
1.1 bmahe 154:
155: public boolean addOwner(Principal caller, Principal owner)
156: throws NotOwnerException
157: {
158: return false;
159: }
160:
161: public boolean deleteOwner(Principal caller, Principal owner)
162: throws NotOwnerException, LastOwnerException
163: {
164: return false;
165: }
166:
167: public boolean isOwner(Principal owner) {
168: return false;
169: }
170:
171: public void setName(Principal caller, String name)
172: throws NotOwnerException
173: {
1.3 bmahe 174: throw new NotOwnerException();
1.1 bmahe 175: }
176:
177: public String getName() {
1.3 bmahe 178: return getRealm();
1.1 bmahe 179: }
180:
181: public boolean addEntry(Principal caller, AclEntry entry)
182: throws NotOwnerException
183: {
184: return false;
185: }
186:
187: public boolean removeEntry(Principal caller, AclEntry entry)
188: throws NotOwnerException
189: {
190: return false;
191: }
192:
193: public Enumeration getPermissions(Principal user) {
194: return null;
195: }
196:
197: public Enumeration entries() {
198: return null;
199: }
200:
201: public boolean checkPermission(Principal principal, Permission permission)
202: {
1.4 ! bmahe 203: acquireRealm();
1.3 bmahe 204: String methods[] = getMethods();
205: boolean methodprotected = false;
206: if (methods != null) {
207: for (int i = 0 ; i < methods.length ; i++) {
208: if (permission.equals(methods[i]))
209: methodprotected = true;
210: }
211: } else {
212: methodprotected = true;
213: }
214: if (! methodprotected)
215: return true;
1.4 ! bmahe 216: return hasPrincipal(principal);
1.1 bmahe 217: }
218:
219: public String toString() {
1.3 bmahe 220: return getName();
221: }
222:
223: /**
224: * Initialize the Acl.
225: */
226: public void initialize(Object values[]) {
227: super.initialize(values) ;
1.1 bmahe 228: }
229:
230: }
Webmaster