KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > dao > generic > security > GenericGroupSecurityDAO


1 /*
2  * Copyright (c) Rafael Steil
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
45 import java.sql.PreparedStatement JavaDoc;
46 import java.sql.ResultSet JavaDoc;
47 import java.util.ArrayList JavaDoc;
48 import java.util.Arrays JavaDoc;
49 import java.util.Iterator JavaDoc;
50 import java.util.List JavaDoc;
51
52 import net.jforum.JForumExecutionContext;
53 import net.jforum.dao.generic.AutoKeys;
54 import net.jforum.entities.Group;
55 import net.jforum.entities.User;
56 import net.jforum.repository.RolesRepository;
57 import net.jforum.security.Role;
58 import net.jforum.security.RoleCollection;
59 import net.jforum.security.RoleValue;
60 import net.jforum.security.RoleValueCollection;
61 import net.jforum.security.UserSecurityHelper;
62 import net.jforum.util.preferences.SystemGlobals;
63
64 /**
65  * @author Rafael Steil
66  * @version $Id: GenericGroupSecurityDAO.java,v 1.8 2006/01/29 15:06:30 rafaelsteil Exp $
67  */

68 public class GenericGroupSecurityDAO extends AutoKeys implements net.jforum.dao.security.GroupSecurityDAO
69 {
70     /**
71      * @see net.jforum.dao.security.SecurityDAO#deleteAllRoles(int)
72      */

73     public void deleteAllRoles(int id) throws Exception JavaDoc
74     {
75         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(
76                 SystemGlobals.getSql("PermissionControl.deleteAllRoleValues"));
77         p.setInt(1, id);
78         p.executeUpdate();
79         p.close();
80         
81         p = JForumExecutionContext.getConnection().prepareStatement(
82                 SystemGlobals.getSql("PermissionControl.deleteAllGroupRoles"));
83         p.setInt(1, id);
84         p.executeUpdate();
85         p.close();
86     }
87
88     /**
89      * @see net.jforum.dao.security.SecurityDAO#deleteRole(int, java.lang.String)
90      */

91     public void deleteRole(int id, String JavaDoc roleName) throws Exception JavaDoc
92     {
93         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(
94                 SystemGlobals.getSql("PermissionControl.deleteGroupRole"));
95         p.setString(1, roleName);
96         p.setInt(2, id);
97         p.executeUpdate();
98         p.close();
99     }
100
101     /**
102      * @see net.jforum.dao.security.SecurityDAO#addRole(int, net.jforum.security.Role)
103      */

104     public void addRole(int id, Role role) throws Exception JavaDoc
105     {
106         this.addRole(id, role, null);
107     }
108
109     /**
110      * @see net.jforum.dao.security.SecurityDAO#addRole(int, net.jforum.security.Role, net.jforum.security.RoleValueCollection)
111      */

112     public void addRole(int id, Role role, RoleValueCollection roleValues) throws Exception JavaDoc
113     {
114         this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("PermissionControl.lastGeneratedRoleId"));
115         SecurityCommon.executeAddRole(SystemGlobals.getSql("PermissionControl.addGroupRole"), id, role,
116                 roleValues, this.supportAutoGeneratedKeys(), this.getAutoGeneratedKeysQuery());
117     }
118
119     /**
120      * @see net.jforum.dao.security.SecurityDAO#loadRoles(int)
121      */

122     public RoleCollection loadRoles(int id) throws Exception JavaDoc
123     {
124         return SecurityCommon.processLoadRoles(SystemGlobals.getSql("PermissionControl.loadGroupRoles"), id);
125     }
126
127     /**
128      * @see net.jforum.dao.security.SecurityDAO#addRoleValue(int, Role, RoleValueCollection)
129      */

130     public void addRoleValue(int id, Role role, RoleValueCollection rvc) throws Exception JavaDoc
131     {
132         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(
133                 SystemGlobals.getSql("PermissionControl.getRoleIdByName"));
134         p.setString(1, role.getName());
135         p.setInt(2, id);
136         
137         int roleId = -1;
138         
139         ResultSet JavaDoc rs = p.executeQuery();
140         if (rs.next()) {
141             roleId = rs.getInt("role_id");
142         }
143         
144         rs.close();
145         p.close();
146         
147         if (roleId == -1) {
148             this.addRole(id, role, rvc);
149         }
150         else {
151             p = JForumExecutionContext.getConnection().prepareStatement(
152                     SystemGlobals.getSql("PermissionControl.addRoleValues"));
153             p.setInt(1, roleId);
154             
155             for (Iterator JavaDoc iter = rvc.iterator(); iter.hasNext(); ) {
156                 RoleValue rv = (RoleValue)iter.next();
157                 p.setString(2, rv.getValue());
158                 p.setInt(3, rv.getType());
159                 p.executeUpdate();
160             }
161             
162             p.close();
163         }
164     }
165
166     public RoleCollection loadRolesByUserGroups(User user) throws Exception JavaDoc
167     {
168         List JavaDoc groups = user.getGroupsList();
169         
170         // For single group, we don't need to check for merged roles
171
if (groups.size() == 1) {
172             return (RoleCollection)this.loadGroupRoles(groups).get(0);
173         }
174         
175         // When the user is associated to more than one group, we
176
// should check the merged roles
177
int[] groupIds = this.getSortedGroupIds(groups);
178         
179         RoleCollection groupRoles = RolesRepository.getGroupRoles(groupIds);
180         
181         // Not cached yet? then do it now
182
if (groupRoles == null) {
183             List JavaDoc l = this.loadGroupRoles(groups);
184             
185             groupRoles = new RoleCollection();
186             UserSecurityHelper.mergeUserGroupRoles(groupRoles, l);
187             
188             RolesRepository.addMergedGroupRoles(groupIds, groupRoles);
189         }
190         
191         return groupRoles;
192     }
193     
194     /**
195      * Load roles from the groups.
196      * @param groups The groups to load the roles
197      * @throws Exception
198      */

199     private List JavaDoc loadGroupRoles(List JavaDoc groups) throws Exception JavaDoc
200     {
201         List JavaDoc groupRolesList = new ArrayList JavaDoc();
202         
203         for (Iterator JavaDoc iter = groups.iterator(); iter.hasNext(); ) {
204             Group g = (Group)iter.next();
205             
206             RoleCollection roles = RolesRepository.getGroupRoles(g.getId());
207             
208             if (roles == null) {
209                 roles = this.loadRoles(g.getId());
210                 RolesRepository.addGroupRoles(g.getId(), roles);
211             }
212             
213             groupRolesList.add(roles);
214         }
215         
216         return groupRolesList;
217     }
218     
219     private int[] getSortedGroupIds(List JavaDoc groups)
220     {
221         int[] groupsIds = new int[groups.size()];
222         int i = 0;
223         
224         for (Iterator JavaDoc iter = groups.iterator(); iter.hasNext(); ) {
225             groupsIds[i++] = ((Group)iter.next()).getId();
226         }
227         
228         Arrays.sort(groupsIds);
229         
230         return groupsIds;
231     }
232
233 }
234
Popular Tags