Fixing the user based policies

revert-70aa11f8
geethkokila 9 years ago
parent 34a8641582
commit f7abe86e3c

@ -31,5 +31,6 @@ public interface PolicyFilter {
void filterDeviceTypeBasedPolicies(String deviceType, List<Policy> policies);
void filterUserBasedPolicies(List<String> usernames, List<Policy> policies);
}

@ -19,9 +19,11 @@
package org.wso2.carbon.policy.mgt.core.dao;
import org.wso2.carbon.device.mgt.core.dto.Device;
import org.wso2.carbon.policy.mgt.common.*;
import org.wso2.carbon.policy.mgt.common.Criterion;
import org.wso2.carbon.policy.mgt.common.Policy;
import org.wso2.carbon.policy.mgt.common.PolicyCriterion;
import org.wso2.carbon.policy.mgt.common.ProfileFeature;
import java.sql.Date;
import java.util.List;
public interface PolicyDAO {
@ -38,12 +40,6 @@ public interface PolicyDAO {
boolean updatePolicyPriorities(List<Policy> policies) throws PolicyManagerDAOException;
// Policy addDatesToPolicy(Date startDate, Date endDate, Policy policy) throws PolicyManagerDAOException;
//
// Policy addTimesToPolicy(int startTime, int endTime, Policy policy) throws PolicyManagerDAOException;
//
// Policy addLocationToPolicy(String latitude, String longitude, Policy policy) throws PolicyManagerDAOException;
Criterion addCriterion(Criterion criteria) throws PolicyManagerDAOException;
Criterion updateCriterion(Criterion criteria) throws PolicyManagerDAOException;
@ -92,12 +88,6 @@ public interface PolicyDAO {
List<String> getPolicyAppliedUsers(int policyId) throws PolicyManagerDAOException;
// PolicyTimes getTimesOfPolicy(Policy policy) throws PolicyManagerDAOException;
//
// PolicyDates getDatesOfPolicy(Policy policy) throws PolicyManagerDAOException;
//
// PolicyLocations getLocationsOfPolicy(Policy policy) throws PolicyManagerDAOException;
void addEffectivePolicyToDevice(int deviceId, int policyId, List<ProfileFeature> profileFeatures)
throws PolicyManagerDAOException;

@ -56,8 +56,6 @@ public class FeatureDAOImpl implements FeatureDAO {
stmt.setString(1, feature.getName());
stmt.setString(2, feature.getCode());
stmt.setString(3, feature.getDescription());
// stmt.setString(4, feature.getRuleValue());
// stmt.setInt(5, feature.getDeviceTypeId());
int affectedRows = stmt.executeUpdate();
if (log.isDebugEnabled()) {
@ -96,8 +94,6 @@ public class FeatureDAOImpl implements FeatureDAO {
stmt.setString(1, feature.getName());
stmt.setString(2, feature.getCode());
stmt.setString(3, feature.getDescription());
// stmt.setString(4, feature.getRuleValue());
// stmt.setInt(5, feature.getDeviceTypeId());
stmt.addBatch();
}
@ -139,7 +135,6 @@ public class FeatureDAOImpl implements FeatureDAO {
stmt.setString(1, feature.getName());
stmt.setString(2, feature.getCode());
stmt.setString(3, feature.getDescription());
// stmt.setString(4, feature.getRuleValue());
stmt.setInt(4, feature.getId());
stmt.executeUpdate();
@ -222,9 +217,9 @@ public class FeatureDAOImpl implements FeatureDAO {
stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS);
for (ProfileFeature feature : features) {
if (conn.getMetaData().getDriverName().contains("H2")) {
stmt.setObject(4, feature.getContent(), Types.JAVA_OBJECT);
stmt.setObject(1, feature.getContent(), Types.JAVA_OBJECT);
} else {
stmt.setObject(4, feature.getContent());
stmt.setObject(1, feature.getContent());
}
stmt.setInt(2, profileId);
stmt.setString(3, feature.getFeatureCode());
@ -284,17 +279,9 @@ public class FeatureDAOImpl implements FeatureDAO {
while (resultSet.next()) {
ProfileFeature profileFeature = new ProfileFeature();
// Feature feature = new Feature();
// feature.setId(resultSet.getInt("FEATURE_ID"));
// feature.setCode(resultSet.getString("CODE"));
// feature.setName(resultSet.getString("NAME"));
// feature.setRuleValue(resultSet.getString("RULE"));
// profileFeature.setFeature(feature);
profileFeature.setFeatureCode(resultSet.getString("FEATURE_CODE"));
profileFeature.setDeviceTypeId(resultSet.getInt("DEVICE_TYPE_ID"));
profileFeature.setId(resultSet.getInt("ID"));
// profileFeature.setContent(resultSet.getObject("CONTENT"));
profileFeature.setProfileId(resultSet.getInt("PROFILE_ID"));
ByteArrayInputStream bais = null;
@ -367,8 +354,6 @@ public class FeatureDAOImpl implements FeatureDAO {
feature.setId(resultSet.getInt("ID"));
feature.setCode(resultSet.getString("CODE"));
feature.setName(resultSet.getString("NAME"));
// feature.setDeviceTypeId(resultSet.getInt("DEVICE_TYPE_ID"));
// feature.setRuleValue(resultSet.getString("EVALUATION_RULE"));
featureList.add(feature);
}

@ -21,7 +21,10 @@ package org.wso2.carbon.policy.mgt.core.dao.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.core.dto.Device;
import org.wso2.carbon.policy.mgt.common.*;
import org.wso2.carbon.policy.mgt.common.Criterion;
import org.wso2.carbon.policy.mgt.common.Policy;
import org.wso2.carbon.policy.mgt.common.PolicyCriterion;
import org.wso2.carbon.policy.mgt.common.ProfileFeature;
import org.wso2.carbon.policy.mgt.core.dao.PolicyDAO;
import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory;
import org.wso2.carbon.policy.mgt.core.dao.PolicyManagerDAOException;
@ -29,8 +32,10 @@ import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil;
import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil;
import java.sql.*;
import java.sql.Date;
import java.util.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Properties;
public class PolicyDAOImpl implements PolicyDAO {
@ -169,81 +174,6 @@ public class PolicyDAOImpl implements PolicyDAO {
return true;
}
// @Override
// public Policy addDatesToPolicy(Date startDate, Date endDate, Policy policy) throws PolicyManagerDAOException {
//
// Connection conn;
// PreparedStatement stmt = null;
// try {
// conn = this.getConnection();
// String query = "INSERT INTO DM_DATE (START_DATE, END_DATE, POLICY_ID) VALUES (?, ?, ?)";
// stmt = conn.prepareStatement(query);
// stmt.setDate(1, startDate);
// stmt.setDate(2, endDate);
// stmt.setInt(3, policy.getId());
// stmt.executeUpdate();
//
// } catch (SQLException e) {
// String msg = "Error occurred while adding the start date (" + startDate + ") and end date (" +
// endDate + ") with policy to database.";
// log.error(msg, e);
// throw new PolicyManagerDAOException(msg, e);
// } finally {
// PolicyManagementDAOUtil.cleanupResources(stmt, null);
// }
// return policy;
// }
// @Override
// public Policy addTimesToPolicy(int startTime, int endTime, Policy policy) throws PolicyManagerDAOException {
//
// Connection conn;
// PreparedStatement stmt = null;
// try {
// conn = this.getConnection();
// String query = "INSERT INTO DM_TIME (STARTING_TIME, ENDING_TIME, POLICY_ID) VALUES (?, ?, ?)";
// stmt = conn.prepareStatement(query);
// stmt.setInt(1, startTime);
// stmt.setInt(2, endTime);
// stmt.setInt(3, policy.getId());
// stmt.executeUpdate();
//
// } catch (SQLException e) {
// String msg = "Error occurred while adding the start time (" + startTime + ") and end time (" +
// endTime + ") with policy to database.";
// log.error(msg, e);
// throw new PolicyManagerDAOException(msg, e);
// } finally {
// PolicyManagementDAOUtil.cleanupResources(stmt, null);
// }
// return policy;
// }
// @Override
// public Policy addLocationToPolicy(String latitude, String longitude, Policy policy) throws
// PolicyManagerDAOException {
//
// Connection conn;
// PreparedStatement stmt = null;
// try {
// conn = this.getConnection();
// String query = "INSERT INTO DM_LOCATION (LATITUDE, LONGITUDE, POLICY_ID) VALUES (?, ?, ?)";
// stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS);
// stmt.setString(1, latitude);
// stmt.setString(2, longitude);
// stmt.setInt(3, policy.getId());
// stmt.executeUpdate();
//
// } catch (SQLException e) {
// String msg = "Error occurred while adding the Location (" + latitude + ") (" +
// longitude + ") with policy to database.";
// log.error(msg, e);
// throw new PolicyManagerDAOException(msg, e);
// } finally {
// PolicyManagementDAOUtil.cleanupResources(stmt, null);
// }
// return policy;
// }
@Override
public Criterion addCriterion(Criterion criteria) throws PolicyManagerDAOException {
@ -810,98 +740,6 @@ public class PolicyDAOImpl implements PolicyDAO {
}
// public PolicyTimes getTimesOfPolicy(Policy policy) throws PolicyManagerDAOException {
//
// Connection conn;
// PreparedStatement stmt = null;
// ResultSet resultSet = null;
// PolicyTimes times = new PolicyTimes();
//
// try {
// conn = this.getConnection();
// String query = "SELECT STARTING_TIME, ENDING_TIME FROM DM_TIME WHERE POLICY_ID = ?";
// stmt = conn.prepareStatement(query);
// stmt.setInt(1, policy.getId());
// resultSet = stmt.executeQuery();
//
// while (resultSet.next()) {
//
// times.setStartTime(resultSet.getInt("STARTING_TIME"));
// times.setEndTime(resultSet.getInt("ENDING_TIME"));
// }
//
// } catch (SQLException e) {
// String msg = "Error occurred while getting the start time and end time related to policies.";
// log.error(msg, e);
// throw new PolicyManagerDAOException(msg, e);
// } finally {
// PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
// this.closeConnection();
// }
// return times;
// }
// public PolicyDates getDatesOfPolicy(Policy policy) throws PolicyManagerDAOException {
//
// Connection conn;
// PreparedStatement stmt = null;
// ResultSet resultSet = null;
// PolicyDates dates = new PolicyDates();
//
// try {
// conn = this.getConnection();
// String query = "SELECT START_DATE, END_DATE FROM DM_DATE WHERE POLICY_ID = ?";
// stmt = conn.prepareStatement(query);
// stmt.setInt(1, policy.getId());
// resultSet = stmt.executeQuery();
//
// while (resultSet.next()) {
// dates.setStartDate(resultSet.getDate("START_DATE"));
// dates.setEndDate(resultSet.getDate("END_DATE"));
// }
//
// } catch (SQLException e) {
// String msg = "Error occurred while getting the start date and end date related to policies.";
// log.error(msg, e);
// throw new PolicyManagerDAOException(msg, e);
// } finally {
// PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
// this.closeConnection();
// }
// return dates;
// }
// public PolicyLocations getLocationsOfPolicy(Policy policy) throws PolicyManagerDAOException {
//
// Connection conn;
// PreparedStatement stmt = null;
// ResultSet resultSet = null;
// PolicyLocations locations = new PolicyLocations();
//
// try {
// conn = this.getConnection();
// String query = "SELECT LATITUDE, LONGITUDE FROM DM_LOCATION WHERE POLICY_ID = ?";
// stmt = conn.prepareStatement(query);
// stmt.setInt(1, policy.getId());
// resultSet = stmt.executeQuery();
//
// while (resultSet.next()) {
// locations.setLatitude(resultSet.getString("LATITUDE"));
// locations.setLongitude(resultSet.getString("LONGITUDE"));
// }
//
// } catch (SQLException e) {
// String msg = "Error occurred while getting the start time and end time related to policies.";
// log.error(msg, e);
// throw new PolicyManagerDAOException(msg, e);
// } finally {
// PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
// this.closeConnection();
// }
// return locations;
// }
@Override
public void addEffectivePolicyToDevice(int deviceId, int policyId, List<ProfileFeature> profileFeatures)
@ -1181,24 +1019,6 @@ public class PolicyDAOImpl implements PolicyDAO {
stmt.executeUpdate();
/*String locationPolicy = "DELETE FROM DM_LOCATION WHERE POLICY_ID = ?";
stmt = conn.prepareStatement(locationPolicy);
stmt.setInt(1, policyId);
stmt.executeUpdate();
String timePolicy = "DELETE FROM DM_TIME WHERE POLICY_ID = ?";
stmt = conn.prepareStatement(timePolicy);
stmt.setInt(1, policyId);
stmt.executeUpdate();
String datePolicy = "DELETE FROM DM_DATE WHERE POLICY_ID = ?";
stmt = conn.prepareStatement(datePolicy);
stmt.setInt(1, policyId);
stmt.executeUpdate();*/
String deleteCriteria = "DELETE FROM DM_POLICY_CRITERIA WHERE POLICY_ID = ?";
stmt = conn.prepareStatement(deleteCriteria);
stmt.setInt(1, policyId);

@ -83,5 +83,20 @@ public class PolicyFilterImpl implements PolicyFilter {
policies = temp;
}
@Override
public void filterUserBasedPolicies(List<String> usernames, List<Policy> policies) {
List<Policy> temp = new ArrayList<Policy>();
for (Policy policy : policies) {
List<String> users = policy.getUsers();
for (String user : users) {
if(usernames.contains(user)) {
temp.add(policy);
}
}
}
policies = temp;
}
}

@ -90,9 +90,19 @@ public class PolicyInformationPointImpl implements PolicyInformationPoint {
List<Policy> policies = policyManager.getPoliciesOfDeviceType(pipDevice.getDeviceType().getName());
PolicyFilter policyFilter = new PolicyFilterImpl();
policyFilter.filterDeviceTypeBasedPolicies(pipDevice.getDeviceType().getName(), policies);
policyFilter.filterOwnershipTypeBasedPolicies(pipDevice.getOwnershipType(), policies);
policyFilter.filterRolesBasedPolicies(pipDevice.getRoles(), policies);
if (pipDevice.getDeviceType() != null) {
policyFilter.filterDeviceTypeBasedPolicies(pipDevice.getDeviceType().getName(), policies);
}
if (!pipDevice.getOwnershipType().isEmpty()) {
policyFilter.filterOwnershipTypeBasedPolicies(pipDevice.getOwnershipType(), policies);
}
if (pipDevice.getRoles() != null) {
policyFilter.filterRolesBasedPolicies(pipDevice.getRoles(), policies);
}
if(pipDevice.getUserIds()!=null) {
policyFilter.filterUserBasedPolicies(pipDevice.getUserIds(), policies);
}
return policies;
}

@ -548,12 +548,9 @@ public class PolicyManagerImpl implements PolicyManager {
List<Policy> policies = new ArrayList<Policy>();
try {
// DeviceType deviceType = deviceTypeDAO.getDeviceType(deviceTypeName);
List<Profile> profileList = profileManager.getProfilesOfDeviceType(deviceTypeName);
List<Policy> allPolicies = this.getPolicies();
for (Profile profile : profileList) {
for (Policy policy : allPolicies) {
if (policy.getProfileId() == profile.getProfileId()) {
@ -562,20 +559,7 @@ public class PolicyManagerImpl implements PolicyManager {
}
}
}
Collections.sort(policies);
// } catch (PolicyManagerDAOException e) {
// String msg = "Error occurred while getting all the policies.";
// log.error(msg, e);
// throw new PolicyManagementException(msg, e);
// } catch (ProfileManagerDAOException e) {
// String msg = "Error occurred while getting the profiles related to device type (" + deviceTypeName + ")";
// log.error(msg, e);
// throw new PolicyManagementException(msg, e);
// } catch (DeviceManagementDAOException e) {
// String msg = "Error occurred while getting device type object related to (" + deviceTypeName + ")";
// log.error(msg, e);
// throw new PolicyManagementException(msg, e);
} catch (ProfileManagementException e) {
String msg = "Error occurred while getting all the profile features.";
log.error(msg, e);

Loading…
Cancel
Save