UsersApi { @ApiOperation( value = "Update a user", notes = "Updates a catalog user.", nickname = "updateUser") @RequestMapping(value = "/{userIdentifier}", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.PUT) @ResponseStatus(value = HttpStatus.OK) @PreAuthorize("isAuthenticated()") @ResponseBody public ResponseEntity<String> updateUser( @ApiParam( value = "User identifier." ) @PathVariable Integer userIdentifier, @ApiParam( name = "user" ) @RequestBody UserDto userDto, @ApiIgnore ServletRequest request, @ApiIgnore HttpSession httpSession ) throws Exception { Profile profile = Profile.findProfileIgnoreCase(userDto.getProfile()); UserSession session = ApiUtils.getUserSession(httpSession); Profile myProfile = session.getProfile(); String myUserId = session.getUserId(); if (myProfile != Profile.Administrator && myProfile != Profile.UserAdmin && !myUserId.equals(Integer.toString(userIdentifier))) { throw new IllegalArgumentException("You don't have rights to do this"); } if (profile == Profile.Administrator) { checkIfAtLeastOneAdminIsEnabled(userDto, userRepository); } User user = userRepository.findOne(userIdentifier); if (user == null) { throw new IllegalArgumentException("No user found with id: " + userDto.getId()); } List<User> usersWithUsernameIgnoreCase = userRepository.findByUsernameIgnoreCase(userDto.getUsername()); if (usersWithUsernameIgnoreCase.size() != 0 && (!usersWithUsernameIgnoreCase.stream().anyMatch(u -> u.getId() == userIdentifier) || usersWithUsernameIgnoreCase.stream().anyMatch(u -> u.getUsername().equals(userDto.getUsername()) && u.getId() != userIdentifier) )) { throw new IllegalArgumentException(String.format( "Another user with username '%s' ignore case already exists", user.getUsername())); } if (!myProfile.getAll().contains(profile)) { throw new IllegalArgumentException( "Trying to set profile to " + profile + " max profile permitted is: " + myProfile); } List<GroupElem> groups = new LinkedList<>(); groups.addAll(processGroups(userDto.getGroupsRegisteredUser(), Profile.RegisteredUser)); groups.addAll(processGroups(userDto.getGroupsEditor(), Profile.Editor)); groups.addAll(processGroups(userDto.getGroupsReviewer(), Profile.Reviewer)); groups.addAll(processGroups(userDto.getGroupsUserAdmin(), Profile.UserAdmin)); if (!myProfile.equals(Profile.Administrator)) { List<Integer> myUserAdminGroups = userGroupRepository.findGroupIds(Specifications.where( hasProfile(myProfile)).and(hasUserId(Integer.parseInt(myUserId)))); List<UserGroup> usergroups = userGroupRepository.findAll(Specifications.where( hasUserId(Integer.parseInt(userDto.getId())))); for (UserGroup ug : usergroups) { if (!myUserAdminGroups.contains(ug.getGroup().getId())) { groups.add(new GroupElem(ug.getProfile().name(), ug.getGroup().getId())); } } } fillUserFromParams(user, userDto); user = userRepository.save(user); setUserGroups(user, groups); return new ResponseEntity(HttpStatus.NO_CONTENT); } UsersApi(); @ApiOperation( value = "Get users", notes = "", nickname = "getUsers") @RequestMapping( produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET) @ResponseStatus(value = HttpStatus.OK) @PreAuthorize("isAuthenticated()") @ResponseBody List<User> getUsers(
        @ApiIgnore
            HttpSession httpSession
    ); @ApiOperation( value = "Get user", notes = "", nickname = "getUser") @RequestMapping( value = "/{userIdentifier}", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET) @ResponseStatus(value = HttpStatus.OK) @PreAuthorize("isAuthenticated()") @ResponseBody User getUser(
        @ApiParam(
            value = "User identifier."
        )
        @PathVariable
            Integer userIdentifier,
        @ApiIgnore
            HttpSession httpSession

    ); @ApiOperation( value = "Get user identicon", notes = "", nickname = "getUserIdentIcon") @RequestMapping( value = "/{userIdentifier}.png", produces = MediaType.IMAGE_PNG_VALUE, method = RequestMethod.GET) @ResponseBody void getUser(
        @ApiParam(
            value = "User identifier."
        )
        @PathVariable
            Integer userIdentifier,
        @ApiParam(
            value = "Size."
        )
        @RequestParam(defaultValue = "18")
            Integer size,
        @ApiIgnore
            HttpServletResponse response
    ); @ApiOperation( value = "Delete a user", notes = "Deletes a catalog user by identifier.", nickname = "deleteUser") @RequestMapping(value = "/{userIdentifier}", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.DELETE) @ResponseStatus(value = HttpStatus.OK) @PreAuthorize("hasRole('UserAdmin') or hasRole('Administrator')") @ResponseBody ResponseEntity<String> deleteUser(
        @ApiParam(
            value = "User identifier."
        )
        @PathVariable
            Integer userIdentifier,
        @ApiIgnore
            ServletRequest request,
        @ApiIgnore
            HttpSession httpSession
    ); @ApiOperation( value = "Check if a user property already exist", notes = "", authorizations = { @Authorization(value = "basicAuth") }, nickname = "checkUserPropertyExist") @RequestMapping( value = "/properties/{property}", method = RequestMethod.GET ) @ResponseStatus(value = HttpStatus.OK) @PreAuthorize("hasRole('UserAdmin')") @ApiResponses(value = { @ApiResponse(code = 200, message = "Property does not exist."), @ApiResponse(code = 404, message = "A property with that value already exist."), @ApiResponse(code = 403, message = ApiParams.API_RESPONSE_NOT_ALLOWED_ONLY_USER_ADMIN) }) ResponseEntity<HttpStatus> checkUserPropertyExist(
        @ApiParam(
            value = "The user property to check"
        )
        @PathVariable
            String property,
        @ApiParam(
            value = "The value to search"
        )
        @RequestParam
            String exist); @ApiOperation( value = "Creates a user", notes = "Creates a catalog user.", nickname = "createUser") @RequestMapping( produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.PUT) @ResponseStatus(value = HttpStatus.OK) @PreAuthorize("hasRole('UserAdmin') or hasRole('Administrator')") @ResponseBody ResponseEntity<String> createUser(
        @ApiParam(
            name = "user"
        )
        @RequestBody
            UserDto userDto,
        @ApiIgnore
            ServletRequest request,
        @ApiIgnore
            HttpSession httpSession
    ); @ApiOperation( value = "Update a user", notes = "Updates a catalog user.", nickname = "updateUser") @RequestMapping(value = "/{userIdentifier}", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.PUT) @ResponseStatus(value = HttpStatus.OK) @PreAuthorize("isAuthenticated()") @ResponseBody ResponseEntity<String> updateUser(
        @ApiParam(
            value = "User identifier."
        )
        @PathVariable
            Integer userIdentifier,
        @ApiParam(
            name = "user"
        )
        @RequestBody
            UserDto userDto,
        @ApiIgnore
            ServletRequest request,
        @ApiIgnore
            HttpSession httpSession
    ); @ApiOperation( value = "Resets user password", notes = "Resets the user password.", nickname = "resetUserPassword") @RequestMapping(value = "/{userIdentifier}/actions/forget-password", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST) @ResponseStatus(value = HttpStatus.OK) @PreAuthorize("isAuthenticated()") @ResponseBody ResponseEntity<String> resetUserPassword(
        @ApiParam(
            value = "User identifier."
        )
        @PathVariable
            Integer userIdentifier,
        @ApiParam(
            value = "Password to change."
        )
        @RequestParam(value = Params.PASSWORD) String password,
        @ApiParam(
            value = "Password to change (repeat)."
        )
        @RequestParam(value = Params.PASSWORD + "2") String password2,
        @ApiIgnore
            ServletRequest request,
        @ApiIgnore
            HttpSession httpSession
    ); @ApiOperation( value = "Retrieve user groups", notes = "Retrieve the user groups.", nickname = "retrieveUserGroups") @RequestMapping(value = "/{userIdentifier}/groups", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET ) @ResponseStatus(value = HttpStatus.OK) @PreAuthorize("isAuthenticated()") @ResponseBody List<UserGroup> retrieveUserGroups(
        @ApiParam(
            value = "User identifier."
        )
        @PathVariable
            Integer userIdentifier,
        @ApiIgnore
            ServletRequest request,
        @ApiIgnore
            HttpSession httpSession
    );  }
@Test public void updateUser() throws Exception { User userToUpdate = _userRepo.findOneByUsername("testuser-editor"); Assert.assertNotNull(userToUpdate); UserDto user = new UserDto(); user.setUsername(userToUpdate.getUsername()); user.setName(userToUpdate.getName() + "-updated"); user.setProfile(userToUpdate.getProfile().toString()); user.setGroupsEditor(Collections.singletonList("2")); user.setEmail(new ArrayList(userToUpdate.getEmailAddresses())); user.setEnabled(true); Gson gson = new Gson(); String json = gson.toJson(user); this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); this.mockHttpSession = loginAsAdmin(); this.mockMvc.perform(put("/srv/api/users/" + userToUpdate.getId()) .content(json) .contentType(API_JSON_EXPECTED_ENCODING) .session(this.mockHttpSession) .accept(MediaType.parseMediaType("application/json"))) .andExpect(status().is(204)); }