From f8f7dd08cde422585313a7e986bd551e4b198933 Mon Sep 17 00:00:00 2001 From: sameera910409 Date: Fri, 28 Oct 2016 14:07:50 +0530 Subject: [PATCH] fixed share groups and group analytics in device grouping --- .../service/api/GroupManagementService.java | 52 +++++++++++++++++++ .../impl/GroupManagementServiceImpl.java | 36 +++++++++++-- .../app/modules/business-controllers/group.js | 4 +- .../cdmf.page.group.analytics/analytics.js | 11 ++-- .../cdmf.page.group.analytics/analytics.json | 2 +- .../cdmf.page.groups/public/js/listing.js | 52 +++++++++++++------ 6 files changed, 131 insertions(+), 26 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GroupManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GroupManagementService.java index 0fa5b9e0b5e..6dd76e1823a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GroupManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GroupManagementService.java @@ -467,6 +467,58 @@ public interface GroupManagementService { required = true) @PathParam("groupId") int groupId); + @Path("/id/{groupId}/roles") + @GET + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = HTTPConstants.HEADER_GET, + value = "View list of roles of a device group.", + notes = "Returns details of roles which particular group has been shared with.", + tags = "Device Group Management") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. \n Successfully fetched the users.", + response = DeviceGroupUsersList.class, + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body"), + @ResponseHeader( + name = "ETag", + description = "Entity Tag of the response resource.\n" + + "Used by caches, or in conditional requests."), + @ResponseHeader( + name = "Last-Modified", + description = "Date and time the resource has been modified the last time.\n" + + "Used by caches, or in conditional requests."), + }), + @ApiResponse( + code = 304, + message = "Not Modified. \n Empty body because the client has already the latest version of " + + "the requested resource."), + @ApiResponse( + code = 404, + message = "No groups found.", + response = ErrorResponse.class), + @ApiResponse( + code = 406, + message = "Not Acceptable.\n The requested media type is not supported."), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Server error occurred while fetching the roles.", + response = ErrorResponse.class) + }) + @Permission(name = "View roles", permission = "/device-mgt/groups/roles/view") + Response getRolesOfGroup(@ApiParam( + name = "groupId", + value = "ID of the group.", + required = true) + @PathParam("groupId") int groupId, + @ApiParam( + name = "userName", + value = "User name of the current user.", + required = false) + @QueryParam("userName") String userName); + @Path("/id/{groupId}/devices") @GET @ApiOperation( diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java index 0d9bea2e8bf..ed0264feda8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java @@ -34,13 +34,11 @@ import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; import org.wso2.carbon.device.mgt.common.group.mgt.GroupUser; import org.wso2.carbon.device.mgt.common.group.mgt.RoleDoesNotExistException; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; -import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupList; -import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupShare; -import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupUsersList; -import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList; +import org.wso2.carbon.device.mgt.jaxrs.beans.*; import org.wso2.carbon.device.mgt.jaxrs.service.api.GroupManagementService; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; +import org.wso2.carbon.user.core.UserStoreException; import org.wso2.carbon.user.core.multiplecredentials.UserDoesNotExistException; import javax.ws.rs.core.Response; @@ -54,6 +52,7 @@ public class GroupManagementServiceImpl implements GroupManagementService { private static final String DEFAULT_ADMIN_ROLE = "admin"; private static final String[] DEFAULT_ADMIN_PERMISSIONS = {"/permission/device-mgt/admin/groups", "/permission/device-mgt/user/groups"}; + private static final String EMPTY_RESULT = "EMPTY"; @Override public Response getGroups(String name, String owner, int offset, int limit) { @@ -201,6 +200,35 @@ public class GroupManagementServiceImpl implements GroupManagementService { } } + @Override + public Response getRolesOfGroup(int groupId, String userName) { + try { + List groupRoles; + if(userName != null) { + groupRoles = DeviceMgtAPIUtils.getGroupManagementProviderService().getRoles(userName, groupId); + } else { + groupRoles = DeviceMgtAPIUtils.getGroupManagementProviderService().getRoles(groupId); + } + + if(groupRoles != null && groupRoles.size() > 0) { + RoleList deviceGroupRolesList = new RoleList(); + deviceGroupRolesList.setList(groupRoles); + deviceGroupRolesList.setCount(groupRoles.size()); + return Response.status(Response.Status.OK).entity(deviceGroupRolesList).build(); + } else { + return Response.status(Response.Status.OK).entity(EMPTY_RESULT).build(); + } + } catch (GroupManagementException e) { + String msg = "Error occurred while getting roles of the group."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } catch (UserStoreException e) { + String msg = "Error while retrieving the user."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } + @Override public Response getDevicesOfGroup(int groupId, int offset, int limit) { try { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/group.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/group.js index 1bc1d80e179..5862f6bc8e4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/group.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/group.js @@ -66,8 +66,8 @@ var groupModule = {}; ); }; - groupModule.getGroupDevices = function (groupName, owner) { - endPoint = deviceServiceEndpoint + "/groups/owner/" + owner + "/name/" + groupName + "/devices"; + groupModule.getGroupDevices = function (groupId) { + endPoint = deviceServiceEndpoint + "/groups/id/" + groupId + "/devices?limit=10"; return serviceInvokers.XMLHttp.get( endPoint, function (responsePayload) { return responsePayload; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.group.analytics/analytics.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.group.analytics/analytics.js index fe9a550516a..19f783ed561 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.group.analytics/analytics.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.group.analytics/analytics.js @@ -20,11 +20,16 @@ function onRequest(context) { var utility = require("/app/modules/utility.js").utility; var groupModule = require("/app/modules/business-controllers/group.js")["groupModule"]; var groupName = context.uriParams.name; - var groupOwner = context.uriParams.owner; - var devices = groupModule.getGroupDevices(groupName, groupOwner).data; + var groupId = context.uriParams.id; + var devices = []; + var deviceResponse = groupModule.getGroupDevices(groupId).responseText; + + if(deviceResponse != null) { + var deviceResponseObj = parse(deviceResponse); + devices = deviceResponseObj.devices; + } var page = { "groupName": groupName, - "groupOwner": groupOwner, "title": groupName + " Analytics" }; if (devices) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.group.analytics/analytics.json b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.group.analytics/analytics.json index 9a06298a8c4..2e2fea333e4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.group.analytics/analytics.json +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.group.analytics/analytics.json @@ -1,5 +1,5 @@ { "version": "1.0.0", - "uri": "/group/{owner}/{name}/analytics", + "uri": "/group/{name}/{id}/analytics", "layout": "cdmf.layout.default" } \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.groups/public/js/listing.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.groups/public/js/listing.js index 6a5ca169141..9b8edbb6d5c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.groups/public/js/listing.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.groups/public/js/listing.js @@ -151,21 +151,31 @@ function loadGroups() { data: 'name', class: 'fade-edge' }, + { + targets: 2, + data: 'owner', + class: 'fade-edge remove-padding-top', + }, { targets: 3, + data: 'description', + class: 'fade-edge remove-padding-top', + }, + { + targets: 4, data: 'id', class: 'text-right content-fill text-left-on-grid-view no-wrap', render: function (id, type, row, meta) { var html; - html = '' + + html = '' + '' + ''; - html += '' + + html += '' + '' + ''; - html += ''; @@ -198,6 +208,11 @@ function loadGroups() { $(this).attr('data-search', data.owner); $(this).attr('data-display', data.owner); break; + case 3: + $(this).attr('data-grid-label', "Description"); + $(this).attr('data-search', data.description); + $(this).attr('data-display', data.description); + break; } }); }; @@ -326,7 +341,7 @@ function attachEvents() { * on Group Management page in WSO2 Device Management Server Console. */ $("a.share-group-link").click(function () { - var groupName = $(this).data("group-name"); + var groupId = $(this).data("group-id"); var groupOwner = $(this).data("group-owner"); $(modalPopupContent).html($('#share-group-w1-modal-content').html()); $("a#share-group-next-link").show(); @@ -337,7 +352,7 @@ function attachEvents() { $("#user-names").html("Please specify a user other than current user."); $("a#share-group-next-link").hide(); } else { - getAllRoles(groupName, groupOwner, selectedUser); + getAllRoles(groupId, selectedUser); } }); $("a#share-group-w1-cancel-link").click(function () { @@ -352,6 +367,7 @@ function attachEvents() { */ $("a.remove-group-link").click(function () { var groupId = $(this).data("group-id"); + var groupOwner = $(this).data("group-owner"); $(modalPopupContent).html($('#remove-group-modal-content').html()); showPopup(); @@ -426,15 +442,15 @@ function attachEvents() { }); } -function getAllRoles(groupName, groupOwner, selectedUser) { +function getAllRoles(groupId, selectedUser) { $(modalPopupContent).html($('#share-group-w2-modal-content').html()); $('#user-roles').html('
'); $("a#share-group-yes-link").hide(); var successCallback = function (data, textStatus, xhr) { data = JSON.parse(data); if (xhr.status == 200) { - if (data.length > 0) { - generateRoleMap(groupName, groupOwner, selectedUser, data); + if (data.roles.length > 0) { + generateRoleMap(groupId, selectedUser, data.roles); } else { $('#user-roles').html("There is no any roles for this group."); } @@ -443,7 +459,7 @@ function getAllRoles(groupName, groupOwner, selectedUser) { } }; - invokerUtil.get("/devicemgt_admin/groups/owner/" + groupOwner + "/name/" + groupName + "/share/roles", + invokerUtil.get("/api/device-mgt/v1.0/groups/id/" + groupId + "/roles", successCallback, function (message) { displayErrors(message); }); @@ -453,11 +469,14 @@ function getAllRoles(groupName, groupOwner, selectedUser) { }); } -function generateRoleMap(groupName, groupOwner, selectedUser, allRoles) { +function generateRoleMap(groupId, selectedUser, allRoles) { var successCallback = function (data, textStatus, xhr) { data = JSON.parse(data); if (xhr.status == 200) { - var userRoles = data; + var userRoles = []; + if(data != "EMPTY") { + userRoles = data.roles; + } var str = ''; for (var i = 0; i < allRoles.length; i++) { @@ -482,14 +501,14 @@ function generateRoleMap(groupName, groupOwner, selectedUser, allRoles) { roles.push(allRoles[i]); } } - updateGroupShare(groupName, groupOwner, selectedUser, roles); + updateGroupShare(groupId, selectedUser, roles); }); } else { displayErrors(xhr); } }; - invokerUtil.get("/devicemgt_admin/groups/owner/" + groupOwner + "/name/" + groupName + "/share/roles?userName=" + selectedUser, + invokerUtil.get("/api/device-mgt/v1.0/groups/id/" + groupId + "/roles?userName=" + selectedUser, successCallback, function (message) { displayErrors(message); }); @@ -499,7 +518,7 @@ function generateRoleMap(groupName, groupOwner, selectedUser, allRoles) { }); } -function updateGroupShare(groupName, groupOwner, selectedUser, roles) { +function updateGroupShare(groupId, selectedUser, roles) { var successCallback = function (data) { $(modalPopupContent).html($('#share-group-200-content').html()); setTimeout(function () { @@ -508,8 +527,9 @@ function updateGroupShare(groupName, groupOwner, selectedUser, roles) { }, 2000); }; - invokerUtil.put("/devicemgt_admin/groups/owner/" + groupOwner + "/name/" + groupName + "/user/" + selectedUser + "/share/roles", - roles, successCallback, function (message) { + var deviceGroupShare = {"username": selectedUser, "groupRoles": roles }; + invokerUtil.post("/api/device-mgt/v1.0/groups/id/" + groupId + "/share", + deviceGroupShare, successCallback, function (message) { displayErrors(message); }); }