UsersApi { @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 public ResponseEntity<String> createUser( @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(); if (profile == Profile.Administrator) { checkIfAtLeastOneAdminIsEnabled(userDto, userRepository); } if (!myProfile.getAll().contains(profile)) { throw new IllegalArgumentException( "Trying to set profile to " + profile + " max profile permitted is: " + myProfile); } if (StringUtils.isEmpty(userDto.getUsername())) { throw new IllegalArgumentException(Params.USERNAME + " is a required parameter for " + Params.Operation.NEWUSER + " " + "operation"); } List<User> existingUsers = userRepository.findByUsernameIgnoreCase(userDto.getUsername()); if (!existingUsers.isEmpty()) { throw new IllegalArgumentException("Users with username " + userDto.getUsername() + " ignore case already exists"); } 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)); User user = new User(); user.getSecurity().setPassword( PasswordUtil.encoder(ApplicationContextHolder.get()).encode( userDto.getPassword())); 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 createUser() throws Exception { UserDto user = new UserDto(); user.setUsername("newuser"); user.setName("new"); user.setProfile(Profile.Editor.name()); user.setGroupsEditor(Collections.singletonList("2")); user.setEmail(Collections.singletonList("mail@test.com")); user.setPassword("password"); 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") .content(json) .contentType(API_JSON_EXPECTED_ENCODING) .session(this.mockHttpSession) .accept(MediaType.parseMediaType("application/json"))) .andExpect(status().is(204)); User userCreated = _userRepo.findOneByUsername(user.getUsername()); Assert.assertNotNull(userCreated); }