-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9c6e893
commit b795155
Showing
4 changed files
with
85 additions
and
3 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
src/main/java/ch/fhnw/deardevbackend/annotations/ValidateUserId.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,11 @@ | ||
package ch.fhnw.deardevbackend.annotations; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface ValidateUserId { | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/ch/fhnw/deardevbackend/annotations/ValidateUserIdParam.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,11 @@ | ||
package ch.fhnw.deardevbackend.annotations; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.PARAMETER) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface ValidateUserIdParam { | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/ch/fhnw/deardevbackend/aspect/UserValidationAspect.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,55 @@ | ||
package ch.fhnw.deardevbackend.aspect; | ||
|
||
import ch.fhnw.deardevbackend.controller.exceptions.YappiException; | ||
import ch.fhnw.deardevbackend.util.SecurityUtil; | ||
import org.aspectj.lang.JoinPoint; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.annotation.Before; | ||
import org.aspectj.lang.reflect.MethodSignature; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Method; | ||
|
||
@Aspect | ||
@Component | ||
public class UserValidationAspect { | ||
|
||
@Before("@annotation(ch.fhnw.deardevbackend.annotations.ValidateUserId) && args(dto,..)") | ||
public void validateUserId(Object dto) { | ||
Integer currentUserId = SecurityUtil.getCurrentUserId(); | ||
Integer userId; | ||
|
||
try { | ||
userId = (Integer) dto.getClass().getMethod("getUserId").invoke(dto); | ||
} catch (Exception e) { | ||
throw new YappiException("Unable to validate user ID"); | ||
} | ||
|
||
assert currentUserId != null; | ||
if (!currentUserId.equals(userId)) { | ||
throw new YappiException("User ID mismatch"); | ||
} | ||
} | ||
|
||
@Before("execution(* *(.., @ch.fhnw.deardevbackend.annotations.ValidateUserIdParam (*), ..))") | ||
public void validateUserIdParam(JoinPoint joinPoint) { | ||
Integer currentUserId = SecurityUtil.getCurrentUserId(); | ||
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); | ||
Method method = methodSignature.getMethod(); | ||
Object[] args = joinPoint.getArgs(); | ||
Annotation[][] parameterAnnotations = method.getParameterAnnotations(); | ||
|
||
for (int i = 0; i < parameterAnnotations.length; i++) { | ||
for (Annotation annotation : parameterAnnotations[i]) { | ||
if (annotation instanceof ch.fhnw.deardevbackend.annotations.ValidateUserIdParam) { | ||
Integer userId = (Integer) args[i]; | ||
assert currentUserId != null; | ||
if (!currentUserId.equals(userId)) { | ||
throw new YappiException("User ID mismatch"); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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