1 17 18 package org.objectweb.jac.aspects.user; 19 20 import java.util.Collection ; 21 import java.util.Iterator ; 22 import java.util.Vector ; 23 import org.objectweb.jac.core.ACManager; 24 import org.objectweb.jac.core.rtti.MetaItem; 25 import org.objectweb.jac.util.Log; 26 import org.objectweb.jac.util.Stack; 27 import java.util.List ; 28 29 public class Profile { 30 31 public Profile() {} 32 33 public Profile(String name) { 34 this.name = name; 35 } 36 37 public Profile(String name, Profile parent) { 38 this.name = name; 39 this.parent = parent; 40 } 41 42 String name; 43 44 48 public String getName() { 49 return name; 50 } 51 52 56 public void setName(String v) { 57 this.name = v; 58 } 59 60 61 Profile parent = null; 62 63 public void setParent(Profile parent) { 64 this.parent = parent; 65 invalidateCache(); 66 } 67 public Profile getParent() { 68 return parent; 69 } 70 71 transient boolean isNew = true; 72 public void setIsNew(boolean isNew) { 73 this.isNew = isNew; 74 } 75 public boolean isNew() { 76 return isNew; 77 } 78 79 82 public void clear() { 83 readRules.clear(); 84 writeRules.clear(); 85 addRules.clear(); 86 createRules.clear(); 87 removeRules.clear(); 88 invalidateCache(); 89 } 90 91 Vector readRules = new Vector (); 92 Vector writeRules = new Vector (); 93 Vector addRules = new Vector (); 94 Vector removeRules = new Vector (); 95 Vector createRules = new Vector (); 96 97 public List getReadRules() { 98 return readRules; 99 } 100 public void addReadRule(Rule rule) { 101 readRules.add(rule); 102 invalidateCache(); 103 } 104 public void removeReadRule(Rule rule) { 105 readRules.remove(rule); 106 invalidateCache(); 107 } 108 109 public List getWriteRules() { 110 return writeRules; 111 } 112 public void addWriteRule(Rule rule) { 113 writeRules.add(rule); 114 invalidateCache(); 115 } 116 public void removeWriteRule(Rule rule) { 117 writeRules.remove(rule); 118 invalidateCache(); 119 } 120 121 public List getAddRules() { 122 return addRules; 123 } 124 public void addAddRule(Rule rule) { 125 addRules.add(rule); 126 invalidateCache(); 127 } 128 public void removeAddRule(Rule rule) { 129 addRules.remove(rule); 130 invalidateCache(); 131 } 132 133 public List getCreateRules() { 134 return createRules; 135 } 136 public void addCreateRule(Rule rule) { 137 createRules.add(rule); 138 invalidateCache(); 139 } 140 public void removeCreateRule(Rule rule) { 141 createRules.remove(rule); 142 invalidateCache(); 143 } 144 145 public List getRemoveRules() { 146 return removeRules; 147 } 148 public void addRemoveRule(Rule rule) { 149 removeRules.add(rule); 150 invalidateCache(); 151 } 152 public void removeRemoveRule(Rule rule) { 153 removeRules.remove(rule); 154 invalidateCache(); 155 } 156 157 public void addReadable(String resourceExpr) { 158 readRules.add(new Rule(Rule.ALLOW,resourceExpr)); 159 invalidateCache(); 160 } 161 162 public void addUnreadable(String resourceExpr) { 163 readRules.add(new Rule(Rule.DENY,resourceExpr)); 164 invalidateCache(); 165 } 166 167 public void addWritable(String resourceExpr) { 168 writeRules.add(new Rule(Rule.ALLOW,resourceExpr)); 169 invalidateCache(); 170 } 171 172 public void addUnwritable(String resourceExpr) { 173 writeRules.add(new Rule(Rule.DENY,resourceExpr)); 174 invalidateCache(); 175 } 176 177 public void addAddable(String resourceExpr) { 178 addRules.add(new Rule(Rule.ALLOW,resourceExpr)); 179 invalidateCache(); 180 } 181 182 public void addCreatable(String resourceExpr) { 183 createRules.add(new Rule(Rule.ALLOW,resourceExpr)); 184 invalidateCache(); 185 } 186 187 public void addUnaddable(String resourceExpr) { 188 addRules.add(new Rule(Rule.DENY,resourceExpr)); 189 invalidateCache(); 190 } 191 192 public void addRemovable(String resourceExpr) { 193 removeRules.add(new Rule(Rule.ALLOW,resourceExpr)); 194 invalidateCache(); 195 } 196 197 public void addUnremovable(String resourceExpr) { 198 removeRules.add(new Rule(Rule.DENY,resourceExpr)); 199 invalidateCache(); 200 } 201 202 public Stack getProfileStack() { 203 Stack profiles = new Stack(); 204 Profile profile = this; 205 while (profile!=null) { 206 profiles.push(profile); 207 profile = profile.getParent(); 208 } 209 return profiles; 210 } 211 212 public boolean isReadable(MetaItem item) { 213 Stack profiles = getProfileStack(); 214 while(!profiles.empty()) { 215 Profile profile = (Profile)profiles.pop(); 216 Collection rules = profile.getReadRules(); 217 Iterator it = rules.iterator(); 218 while (it.hasNext()) { 219 Rule rule = (Rule)it.next(); 220 if (rule.match(item)) 221 return rule.getAllow(); 222 } 223 } 224 return false; 225 } 226 227 public boolean isWritable(MetaItem item) { 228 Log.trace("profile",2,"isWritable "+item); 229 Stack profiles = getProfileStack(); 230 while(!profiles.empty()) { 231 Profile profile = (Profile)profiles.pop(); 232 Log.trace("profile",2,"Current profile "+profile); 233 Collection rules = profile.getWriteRules(); 234 Iterator it = rules.iterator(); 235 while (it.hasNext()) { 236 Rule rule = (Rule)it.next(); 237 if (rule.match(item)) 238 return rule.getAllow(); 239 } 240 } 241 return false; 242 } 243 244 public boolean isAddable(MetaItem item) { 245 Stack profiles = getProfileStack(); 246 while(!profiles.empty()) { 247 Profile profile = (Profile)profiles.pop(); 248 Collection rules = profile.getAddRules(); 249 Iterator it = rules.iterator(); 250 while (it.hasNext()) { 251 Rule rule = (Rule)it.next(); 252 if (rule.match(item)) 253 return rule.getAllow(); 254 } 255 } 256 return false; 257 } 258 259 public boolean isCreatable(MetaItem item) { 260 Stack profiles = getProfileStack(); 261 while (!profiles.empty()) { 262 Profile profile = (Profile)profiles.pop(); 263 Collection rules = profile.getCreateRules(); 264 Iterator it = rules.iterator(); 265 while (it.hasNext()) { 266 Rule rule = (Rule)it.next(); 267 if (rule.match(item)) 268 return rule.getAllow(); 269 } 270 } 271 return false; 272 } 273 274 public boolean isRemovable(MetaItem item) { 275 Stack profiles = getProfileStack(); 276 while(!profiles.empty()) { 277 Profile profile = (Profile)profiles.pop(); 278 Collection rules = profile.getRemoveRules(); 279 Iterator it = rules.iterator(); 280 while (it.hasNext()) { 281 Rule rule = (Rule)it.next(); 282 if (rule.match(item)) 283 return rule.getAllow(); 284 } 285 } 286 return false; 287 } 288 289 public static boolean isReadable(Collection profiles,MetaItem item) { 290 Iterator it = profiles.iterator(); 291 while(it.hasNext()) { 292 Profile profile = (Profile)it.next(); 293 if (profile.isReadable(item)) 294 return true; 295 } 296 return false; 297 } 298 299 public static boolean isWritable(Collection profiles,MetaItem item) { 300 Iterator it = profiles.iterator(); 301 while(it.hasNext()) { 302 Profile profile = (Profile)it.next(); 303 if (profile.isWritable(item)) 304 return true; 305 } 306 return false; 307 } 308 309 public static boolean isAddable(Collection profiles,MetaItem item) { 310 Iterator it = profiles.iterator(); 311 while(it.hasNext()) { 312 Profile profile = (Profile)it.next(); 313 if (profile.isAddable(item)) 314 return true; 315 } 316 return false; 317 } 318 319 public static boolean isCreatable(Collection profiles,MetaItem item) { 320 Iterator it = profiles.iterator(); 321 while(it.hasNext()) { 322 Profile profile = (Profile)it.next(); 323 if (profile.isCreatable(item)) 324 return true; 325 } 326 return false; 327 } 328 329 public static boolean isRemovable(Collection profiles,MetaItem item) { 330 Iterator it = profiles.iterator(); 331 while(it.hasNext()) { 332 Profile profile = (Profile)it.next(); 333 if (profile.isRemovable(item)) 334 return true; 335 } 336 return false; 337 } 338 339 protected void invalidateCache() { 340 UserAC ac = ((UserAC)ACManager.getACM().getAC("user")); 341 if (ac!=null) 342 ac.invalidateCache(); 343 } 344 345 public String toString() { 346 return name; 347 } 348 } 349 | Popular Tags |