GenericGroupSecurityDAO.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: 19/03/2004 - 18:44:56
  40.  * The JForum Project
  41.  * http://www.jforum.net
  42.  */
  43. package net.jforum.dao.generic.security;
  44. import java.sql.PreparedStatement;
  45. import java.sql.ResultSet;
  46. import java.sql.SQLException;
  47. import java.util.ArrayList;
  48. import java.util.Arrays;
  49. import java.util.Iterator;
  50. import java.util.List;
  51. import net.jforum.JForumExecutionContext;
  52. import net.jforum.dao.GroupSecurityDAO;
  53. import net.jforum.dao.generic.AutoKeys;
  54. import net.jforum.entities.Group;
  55. import net.jforum.entities.User;
  56. import net.jforum.exceptions.DatabaseException;
  57. import net.jforum.repository.RolesRepository;
  58. import net.jforum.security.Role;
  59. import net.jforum.security.RoleCollection;
  60. import net.jforum.security.RoleValue;
  61. import net.jforum.security.RoleValueCollection;
  62. import net.jforum.util.DbUtils;
  63. import net.jforum.util.preferences.SystemGlobals;
  64. import org.apache.commons.lang.StringUtils;
  65. /**
  66.  * @author Rafael Steil
  67.  * @version $Id: GenericGroupSecurityDAO.java,v 1.16 2007/08/25 00:11:29 rafaelsteil Exp $
  68.  */
  69. public class GenericGroupSecurityDAO extends AutoKeys implements GroupSecurityDAO
  70. {
  71. private List selectForumRoles(int forumId) 
  72. {
  73. List l = new ArrayList();
  74. PreparedStatement p = null;
  75. ResultSet rs = null;
  76. try {
  77. p = JForumExecutionContext.getConnection().prepareStatement(
  78. SystemGlobals.getSql("PermissionControl.selectForumRoles"));
  79. p.setString(1, String.valueOf(forumId));
  80. rs = p.executeQuery();
  81. while (rs.next()) {
  82. l.add(new Integer(rs.getInt("role_id")));
  83. }
  84. }
  85. catch (SQLException e) {
  86. throw new DatabaseException(e);
  87. }
  88. finally {
  89. DbUtils.close(rs, p);
  90. }
  91. return l;
  92. }
  93. public void deleteForumRoles(int forumId) 
  94. {
  95. PreparedStatement p = null;
  96. List roleIds = this.selectForumRoles(forumId);
  97. try {
  98. StringBuffer ids = new StringBuffer();
  99. for (Iterator iterator = roleIds.iterator(); iterator.hasNext();) {
  100. Integer id = (Integer)iterator.next();
  101. ids.append(id).append(',');
  102. }
  103. ids.append("-1");
  104. // Role values
  105. String sql = SystemGlobals.getSql("PermissionControl.deleteRoleValues");
  106. sql = StringUtils.replace(sql, "#IDS#", ids.toString());
  107. p = JForumExecutionContext.getConnection().prepareStatement(sql);
  108. p.setString(1, String.valueOf(forumId));
  109. p.executeUpdate();
  110. }
  111. catch (SQLException e) {
  112. throw new DatabaseException(e);
  113. }
  114. finally {
  115. DbUtils.close(p);
  116. }
  117. }
  118. /**
  119.  * @see net.jforum.dao.security.SecurityDAO#deleteAllRoles(int)
  120.  */
  121. public void deleteAllRoles(int groupId)
  122. {
  123. PreparedStatement p = null;
  124. try {
  125. p = JForumExecutionContext.getConnection().prepareStatement(
  126. SystemGlobals.getSql("PermissionControl.deleteAllRoleValues"));
  127. p.setInt(1, groupId);
  128. p.executeUpdate();
  129. p.close();
  130. p = JForumExecutionContext.getConnection().prepareStatement(
  131. SystemGlobals.getSql("PermissionControl.deleteAllGroupRoles"));
  132. p.setInt(1, groupId);
  133. p.executeUpdate();
  134. }
  135. catch (SQLException e) {
  136. throw new DatabaseException(e);
  137. }
  138. finally {
  139. DbUtils.close(p);
  140. }
  141. }
  142. /**
  143.  * @see net.jforum.dao.security.SecurityDAO#addRole(int, net.jforum.security.Role)
  144.  */
  145. public void addRole(int id, Role role)
  146. {
  147. this.addRole(id, role, null);
  148. }
  149. /**
  150.  * @see net.jforum.dao.security.SecurityDAO#addRole(int, net.jforum.security.Role,
  151.  *      net.jforum.security.RoleValueCollection)
  152.  */
  153. public void addRole(int id, Role role, RoleValueCollection roleValues)
  154. {
  155. this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("PermissionControl.lastGeneratedRoleId"));
  156. SecurityCommon.executeAddRole(SystemGlobals.getSql("PermissionControl.addGroupRole"), id, role, roleValues,
  157. this.supportAutoGeneratedKeys(), this.getAutoGeneratedKeysQuery());
  158. }
  159. /**
  160.  * @see net.jforum.dao.security.SecurityDAO#loadRoles(int)
  161.  */
  162. public RoleCollection loadRoles(int groupId)
  163. {
  164. return this.loadRoles(new int[] { groupId });
  165. }
  166. protected RoleCollection loadRoles(int[] groupIds)
  167. {
  168. String sql = SystemGlobals.getSql("PermissionControl.loadGroupRoles");
  169. String groupIdAsString = SecurityCommon.groupIdAsString(groupIds);
  170. if ("".equals(groupIdAsString)) {
  171. // We suppose there is no "negative" group ids
  172. sql = sql.replaceAll("#IN#", "-1");
  173. }
  174. else {
  175. sql = sql.replaceAll("#IN#", groupIdAsString);
  176. }
  177. RoleCollection roles = new RoleCollection();
  178. PreparedStatement p = null;
  179. ResultSet rs = null;
  180. try {
  181. p = JForumExecutionContext.getConnection().prepareStatement(sql);
  182. rs = p.executeQuery();
  183. roles = SecurityCommon.loadRoles(rs);
  184. }
  185. catch (Exception e) {
  186. throw new DatabaseException(e);
  187. }
  188. finally {
  189. DbUtils.close(rs, p);
  190. }
  191. return roles;
  192. }
  193. /**
  194.  * @see net.jforum.dao.GroupSecurityDAO#addRoleValue(int, net.jforum.security.Role, net.jforum.security.RoleValueCollection)
  195.  */
  196. public void addRoleValue(int groupId, Role role, RoleValueCollection rvc)
  197. {
  198. PreparedStatement p = null;
  199. ResultSet rs = null;
  200. try {
  201. p = JForumExecutionContext.getConnection().prepareStatement(
  202. SystemGlobals.getSql("PermissionControl.getRoleIdByName"));
  203. p.setString(1, role.getName());
  204. p.setInt(2, groupId);
  205. int roleId = -1;
  206. rs = p.executeQuery();
  207. if (rs.next()) {
  208. roleId = rs.getInt("role_id");
  209. }
  210. rs.close();
  211. rs = null;
  212. p.close();
  213. p = null;
  214. if (roleId == -1) {
  215. this.addRole(groupId, role, rvc);
  216. }
  217. else {
  218. p = JForumExecutionContext.getConnection().prepareStatement(
  219. SystemGlobals.getSql("PermissionControl.addRoleValues"));
  220. p.setInt(1, roleId);
  221. for (Iterator iter = rvc.iterator(); iter.hasNext();) {
  222. RoleValue rv = (RoleValue) iter.next();
  223. p.setString(2, rv.getValue());
  224. p.executeUpdate();
  225. }
  226. }
  227. }
  228. catch (SQLException e) {
  229. throw new DatabaseException(e);
  230. }
  231. finally {
  232. DbUtils.close(rs, p);
  233. }
  234. }
  235. /**
  236.  * @see net.jforum.dao.GroupSecurityDAO#loadRolesByUserGroups(net.jforum.entities.User)
  237.  */
  238. public RoleCollection loadRolesByUserGroups(User user)
  239. {
  240. List groups = user.getGroupsList();
  241. // When the user is associated to more than one group, we
  242. // should check the merged roles
  243. int[] groupIds = this.getSortedGroupIds(groups);
  244. RoleCollection groupRoles = RolesRepository.getGroupRoles(groupIds);
  245. // Not cached yet? then do it now
  246. if (groupRoles == null) {
  247. groupRoles = this.loadRoles(groupIds);
  248. RolesRepository.addGroupRoles(groupIds, groupRoles);
  249. }
  250. return groupRoles;
  251. }
  252. private int[] getSortedGroupIds(List groups)
  253. {
  254. int[] groupsIds = new int[groups.size()];
  255. int i = 0;
  256. for (Iterator iter = groups.iterator(); iter.hasNext();) {
  257. groupsIds[i++] = ((Group)iter.next()).getId();
  258. }
  259. Arrays.sort(groupsIds);
  260. return groupsIds;
  261. }
  262. }