ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/util/uk/org/iscream/cms/util/ACL.java
(Generate patch)

Comparing projects/cms/source/util/uk/org/iscream/cms/util/ACL.java (file contents):
Revision 1.3 by tdb, Tue Mar 19 12:18:22 2002 UTC vs.
Revision 1.8 by tdb, Sun Aug 1 10:41:08 2004 UTC

# Line 1 | Line 1
1 + /*
2 + * i-scream central monitoring system
3 + * http://www.i-scream.org
4 + * Copyright (C) 2000-2002 i-scream
5 + *
6 + * This program is free software; you can redistribute it and/or
7 + * modify it under the terms of the GNU General Public License
8 + * as published by the Free Software Foundation; either version 2
9 + * of the License, or (at your option) any later version.
10 + *
11 + * This program is distributed in the hope that it will be useful,
12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 + * GNU General Public License for more details.
15 + *
16 + * You should have received a copy of the GNU General Public License
17 + * along with this program; if not, write to the Free Software
18 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 + */
20 +
21   //---PACKAGE DECLARATION---
22 < package uk.org.iscream.cms.server.util;
22 > package uk.org.iscream.cms.util;
23  
24   //---IMPORTS---
25   import java.util.ArrayList;
# Line 36 | Line 56 | public class ACL implements Serializable {
56       * static to be used when adding a DENY rule to the ACL.
57       */
58      public static final boolean DENY = false;
59 +    
60 +    /**
61 +     * default setting for the default mode for a new ACL.
62 +     */
63 +    public static final boolean DEFMODE = ACL.ALLOW;
64  
65   //---STATIC METHODS---
66  
# Line 47 | Line 72 | public class ACL implements Serializable {
72       * blocked by a rule.
73       */
74      public ACL() {
75 <        // default to ACL.ALLOW
76 <        this(ACL.ALLOW);
75 >        // default to DEFMODE
76 >        this(DEFMODE);
77      }
78      
79      /**
# Line 59 | Line 84 | public class ACL implements Serializable {
84       * @param defaultMode the default mode for non-matched checks
85       */
86      public ACL(boolean defaultMode) {
87 <        _defaultMode = defaultMode;
87 >        setDefaultMode(defaultMode);
88      }
89  
90      /**
# Line 73 | Line 98 | public class ACL implements Serializable {
98       * rule (what should happen if no expression is matched
99       * when performing a check).
100       * The default mode is set to ALLOW if one is not
101 <     * specified.
101 >     * specified in the String.
102       *
103       * @param acl a String representation of the ACL.
104       */
105      public ACL(String acl) {
106 <        // default to ALLOW
107 <        _defaultMode = ACL.ALLOW;
106 >        setDefaultMode(DEFMODE);
107 >        add(acl);
108 >    }
109 >
110 > //---PUBLIC METHODS---
111 >
112 >    /**
113 >     * Add a new rule to the ACL immediately after the
114 >     * previous rule. The rule can either be an ACL.ALLOW
115 >     * rule, or an ACL.DENY rule. The expression can
116 >     * contain a wildcard (a * only). Rules can only be
117 >     * added to the end of the list.
118 >     *
119 >     * param allow whether this is an ALLOW or DENY rule
120 >     * param expression what this rule matches using wildcards
121 >     */
122 >    public void add(boolean allow, String expression) {
123 >        // try and convert the expression into an IP address
124 >        short[] ipaddr = ipStringToShort(expression);
125 >        // a result of null means it's not an IP address
126 >        // add either a name rule or an IP rule
127 >        if(ipaddr != null) {
128 >            _acl.add(new ACLRule(allow, expression, ipaddr, true));
129 >        }
130 >        else {
131 >            _acl.add(new ACLRule(allow, expression, ipaddr, false));
132 >        }
133 >    }
134 >    
135 >    /**
136 >     * Add some new rules to the Access Control List in
137 >     * the form of a String. The String should be of the
138 >     * following format:
139 >     *     expression:rule;expression:rule;expression:rule...
140 >     * Where expression is a wildcard to match against, and
141 >     * rule is either 'ALLOW' or 'DENY'. There is a special
142 >     * expression of 'DEFAULT' which represents the default
143 >     * rule (what should happen if no expression is matched
144 >     * when performing a check).
145 >     *
146 >     * @param acl a String representation of the ACL.
147 >     */
148 >    public void add(String acl) {
149          if(acl != null) {
150              // split the String into expression:rule parts
151              StringTokenizer st1 = new StringTokenizer(acl, ";");
# Line 109 | Line 175 | public class ACL implements Serializable {
175                      if(rule.equals("ALLOW")) {
176                          // case for special 'DEFAULT' expression
177                          if(expression.equals("DEFAULT")) {
178 <                            _defaultMode = ACL.ALLOW;
178 >                            setDefaultMode(ACL.ALLOW);
179                          }
180                          else {
181                              add(ACL.ALLOW, expression);
# Line 118 | Line 184 | public class ACL implements Serializable {
184                      else if(rule.equals("DENY")) {
185                          // case for special 'DEFAULT' expression
186                          if(expression.equals("DEFAULT")) {
187 <                            _defaultMode = ACL.DENY;
187 >                            setDefaultMode(ACL.DENY);
188                          }
189                          else {
190                              add(ACL.DENY, expression);
# Line 130 | Line 196 | public class ACL implements Serializable {
196              }
197          }
198      }
133
134 //---PUBLIC METHODS---
135
136    /**
137     * Add a new rule to the ACL immediately after the
138     * previous rule. The rule can either be an ACL.ALLOW
139     * rule, or an ACL.DENY rule. The expression can
140     * contain a wildcard (a * only). Rules can only be
141     * added to the end of the list.
142     *
143     * param allow whether this is an ALLOW or DENY rule
144     * param expression what this rule matches using wildcards
145     */
146    public void add(boolean allow, String expression) {
147        // try and convert the expression into an IP address
148        short[] ipaddr = ipStringToShort(expression);
149        // a result of null means it's not an IP address
150        // add either a name rule or an IP rule
151        if(ipaddr != null) {
152            _acl.add(new ACLRule(allow, expression, ipaddr, true));
153        }
154        else {
155            _acl.add(new ACLRule(allow, expression, ipaddr, false));
156        }
157    }
199      
200      /**
201       * Check to see if a string is permitted by the
# Line 213 | Line 254 | public class ACL implements Serializable {
254      }
255      
256      /**
257 +     * Clears the ACL and resets the default mode.
258 +     */
259 +    public void clear() {
260 +        // just clear out our underlying ArrayList
261 +        // containing our ACL objects
262 +        _acl.clear();
263 +        // and reset the default mode to the default
264 +        setDefaultMode(DEFMODE);
265 +    }
266 +    
267 +    /**
268 +     * Changes the default mode of the ACL. This is what
269 +     * the check will return if it does not find an explict
270 +     * rule to match against.
271 +     *
272 +     * @param defaultMode the new default mode
273 +     */
274 +    public void setDefaultMode(boolean defaultMode) {
275 +        _defaultMode = defaultMode;
276 +    }
277 +    
278 +    /**
279       * Gives a String representation of this ACL.
280       *
281       * @return A String representation of this ACL.
# Line 350 | Line 413 | public class ACL implements Serializable {
413      /**
414       * The default mode of this ACL.
415       */
416 <    private boolean _defaultMode;
416 >    private boolean _defaultMode = DEFMODE;
417  
418   //---STATIC ATTRIBUTES---
419  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines