@Test public void removeUserFromSuperAdminRoleSuccess() throws Exception { AuthorizationResource authorizationResource = new AuthorizationResource(authorization, new HttpHeader("jaba-???", "600")); UserInfo assigningUserInfo = UserInfo.from(TESTUSER).build(); UserInfo candidateUserInfo = UserInfo.from(USER).build(); when(authorization.getUser(AUTHHEADER)).thenReturn(USER); when(authorization.getUserInfo(USER)).thenReturn(candidateUserInfo); when(authorization.getUserInfo(TESTUSER)).thenReturn(assigningUserInfo); Response response = authorizationResource.removeUserFromSuperAdmin(TESTUSER, AUTHHEADER); Mockito.verify(authorization, times(1)).checkSuperAdmin(USER); Mockito.verify(authorization, times(1)) .removeUserFromSuperAdminRole(assigningUserInfo, candidateUserInfo); assertThat(response.getStatus(), CoreMatchers.<Object>equalTo(204)); }
@DELETE @Path("/superadmins/{userID}") @Produces(APPLICATION_JSON) @ApiOperation(value = "Remove superadmin privileges to user") @Timed public Response removeUserFromSuperAdmin( @PathParam("userID") @ApiParam(value = "User ID") final Username userID, @HeaderParam(AUTHORIZATION) @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true) final String authorizationHeader) { try { LOGGER.debug("Removing user {} from superadmin ", userID); Username assigningUser = authorization.getUser(authorizationHeader); UserInfo assigningUserInfo = authorization.getUserInfo(assigningUser); authorization.checkSuperAdmin(assigningUser); UserInfo candidateUserInfo = authorization.getUserInfo(userID); Preconditions.checkArgument(nonNull(candidateUserInfo), "User id %s is invalid", userID); authorization.removeUserFromSuperAdminRole(candidateUserInfo, assigningUserInfo); return httpHeader.headers(Status.NO_CONTENT).build(); } catch (Exception exception) { LOGGER.error("removeUserFromSuperAdmin failed for usedID={} with error:", userID, exception); throw exception; } }
AuthorizationResource { @DELETE @Path("/superadmins/{userID}") @Produces(APPLICATION_JSON) @ApiOperation(value = "Remove superadmin privileges to user") @Timed public Response removeUserFromSuperAdmin( @PathParam("userID") @ApiParam(value = "User ID") final Username userID, @HeaderParam(AUTHORIZATION) @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true) final String authorizationHeader) { try { LOGGER.debug("Removing user {} from superadmin ", userID); Username assigningUser = authorization.getUser(authorizationHeader); UserInfo assigningUserInfo = authorization.getUserInfo(assigningUser); authorization.checkSuperAdmin(assigningUser); UserInfo candidateUserInfo = authorization.getUserInfo(userID); Preconditions.checkArgument(nonNull(candidateUserInfo), "User id %s is invalid", userID); authorization.removeUserFromSuperAdminRole(candidateUserInfo, assigningUserInfo); return httpHeader.headers(Status.NO_CONTENT).build(); } catch (Exception exception) { LOGGER.error("removeUserFromSuperAdmin failed for usedID={} with error:", userID, exception); throw exception; } } }
AuthorizationResource { @DELETE @Path("/superadmins/{userID}") @Produces(APPLICATION_JSON) @ApiOperation(value = "Remove superadmin privileges to user") @Timed public Response removeUserFromSuperAdmin( @PathParam("userID") @ApiParam(value = "User ID") final Username userID, @HeaderParam(AUTHORIZATION) @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true) final String authorizationHeader) { try { LOGGER.debug("Removing user {} from superadmin ", userID); Username assigningUser = authorization.getUser(authorizationHeader); UserInfo assigningUserInfo = authorization.getUserInfo(assigningUser); authorization.checkSuperAdmin(assigningUser); UserInfo candidateUserInfo = authorization.getUserInfo(userID); Preconditions.checkArgument(nonNull(candidateUserInfo), "User id %s is invalid", userID); authorization.removeUserFromSuperAdminRole(candidateUserInfo, assigningUserInfo); return httpHeader.headers(Status.NO_CONTENT).build(); } catch (Exception exception) { LOGGER.error("removeUserFromSuperAdmin failed for usedID={} with error:", userID, exception); throw exception; } } @Inject  AuthorizationResource(final Authorization authorization, final HttpHeader httpHeader); }
AuthorizationResource { @DELETE @Path("/superadmins/{userID}") @Produces(APPLICATION_JSON) @ApiOperation(value = "Remove superadmin privileges to user") @Timed public Response removeUserFromSuperAdmin( @PathParam("userID") @ApiParam(value = "User ID") final Username userID, @HeaderParam(AUTHORIZATION) @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true) final String authorizationHeader) { try { LOGGER.debug("Removing user {} from superadmin ", userID); Username assigningUser = authorization.getUser(authorizationHeader); UserInfo assigningUserInfo = authorization.getUserInfo(assigningUser); authorization.checkSuperAdmin(assigningUser); UserInfo candidateUserInfo = authorization.getUserInfo(userID); Preconditions.checkArgument(nonNull(candidateUserInfo), "User id %s is invalid", userID); authorization.removeUserFromSuperAdminRole(candidateUserInfo, assigningUserInfo); return httpHeader.headers(Status.NO_CONTENT).build(); } catch (Exception exception) { LOGGER.error("removeUserFromSuperAdmin failed for usedID={} with error:", userID, exception); throw exception; } } @Inject  AuthorizationResource(final Authorization authorization, final HttpHeader httpHeader); @GET @Path("/roles/{role}/permissions") @Produces(APPLICATION_JSON) @ApiOperation(value = "Get permissions associated with a specific user role") @Timed Response getRolePermissions(
            @PathParam("role")
            @ApiParam(defaultValue = DEFAULT_ROLE, value = EXAMPLE_ALL_ROLES)
            final String role); @GET @Path("/users/{userID}/permissions") @Produces(APPLICATION_JSON) @ApiOperation(value = "Get permissions for a user across applications") @Timed Response getUserPermissions(
            @PathParam("userID")
            @ApiParam(value = "User ID")
            final Username userID,

            @HeaderParam(AUTHORIZATION)
            @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true)
            final String authorizationHeader); @GET @Path("/users/{userID}/applications/{applicationName}/permissions") @Produces(APPLICATION_JSON) @ApiOperation(value = "Get permissions of one user within a single application") @Timed Response getUserAppPermissions(
            @PathParam("userID")
            @ApiParam(value = "User ID")
            final Username userID,

            @PathParam("applicationName")
            @ApiParam(value = "Application Name")
            final Application.Name applicationName,

            @HeaderParam(AUTHORIZATION)
            @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true)
            final String authorizationHeader); @POST @Path("/roles") @Consumes(APPLICATION_JSON) @Produces(APPLICATION_JSON) @ApiOperation(value = "Assign roles for a list of users and applications") @Timed Response assignUserRoles(
            @ApiParam(name = "userRoleList", value = "Please see model example", required = true)
            final UserRoleList userRoleList,

            @HeaderParam(AUTHORIZATION)
            @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true)
            final String authorizationHeader); @POST @Path("/superadmins/{userID}") @Produces(APPLICATION_JSON) @ApiOperation(value = "Assign superadmin privileges to user") @Timed Response assignUserToSuperAdmin(
            @PathParam("userID")
            @ApiParam(value = "User ID")
            final Username userID,
            @HeaderParam(AUTHORIZATION)
            @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true)
            final String authorizationHeader); @DELETE @Path("/superadmins/{userID}") @Produces(APPLICATION_JSON) @ApiOperation(value = "Remove superadmin privileges to user") @Timed Response removeUserFromSuperAdmin(
            @PathParam("userID")
            @ApiParam(value = "User ID")
            final Username userID,
            @HeaderParam(AUTHORIZATION)
            @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true)
            final String authorizationHeader); @GET @Path("/superadmins") @Produces(APPLICATION_JSON) @ApiOperation(value = "Get all superadmins") @Timed Response getAllSuperAdminRoleList(
            @HeaderParam(AUTHORIZATION)
            @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true)
            final String authorizationHeader); @GET @Path("/users/{userID}/roles") @Produces(APPLICATION_JSON) @ApiOperation(value = "Get access roles for a user across applications") @Timed Response getUserRole(
            @PathParam("userID")
            @ApiParam(value = "User ID")
            final Username userID,

            @HeaderParam(AUTHORIZATION)
            @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true)
            final String authorizationHeader); @PUT @Path("/roles") @Consumes(APPLICATION_JSON) @Produces(APPLICATION_JSON) @ApiOperation(value = "Update roles for a list of users and applications") @Timed Response updateUserRoles(
            @ApiParam(name = "userRoleList", value = "Please see model example", required = true)
            final UserRoleList userRoleList,

            @HeaderParam(AUTHORIZATION)
            @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true)
            final String authorizationHeader); @DELETE @Path("/applications/{applicationName}/users/{userID}/roles") @Produces(APPLICATION_JSON) // @RolesAllowed("ADMIN") @ApiOperation(value = "Delete a user's role within an application") @Timed Response deleteUserRoles(
            @PathParam("applicationName")
            @ApiParam(value = "Application Name")
            final Application.Name applicationName,

            @PathParam("userID")
            @ApiParam(value = "User ID")
            final Username userID,

            @HeaderParam(AUTHORIZATION)
            @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true)
            final String authorizationHeader); @GET @Path("/applications/{applicationName}") @Produces(APPLICATION_JSON) @ApiOperation(value = "Get roles for all users within an application") @Timed Response getApplicationUsersByRole(
            @PathParam("applicationName")
            @ApiParam(value = "Application Name")
            final Application.Name applicationName,

            @HeaderParam(AUTHORIZATION)
            @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true)
            final String authorizationHeader); @GET @Path("/applications") @Produces(APPLICATION_JSON) @ApiOperation(value = "Get access roles for all users for all applications that the given user belongs to") @Timed Response getUserList(
            @HeaderParam(AUTHORIZATION)
            @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true) String authHeader); }
AuthorizationResource { @DELETE @Path("/superadmins/{userID}") @Produces(APPLICATION_JSON) @ApiOperation(value = "Remove superadmin privileges to user") @Timed public Response removeUserFromSuperAdmin( @PathParam("userID") @ApiParam(value = "User ID") final Username userID, @HeaderParam(AUTHORIZATION) @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true) final String authorizationHeader) { try { LOGGER.debug("Removing user {} from superadmin ", userID); Username assigningUser = authorization.getUser(authorizationHeader); UserInfo assigningUserInfo = authorization.getUserInfo(assigningUser); authorization.checkSuperAdmin(assigningUser); UserInfo candidateUserInfo = authorization.getUserInfo(userID); Preconditions.checkArgument(nonNull(candidateUserInfo), "User id %s is invalid", userID); authorization.removeUserFromSuperAdminRole(candidateUserInfo, assigningUserInfo); return httpHeader.headers(Status.NO_CONTENT).build(); } catch (Exception exception) { LOGGER.error("removeUserFromSuperAdmin failed for usedID={} with error:", userID, exception); throw exception; } } @Inject  AuthorizationResource(final Authorization authorization, final HttpHeader httpHeader); @GET @Path("/roles/{role}/permissions") @Produces(APPLICATION_JSON) @ApiOperation(value = "Get permissions associated with a specific user role") @Timed Response getRolePermissions(
            @PathParam("role")
            @ApiParam(defaultValue = DEFAULT_ROLE, value = EXAMPLE_ALL_ROLES)
            final String role); @GET @Path("/users/{userID}/permissions") @Produces(APPLICATION_JSON) @ApiOperation(value = "Get permissions for a user across applications") @Timed Response getUserPermissions(
            @PathParam("userID")
            @ApiParam(value = "User ID")
            final Username userID,

            @HeaderParam(AUTHORIZATION)
            @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true)
            final String authorizationHeader); @GET @Path("/users/{userID}/applications/{applicationName}/permissions") @Produces(APPLICATION_JSON) @ApiOperation(value = "Get permissions of one user within a single application") @Timed Response getUserAppPermissions(
            @PathParam("userID")
            @ApiParam(value = "User ID")
            final Username userID,

            @PathParam("applicationName")
            @ApiParam(value = "Application Name")
            final Application.Name applicationName,

            @HeaderParam(AUTHORIZATION)
            @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true)
            final String authorizationHeader); @POST @Path("/roles") @Consumes(APPLICATION_JSON) @Produces(APPLICATION_JSON) @ApiOperation(value = "Assign roles for a list of users and applications") @Timed Response assignUserRoles(
            @ApiParam(name = "userRoleList", value = "Please see model example", required = true)
            final UserRoleList userRoleList,

            @HeaderParam(AUTHORIZATION)
            @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true)
            final String authorizationHeader); @POST @Path("/superadmins/{userID}") @Produces(APPLICATION_JSON) @ApiOperation(value = "Assign superadmin privileges to user") @Timed Response assignUserToSuperAdmin(
            @PathParam("userID")
            @ApiParam(value = "User ID")
            final Username userID,
            @HeaderParam(AUTHORIZATION)
            @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true)
            final String authorizationHeader); @DELETE @Path("/superadmins/{userID}") @Produces(APPLICATION_JSON) @ApiOperation(value = "Remove superadmin privileges to user") @Timed Response removeUserFromSuperAdmin(
            @PathParam("userID")
            @ApiParam(value = "User ID")
            final Username userID,
            @HeaderParam(AUTHORIZATION)
            @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true)
            final String authorizationHeader); @GET @Path("/superadmins") @Produces(APPLICATION_JSON) @ApiOperation(value = "Get all superadmins") @Timed Response getAllSuperAdminRoleList(
            @HeaderParam(AUTHORIZATION)
            @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true)
            final String authorizationHeader); @GET @Path("/users/{userID}/roles") @Produces(APPLICATION_JSON) @ApiOperation(value = "Get access roles for a user across applications") @Timed Response getUserRole(
            @PathParam("userID")
            @ApiParam(value = "User ID")
            final Username userID,

            @HeaderParam(AUTHORIZATION)
            @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true)
            final String authorizationHeader); @PUT @Path("/roles") @Consumes(APPLICATION_JSON) @Produces(APPLICATION_JSON) @ApiOperation(value = "Update roles for a list of users and applications") @Timed Response updateUserRoles(
            @ApiParam(name = "userRoleList", value = "Please see model example", required = true)
            final UserRoleList userRoleList,

            @HeaderParam(AUTHORIZATION)
            @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true)
            final String authorizationHeader); @DELETE @Path("/applications/{applicationName}/users/{userID}/roles") @Produces(APPLICATION_JSON) // @RolesAllowed("ADMIN") @ApiOperation(value = "Delete a user's role within an application") @Timed Response deleteUserRoles(
            @PathParam("applicationName")
            @ApiParam(value = "Application Name")
            final Application.Name applicationName,

            @PathParam("userID")
            @ApiParam(value = "User ID")
            final Username userID,

            @HeaderParam(AUTHORIZATION)
            @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true)
            final String authorizationHeader); @GET @Path("/applications/{applicationName}") @Produces(APPLICATION_JSON) @ApiOperation(value = "Get roles for all users within an application") @Timed Response getApplicationUsersByRole(
            @PathParam("applicationName")
            @ApiParam(value = "Application Name")
            final Application.Name applicationName,

            @HeaderParam(AUTHORIZATION)
            @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true)
            final String authorizationHeader); @GET @Path("/applications") @Produces(APPLICATION_JSON) @ApiOperation(value = "Get access roles for all users for all applications that the given user belongs to") @Timed Response getUserList(
            @HeaderParam(AUTHORIZATION)
            @ApiParam(value = EXAMPLE_AUTHORIZATION_HEADER, required = true) String authHeader);  }