From aadf87e5d9bd9059e56410d14a170f60a3d973e1 Mon Sep 17 00:00:00 2001 From: charitha Date: Tue, 17 May 2016 23:33:33 +0530 Subject: [PATCH] Bug fixes and fix for https://wso2.org/jira/browse/IOTS-158 --- .../device/mgt/jaxrs/api/impl/GroupImpl.java | 5 ++-- .../device/mgt/jaxrs/api/impl/PolicyImpl.java | 21 +++++++++++++++ .../src/main/webapp/META-INF/permissions.xml | 26 +++++++++++++++++++ .../jaggeryapps/devicemgt/app/modules/user.js | 3 +++ .../app/pages/cdmf.page.policies/policies.hbs | 6 ++++- .../app/pages/cdmf.page.policies/policies.js | 9 ++++--- .../operation-mod.json | 6 +---- .../units/cdmf.unit.policy.create/create.hbs | 2 +- 8 files changed, 65 insertions(+), 13 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/impl/GroupImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/impl/GroupImpl.java index 4c47f1b35b0..ebe9228c31b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/impl/GroupImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/impl/GroupImpl.java @@ -118,7 +118,7 @@ public class GroupImpl implements Group { @Override @GET @Produces("application/json") - public Response getGroups(@QueryParam("start") int startIndex, @PathParam("length") int length) { + public Response getGroups(@QueryParam("start") int startIndex, @QueryParam("length") int length) { try { PaginationResult paginationResult = DeviceMgtAPIUtils.getGroupManagementProviderService() .getGroups(startIndex, length); @@ -331,8 +331,7 @@ public class GroupImpl implements Group { @Produces("application/json") public Response addSharing(@QueryParam("shareUser") String shareUser, @PathParam("groupName") String groupName, @PathParam("owner") String owner, - @PathParam("roleName") String roleName, - @FormParam("permissions") String[] permissions) { + @PathParam("roleName") String roleName, String[] permissions) { try { boolean isAdded = DeviceMgtAPIUtils.getGroupManagementProviderService().addGroupSharingRole( diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/impl/PolicyImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/impl/PolicyImpl.java index a1c84a0d05b..5947b5ec4a8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/impl/PolicyImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/impl/PolicyImpl.java @@ -21,6 +21,11 @@ package org.wso2.carbon.device.mgt.jaxrs.api.impl; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException; +import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService; +import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.jaxrs.api.common.MDMAPIException; import org.wso2.carbon.device.mgt.jaxrs.api.util.DeviceMgtAPIUtils; import org.wso2.carbon.device.mgt.jaxrs.beans.PriorityUpdatedPolicyWrapper; @@ -97,6 +102,22 @@ public class PolicyImpl implements org.wso2.carbon.device.mgt.jaxrs.api.Policy { private Response addPolicy(PolicyManagerService policyManagementService, ResponsePayload responseMsg, org.wso2.carbon.policy.mgt.common.Policy policy) { + List devices = policy.getDevices(); + if (devices != null && devices.size() == 1) { + DeviceAccessAuthorizationService deviceAccessAuthorizationService = DeviceManagementDataHolder.getInstance().getDeviceAccessAuthorizationService(); + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(devices.get(0).getDeviceIdentifier(), devices.get(0).getType()); + PrivilegedCarbonContext threadLocalCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + String username = threadLocalCarbonContext.getUsername(); + try { + if (!deviceAccessAuthorizationService.isUserAuthorized(deviceIdentifier, username)) { + return Response.status(Response.Status.UNAUTHORIZED).build(); + } + } catch (DeviceAccessAuthorizationException e) { + String msg = "Device access authorization exception"; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } try { PolicyAdministratorPoint pap = policyManagementService.getPAP(); pap.addPolicy(policy); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/META-INF/permissions.xml b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/META-INF/permissions.xml index 89aa3f02f24..ca4664f9cb6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/META-INF/permissions.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/META-INF/permissions.xml @@ -669,6 +669,12 @@ /policies/inactive-policy POST + + Add Policy + /device-mgt/user/policies/add + /policies/inactive-policy + POST + List policies @@ -704,6 +710,12 @@ /policies/active-policy POST + + Add Policy + /device-mgt/user/policies/add + /policies/inactive-policy + POST + Remove policy @@ -726,6 +738,20 @@ GET + + List user policies + /device-mgt/user/policies/list + /policies + GET + + + + List user policies + /device-mgt/user/policies/list + /policies/* + GET + + Update policy /device-mgt/admin/policies/update diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/user.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/user.js index bca48a0362c..b0eb98c5548 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/user.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/user.js @@ -668,6 +668,9 @@ var userModule = function () { if (publicMethods.isAuthorized("/permission/admin/device-mgt/admin/roles/add")) { permissions["ADD_ROLE"] = true; } + if (publicMethods.isAuthorized("/permission/admin/device-mgt/admin/policies/add")) { + permissions["ADD_ADMIN_POLICY"] = true; + } if (publicMethods.isAuthorized("/permission/admin/device-mgt/user/policies/add")) { permissions["ADD_POLICY"] = true; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.policies/policies.hbs b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.policies/policies.hbs index 55117a4cf06..83299a80b7d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.policies/policies.hbs +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.policies/policies.hbs @@ -16,6 +16,7 @@ {{/zone}} {{#zone "navbarActions"}} + {{#if permissions.ADD_ADMIN_POLICY}}
  • @@ -25,7 +26,9 @@ Add Policy
  • -
  • + {{/if}} + {{#if permissions.CHANGE_POLICY_PRIORITY}} +
  • @@ -34,6 +37,7 @@ Policy Priority
  • + {{/if}}
  • diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.policies/policies.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.policies/policies.js index ccc71cc1504..90e536e62dd 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.policies/policies.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.policies/policies.js @@ -16,14 +16,17 @@ * under the License. */ -function onRequest(context){ +function onRequest(context) { context.handlebars.registerHelper('equal', function (lvalue, rvalue, options) { - if (arguments.length < 3) + if (arguments.length < 3) { throw new Error("Handlebars Helper equal needs 2 parameters"); - if( lvalue!=rvalue ) { + } + if (lvalue != rvalue) { return options.inverse(this); } else { return options.fn(this); } }); + var userModule = require("/app/modules/user.js").userModule; + return {permissions: userModule.getUIPermissions()}; } \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.operation-mod/operation-mod.json b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.operation-mod/operation-mod.json index d6642342f13..688e939808b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.operation-mod/operation-mod.json +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.operation-mod/operation-mod.json @@ -1,7 +1,3 @@ { - "version": "1.0.0", - "pushedUris": [ - "/policies", - "/policy/{+any}" - ] + "version": "1.0.0" } \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.create/create.hbs b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.create/create.hbs index 6a2d8e035c4..ecf3aedacfb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.create/create.hbs +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.create/create.hbs @@ -22,7 +22,7 @@
    3
    - +