Skip to content

Commit

Permalink
Merge pull request #17 from tofiksa/16-expand-userdetails-api
Browse files Browse the repository at this point in the history
[ADD] - Lagt til uthenting av brukerobjekt
  • Loading branch information
tofiksa authored Jul 6, 2024
2 parents bffe28a + 4ab1475 commit eb71b71
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
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()));
}
}
7 changes: 7 additions & 0 deletions src/main/java/no/josefushighscore/dto/UserDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ public UserDto(String username, String password, String firstname, String lastna
this.email = email;
}

public UserDto(String username, String firstname, String lastname, String email) {
this.username = username;
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
}

public UserDto() {

}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/no/josefushighscore/register/UserRegister.java
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 src/main/java/no/josefushighscore/service/UserService.java
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);
}
}

0 comments on commit eb71b71

Please sign in to comment.