From e2fd9ace952b51ba98e3e8f75b6e6608406a0051 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=9A=A9=EC=88=98?= Date: Mon, 23 Sep 2024 22:24:18 +0900 Subject: [PATCH] =?UTF-8?q?Refactor:=20Aspect=20=EC=B5=9C=EC=A0=81?= =?UTF-8?q?=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../global/aspect/PrivilegeCheckAspect.java | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/backend/src/main/java/com/worlabel/global/aspect/PrivilegeCheckAspect.java b/backend/src/main/java/com/worlabel/global/aspect/PrivilegeCheckAspect.java index 1f54167..2b68599 100644 --- a/backend/src/main/java/com/worlabel/global/aspect/PrivilegeCheckAspect.java +++ b/backend/src/main/java/com/worlabel/global/aspect/PrivilegeCheckAspect.java @@ -30,27 +30,34 @@ public class PrivilegeCheckAspect { // CheckPrivilege 어노테이션이 붙은 메서드가 실행되기전 실행 @Before("@annotation(checkPrivilege)") public void checkPrivilege(JoinPoint joinPoint, CheckPrivilege checkPrivilege) { + int memberId = getMemberId(); + int projectId = getProjectId(joinPoint); + + checkPrivilegeUnauthorized(memberId, projectId, checkPrivilege.value()); + } + + private int getProjectId(JoinPoint joinPoint) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); Object[] args = joinPoint.getArgs(); Parameter[] parameters = method.getParameters(); - Object principal = SecurityContextHolder.getContext() - .getAuthentication() - .getPrincipal(); - int memberId = ((AuthMemberDto) principal).getId(); - - Integer projectId = null; for (int paramIdx = 0; paramIdx < parameters.length; paramIdx++) { String paramName = parameters[paramIdx].getName(); if (paramName.equals("projectId")) { - projectId = (Integer) args[paramIdx]; - break; + return (Integer) args[paramIdx]; } } - checkPrivilegeUnauthorized(memberId, projectId, checkPrivilege.value()); + throw new CustomException(ErrorCode.SERVER_ERROR); + } + + private int getMemberId() { + Object principal = SecurityContextHolder.getContext() + .getAuthentication() + .getPrincipal(); + return ((AuthMemberDto) principal).getId(); } public void checkPrivilegeUnauthorized(final Integer memberId, final Integer projectId, final PrivilegeType privilegeType) {