Update whoami endpoint to return a userWithRole projection

This commit is contained in:
Marcel Schwarz 2020-05-13 14:44:45 +02:00
parent bd89747020
commit 0d95e5f8f6
3 changed files with 14 additions and 6 deletions

View File

@ -4,6 +4,7 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@SpringBootApplication @SpringBootApplication
@ -19,4 +20,9 @@ public class GeotimeApplication {
return new BCryptPasswordEncoder(); return new BCryptPasswordEncoder();
} }
@Bean
public SpelAwareProxyProjectionFactory projectionFactory() {
return new SpelAwareProxyProjectionFactory();
}
} }

View File

@ -1,7 +1,9 @@
package de.hft.geotime.controllers; package de.hft.geotime.controllers;
import de.hft.geotime.entities.TimetrackUser; import de.hft.geotime.entities.TimetrackUser;
import de.hft.geotime.entities.projections.UserWithRoleProjection;
import de.hft.geotime.repositories.TimetrackUserRepository; import de.hft.geotime.repositories.TimetrackUserRepository;
import org.springframework.data.projection.ProjectionFactory;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
@ -16,16 +18,18 @@ public class UserController {
private final TimetrackUserRepository userRepository; private final TimetrackUserRepository userRepository;
private final BCryptPasswordEncoder bCryptPasswordEncoder; private final BCryptPasswordEncoder bCryptPasswordEncoder;
private final ProjectionFactory projectionFactory;
public UserController(TimetrackUserRepository userRepository, BCryptPasswordEncoder bCryptPasswordEncoder) { public UserController(TimetrackUserRepository userRepository, BCryptPasswordEncoder bCryptPasswordEncoder, ProjectionFactory projectionFactory) {
this.userRepository = userRepository; this.userRepository = userRepository;
this.bCryptPasswordEncoder = bCryptPasswordEncoder; this.bCryptPasswordEncoder = bCryptPasswordEncoder;
this.projectionFactory = projectionFactory;
} }
@GetMapping("/whoami") @GetMapping("/whoami")
public String getUsername(Authentication authentication) { public UserWithRoleProjection getUsername(Authentication authentication) {
TimetrackUser timetrackUser = userRepository.findFirstByUsername(authentication.getName()); TimetrackUser user = userRepository.findFirstByUsername(authentication.getName());
return "Welcome back " + timetrackUser.getFirstname() + " " + timetrackUser.getLastname(); return projectionFactory.createProjection(UserWithRoleProjection.class, user);
} }
@PostMapping("/sign-up") @PostMapping("/sign-up")

View File

@ -1,8 +1,6 @@
package de.hft.geotime.entities; package de.hft.geotime.entities;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import de.hft.geotime.entities.Role;
import de.hft.geotime.entities.TimetrackAccount;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;