From f7abe86e3c405239b8c5f688773388095c5ed3ce Mon Sep 17 00:00:00 2001 From: geethkokila Date: Mon, 1 Jun 2015 12:00:58 -0400 Subject: [PATCH 1/2] Fixing the user based policies --- .../policy/mgt/common/PolicyFilter.java | 1 + .../carbon/policy/mgt/core/dao/PolicyDAO.java | 18 +- .../mgt/core/dao/impl/FeatureDAOImpl.java | 19 +- .../mgt/core/dao/impl/PolicyDAOImpl.java | 196 +----------------- .../mgt/core/impl/PolicyFilterImpl.java | 15 ++ .../core/impl/PolicyInformationPointImpl.java | 16 +- .../mgt/core/mgt/impl/PolicyManagerImpl.java | 16 -- 7 files changed, 43 insertions(+), 238 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyFilter.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyFilter.java index 3e8cf71756..32a86d9de0 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyFilter.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyFilter.java @@ -31,5 +31,6 @@ public interface PolicyFilter { void filterDeviceTypeBasedPolicies(String deviceType, List policies); + void filterUserBasedPolicies(List usernames, List policies); } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java index a5af43affc..1dc8987068 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java @@ -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 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 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 profileFeatures) throws PolicyManagerDAOException; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java index 66f83ee803..9324d3ba45 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java @@ -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); } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java index 92e54dde6c..a8442f1b5d 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java @@ -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 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); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java index 0ebfe3daa2..391d5e4b7d 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java @@ -83,5 +83,20 @@ public class PolicyFilterImpl implements PolicyFilter { policies = temp; } + @Override + public void filterUserBasedPolicies(List usernames, List policies) { + List temp = new ArrayList(); + + for (Policy policy : policies) { + List users = policy.getUsers(); + for (String user : users) { + if(usernames.contains(user)) { + temp.add(policy); + } + } + } + policies = temp; + } + } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java index ef537bbc1c..f4c5576a9e 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java @@ -90,9 +90,19 @@ public class PolicyInformationPointImpl implements PolicyInformationPoint { List 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; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java index 4a338a7e0f..f5b413c58a 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java @@ -548,12 +548,9 @@ public class PolicyManagerImpl implements PolicyManager { List policies = new ArrayList(); try { -// DeviceType deviceType = deviceTypeDAO.getDeviceType(deviceTypeName); - List profileList = profileManager.getProfilesOfDeviceType(deviceTypeName); List 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); From 009c9dd6df15d00b6cda37b83ff7adf937cf3b77 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Mon, 1 Jun 2015 12:05:12 -0400 Subject: [PATCH 2/2] Fixing the user based policies --- .../mgt/core/impl/PolicyFilterImpl.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java index 391d5e4b7d..0927bfc64b 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java @@ -63,13 +63,14 @@ public class PolicyFilterImpl implements PolicyFilter { @Override public void filterOwnershipTypeBasedPolicies(String ownershipType, List policies) { -// List temp = new ArrayList(); -// for (Policy policy : policies) { -// if (ownershipType.equalsIgnoreCase(policy.getOwnershipType())) { -// temp.add(policy); -// } -// } -// policies = temp; + List temp = new ArrayList(); + for (Policy policy : policies) { + if (ownershipType.equalsIgnoreCase(policy.getOwnershipType()) || + PolicyManagementConstants.ANY.equalsIgnoreCase(policy.getOwnershipType())) { + temp.add(policy); + } + } + policies = temp; } @Override @@ -89,6 +90,10 @@ public class PolicyFilterImpl implements PolicyFilter { for (Policy policy : policies) { List users = policy.getUsers(); + if(users.contains(PolicyManagementConstants.ANY)) { + temp.add(policy); + continue; + } for (String user : users) { if(usernames.contains(user)) { temp.add(policy);