-
Notifications
You must be signed in to change notification settings - Fork 2
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 #70 from nastiausenko/custom_annotation_feature
Added custom annoation to confirm the user's ownership of the shortened link.
- Loading branch information
Showing
3 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
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
41 changes: 41 additions & 0 deletions
41
src/main/java/com/linkurlshorter/urlshortener/user/ShortenedLinkOwnerValidation.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,41 @@ | ||
package com.linkurlshorter.urlshortener.user; | ||
|
||
import jakarta.validation.Constraint; | ||
import jakarta.validation.Payload; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/** | ||
* The annotation used to validate user ownership of the shortened link. | ||
* The validation is performed using {@link ShortenedLinkOwnerValidationImpl}. | ||
* | ||
* @author Vlas Potoskyi | ||
*/ | ||
@Documented | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE}) | ||
@Constraint(validatedBy = ShortenedLinkOwnerValidationImpl.class) | ||
public @interface ShortenedLinkOwnerValidation { | ||
/** | ||
* The message to be used to inform about the failed validation. | ||
* Default: "". | ||
* | ||
* @return Error message. | ||
*/ | ||
String message() default ""; | ||
|
||
/** | ||
* Groups to which this constraint belongs. Default: empty array. | ||
* | ||
* @return Constraint groups. | ||
*/ | ||
Class<?>[] groups() default {}; | ||
|
||
/** | ||
* Parameters that can be used to configure the constraint. | ||
* Default: an empty array. | ||
* | ||
* @return The parameters of the constraint. | ||
*/ | ||
Class<? extends Payload>[] payload() default {}; | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/com/linkurlshorter/urlshortener/user/ShortenedLinkOwnerValidationImpl.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,54 @@ | ||
package com.linkurlshorter.urlshortener.user; | ||
|
||
import com.linkurlshorter.urlshortener.link.LinkService; | ||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
|
||
import java.util.Objects; | ||
import java.util.UUID; | ||
|
||
/** | ||
* Implementation of user ownership verification for a shortened link. | ||
* The check is performed using the link service {@link LinkService} and the user service {@link UserService}. | ||
*/ | ||
@RequiredArgsConstructor | ||
public class ShortenedLinkOwnerValidationImpl implements ConstraintValidator<ShortenedLinkOwnerValidation, String> { | ||
private final LinkService linkService; | ||
private final UserService userService; | ||
|
||
/** | ||
* Checks user ownership of the shortened link. | ||
* | ||
* @param shortLink The shortened link to check ownership for. | ||
* @param context The context of the constraint check. | ||
* @return true if the current user owns the link; false otherwise. | ||
*/ | ||
@Override | ||
public boolean isValid(String shortLink, ConstraintValidatorContext context) { | ||
if (shortLink == null || shortLink.isEmpty()) { | ||
context.buildConstraintViolationWithTemplate("Invalid short link!"); | ||
return false; | ||
} | ||
UUID currentUserId = getCurrentUserId(); | ||
UUID linkUserId = linkService.findByShortLink(shortLink).getId(); | ||
if (!Objects.equals(currentUserId, linkUserId)) { | ||
context.buildConstraintViolationWithTemplate("You cannot do this!"); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Gets the ID of the current user. | ||
* | ||
* @return The ID of the current user. | ||
*/ | ||
private UUID getCurrentUserId() { | ||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
String userEmail = authentication.getName(); | ||
return userService.findByEmail(userEmail).getId(); | ||
} | ||
} |