--- projects/cms/source/util/uk/org/iscream/cms/util/ACL.java 2002/02/15 22:27:15 1.2 +++ projects/cms/source/util/uk/org/iscream/cms/util/ACL.java 2002/03/19 12:18:22 1.3 @@ -16,7 +16,7 @@ import java.io.Serializable; * the relevant check method. * * @author $Author: tdb $ - * @version $Id: ACL.java,v 1.2 2002/02/15 22:27:15 tdb Exp $ + * @version $Id: ACL.java,v 1.3 2002/03/19 12:18:22 tdb Exp $ */ public class ACL implements Serializable { @@ -25,7 +25,7 @@ public class ACL implements Serializable { /** * The current CVS revision of this class */ - public static final String REVISION = "$Revision: 1.2 $"; + public static final String REVISION = "$Revision: 1.3 $"; /** * static to be used when adding an ALLOW rule to the ACL. @@ -60,6 +60,75 @@ public class ACL implements Serializable { */ public ACL(boolean defaultMode) { _defaultMode = defaultMode; + } + + /** + * Construct a new Access Control List with a given + * String representation of the ACL rules. The String + * should be of the format: + * expression:rule;expression:rule;expression:rule... + * Where expression is a wildcard to match against, and + * rule is either 'ALLOW' or 'DENY'. There is a special + * expression of 'DEFAULT' which represents the default + * rule (what should happen if no expression is matched + * when performing a check). + * The default mode is set to ALLOW if one is not + * specified. + * + * @param acl a String representation of the ACL. + */ + public ACL(String acl) { + // default to ALLOW + _defaultMode = ACL.ALLOW; + if(acl != null) { + // split the String into expression:rule parts + StringTokenizer st1 = new StringTokenizer(acl, ";"); + while(st1.hasMoreTokens()) { + String token1 = st1.nextToken(); + // if it doesn't have a :, it's not the correct format + if(token1.indexOf(":") != -1) { + // split into expression and rule part + StringTokenizer st2 = new StringTokenizer(token1, ":"); + String expression = ""; + String rule = ""; + if(st2.hasMoreTokens()) { + expression = st2.nextToken(); + } + else { + // mall-formed? + continue; + } + if(st2.hasMoreTokens()) { + rule = st2.nextToken(); + } + else { + // mall-formed? + continue; + } + // check to see what sort of rule + if(rule.equals("ALLOW")) { + // case for special 'DEFAULT' expression + if(expression.equals("DEFAULT")) { + _defaultMode = ACL.ALLOW; + } + else { + add(ACL.ALLOW, expression); + } + } + else if(rule.equals("DENY")) { + // case for special 'DEFAULT' expression + if(expression.equals("DEFAULT")) { + _defaultMode = ACL.DENY; + } + else { + add(ACL.DENY, expression); + } + } + // if it's not ALLOW or DENY, it's not a + // proper rule, so we'll ignore it + } + } + } } //---PUBLIC METHODS---