|
1 | 1 | package com.example.solidconnection.security.aspect; |
2 | 2 |
|
3 | 3 | import static com.example.solidconnection.common.exception.ErrorCode.ACCESS_DENIED; |
| 4 | +import static com.example.solidconnection.common.exception.ErrorCode.USER_NOT_FOUND; |
4 | 5 |
|
5 | 6 | import com.example.solidconnection.common.exception.CustomException; |
| 7 | +import com.example.solidconnection.common.resolver.AuthorizedUser; |
6 | 8 | import com.example.solidconnection.security.annotation.RequireRoleAccess; |
7 | 9 | import com.example.solidconnection.siteuser.domain.Role; |
8 | 10 | import com.example.solidconnection.siteuser.domain.SiteUser; |
| 11 | +import com.example.solidconnection.siteuser.repository.SiteUserRepository; |
| 12 | +import java.lang.reflect.Parameter; |
9 | 13 | import java.util.Arrays; |
10 | 14 | import lombok.RequiredArgsConstructor; |
11 | 15 | import org.aspectj.lang.ProceedingJoinPoint; |
12 | 16 | import org.aspectj.lang.annotation.Around; |
13 | 17 | import org.aspectj.lang.annotation.Aspect; |
| 18 | +import org.aspectj.lang.reflect.MethodSignature; |
14 | 19 | import org.springframework.stereotype.Component; |
15 | 20 |
|
16 | 21 | @Aspect |
17 | 22 | @Component |
18 | 23 | @RequiredArgsConstructor |
19 | 24 | public class RoleAuthorizationAspect { |
20 | 25 |
|
21 | | - // todo: 추후 siteUserId로 파라미터 변경 시 수정 필요 |
| 26 | + private final SiteUserRepository siteUserRepository; |
| 27 | + |
| 28 | + // todo: 추후 개선 필요 |
22 | 29 | @Around("@annotation(requireRoleAccess)") |
23 | 30 | public Object checkRoleAccess(ProceedingJoinPoint joinPoint, RequireRoleAccess requireRoleAccess) throws Throwable { |
24 | | - SiteUser siteUser = null; |
25 | | - for (Object arg : joinPoint.getArgs()) { |
26 | | - if (arg instanceof SiteUser) { |
27 | | - siteUser = (SiteUser) arg; |
28 | | - break; |
29 | | - } |
30 | | - } |
31 | | - if (siteUser == null) { |
| 31 | + |
| 32 | + Long siteUserId = extractAuthorizedUserId(joinPoint); |
| 33 | + |
| 34 | + if (siteUserId == null) { |
32 | 35 | throw new CustomException(ACCESS_DENIED); |
33 | 36 | } |
34 | | - Role[] allowedRoles = requireRoleAccess.roles(); |
| 37 | + |
| 38 | + SiteUser siteUser = siteUserRepository.findById(siteUserId) |
| 39 | + .orElseThrow(() -> new CustomException(USER_NOT_FOUND)); |
| 40 | + |
| 41 | + validateUserRole(siteUser, requireRoleAccess.roles()); |
| 42 | + |
| 43 | + return joinPoint.proceed(); |
| 44 | + } |
| 45 | + |
| 46 | + private Long extractAuthorizedUserId(ProceedingJoinPoint joinPoint) { |
| 47 | + MethodSignature signature = (MethodSignature) joinPoint.getSignature(); |
| 48 | + Parameter[] parameters = signature.getMethod().getParameters(); |
| 49 | + Object[] args = joinPoint.getArgs(); |
| 50 | + |
| 51 | + for (int i = 0; i < parameters.length; i++) { |
| 52 | + if (parameters[i].isAnnotationPresent(AuthorizedUser.class)) { |
| 53 | + Object arg = args[i]; |
| 54 | + if (arg instanceof Long) { |
| 55 | + return (Long) arg; |
| 56 | + } else if (parameters[i].getType() == long.class) { |
| 57 | + return (Long) arg; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + return null; |
| 62 | + } |
| 63 | + |
| 64 | + private void validateUserRole(SiteUser siteUser, Role[] allowedRoles) { |
35 | 65 | boolean hasAccess = Arrays.asList(allowedRoles).contains(siteUser.getRole()); |
| 66 | + |
36 | 67 | if (!hasAccess) { |
37 | 68 | throw new CustomException(ACCESS_DENIED); |
38 | 69 | } |
39 | | - return joinPoint.proceed(); |
40 | 70 | } |
41 | 71 | } |
0 commit comments