1 43 package net.jforum.dao.generic.security; 44 45 import java.sql.PreparedStatement ; 46 import java.sql.ResultSet ; 47 import java.util.ArrayList ; 48 import java.util.Arrays ; 49 import java.util.Iterator ; 50 import java.util.List ; 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 68 public class GenericGroupSecurityDAO extends AutoKeys implements net.jforum.dao.security.GroupSecurityDAO 69 { 70 73 public void deleteAllRoles(int id) throws Exception 74 { 75 PreparedStatement 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 91 public void deleteRole(int id, String roleName) throws Exception 92 { 93 PreparedStatement 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 104 public void addRole(int id, Role role) throws Exception 105 { 106 this.addRole(id, role, null); 107 } 108 109 112 public void addRole(int id, Role role, RoleValueCollection roleValues) throws Exception 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 122 public RoleCollection loadRoles(int id) throws Exception 123 { 124 return SecurityCommon.processLoadRoles(SystemGlobals.getSql("PermissionControl.loadGroupRoles"), id); 125 } 126 127 130 public void addRoleValue(int id, Role role, RoleValueCollection rvc) throws Exception 131 { 132 PreparedStatement 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 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 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 167 { 168 List groups = user.getGroupsList(); 169 170 if (groups.size() == 1) { 172 return (RoleCollection)this.loadGroupRoles(groups).get(0); 173 } 174 175 int[] groupIds = this.getSortedGroupIds(groups); 178 179 RoleCollection groupRoles = RolesRepository.getGroupRoles(groupIds); 180 181 if (groupRoles == null) { 183 List 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 199 private List loadGroupRoles(List groups) throws Exception 200 { 201 List groupRolesList = new ArrayList (); 202 203 for (Iterator 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 groups) 220 { 221 int[] groupsIds = new int[groups.size()]; 222 int i = 0; 223 224 for (Iterator 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 |