XMLPermissionControl.java
Upload User: gdxydsw
Upload Date: 2019-01-29
Package Size: 16721k
Code Size: 8k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright (c) JForum Team
  3.  * All rights reserved.
  4.  * 
  5.  * Redistribution and use in source and binary forms, 
  6.  * with or without modification, are permitted provided 
  7.  * that the following conditions are met:
  8.  * 
  9.  * 1) Redistributions of source code must retain the above 
  10.  * copyright notice, this list of conditions and the 
  11.  * following  disclaimer.
  12.  * 2)  Redistributions in binary form must reproduce the 
  13.  * above copyright notice, this list of conditions and 
  14.  * the following disclaimer in the documentation and/or 
  15.  * other materials provided with the distribution.
  16.  * 3) Neither the name of "Rafael Steil" nor 
  17.  * the names of its contributors may be used to endorse 
  18.  * or promote products derived from this software without 
  19.  * specific prior written permission.
  20.  * 
  21.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT 
  22.  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
  23.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
  24.  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
  25.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  26.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
  27.  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
  29.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
  30.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
  31.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
  32.  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
  33.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
  34.  * IN CONTRACT, STRICT LIABILITY, OR TORT 
  35.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
  36.  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
  37.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  38.  * 
  39.  * This file creation date: 21/09/2003 / 16:36:44
  40.  * The JForum Project
  41.  * http://www.jforum.net
  42.  */
  43. package net.jforum.security;
  44. import java.io.File;
  45. import java.sql.PreparedStatement;
  46. import java.sql.ResultSet;
  47. import java.util.ArrayList;
  48. import java.util.HashMap;
  49. import java.util.Iterator;
  50. import java.util.List;
  51. import java.util.Map;
  52. import javax.xml.parsers.SAXParser;
  53. import javax.xml.parsers.SAXParserFactory;
  54. import net.jforum.JForumExecutionContext;
  55. import net.jforum.exceptions.DatabaseException;
  56. import net.jforum.exceptions.ForumException;
  57. import net.jforum.util.DbUtils;
  58. import net.jforum.util.FormSelectedData;
  59. import net.jforum.util.I18n;
  60. import net.jforum.util.preferences.SystemGlobals;
  61. import org.xml.sax.Attributes;
  62. import org.xml.sax.InputSource;
  63. import org.xml.sax.SAXException;
  64. import org.xml.sax.SAXParseException;
  65. import org.xml.sax.helpers.DefaultHandler;
  66. /**
  67.  * Manipulates XML permission control file definition 
  68.  * 
  69.  * @author Rafael Steil
  70.  * @version $Id: XMLPermissionControl.java,v 1.18 2007/09/21 03:47:41 rafaelsteil Exp $
  71.  */
  72. public class XMLPermissionControl extends DefaultHandler 
  73. {
  74.     private PermissionSection section;
  75. private PermissionControl pc;
  76. private List listSections;
  77. private List permissionData;
  78. private Map queries;
  79. private String permissionName;
  80. private String permissionId;
  81. private String permissionType;
  82. private boolean alreadySelected;
  83. private static class SelectData
  84. {
  85. private int id;
  86. private String name;
  87. public SelectData(int id, String name)
  88. {
  89. this.id = id;
  90. this.name = name;
  91. }
  92. public int getId()
  93. {
  94. return this.id;
  95. }
  96. public String getName()
  97. {
  98. return this.name;
  99. }
  100. }
  101. public XMLPermissionControl(PermissionControl pc)
  102. {
  103. this.listSections = new ArrayList();
  104. this.permissionData = new ArrayList();
  105. this.queries = new HashMap();
  106. this.pc = pc;
  107. }
  108. /**
  109.  * @return <code>List</code> object containing <code>Section</code> objects. Each
  110.  * <code>Section</code>  contains many <code>PermissionItem</code> objects, 
  111.  * which represent the permission elements of some section. For its turn, the
  112.  * <code>PermissionItem</code> objects have many <code>FormSelectedData</code>
  113.  * objects, which are the ones responsible to store field values, and which values
  114.  * are checked and which not.
  115.      * @param xmlFile String
  116.  */
  117. public List loadConfigurations(String xmlFile)
  118. {
  119.         try
  120.         {
  121.             SAXParserFactory factory = SAXParserFactory.newInstance();
  122.             factory.setValidating(false);
  123.             SAXParser parser = factory.newSAXParser();
  124.             File fileInput = new File(xmlFile);
  125.             if (fileInput.exists()) {
  126.                 parser.parse(fileInput, this);
  127.             }
  128.             else {
  129.                 InputSource inputSource = new InputSource(xmlFile);
  130.                 parser.parse(inputSource, this);
  131.             }
  132.             return this.listSections;
  133.         }
  134.         catch (Exception e)
  135.         {
  136.             throw new ForumException(e);
  137.         }
  138.     }
  139. /**
  140.  * @see org.xml.sax.ContentHandler#endElement(String, String, String)
  141.  */
  142. public void endElement(String namespaceURI, String localName, String tag)
  143. throws SAXException 
  144. {
  145. if (tag.equals("section")) {
  146. this.listSections.add(this.section);
  147. }
  148. else if (tag.equals("permission")) {
  149. this.section.addPermission(new PermissionItem(this.permissionName, this.permissionId, this.permissionType, this.permissionData));
  150. this.permissionData = new ArrayList();
  151. }
  152. }
  153. /**
  154.  * @see org.xml.sax.ErrorHandler#error(SAXParseException)
  155.  */
  156. public void error(SAXParseException exception) throws SAXException 
  157. {
  158. throw exception;
  159. }
  160. /**
  161.  * @see org.xml.sax.ContentHandler#startElement(String, String, String, Attributes)
  162.  */
  163. public void startElement(
  164. String namespaceURI,
  165. String localName,
  166. String tag,
  167. Attributes atts)
  168. throws SAXException 
  169. {
  170. if (tag.equals("section")) {
  171. String title = I18n.getMessage(atts.getValue("title"));
  172. this.section = new PermissionSection(title, atts.getValue("id"));
  173. }
  174. else if (tag.equals("permission")) {
  175. String title = I18n.getMessage(atts.getValue("title"));
  176. this.permissionName = title;
  177. this.permissionId = atts.getValue("id");
  178. this.permissionType = atts.getValue("type");
  179. this.alreadySelected = false;
  180. }
  181. else if (tag.equals("sql")) {
  182. String refName = atts.getValue("refName");
  183. // If refName is present, then we have a template query
  184. if (refName != null) {
  185.                 ResultSet rs = null;
  186.                 PreparedStatement p = null;
  187.                 
  188. try {
  189. p = JForumExecutionContext.getConnection().prepareStatement(
  190. SystemGlobals.getSql(atts.getValue("queryName")));
  191. rs = p.executeQuery();
  192. String valueField = atts.getValue("valueField");
  193. String captionField = atts.getValue("captionField");
  194. List l = new ArrayList();
  195. while (rs.next()) {
  196. l.add(new SelectData(rs.getInt(valueField), rs.getString(captionField)));
  197. }
  198. this.queries.put(refName, l);
  199. }
  200. catch (Exception e) {
  201.                     throw new DatabaseException(e);
  202. }
  203. finally {
  204.                     DbUtils.close(rs, p);
  205. }
  206. }
  207. else {
  208. // If it gets here, then it should be a <sql ref="xxxx"> section
  209. RoleValueCollection roleValues = new RoleValueCollection();
  210. Role role = this.pc.getRole(this.permissionId);
  211. if (role != null) {
  212. roleValues = role.getValues();
  213. }
  214. List l = (List)this.queries.get(atts.getValue("ref"));
  215. for (Iterator iter = l.iterator(); iter.hasNext(); ) {
  216. SelectData data = (SelectData)iter.next();
  217. String id = Integer.toString(data.getId());
  218. RoleValue rv = roleValues.get(id);
  219. this.permissionData.add(new FormSelectedData(data.getName(), id, rv == null));
  220. }
  221. }
  222. }
  223. else if (tag.equals("option")) {
  224. boolean selected = false;
  225. if (this.permissionType.equals("single")) {
  226. if (this.pc.canAccess(this.permissionId) && atts.getValue("value").equals("allow") && !this.alreadySelected) {
  227. selected = true;
  228. this.alreadySelected = true;
  229. }
  230. }
  231. else {
  232. throw new UnsupportedOperationException("'option' tag with 'multiple' attribute support not yet implemented");
  233. }
  234. this.permissionData.add(new FormSelectedData(
  235. I18n.getMessage(atts.getValue("description")), atts.getValue("value"), selected));
  236. }
  237. }
  238. }