Skip to content

Commit

Permalink
Merge pull request #70 from nastiausenko/custom_annotation_feature
Browse files Browse the repository at this point in the history
Added custom annoation to confirm the user's ownership of the shortened link.
  • Loading branch information
IvanShalaev1990 authored Apr 21, 2024
2 parents cb93901 + 56ea377 commit a4fb840
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
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();
}
}

0 comments on commit a4fb840

Please sign in to comment.