From 02eec1033e62306ed9893057f9044e3e3a61dbec Mon Sep 17 00:00:00 2001 From: sameera910409 Date: Sun, 6 Nov 2016 14:29:13 +0530 Subject: [PATCH 1/9] add group sharing role create functionality --- .../service/api/GroupManagementService.java | 66 ++++- .../service/api/UserManagementService.java | 34 +++ .../impl/GroupManagementServiceImpl.java | 27 ++- .../impl/UserManagementServiceImpl.java | 19 ++ .../app/modules/business-controllers/user.js | 24 ++ .../app/pages/cdmf.page.devices/devices.hbs | 63 ++++- .../cdmf.page.devices/public/js/listing.js | 48 ++-- .../app/pages/cdmf.page.groups/groups.hbs | 227 +++++++++++++----- .../app/pages/cdmf.page.groups/groups.js | 2 + .../public/group-permissions.json | 15 ++ .../cdmf.page.groups/public/js/listing.js | 148 +++++++++--- 11 files changed, 550 insertions(+), 123 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.groups/public/group-permissions.json 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 6dd76e1823..75fd081d96 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 @@ -35,6 +35,7 @@ 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.ErrorResponse; +import org.wso2.carbon.device.mgt.jaxrs.beans.RoleInfo; import javax.validation.Valid; import javax.ws.rs.Consumes; @@ -467,6 +468,65 @@ public interface GroupManagementService { required = true) @PathParam("groupId") int groupId); + + @Path("id/{groupId}/roles/create") + @POST + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = HTTPConstants.HEADER_GET, + value = "Create a group sharing role to a device group.", + notes = "Group sharing is done through a group sharing role.", + tags = "Device Group Management") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. \n Successfully created the role.", + 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 creating the role.", + response = ErrorResponse.class) + }) + @Permission(name = "Create roles", permission = "/device-mgt/groups/roles/create") + Response createGroupSharingRole( + @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, + @ApiParam( + name = "roleInfo", + value = "Group role information with permissions and users", + required = true) + @Valid RoleInfo roleInfo); + @Path("/id/{groupId}/roles") @GET @ApiOperation( @@ -621,7 +681,7 @@ public interface GroupManagementService { required = true) @PathParam("groupId") int groupId); - @Path("/id/{groupId}/devices") + @Path("/id/{groupId}/devices/add") @POST @ApiOperation( produces = MediaType.APPLICATION_JSON, @@ -672,8 +732,8 @@ public interface GroupManagementService { required = true) @Valid List deviceIdentifiers); - @Path("/id/{groupId}/devices") - @DELETE + @Path("/id/{groupId}/devices/remove") + @POST @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = HTTPConstants.HEADER_DELETE, diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/UserManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/UserManagementService.java index 06569d21cf..845cf11b82 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/UserManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/UserManagementService.java @@ -408,6 +408,40 @@ public interface UserManagementService { @Permission(name = "View Users", permission = "/device-mgt/users/view") Response getUserCount(); + @GET + @Path("/checkUser") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "Getting the User existence status", + notes = "Check if the user exists in the user store.", + tags = "User Management") + @ApiResponses(value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully fetched user exist status.", + response = BasicUserInfoList.class, + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body") + }), + @ApiResponse( + code = 406, + message = "Not Acceptable.\n The requested media type is not supported", + response = ErrorResponse.class), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Server error occurred while fetching the total user exist status.", + response = ErrorResponse.class) + }) + @Permission(name = "View Users", permission = "/device-mgt/users/view") + Response isUserExists(@ApiParam( + name = "username", + value = "The username of the user.", + required = true) + @QueryParam("username") String userName); + @GET @Path("/search/usernames") @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 ed0264feda..90208d4409 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 @@ -38,7 +38,8 @@ 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.api.UserStoreException; +import org.wso2.carbon.user.api.UserStoreManager; import org.wso2.carbon.user.core.multiplecredentials.UserDoesNotExistException; import javax.ws.rs.core.Response; @@ -200,11 +201,32 @@ public class GroupManagementServiceImpl implements GroupManagementService { } } + @Override + public Response createGroupSharingRole(int groupId, String userName, RoleInfo roleInfo) { + try { + DeviceMgtAPIUtils.getGroupManagementProviderService() + .addGroupSharingRole(userName, groupId, roleInfo.getRoleName(), roleInfo.getPermissions()); + return Response.status(Response.Status.CREATED).build(); + } catch (GroupManagementException e) { + String msg = "Error occurred while creating group sharing role."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } + @Override public Response getRolesOfGroup(int groupId, String userName) { try { List groupRoles; if(userName != null) { + UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); + if (!userStoreManager.isExistingUser(userName)) { + // returning response with bad request state + return Response.status(Response.Status.CONFLICT).entity( + new ErrorResponse.ErrorResponseBuilder().setMessage("User by username: " + + userName + " doesn't exists. Therefore, request made to get user " + + "was refused.").build()).build(); + } groupRoles = DeviceMgtAPIUtils.getGroupManagementProviderService().getRoles(userName, groupId); } else { groupRoles = DeviceMgtAPIUtils.getGroupManagementProviderService().getRoles(groupId); @@ -275,8 +297,7 @@ public class GroupManagementServiceImpl implements GroupManagementService { } } - @Override - public Response removeDevicesFromGroup(int groupId, List deviceIdentifiers) { + @Override public Response removeDevicesFromGroup(int groupId, List deviceIdentifiers) { try { DeviceMgtAPIUtils.getGroupManagementProviderService().removeDevice(groupId, deviceIdentifiers); return Response.status(Response.Status.OK).build(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java index 8fc57a0c89..96d0cf2c12 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java @@ -359,6 +359,25 @@ public class UserManagementServiceImpl implements UserManagementService { } } + @GET + @Path("/checkUser") + @Override public Response isUserExists(@QueryParam("username") String userName) { + try { + UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); + boolean userExists = false; + if (userStoreManager.isExistingUser(userName)) { + userExists = true; + return Response.status(Response.Status.OK).entity(userExists).build(); + } else { + return Response.status(Response.Status.OK).entity(userExists).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(); + } + } + @GET @Path("/search/usernames") @Override diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/user.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/user.js index 950b9158bc..de3c30a739 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/user.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/user.js @@ -512,9 +512,33 @@ var userModule = function () { if (publicMethods.isAuthorized("/permission/admin/device-mgt/users/add")) { permissions["ADD_USER"] = true; } + if (publicMethods.isAuthorized("/permission/admin/device-mgt/groups/devices/add")) { + permissions["ADD_GROUP_DEVICES"] = true; + } + if (publicMethods.isAuthorized("/permission/admin/device-mgt/groups/devices/remove")) { + permissions["REMOVE_GROUP_DEVICES"] = true; + } + if (publicMethods.isAuthorized("/permission/admin/device-mgt/groups/devices/view")) { + permissions["VIEW_GROUP_DEVICES"] = true; + } + if (publicMethods.isAuthorized("/permission/admin/device-mgt/groups/roles/create")) { + permissions["CREATE_GROUP_ROLES"] = true; + } + if (publicMethods.isAuthorized("/permission/admin/device-mgt/groups/roles/view")) { + permissions["VIEW_GROUP_ROLES"] = true; + } + if (publicMethods.isAuthorized("/permission/admin/device-mgt/groups/update")) { + permissions["UPDATE_GROUP"] = true; + } + if (publicMethods.isAuthorized("/permission/admin/device-mgt/groups/share")) { + permissions["SHARE_GROUP"] = true; + } if (publicMethods.isAuthorized("/permission/admin/device-mgt/users/remove")) { permissions["REMOVE_USER"] = true; } + if (publicMethods.isAuthorized("/permission/admin/device-mgt/groups/remove")) { + permissions["REMOVE_GROUP"] = true; + } if (publicMethods.isAuthorized("/permission/admin/device-mgt/roles/add")) { permissions["ADD_ROLE"] = true; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/devices.hbs b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/devices.hbs index b225f9c983..7cf43cc2af 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/devices.hbs +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/devices.hbs @@ -190,21 +190,39 @@ {{/if}}
- @@ -273,6 +291,25 @@
+
+ + + +
+