1 43 package net.jforum.security; 44 45 import java.io.File ; 46 import java.sql.PreparedStatement ; 47 import java.sql.ResultSet ; 48 import java.util.ArrayList ; 49 import java.util.HashMap ; 50 import java.util.Iterator ; 51 import java.util.List ; 52 import java.util.Map ; 53 54 import javax.xml.parsers.SAXParser ; 55 import javax.xml.parsers.SAXParserFactory ; 56 57 import net.jforum.JForumExecutionContext; 58 import net.jforum.exceptions.XMLException; 59 import net.jforum.util.FormSelectedData; 60 import net.jforum.util.preferences.SystemGlobals; 61 62 import org.xml.sax.Attributes ; 63 import org.xml.sax.InputSource ; 64 import org.xml.sax.SAXException ; 65 import org.xml.sax.SAXParseException ; 66 import org.xml.sax.helpers.DefaultHandler ; 67 68 74 public class XMLPermissionControl extends DefaultHandler 75 { 76 private PermissionSection section; 77 78 private PermissionControl pc; 79 80 private List listSections; 81 private List permissionData; 82 83 private Map queries; 84 85 private String permissionName; 86 private String permissionId; 87 private String permissionType; 88 89 private boolean alreadySelected; 90 91 private static class SelectData 92 { 93 private int id; 94 private String name; 95 96 public SelectData(int id, String name) 97 { 98 this.id = id; 99 this.name = name; 100 } 101 102 public int getId() 103 { 104 return this.id; 105 } 106 107 public String getName() 108 { 109 return this.name; 110 } 111 } 112 113 public XMLPermissionControl(PermissionControl pc) 114 { 115 this.listSections = new ArrayList (); 116 this.permissionData = new ArrayList (); 117 this.queries = new HashMap (); 118 this.pc = pc; 119 } 120 121 129 public List loadConfigurations(String xmlFile) 130 throws Exception 131 { 132 SAXParserFactory factory = SAXParserFactory.newInstance(); 133 factory.setValidating(false); 134 135 SAXParser parser = factory.newSAXParser(); 136 File fileInput = new File (xmlFile); 137 138 if (fileInput.exists()) { 139 parser.parse(fileInput, this); 140 } 141 else { 142 InputSource inputSource = new InputSource (xmlFile); 143 parser.parse(inputSource, this); 144 } 145 146 return this.listSections; 147 } 148 149 152 public void endElement(String namespaceURI, String localName, String tag) 153 throws SAXException 154 { 155 if (tag.equals("section")) { 156 this.listSections.add(this.section); 157 } 158 else if (tag.equals("permission")) { 159 this.section.addPermission(new PermissionItem(this.permissionName, this.permissionId, this.permissionType, this.permissionData)); 160 161 this.permissionData = new ArrayList (); 162 } 163 } 164 165 168 public void error(SAXParseException exception) throws SAXException 169 { 170 throw exception; 171 } 172 173 176 public void startElement( 177 String namespaceURI, 178 String localName, 179 String tag, 180 Attributes atts) 181 throws SAXException 182 { 183 if (tag.equals("section")) { 184 this.section = new PermissionSection(atts.getValue("title"), atts.getValue("id")); 185 } 186 else if (tag.equals("permission")) { 187 this.permissionName = atts.getValue("title"); 188 this.permissionId = atts.getValue("id"); 189 this.permissionType = atts.getValue("type"); 190 this.alreadySelected = false; 191 } 192 else if (tag.equals("sql")) { 193 ResultSet rs = null; 194 PreparedStatement p = null; 195 196 String refName = atts.getValue("refName"); 197 198 if (refName != null) { 200 try { 201 p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql(atts.getValue("queryName"))); 202 rs = p.executeQuery(); 203 204 String valueField = atts.getValue("valueField"); 205 String captionField = atts.getValue("captionField"); 206 207 List l = new ArrayList (); 208 209 while (rs.next()) { 210 l.add(new SelectData(rs.getInt(valueField), rs.getString(captionField))); 211 } 212 213 this.queries.put(refName, l); 214 } 215 catch (Exception e) { 216 throw new XMLException(e); 217 } 218 finally { 219 try { 220 if (rs != null) { 221 rs.close(); 222 p.close(); 223 } 224 } 225 catch (Exception e) { 226 throw new XMLException(e); 227 } 228 } 229 } 230 else { 231 RoleValueCollection roleValues = new RoleValueCollection(); 233 Role role = this.pc.getRole(this.permissionId); 234 235 if (role != null) { 236 roleValues = role.getValues(); 237 } 238 239 List l = (List )this.queries.get(atts.getValue("ref")); 240 241 for (Iterator iter = l.iterator(); iter.hasNext(); ) { 242 SelectData data = (SelectData)iter.next(); 243 244 String id = Integer.toString(data.getId()); 245 RoleValue rv = roleValues.get(id); 246 247 this.permissionData.add( 248 new FormSelectedData( 249 data.getName(), 250 id, 251 rv != null && rv.getType() == PermissionControl.ROLE_DENY 252 ) 253 ); 254 } 255 } 256 } 257 else if (tag.equals("option")) { 258 boolean selected = false; 259 260 if (this.permissionType.equals("single")) { 261 if (this.pc.canAccess(this.permissionId) && atts.getValue("value").equals("allow") && !this.alreadySelected) { 262 selected = true; 263 this.alreadySelected = true; 264 } 265 } 266 else { 267 throw new UnsupportedOperationException ("'option' tag with 'multiple' attribute support not yet implemented"); 269 } 270 271 this.permissionData.add(new FormSelectedData(atts.getValue("description"), atts.getValue("value"), selected)); 272 } 273 } 274 275 } | Popular Tags |