1 43 package net.jforum.dao.generic.security; 44 45 import java.sql.PreparedStatement ; 46 import java.sql.ResultSet ; 47 import java.sql.SQLException ; 48 import java.sql.Statement ; 49 import java.util.Iterator ; 50 51 import net.jforum.JForumExecutionContext; 52 import net.jforum.security.PermissionControl; 53 import net.jforum.security.Role; 54 import net.jforum.security.RoleCollection; 55 import net.jforum.security.RoleValue; 56 import net.jforum.security.RoleValueCollection; 57 import net.jforum.util.preferences.SystemGlobals; 58 59 63 public class SecurityCommon 64 { 65 83 public static void executeAddRole(String sql, int id, Role role, RoleValueCollection roleValues, 84 boolean supportAutoGeneratedKeys, String autoKeysQuery) throws Exception 85 { 86 PreparedStatement p; 87 88 if (supportAutoGeneratedKeys) { 89 p = JForumExecutionContext.getConnection().prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); 90 } 91 else { 92 p = JForumExecutionContext.getConnection().prepareStatement(sql); 93 } 94 95 p.setInt(1, id); 96 p.setString(2, role.getName()); 97 p.setInt(3, role.getType()); 98 99 p.executeUpdate(); 100 101 if (roleValues != null) { 102 int roleId = -1; 103 104 if (supportAutoGeneratedKeys) { 105 ResultSet rs = p.getGeneratedKeys(); 106 rs.next(); 107 roleId = rs.getInt(1); 108 rs.close(); 109 } 110 else { 111 p = JForumExecutionContext.getConnection().prepareStatement(autoKeysQuery); 112 ResultSet rs = p.executeQuery(); 113 if (rs.next()) { 114 roleId = rs.getInt(1); 115 } 116 rs.close(); 117 } 118 119 if (roleId == -1) { 120 p.close(); 121 throw new SQLException ("Could not obtain the latest role id"); 122 } 123 124 p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PermissionControl.addRoleValues")); 125 126 for (Iterator iter = roleValues.iterator(); iter.hasNext(); ) { 127 RoleValue rv = (RoleValue)iter.next(); 128 129 p.setInt(1, roleId); 130 p.setString(2, rv.getValue()); 131 p.setInt(3, rv.getType()); 132 133 p.executeUpdate(); 134 } 135 } 136 137 p.close(); 138 } 139 140 149 public static RoleCollection processLoadRoles(String sql, int id) throws Exception 150 { 151 RoleCollection rc = new RoleCollection(); 152 153 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(sql); 154 p.setInt(1, id); 155 156 Role r = null; 157 int lastId = -1; 158 159 ResultSet rs = p.executeQuery(); 160 while (rs.next()) { 161 if (rs.getInt("role_id") != lastId) { 162 if (r != null) { 163 rc.add(r); 164 } 165 166 r = new Role(); 167 168 r.setGroupId(id); 169 r.setName(rs.getString("name")); 170 r.setType(rs.getInt("role_type")); 171 r.setId(rs.getInt("role_id")); 172 173 lastId = r.getId(); 174 } 175 176 if (rs.getString("role_value") != null) { 177 RoleValue rv = new RoleValue(); 178 rv.setRoleId(r.getId()); 179 rv.setType(rs.getInt("rv_type")); 180 rv.setValue(rs.getString("role_value")); 181 182 r.getValues().add(rv); 183 } 184 } 185 186 if (r != null) { 187 rc.add(r); 188 } 189 190 rs.close(); 191 p.close(); 192 193 return rc; 194 } 195 } 196 | Popular Tags |