001 /* 002 Copyright (C) 2002 Renaud Pawlak <renaud@aopsys.com> 003 004 This program is free software; you can redistribute it and/or modify 005 it under the terms of the GNU Lesser General Public License as 006 published by the Free Software Foundation; either version 2 of the 007 License, or (at your option) any later version. 008 009 This program is distributed in the hope that it will be useful, 010 but WITHOUT ANY WARRANTY; without even the implied warranty of 011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 GNU Lesser General Public License for more details. 013 014 You should have received a copy of the GNU Lesser General Public License 015 along with this program; if not, write to the Free Software 016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 017 018 package org.objectweb.jac.aspects.user; 019 020 import java.util.Collection; 021 import java.util.Iterator; 022 import java.util.Vector; 023 import org.objectweb.jac.core.ACManager; 024 import org.objectweb.jac.core.rtti.MetaItem; 025 import org.objectweb.jac.util.Log; 026 import org.objectweb.jac.util.Stack; 027 import java.util.List; 028 029 public class Profile { 030 031 String name; 032 033 /** 034 * Get the value of name. 035 * @return value of name. 036 */ 037 public String getName() { 038 return name; 039 } 040 041 /** 042 * Set the value of name. 043 * @param v Value to assign to name. 044 */ 045 public void setName(String v) { 046 this.name = v; 047 } 048 049 /** inherited profile */ 050 Profile parent=null; 051 052 public void setParent(Profile parent) { 053 this.parent=parent; 054 invalidateCache(); 055 } 056 public Profile getParent() { 057 return parent; 058 } 059 060 transient boolean isNew = true; 061 public void setIsNew(boolean isNew) { 062 this.isNew = isNew; 063 } 064 public boolean isNew() { 065 return isNew; 066 } 067 068 /** 069 * Clear all rules 070 */ 071 public void clear() { 072 readRules.clear(); 073 writeRules.clear(); 074 addRules.clear(); 075 createRules.clear(); 076 removeRules.clear(); 077 invalidateCache(); 078 } 079 080 Vector readRules = new Vector(); 081 Vector writeRules = new Vector(); 082 Vector addRules = new Vector(); 083 Vector removeRules = new Vector(); 084 Vector createRules = new Vector(); 085 086 public List getReadRules() { 087 return readRules; 088 } 089 public void addReadRule(Rule rule) { 090 readRules.add(rule); 091 invalidateCache(); 092 } 093 public void removeReadRule(Rule rule) { 094 readRules.remove(rule); 095 invalidateCache(); 096 } 097 098 public List getWriteRules() { 099 return writeRules; 100 } 101 public void addWriteRule(Rule rule) { 102 writeRules.add(rule); 103 invalidateCache(); 104 } 105 public void removeWriteRule(Rule rule) { 106 writeRules.remove(rule); 107 invalidateCache(); 108 } 109 110 public List getAddRules() { 111 return addRules; 112 } 113 public void addAddRule(Rule rule) { 114 addRules.add(rule); 115 invalidateCache(); 116 } 117 public void removeAddRule(Rule rule) { 118 addRules.remove(rule); 119 invalidateCache(); 120 } 121 122 public List getCreateRules() { 123 return createRules; 124 } 125 public void addCreateRule(Rule rule) { 126 createRules.add(rule); 127 invalidateCache(); 128 } 129 public void removeCreateRule(Rule rule) { 130 createRules.remove(rule); 131 invalidateCache(); 132 } 133 134 public List getRemoveRules() { 135 return removeRules; 136 } 137 public void addRemoveRule(Rule rule) { 138 removeRules.add(rule); 139 invalidateCache(); 140 } 141 public void removeRemoveRule(Rule rule) { 142 removeRules.remove(rule); 143 invalidateCache(); 144 } 145 146 public void addReadable(String resourceExpr) { 147 readRules.add(new Rule(Rule.ALLOW,resourceExpr)); 148 invalidateCache(); 149 } 150 151 public void addUnreadable(String resourceExpr) { 152 readRules.add(new Rule(Rule.DENY,resourceExpr)); 153 invalidateCache(); 154 } 155 156 public void addWritable(String resourceExpr) { 157 writeRules.add(new Rule(Rule.ALLOW,resourceExpr)); 158 invalidateCache(); 159 } 160 161 public void addUnwritable(String resourceExpr) { 162 writeRules.add(new Rule(Rule.DENY,resourceExpr)); 163 invalidateCache(); 164 } 165 166 public void addAddable(String resourceExpr) { 167 addRules.add(new Rule(Rule.ALLOW,resourceExpr)); 168 invalidateCache(); 169 } 170 171 public void addCreatable(String resourceExpr) { 172 createRules.add(new Rule(Rule.ALLOW,resourceExpr)); 173 invalidateCache(); 174 } 175 176 public void addUnaddable(String resourceExpr) { 177 addRules.add(new Rule(Rule.DENY,resourceExpr)); 178 invalidateCache(); 179 } 180 181 public void addRemovable(String resourceExpr) { 182 removeRules.add(new Rule(Rule.ALLOW,resourceExpr)); 183 invalidateCache(); 184 } 185 186 public void addUnremovable(String resourceExpr) { 187 removeRules.add(new Rule(Rule.DENY,resourceExpr)); 188 invalidateCache(); 189 } 190 191 public Stack getProfileStack() { 192 Stack profiles = new Stack(); 193 Profile profile = this; 194 while (profile!=null) { 195 profiles.push(profile); 196 profile = profile.getParent(); 197 } 198 return profiles; 199 } 200 201 public boolean isReadable(MetaItem item) { 202 Stack profiles = getProfileStack(); 203 while(!profiles.empty()) { 204 Profile profile = (Profile)profiles.pop(); 205 Collection rules = profile.getReadRules(); 206 Iterator it = rules.iterator(); 207 while (it.hasNext()) { 208 Rule rule = (Rule)it.next(); 209 if (rule.match(item)) 210 return rule.getAllow(); 211 } 212 } 213 return false; 214 } 215 216 public boolean isWritable(MetaItem item) { 217 Log.trace("profile",2,"isWritable "+item); 218 Stack profiles = getProfileStack(); 219 while(!profiles.empty()) { 220 Profile profile = (Profile)profiles.pop(); 221 Log.trace("profile",2,"Current profile "+profile); 222 Collection rules = profile.getWriteRules(); 223 Iterator it = rules.iterator(); 224 while (it.hasNext()) { 225 Rule rule = (Rule)it.next(); 226 if (rule.match(item)) 227 return rule.getAllow(); 228 } 229 } 230 return false; 231 } 232 233 public boolean isAddable(MetaItem item) { 234 Stack profiles = getProfileStack(); 235 while(!profiles.empty()) { 236 Profile profile = (Profile)profiles.pop(); 237 Collection rules = profile.getAddRules(); 238 Iterator it = rules.iterator(); 239 while (it.hasNext()) { 240 Rule rule = (Rule)it.next(); 241 if (rule.match(item)) 242 return rule.getAllow(); 243 } 244 } 245 return false; 246 } 247 248 public boolean isCreatable(MetaItem item) { 249 Stack profiles = getProfileStack(); 250 while (!profiles.empty()) { 251 Profile profile = (Profile)profiles.pop(); 252 Collection rules = profile.getCreateRules(); 253 Iterator it = rules.iterator(); 254 while (it.hasNext()) { 255 Rule rule = (Rule)it.next(); 256 if (rule.match(item)) 257 return rule.getAllow(); 258 } 259 } 260 return false; 261 } 262 263 public boolean isRemovable(MetaItem item) { 264 Stack profiles = getProfileStack(); 265 while(!profiles.empty()) { 266 Profile profile = (Profile)profiles.pop(); 267 Collection rules = profile.getRemoveRules(); 268 Iterator it = rules.iterator(); 269 while (it.hasNext()) { 270 Rule rule = (Rule)it.next(); 271 if (rule.match(item)) 272 return rule.getAllow(); 273 } 274 } 275 return false; 276 } 277 278 public static boolean isReadable(Collection profiles,MetaItem item) { 279 Iterator it = profiles.iterator(); 280 while(it.hasNext()) { 281 Profile profile = (Profile)it.next(); 282 if (profile.isReadable(item)) 283 return true; 284 } 285 return false; 286 } 287 288 public static boolean isWritable(Collection profiles,MetaItem item) { 289 Iterator it = profiles.iterator(); 290 while(it.hasNext()) { 291 Profile profile = (Profile)it.next(); 292 if (profile.isWritable(item)) 293 return true; 294 } 295 return false; 296 } 297 298 public static boolean isAddable(Collection profiles,MetaItem item) { 299 Iterator it = profiles.iterator(); 300 while(it.hasNext()) { 301 Profile profile = (Profile)it.next(); 302 if (profile.isAddable(item)) 303 return true; 304 } 305 return false; 306 } 307 308 public static boolean isCreatable(Collection profiles,MetaItem item) { 309 Iterator it = profiles.iterator(); 310 while(it.hasNext()) { 311 Profile profile = (Profile)it.next(); 312 if (profile.isCreatable(item)) 313 return true; 314 } 315 return false; 316 } 317 318 public static boolean isRemovable(Collection profiles,MetaItem item) { 319 Iterator it = profiles.iterator(); 320 while(it.hasNext()) { 321 Profile profile = (Profile)it.next(); 322 if (profile.isRemovable(item)) 323 return true; 324 } 325 return false; 326 } 327 328 protected void invalidateCache() { 329 UserAC ac = ((UserAC)ACManager.getACM().getAC("user")); 330 if (ac!=null) 331 ac.invalidateCache(); 332 } 333 334 public String toString() { 335 return name; 336 } 337 }