1 16 package dlog4j.security; 17 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.util.List ; 21 import java.util.Vector ; 22 23 import org.apache.commons.digester.Digester; 24 import org.apache.commons.digester.ExtendedBaseRules; 25 import org.xml.sax.SAXException ; 26 27 import web.security.Operation; 28 import web.security.Privilege; 29 import web.security.Range; 30 import web.security.Resource; 31 import web.security.Role; 32 import web.security.impl.OperationImpl; 33 import web.security.impl.PrivilegeImpl; 34 import web.security.impl.RangeImpl; 35 import web.security.impl.ResourceImpl; 36 37 42 public class SecurityConfig { 43 44 public final static String root = "web-security"; 45 private static SecurityConfig config; 47 48 static{ 49 Digester dig = new Digester(); 50 dig.setValidating(false); 51 dig.setRules(new ExtendedBaseRules()); 52 53 dig.addObjectCreate(root, SecurityConfig.class); 54 dig.addSetProperties(root); 55 56 String key_range = root+"/ranges/range"; 58 dig.addObjectCreate(key_range, "type", RangeImpl.class); 59 dig.addSetProperties(key_range); 60 dig.addBeanPropertySetter(key_range + "/?"); 61 dig.addSetNext(key_range, "addRange"); 62 63 String key_opt = root+"/operations/operation"; 65 dig.addObjectCreate(key_opt, "type", OperationImpl.class); 66 dig.addSetProperties(key_opt); 67 dig.addBeanPropertySetter(key_opt + "/?"); 68 dig.addSetNext(key_opt, "addOperation"); 69 70 String key_res = root+"/resources/resource"; 72 dig.addObjectCreate(key_res, "type", ResourceImpl.class); 73 dig.addSetProperties(key_res); 74 dig.addBeanPropertySetter(key_res + "/?"); 75 dig.addSetNext(key_res, "addResource"); 76 77 InputStream in = SecurityConfig.class.getResourceAsStream("/security.xml"); 78 try{ 79 config = (SecurityConfig) dig.parse(in); 80 }catch(Exception e){ 81 System.err.println("Initialized security.xml failed."); 82 e.printStackTrace(System.err); 83 }finally{ 84 try{ 85 in.close(); 86 }catch(Exception e){} 87 } 88 89 } 90 91 private List resources; 92 private List operations; 93 private List ranges; 94 private List roles; 95 96 99 public SecurityConfig() { 100 resources = new Vector (); 101 operations = new Vector (); 102 ranges = new Vector (); 103 roles = new Vector (); 104 } 105 106 public Range getRangeByName(String name){ 107 for(int i=0;i<ranges.size();i++){ 108 Range range = (Range)ranges.get(i); 109 if(range.getName().equals(name)) 110 return range; 111 } 112 return null; 113 } 114 115 public Operation getOperationByName(String name){ 116 for(int i=0;i<operations.size();i++){ 117 Operation opt = (Operation)operations.get(i); 118 if(opt.getName().equals(name)) 119 return opt; 120 } 121 return null; 122 } 123 124 public Resource getResourceByName(String name){ 125 for(int i=0;i<resources.size();i++){ 126 Resource res = (Resource)resources.get(i); 127 if(res.getName().equals(name)) 128 return res; 129 } 130 return null; 131 } 132 133 138 public DlogRole getRoleById(int id){ 139 for(int i=0;i<getRoles().size();i++){ 140 DlogRole role = (DlogRole)roles.get(i); 141 if(role.getId()==id) 142 return role; 143 } 144 return null; 145 } 146 147 151 public void addRange(Range range){ 152 ranges.add(range); 153 } 154 155 159 public void addOperation(Operation opt){ 160 operations.add(opt); 161 } 162 163 167 public void addResource(Resource res){ 168 resources.add(res); 169 } 170 171 175 public void addRole(Role role){ 176 roles.add(role); 177 } 178 179 186 public static SecurityConfig getConfig() throws IOException , SAXException { 187 return config; 188 } 189 190 public List getOperations() { 191 return operations; 192 } 193 public List getRanges() { 194 return ranges; 195 } 196 public List getResources() { 197 return resources; 198 } 199 public List getRoles() { 200 if(roles.size()==0){ 201 Digester dig = new Digester(); 202 dig.setValidating(false); 203 dig.setRules(new ExtendedBaseRules()); 204 205 dig.addObjectCreate(root, SecurityConfig.class); 206 dig.addSetProperties(root); 207 208 String key_role = root+"/roles/role"; 210 dig.addObjectCreate(key_role, "type", DlogRole.class); 211 dig.addSetProperties(key_role); 212 String key_pvg = key_role + "/privileges/privilege"; 214 dig.addCallMethod(key_pvg, "addPrivilege", 3); 215 dig.addCallParam(key_pvg+"/resource",0); 216 dig.addCallParam(key_pvg+"/operation",1); 217 dig.addCallParam(key_pvg+"/range",2); 218 dig.addSetNext(key_role, "addRole"); 219 220 InputStream in = SecurityConfig.class.getResourceAsStream("/security.xml"); 221 try{ 222 SecurityConfig config2 = (SecurityConfig) dig.parse(in); 223 config.roles.addAll(config2.getRoles()); 224 }catch(Exception e){ 225 System.err.println("Initialized security.xml failed."); 226 e.printStackTrace(System.err); 227 }finally{ 228 try{ 229 in.close(); 230 }catch(Exception e){} 231 } 232 } 233 return roles; 234 } 235 236 public static void main(String [] args) throws IOException , SAXException { 237 SecurityConfig config = SecurityConfig.getConfig(); 238 Role guest = config.getRoleById(DlogRole.ROLE_GUEST); 239 Role user = config.getRoleById(DlogRole.ROLE_COMMON); 240 Role friend = config.getRoleById(DlogRole.ROLE_FRIEND); 241 Role buddy = config.getRoleById(DlogRole.ROLE_BUDDY); 242 Role manager = config.getRoleById(DlogRole.ROLE_MANAGER); 243 250 251 Resource res = config.getResourceByName("log"); 252 Operation opt = config.getOperationByName("maintain"); 253 Range range = config.getRangeByName("all"); 254 255 Privilege pvg = new PrivilegeImpl(res,opt,range); 256 257 System.out.println("guest cando: "+guest.canDo(pvg)); 258 System.out.println("user cando: "+user.canDo(pvg)); 259 System.out.println("friend cando: "+friend.canDo(pvg)); 260 System.out.println("buddy cando: "+buddy.canDo(pvg)); 261 System.out.println("manager cando: "+manager.canDo(pvg)); 262 263 264 276 } 277 278 } 279 | Popular Tags |