-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from tofiksa/16-expand-userdetails-api
[ADD] - Lagt til uthenting av brukerobjekt
- Loading branch information
Showing
4 changed files
with
41 additions
and
14 deletions.
There are no files selected for viewing
20 changes: 6 additions & 14 deletions
20
src/main/java/no/josefushighscore/controller/UserInfoController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,27 @@ | ||
package no.josefushighscore.controller; | ||
|
||
import no.josefushighscore.exception.InvalidJwtAuthenticationException; | ||
import no.josefushighscore.service.UserService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.access.annotation.Secured; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
import static org.springframework.http.ResponseEntity.ok; | ||
|
||
@RestController() | ||
public class UserInfoController { | ||
|
||
@Autowired | ||
private UserService userService; | ||
|
||
|
||
@Secured("ROLE_USER") | ||
@GetMapping("/me") | ||
public ResponseEntity currentUser(@AuthenticationPrincipal UserDetails userDetails) throws InvalidJwtAuthenticationException { | ||
Map<Object, Object> model = new HashMap<>(); | ||
model.put("username", userDetails.getUsername()); | ||
model.put("isAccountNonExpired", userDetails.isAccountNonExpired()); | ||
model.put("roles", userDetails.getAuthorities() | ||
.stream() | ||
.map(a -> ((GrantedAuthority) a).getAuthority()) | ||
.collect(toList()) | ||
); | ||
return ok(model); | ||
return ok(userService.getUserDetails(userDetails.getUsername())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,20 @@ | ||
package no.josefushighscore.register; | ||
|
||
|
||
import no.josefushighscore.dto.UserDto; | ||
import no.josefushighscore.model.User; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.Optional; | ||
|
||
@Repository | ||
public interface UserRegister extends JpaRepository<User,Long> { | ||
@Query("SELECT NEW no.josefushighscore.dto.UserDto(u.username, u.firstname, u.lastname, u.email) " + | ||
"FROM User u WHERE u.username = :username") | ||
Optional<UserDto> getUserDetails(@Param("username") String username); | ||
Optional<User> findByUsername(String username); | ||
Optional<User> findByEmail(String email); | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/no/josefushighscore/service/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package no.josefushighscore.service; | ||
|
||
import no.josefushighscore.dto.UserDto; | ||
import no.josefushighscore.register.UserRegister; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Optional; | ||
|
||
@Service | ||
public class UserService { | ||
|
||
private final UserRegister userRepository; | ||
|
||
|
||
public UserService(UserRegister userRepository) { | ||
this.userRepository = userRepository; | ||
} | ||
|
||
public Optional<UserDto> getUserDetails(String username) { | ||
return this.userRepository.getUserDetails(username); | ||
} | ||
} |