From 00a16ea2bc1afdf39cbcdce272d9c81e11f0f852 Mon Sep 17 00:00:00 2001 From: prabathabey Date: Tue, 31 May 2016 18:36:00 +0530 Subject: [PATCH] Improving Role management API implementation --- .../device/mgt/jaxrs/beans/RoleList.java | 56 +++++++++++++++++ .../service/api/RoleManagementService.java | 37 ++++++----- .../api/admin/UserManagementAdminService.java | 2 +- .../impl/RoleManagementServiceImpl.java | 63 ++++++++++++------- .../admin/UserManagementAdminServiceImpl.java | 2 +- 5 files changed, 122 insertions(+), 38 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/RoleList.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/RoleList.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/RoleList.java new file mode 100644 index 0000000000..4cb08fa36c --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/RoleList.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.mgt.jaxrs.beans; + +import com.fasterxml.jackson.annotation.JsonProperty; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +import java.util.List; + +@ApiModel(value = "Role List") +public class RoleList extends BasePaginatedResult { + + private List roles; + + @ApiModelProperty(value = "List of roles returned") + @JsonProperty("roles") + public List getList() { + return roles; + } + + public void setList(List roles) { + this.roles = roles; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("{\n"); + + sb.append(" count: ").append(getCount()).append(",\n"); + sb.append(" next: ").append(getNext()).append(",\n"); + sb.append(" previous: ").append(getPrevious()).append(",\n"); + sb.append(" roles: [").append(roles).append("\n"); + sb.append("]}\n"); + return sb.toString(); + } + + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/RoleManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/RoleManagementService.java index 2db6620784..1785d92b82 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/RoleManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/RoleManagementService.java @@ -22,6 +22,8 @@ import io.swagger.annotations.*; import org.wso2.carbon.apimgt.annotations.api.API; import org.wso2.carbon.apimgt.annotations.api.Permission; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; +import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; +import org.wso2.carbon.device.mgt.jaxrs.beans.RoleList; import org.wso2.carbon.device.mgt.jaxrs.beans.RoleWrapper; import org.wso2.carbon.user.mgt.common.UIPermissionNode; @@ -43,18 +45,15 @@ public interface RoleManagementService { produces = MediaType.APPLICATION_JSON, httpMethod = "GET", value = "Get the list of roles.", - responseContainer = "List", notes = "If you wish to get the details of all the roles in EMM, you can do so using this REST API. All " + "internal roles, roles created for Service-providers and application related roles are omitted.", - response = String.class, tags = "Role Management") @ApiResponses( value = { @ApiResponse( code = 200, message = "OK. \n Successfully fetched the requested list of roles.", - response = String.class, - responseContainer = "List", + response = RoleList.class, responseHeaders = { @ResponseHeader( name = "Content-Type", @@ -76,7 +75,8 @@ public interface RoleManagementService { message = "Not Acceptable.\n The requested media type is not supported"), @ApiResponse( code = 500, - message = "Internal Server ErrorResponse. \n Server error occurred while fetching requested list of roles.") + message = "Internal Server ErrorResponse. \n Server error occurred while fetching requested list of roles.", + response = ErrorResponse.class) }) @Permission(scope = "roles-view", permissions = { "/permission/admin/device-mgt/admin/roles/list", @@ -155,7 +155,8 @@ public interface RoleManagementService { message = "Not Acceptable.\n The requested media type is not supported"), @ApiResponse( code = 500, - message = "Internal Server ErrorResponse. \n Server error occurred while fetching the permission list of the requested role.") + message = "Internal Server ErrorResponse. \n Server error occurred while fetching the permission list of the requested role.", + response = ErrorResponse.class) }) @Permission(scope = "roles-view", permissions = {"/permission/admin/device-mgt/admin/roles/list"}) Response getPermissionsOfRole( @@ -209,7 +210,8 @@ public interface RoleManagementService { message = "Not Acceptable.\n The requested media type is not supported"), @ApiResponse( code = 500, - message = "Internal Server ErrorResponse. \n Server error occurred while fetching the requested role.") + message = "Internal Server ErrorResponse. \n Server error occurred while fetching the requested role.", + response = ErrorResponse.class) }) @Permission(scope = "roles-view", permissions = {"/permission/admin/device-mgt/admin/roles/list"}) Response getRole( @@ -260,14 +262,16 @@ public interface RoleManagementService { description = "The Source URL of the document.")}), @ApiResponse( code = 400, - message = "Bad Request. \n Invalid request or validation error."), + message = "Bad Request. \n Invalid request or validation error.", + response = ErrorResponse.class), @ApiResponse( code = 415, message = "Unsupported media type. \n The entity of the request was in a not supported format."), @ApiResponse( code = 500, message = "Internal Server ErrorResponse. \n " + - "Server error occurred while adding a new role.") + "Server error occurred while adding a new role.", + response = ErrorResponse.class) }) @Permission(scope = "roles-modify", permissions = {"/permission/admin/device-mgt/admin/roles/add"}) Response addRole( @@ -307,7 +311,8 @@ public interface RoleManagementService { "Used by caches, or in conditional requests.")}), @ApiResponse( code = 400, - message = "Bad Request. \n Invalid request or validation error."), + message = "Bad Request. \n Invalid request or validation error.", + response = ErrorResponse.class), @ApiResponse( code = 404, message = "Not Found. \n Resource to be deleted does not exist."), @@ -317,7 +322,8 @@ public interface RoleManagementService { @ApiResponse( code = 500, message = "Internal Server ErrorResponse. \n " + - "Server error occurred while updating the role.") + "Server error occurred while updating the role.", + response = ErrorResponse.class) }) @Permission(scope = "roles-modify", permissions = {"/permission/admin/device-mgt/admin/roles/update"}) Response updateRole( @@ -349,7 +355,8 @@ public interface RoleManagementService { @ApiResponse( code = 500, message = "Internal Server ErrorResponse. \n " + - "Server error occurred while removing the role.") + "Server error occurred while removing the role.", + response = ErrorResponse.class) }) @Permission(scope = "roles-modify", permissions = {"/permission/admin/device-mgt/admin/roles/remove"}) Response deleteRole( @@ -395,7 +402,8 @@ public interface RoleManagementService { "Used by caches, or in conditional requests.")}), @ApiResponse( code = 400, - message = "Bad Request. \n Invalid request or validation error."), + message = "Bad Request. \n Invalid request or validation error.", + response = ErrorResponse.class), @ApiResponse( code = 404, message = "Not Found. \n Resource to be deleted does not exist."), @@ -405,7 +413,8 @@ public interface RoleManagementService { @ApiResponse( code = 500, message = "Internal Server ErrorResponse. \n " + - "Server error occurred while updating the user list of the role.") + "Server error occurred while updating the user list of the role.", + response = ErrorResponse.class) }) @Permission(scope = "roles-modify", permissions = {"/permission/admin/device-mgt/admin/roles/update"}) Response updateUsersOfRole( diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/UserManagementAdminService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/UserManagementAdminService.java index baec3eff63..d44bd57f77 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/UserManagementAdminService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/UserManagementAdminService.java @@ -62,7 +62,7 @@ public interface UserManagementAdminService { "Server error occurred while updating credentials of the user.") }) @Permission(scope = "user-modify", permissions = {"/permission/admin/login"}) - Response resetPassword( + Response resetUserPassword( @ApiParam( name = "username", value = "Username of the user.", diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/RoleManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/RoleManagementServiceImpl.java index 2007821939..8979f59641 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/RoleManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/RoleManagementServiceImpl.java @@ -22,7 +22,10 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.CarbonConstants; import org.wso2.carbon.base.MultitenantConstants; +import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; +import org.wso2.carbon.device.mgt.jaxrs.beans.RoleList; import org.wso2.carbon.device.mgt.jaxrs.service.api.RoleManagementService; +import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.UnexpectedServerErrorException; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.device.mgt.jaxrs.beans.RoleWrapper; import org.wso2.carbon.device.mgt.jaxrs.util.SetReferenceTransformer; @@ -54,17 +57,22 @@ public class RoleManagementServiceImpl implements RoleManagementService { @HeaderParam("If-Modified-Since") String ifModifiedSince, @QueryParam("offset") int offset, @QueryParam("limit") int limit) { List filteredRoles; + RoleList targetRoles; try { filteredRoles = getRolesFromUserStore(); - if (filteredRoles == null || filteredRoles.size() == 0) { + if (filteredRoles == null) { return Response.status(Response.Status.NOT_FOUND).entity("No roles found.").build(); } + targetRoles = new RoleList(); + targetRoles.setCount(filteredRoles.size()); + targetRoles.setList(filteredRoles); } catch (UserStoreException e) { - String msg = "ErrorResponse occurred while retrieving roles from the underlying user stores"; + String msg = "Error occurred while retrieving roles from the underlying user stores"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } - return Response.status(Response.Status.OK).entity(filteredRoles).build(); + return Response.status(Response.Status.OK).entity(targetRoles).build(); } @GET @@ -88,14 +96,16 @@ public class RoleManagementServiceImpl implements RoleManagementService { } return Response.status(Response.Status.OK).entity(rolePermissions).build(); } catch (UserAdminException e) { - String msg = "ErrorResponse occurred while retrieving the permissions of role '" + roleName + "'"; + String msg = "Error occurred while retrieving the permissions of role '" + roleName + "'"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } catch (UserStoreException e) { - String msg = "ErrorResponse occurred while retrieving the underlying user realm attached to the " + + String msg = "Error occurred while retrieving the underlying user realm attached to the " + "current logged in user"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } } @@ -150,9 +160,10 @@ public class RoleManagementServiceImpl implements RoleManagementService { roleWrapper.setPermissions(permList.toArray(permListAr)); } } catch (UserStoreException | UserAdminException e) { - String msg = "ErrorResponse occurred while retrieving the user role '" + roleName + "'"; + String msg = "Error occurred while retrieving the user role '" + roleName + "'"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } return Response.status(Response.Status.OK).entity(roleWrapper).build(); } @@ -186,11 +197,13 @@ public class RoleManagementServiceImpl implements RoleManagementService { } userStoreManager.addRole(roleWrapper.getRoleName(), roleWrapper.getUsers(), permissions); } catch (UserStoreException e) { - String msg = "ErrorResponse occurred while adding role '" + roleWrapper.getRoleName() + "'"; + String msg = "Error occurred while adding role '" + roleWrapper.getRoleName() + "'"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } - return Response.status(Response.Status.OK).build(); + return Response.status(Response.Status.OK).entity("Role '" + roleWrapper.getRoleName() + "' has " + + "successfully been added").build(); } @PUT @@ -228,11 +241,13 @@ public class RoleManagementServiceImpl implements RoleManagementService { } } } catch (UserStoreException e) { - String msg = "ErrorResponse occurred while updating role '" + roleName + "'"; + String msg = "Error occurred while updating role '" + roleName + "'"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } - return Response.status(Response.Status.OK).build(); + return Response.status(Response.Status.OK).entity("Role '" + roleWrapper.getRoleName() + "' has " + + "successfully been updated").build(); } @DELETE @@ -249,11 +264,13 @@ public class RoleManagementServiceImpl implements RoleManagementService { // Delete all authorizations for the current role before deleting authorizationManager.clearRoleAuthorization(roleName); } catch (UserStoreException e) { - String msg = "ErrorResponse occurred while deleting the role '" + roleName + "'"; + String msg = "Error occurred while deleting the role '" + roleName + "'"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } - return Response.status(Response.Status.OK).build(); + return Response.status(Response.Status.OK).entity("Role '" + roleName + "' has " + + "successfully been deleted").build(); } @POST @@ -275,11 +292,13 @@ public class RoleManagementServiceImpl implements RoleManagementService { userStoreManager.updateUserListOfRole(roleName, usersToDelete, usersToAdd); } catch (UserStoreException e) { - String msg = "ErrorResponse occurred while updating the users of the role '" + roleName + "'"; + String msg = "Error occurred while updating the users of the role '" + roleName + "'"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); } - return Response.status(Response.Status.OK).build(); + return Response.status(Response.Status.OK).entity("Role '" + roleName + "' has " + + "successfully been updated with the user list").build(); } private List getRolesFromUserStore() throws UserStoreException { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java index 1613c49b87..8067c74498 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java @@ -34,7 +34,7 @@ public class UserManagementAdminServiceImpl implements UserManagementAdminServic @POST @Path("/{username}/credentials") @Override - public Response resetPassword(@PathParam("username") String user, PasswordResetWrapper credentials) { + public Response resetUserPassword(@PathParam("username") String user, PasswordResetWrapper credentials) { return CredentialManagementResponseBuilder.buildResetPasswordResponse(user, credentials); }