Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added custom annoation to confirm the user's ownership of the shortened link. #70

Merged
merged 3 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
@Constraint(validatedBy = UrlShortValidatorImpl.class)
public @interface UrlShortValidator {

Expand Down
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 {};
}
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();
}
}
Loading