Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/main/java/org/blueline/api/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/users")
@RequiredArgsConstructor
Expand Down Expand Up @@ -76,6 +78,20 @@ public ResponseEntity<UserDto> getMe(Authentication authentication) {
return new ResponseEntity<>(userService.getMe(authentication), HttpStatus.OK);
}

@GetMapping()
@Operation(
summary = "Get all users",
description = "The user must be authenticated and admin",
responses = {
@ApiResponse(responseCode = "200", description = "User found"),
@ApiResponse(responseCode = "401", description = "Unauthorized", content = @Content(schema = @Schema(implementation = ExceptionDto.class)))
},
security = @SecurityRequirement(name = "bearerAuth")
)
public ResponseEntity<List<UserDto>> getAll(Authentication authentication) {
return new ResponseEntity<>(userService.getAll(authentication), HttpStatus.OK);
}

@PutMapping("/me")
@Operation(
summary = "Update the authenticated user",
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/blueline/api/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.RequiredArgsConstructor;
import org.blueline.api.exception.ConflictException;
import org.blueline.api.exception.UnauthorizedException;
import org.blueline.api.model.User;
import org.blueline.api.model.dto.UserDto;
import org.blueline.api.repository.UserRepository;
Expand All @@ -11,6 +12,7 @@
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

@Service
Expand Down Expand Up @@ -69,5 +71,15 @@ public static String generateUniqueFriendCode(long number) {
return hashids.encode(number);
}

public List<UserDto> getAll(Authentication authentication) {
User user = authService.authenticate(authentication);

if(!user.isAdmin()) {
throw new UnauthorizedException("You do not have permission get all users");
}
return userRepository.findAll().stream()
.map(u -> modelMapper.map(u, UserDto.class))
.toList();
}
}

Binary file modified target/classes/org/blueline/api/controller/UserController.class
Binary file not shown.
Binary file modified target/classes/org/blueline/api/service/UserService.class
Binary file not shown.