Archived
Add spring security configuration.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package org.codiki.posts;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.Principal;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
@@ -14,8 +15,8 @@ import org.codiki.core.entities.dto.PostDTO;
|
||||
import org.codiki.core.entities.persistence.Post;
|
||||
import org.codiki.core.entities.persistence.User;
|
||||
import org.codiki.core.repositories.PostRepository;
|
||||
import org.codiki.core.security.TokenService;
|
||||
import org.codiki.core.services.ParserService;
|
||||
import org.codiki.core.services.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
@@ -39,10 +40,10 @@ public class PostController {
|
||||
private PostRepository postRepository;
|
||||
|
||||
@Autowired
|
||||
private TokenService tokenService;
|
||||
private PostService postService;
|
||||
|
||||
@Autowired
|
||||
private PostService postService;
|
||||
private UserService userService;
|
||||
|
||||
@GetMapping
|
||||
public List<PostDTO> getAll() {
|
||||
@@ -93,10 +94,11 @@ public class PostController {
|
||||
}
|
||||
|
||||
@GetMapping("/myPosts")
|
||||
public List<PostDTO> getMyPosts(final HttpServletRequest pRequest, final HttpServletResponse pResponse) throws IOException {
|
||||
public List<PostDTO> getMyPosts(final HttpServletRequest pRequest, final HttpServletResponse pResponse,
|
||||
final Principal pPrincipal) throws IOException {
|
||||
List<PostDTO> result = new LinkedList<>();
|
||||
|
||||
final Optional<User> connectedUser = tokenService.getAuthenticatedUserByToken(pRequest);
|
||||
final Optional<User> connectedUser = userService.getUserByPrincipal(pPrincipal);
|
||||
if(connectedUser.isPresent()) {
|
||||
result = postRepository.getByCreator(connectedUser.get().getId())
|
||||
.stream().map(PostDTO::new).collect(Collectors.toList());
|
||||
@@ -123,10 +125,10 @@ public class PostController {
|
||||
|
||||
@PostMapping("/")
|
||||
public PostDTO insert(@RequestBody final PostDTO pPost, final HttpServletRequest pRequest,
|
||||
final HttpServletResponse pResponse) {
|
||||
final HttpServletResponse pResponse, final Principal pPrincipal) {
|
||||
PostDTO result = null;
|
||||
|
||||
Optional<Post> postCreated = postService.insert(pPost, pRequest, pResponse);
|
||||
Optional<Post> postCreated = postService.insert(pPost, pRequest, pResponse, pPrincipal);
|
||||
|
||||
if(postCreated.isPresent()) {
|
||||
result = new PostDTO(postCreated.get());
|
||||
@@ -137,13 +139,13 @@ public class PostController {
|
||||
|
||||
@PutMapping("/")
|
||||
public void update(@RequestBody final PostDTO pPost, final HttpServletRequest pRequest,
|
||||
final HttpServletResponse pResponse) throws IOException {
|
||||
postService.update(pPost, pRequest, pResponse);
|
||||
final HttpServletResponse pResponse, final Principal pPrincipal) throws IOException {
|
||||
postService.update(pPost, pRequest, pResponse, pPrincipal);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{postKey}")
|
||||
public void delete(@PathVariable("postKey") final String pPostKey,
|
||||
final HttpServletRequest pRequest, final HttpServletResponse pResponse) throws IOException {
|
||||
postService.delete(pPostKey, pRequest, pResponse);
|
||||
public void delete(@PathVariable("postKey") final String pPostKey, final HttpServletRequest pRequest,
|
||||
final HttpServletResponse pResponse, final Principal pPrincipal) throws IOException {
|
||||
postService.delete(pPostKey, pRequest, pResponse, pPrincipal);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user