1 16 package org.apache.cocoon.portal.tools.service; 17 18 import java.io.IOException ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 import java.util.Properties ; 23 import java.util.StringTokenizer ; 24 25 import org.apache.avalon.framework.CascadingRuntimeException; 26 import org.apache.cocoon.matching.helpers.WildcardHelper; 27 import org.apache.cocoon.portal.profile.PortalUser; 28 import org.apache.cocoon.portal.tools.helper.MultipleRoleMatcher; 29 import org.apache.cocoon.portal.tools.helper.RoleMatcher; 30 import org.apache.cocoon.portal.tools.helper.SingleRoleMatcher; 31 import org.apache.excalibur.source.Source; 32 33 38 public class UserRightsService { 39 40 43 private Source location; 44 45 48 private Properties properties; 49 50 53 private long lastModified = -1; 54 55 58 private boolean reload = false; 59 60 63 private Map userrights; 64 65 68 69 public Source getLocation() { 70 return this.location; 71 } 72 73 76 77 public void setLocation(Source location) { 78 this.location = location; 79 } 80 81 84 public boolean getReload() { 85 return this.reload; 86 } 87 88 91 public void setReload(boolean reload) { 92 this.reload = reload; 93 } 94 95 98 public void initialize() { 99 boolean load; 100 101 if (this.properties == null) { 103 load = true; 104 } else { 105 load = this.reload; 107 } 108 109 try { 110 if (load) { 111 long lastModified = this.location.getLastModified(); 113 if (this.lastModified >= lastModified) { 114 load = false; 115 } 116 117 if (load) { 118 this.lastModified = lastModified; 119 this.properties = new Properties (); 120 this.properties.load(this.location.getInputStream()); 121 this.parseProperties(); 122 } 123 } 124 } catch (IOException e) { 125 throw new CascadingRuntimeException(e.getMessage(), e); 126 } 127 } 128 129 132 public boolean userIsAllowed(String url, PortalUser user) { 133 this.initialize(); 134 135 boolean isAllowed = true; 136 137 Iterator iterator = this.userrights.entrySet().iterator(); 139 Map.Entry entry; 140 int[] pattern; 141 RoleMatcher[] matcher; 142 while (iterator.hasNext() && isAllowed) { 143 entry = (Map.Entry )iterator.next(); 144 pattern = (int[])entry.getKey(); 145 146 if (WildcardHelper.match(new HashMap (), url, pattern)) { 148 matcher = (RoleMatcher[])entry.getValue(); 149 150 isAllowed = false; 151 152 int length = matcher.length; 153 for (int i = 0; i < length; i++) { 154 if (matcher[i].matches(user)) { 155 isAllowed = true; 156 } 157 } 158 } 159 } 160 161 return isAllowed; 162 } 163 164 public boolean userFunctionIsAllowed(String id, PortalUser user) { 165 this.initialize(); 166 167 boolean isAllowed = true; 168 169 Iterator iterator = this.userrights.entrySet().iterator(); 171 Map.Entry entry; 172 int[] pattern; 173 RoleMatcher[] matcher; 174 while (iterator.hasNext() && isAllowed) { 175 entry = (Map.Entry )iterator.next(); 176 pattern = (int[])entry.getKey(); 177 178 if (WildcardHelper.match(new HashMap (), id, pattern)) { 180 matcher = (RoleMatcher[])entry.getValue(); 181 182 isAllowed = false; 183 184 int length = matcher.length; 185 for (int i = 0; i < length; i++) { 186 if (matcher[i].matches(user)) { 187 isAllowed = true; 188 } 189 } 190 } 191 } 192 193 return isAllowed; 194 } 195 196 199 private void parseProperties() { 200 Map userrights = new HashMap (); 201 202 Iterator iterator = this.properties.entrySet().iterator(); 203 Map.Entry entry; 204 while (iterator.hasNext()) { 205 entry = (Map.Entry )iterator.next(); 206 userrights.put( 207 WildcardHelper.compilePattern((String )entry.getKey()), 208 this.buildRoles((String )entry.getValue())); 209 } 210 211 this.userrights = userrights; 212 } 213 214 217 private RoleMatcher[] buildRoles(String roles) { 218 StringTokenizer tokenizer = new StringTokenizer (roles, ",", false); 219 220 RoleMatcher[] result = new RoleMatcher[tokenizer.countTokens()]; 221 222 String token; 223 int i = 0; 224 while (tokenizer.hasMoreTokens()) { 225 token = tokenizer.nextToken(); 226 if (token.indexOf(MultipleRoleMatcher.ROLE_SEPARATOR) == -1) { 227 result[i] = new SingleRoleMatcher(token); 228 } else { 229 result[i] = new MultipleRoleMatcher(token); 230 } 231 i++; 232 } 233 234 return result; 235 } 236 } 237 | Popular Tags |