Compare commits

...

3 Commits

Author SHA1 Message Date
48a92a4690 nouvelle version
Reviewed-on: #1
2025-06-18 09:58:15 +02:00
260b06653d fix de bugs 2025-06-18 09:56:58 +02:00
4a06b45020 Fix: problème d'authent 2025-05-07 20:12:02 +02:00
5 changed files with 45 additions and 32 deletions

View File

@ -1,9 +1,6 @@
package com.guams.review.configuration; package com.guams.review.configuration;
import com.guams.review.exception.AlreadyExistsException; import com.guams.review.exception.*;
import com.guams.review.exception.InvalidNameOrPasswordException;
import com.guams.review.exception.NotFoundException;
import com.guams.review.exception.UnauthorizedExecption;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ControllerAdvice;
@ -37,4 +34,10 @@ public class Advice {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED) return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
.body(Map.of("message", exception.getMessage())); .body(Map.of("message", exception.getMessage()));
} }
@ExceptionHandler(value = StringTooLongException.class)
public ResponseEntity<Map<String, String>> handleStringTooLong(StringTooLongException exception) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(Map.of("message", exception.getMessage()));
}
} }

View File

@ -36,6 +36,7 @@ public class SpringSecurityConfig {
.requestMatchers(HttpMethod.GET, .requestMatchers(HttpMethod.GET,
"/api/authors", "/api/authors",
"/api/authors/{id}", "/api/authors/{id}",
"/api/authors/{id}/avatar",
"/api/authors/{id}/posts", "/api/authors/{id}/posts",
"/api/posts", "/api/posts",
"/api/posts/{id}", "/api/posts/{id}",

View File

@ -2,9 +2,9 @@ package com.guams.review.controller;
import com.guams.review.configuration.JwtTokenUtil; import com.guams.review.configuration.JwtTokenUtil;
import com.guams.review.exception.AlreadyExistsException; import com.guams.review.exception.AlreadyExistsException;
import com.guams.review.exception.UnauthorizedExecption;
import com.guams.review.exception.InvalidNameOrPasswordException; import com.guams.review.exception.InvalidNameOrPasswordException;
import com.guams.review.exception.NotFoundException; import com.guams.review.exception.NotFoundException;
import com.guams.review.exception.UnauthorizedExecption;
import com.guams.review.model.AuthorRepository; import com.guams.review.model.AuthorRepository;
import com.guams.review.model.Role; import com.guams.review.model.Role;
import com.guams.review.model.dao.Author; import com.guams.review.model.dao.Author;
@ -26,6 +26,7 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.IOException; import java.io.IOException;
import java.util.Base64;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@ -58,25 +59,38 @@ public class AuthorController {
return authorService.insert(updatedAuthor return authorService.insert(updatedAuthor
.setId(authorToUpdate.getId()) .setId(authorToUpdate.getId())
.setRole(authorToUpdate.getRole()) .setRole(authorToUpdate.getRole())
.setProfilePicture(authorToUpdate.getProfilePicture())
.setPassword(passwordEncoder.encode(updatedAuthor.getPassword()))); .setPassword(passwordEncoder.encode(updatedAuthor.getPassword())));
} }
@PutMapping(value = "{id}/avatar", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE}) @PutMapping(value = "{id}/avatar", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
public Author updateUserAvatar(@PathVariable UUID id, @RequestPart MultipartFile avatar, Authentication authentication) throws IOException { public Author updateUserAvatar(@PathVariable UUID id, @RequestPart MultipartFile avatar, Authentication authentication) throws IOException {
Author authorToUpdate = authorService.verifyIfUserIsAuthorized(authentication, id); if (avatar != null) {
ReturnableAuthor authorToReturn = authorService.insert(authorToUpdate.setProfilePicture(avatar.getBytes())); Author authorToUpdate = authorService.verifyIfUserIsAuthorized(authentication, id);
return new Author() ReturnableAuthor authorToReturn = authorService.insert(authorToUpdate.setProfilePicture(avatar.getBytes()));
.setId(authorToReturn.getId()) return new Author()
.setName(authorToReturn.getName()) .setId(authorToReturn.getId())
.setProfilePicture(null) .setName(authorToReturn.getName())
.setRole(authorToReturn.getRole()) .setProfilePicture(null)
.setPassword(""); .setRole(authorToReturn.getRole())
.setPassword("");
} else {
return authorService.verifyIfUserIsAuthorized(authentication, id)
.setProfilePicture(null)
.setPassword("");
}
} }
@GetMapping("/{id}/avatar") @GetMapping("/{id}/avatar")
public byte[] getProfilePicture(@PathVariable UUID id) { public byte[] getProfilePicture(@PathVariable UUID id) {
Author author = authorService.findById(id).orElseThrow(() -> new NotFoundException("Author not found")); Author author = authorService.findById(id).orElseThrow(() -> new NotFoundException("Author not found"));
return author.getProfilePicture(); if (author.getProfilePicture() != null) {
return Base64.getEncoder().encode(author.getProfilePicture());
} else {
return null;
}
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")

View File

@ -1,4 +1,6 @@
package com.guams.review.controller; package com.guams.review.controller;
import com.guams.review.exception.StringTooLongException;
import com.guams.review.exception.UnauthorizedExecption; import com.guams.review.exception.UnauthorizedExecption;
import com.guams.review.exception.NotFoundException; import com.guams.review.exception.NotFoundException;
import com.guams.review.model.AuthorRepository; import com.guams.review.model.AuthorRepository;
@ -45,6 +47,10 @@ public class CommentController {
throw new UnauthorizedExecption("Vous n'êtes pas autorisé à faire ça"); throw new UnauthorizedExecption("Vous n'êtes pas autorisé à faire ça");
} }
if (comment.getContent().length() >= 512) {
throw new StringTooLongException("Votre commentaire est trop long...");
}
Author author = authorRepository.findByName(authentication.getName()).orElseThrow(() -> new NotFoundException("Cet auteur n'existe pas")); Author author = authorRepository.findByName(authentication.getName()).orElseThrow(() -> new NotFoundException("Cet auteur n'existe pas"));
Comment insertedComment = commentService.insert(comment Comment insertedComment = commentService.insert(comment
.setCommentDate(Timestamp.from(Instant.now())) .setCommentDate(Timestamp.from(Instant.now()))
@ -61,24 +67,6 @@ public class CommentController {
return commentService.getCommentsByPostId(postId); return commentService.getCommentsByPostId(postId);
} }
@PutMapping("/{id}")
public void updateComment(@PathVariable Long id, @RequestBody CommentIds commentIds, Authentication authentication) {
if (authentication == null || !authentication.isAuthenticated()) {
throw new UnauthorizedExecption("Vous n'êtes pas autorisé à faire ceci");
}
Author author = authorService.findByName(authentication.getName()).orElseThrow(() -> new NotFoundException("Cet auteur n'existe pas"));
Comment commentToUpdate = commentService.findById(id).orElseThrow(() -> new NotFoundException("Ce commentaire n'existe pas"));
CommentIds concernedCommentIds = commentService.getCommentIdsByCommentId(id).orElseThrow(() -> new NotFoundException("Ce commentaire n'existe pas"));
if (!author.getId().equals(concernedCommentIds.getAuthorId())) {
throw new UnauthorizedExecption("Vous n'êtes pas autorisé à faire ceci");
}
commentService.insert(commentToUpdate
.setIsUpdated(true)
.setContent(commentIds.getContent()));
}
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public void deleteComment(@PathVariable Long id, Authentication authentication) { public void deleteComment(@PathVariable Long id, Authentication authentication) {
if (authentication == null || !authentication.isAuthenticated()) { if (authentication == null || !authentication.isAuthenticated()) {

View File

@ -0,0 +1,7 @@
package com.guams.review.exception;
public class StringTooLongException extends RuntimeException {
public StringTooLongException(String message) {
super(message);
}
}