-
Notifications
You must be signed in to change notification settings - Fork 42
Role based access #485
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
Open
goelsonali
wants to merge
7
commits into
Women-Coding-Community:main
Choose a base branch
from
goelsonali:role_based_access
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,596
−61
Open
Role based access #485
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
52d1b56
add permission and role mapping
goelsonali 5869c1c
create new annotation
goelsonali 219a423
aop implementation
goelsonali e7d1ec6
review changes
goelsonali cef4458
removed unwanted methods
goelsonali 3d21598
unit tests
goelsonali b216515
fix failed unit tests
goelsonali File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
55 changes: 55 additions & 0 deletions
55
src/main/java/com/wcc/platform/configuration/security/AuthorizationAspect.java
This file contains hidden or 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 com.wcc.platform.configuration.security; | ||
|
|
||
| import com.wcc.platform.domain.auth.Permission; | ||
| import com.wcc.platform.domain.platform.type.RoleType; | ||
| import com.wcc.platform.service.AuthService; | ||
| import lombok.AllArgsConstructor; | ||
| import org.aspectj.lang.ProceedingJoinPoint; | ||
| import org.aspectj.lang.annotation.Around; | ||
| import org.aspectj.lang.annotation.Aspect; | ||
| import org.springframework.core.annotation.Order; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Aspect | ||
| @Component | ||
| @Order(1) | ||
| @AllArgsConstructor | ||
| public class AuthorizationAspect { | ||
goelsonali marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private final AuthService authService; | ||
|
|
||
| @Around("@annotation(requiresPermission)") | ||
| public Object checkPermission( | ||
| ProceedingJoinPoint joinPoint, RequiresPermission requiresPermission) throws Throwable { | ||
|
|
||
| Permission[] permissions = requiresPermission.value(); | ||
|
|
||
| if (permissions.length == 0) { | ||
| throw new IllegalArgumentException( | ||
| "@RequiresPermission must specify at least one permission"); | ||
| } | ||
|
|
||
| if (requiresPermission.operator() == LogicalOperator.AND) { | ||
| authService.requireAllPermissions(permissions); | ||
| } else { | ||
| authService.requireAnyPermission(permissions); | ||
| } | ||
|
|
||
| return joinPoint.proceed(); | ||
| } | ||
|
|
||
| @Around("@annotation(requiresRole)") | ||
| public Object checkRole(ProceedingJoinPoint joinPoint, RequiresRole requiresRole) | ||
| throws Throwable { | ||
|
|
||
| RoleType[] roles = requiresRole.value(); | ||
|
|
||
| if (roles.length == 0) { | ||
| throw new IllegalArgumentException("@RequiresRole must specify at least one role"); | ||
| } | ||
|
|
||
| authService.requireRole(roles); | ||
|
|
||
| return joinPoint.proceed(); | ||
| } | ||
| } | ||
6 changes: 6 additions & 0 deletions
6
src/main/java/com/wcc/platform/configuration/security/LogicalOperator.java
This file contains hidden or 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,6 @@ | ||
| package com.wcc.platform.configuration.security; | ||
|
|
||
| public enum LogicalOperator { | ||
| AND, | ||
| OR | ||
| } |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/wcc/platform/configuration/security/RequiresPermission.java
This file contains hidden or 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,15 @@ | ||
| package com.wcc.platform.configuration.security; | ||
|
|
||
| import com.wcc.platform.domain.auth.Permission; | ||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Target({ElementType.METHOD, ElementType.TYPE}) | ||
| public @interface RequiresPermission { | ||
| Permission[] value(); | ||
|
|
||
| LogicalOperator operator() default LogicalOperator.AND; | ||
| } |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/wcc/platform/configuration/security/RequiresRole.java
This file contains hidden or 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,15 @@ | ||
| package com.wcc.platform.configuration.security; | ||
|
|
||
| import com.wcc.platform.domain.platform.type.RoleType; | ||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Target({ElementType.METHOD, ElementType.TYPE}) | ||
| public @interface RequiresRole { | ||
| RoleType[] value(); | ||
|
|
||
| LogicalOperator operator() default LogicalOperator.AND; | ||
| } |
This file contains hidden or 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
127 changes: 127 additions & 0 deletions
127
src/main/java/com/wcc/platform/domain/auth/MemberTypeRoleMapper.java
This file contains hidden or 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,127 @@ | ||
| package com.wcc.platform.domain.auth; | ||
|
|
||
| import com.wcc.platform.domain.platform.type.MemberType; | ||
| import com.wcc.platform.domain.platform.type.RoleType; | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class MemberTypeRoleMapper { | ||
|
|
||
| private static final Map<MemberType, RoleType> MEMBER_TYPE_TO_ROLE_MAP = | ||
| Map.of( | ||
| MemberType.DIRECTOR, RoleType.SUPER_ADMIN, | ||
| MemberType.LEADER, RoleType.ADMIN, | ||
| MemberType.MENTOR, RoleType.MENTOR, | ||
| MemberType.MENTEE, RoleType.MENTEE, | ||
| MemberType.COLLABORATOR, RoleType.CONTRIBUTOR, | ||
| MemberType.EVANGELIST, RoleType.CONTRIBUTOR, | ||
| MemberType.SPEAKER, RoleType.CONTRIBUTOR, | ||
| MemberType.VOLUNTEER, RoleType.VIEWER, | ||
| MemberType.MEMBER, RoleType.VIEWER, | ||
| MemberType.PARTNER, RoleType.VIEWER); | ||
|
|
||
| // Role hierarchy: higher number = more privileged | ||
| private static final Map<RoleType, Integer> ROLE_HIERARCHY = | ||
| Map.of( | ||
| RoleType.SUPER_ADMIN, 100, | ||
| RoleType.ADMIN, 80, | ||
| RoleType.MENTOR, 60, | ||
| RoleType.CONTRIBUTOR, 50, | ||
| RoleType.MENTEE, 40, | ||
| RoleType.VIEWER, 20); | ||
|
|
||
| private MemberTypeRoleMapper() { | ||
| // Utility class | ||
| } | ||
|
|
||
| /** Get role for a single MemberType. */ | ||
| public static RoleType getRoleForMemberType(MemberType memberType) { | ||
| if (memberType == null) { | ||
| throw new IllegalArgumentException("MemberType cannot be null"); | ||
| } | ||
| return MEMBER_TYPE_TO_ROLE_MAP.getOrDefault(memberType, RoleType.VIEWER); | ||
| } | ||
|
|
||
| /** | ||
| * Get all roles for a list of MemberTypes. | ||
| * | ||
| * @param memberTypes list of member types | ||
| * @return set of all roles corresponding to the member types | ||
| */ | ||
| public static Set<RoleType> getRolesForMemberTypes(List<MemberType> memberTypes) { | ||
| if (memberTypes == null || memberTypes.isEmpty()) { | ||
| return Set.of(RoleType.VIEWER); | ||
| } | ||
|
|
||
| return memberTypes.stream() | ||
| .map(MemberTypeRoleMapper::getRoleForMemberType) | ||
| .collect(Collectors.toSet()); | ||
| } | ||
|
|
||
| /** | ||
| * Get the highest privilege role from a list of MemberTypes. This determines the "primary" role | ||
| * when a member has multiple types. | ||
| * | ||
| * @param memberTypes list of member types | ||
| * @return the role with highest privilege level | ||
| */ | ||
| public static RoleType getHighestRole(List<MemberType> memberTypes) { | ||
| if (memberTypes == null || memberTypes.isEmpty()) { | ||
| return RoleType.VIEWER; | ||
| } | ||
|
|
||
| return memberTypes.stream() | ||
| .map(MemberTypeRoleMapper::getRoleForMemberType) | ||
| .max(Comparator.comparing(role -> ROLE_HIERARCHY.getOrDefault(role, 0))) | ||
| .orElse(RoleType.VIEWER); | ||
| } | ||
|
|
||
| /** | ||
| * Get all permissions from multiple MemberTypes (union of all permissions). | ||
| * | ||
| * @param memberTypes list of member types | ||
| * @return set of all unique permissions | ||
| */ | ||
| public static Set<Permission> getAllPermissionsForMemberTypes(List<MemberType> memberTypes) { | ||
| if (memberTypes == null || memberTypes.isEmpty()) { | ||
| return RoleType.VIEWER.getPermissions(); | ||
| } | ||
|
|
||
| return memberTypes.stream() | ||
| .map(MemberTypeRoleMapper::getRoleForMemberType) | ||
| .flatMap(role -> role.getPermissions().stream()) | ||
| .collect(Collectors.toSet()); | ||
| } | ||
|
|
||
| /** Check if any of the member types maps to SUPER_ADMIN. */ | ||
| public static boolean isSuperAdmin(List<MemberType> memberTypes) { | ||
| if (memberTypes == null) { | ||
| return false; | ||
| } | ||
| return memberTypes.stream() | ||
| .anyMatch(type -> getRoleForMemberType(type) == RoleType.SUPER_ADMIN); | ||
| } | ||
|
|
||
| /** Check if any of the member types maps to ADMIN or SUPER_ADMIN. */ | ||
| public static boolean isAdmin(List<MemberType> memberTypes) { | ||
| if (memberTypes == null) { | ||
| return false; | ||
| } | ||
| return memberTypes.stream() | ||
| .map(MemberTypeRoleMapper::getRoleForMemberType) | ||
| .anyMatch(role -> role == RoleType.SUPER_ADMIN || role == RoleType.ADMIN); | ||
| } | ||
|
|
||
| /** Check if member has a specific role through any of their member types. */ | ||
| public static boolean hasRole(List<MemberType> memberTypes, RoleType targetRole) { | ||
| if (memberTypes == null || targetRole == null) { | ||
| return false; | ||
| } | ||
| return memberTypes.stream() | ||
| .map(MemberTypeRoleMapper::getRoleForMemberType) | ||
| .anyMatch(role -> role == targetRole); | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/wcc/platform/domain/auth/Permission.java
This file contains hidden or 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,29 @@ | ||
| package com.wcc.platform.domain.auth; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
|
|
||
| @AllArgsConstructor | ||
| public enum Permission { | ||
| // User Management | ||
| USER_READ("user:read", "View user information"), | ||
| USER_WRITE("user:write", "Create and update users"), | ||
| USER_DELETE("user:delete", "Delete users"), | ||
|
|
||
| // Mentor Operations | ||
| MENTOR_APPLICATION_READ("mentor:application:read", "View mentor applications"), | ||
| MENTOR_APPLICATION_WRITE("mentor:application:write", "Accept/decline mentee applications"), | ||
| MENTOR_PROFILE_UPDATE("mentor:profile:update", "Update mentor profile"), | ||
|
|
||
| // Mentee Operations | ||
| MENTEE_APPLICATION_SUBMIT("mentee:application:submit", "Submit mentee applications"), | ||
| MENTEE_APPLICATION_READ("mentee:application:read", "View own application status"), | ||
|
|
||
| // Admin Operations | ||
| MENTOR_APPROVE("admin:mentor:approve", "Approve/reject mentors"), | ||
| MENTEE_APPROVE("admin:mentee:approve", "Approve/reject mentees"), | ||
| CYCLE_EMAIL_SEND("admin:cycle:email", "Send cycle emails"), | ||
| MATCH_MANAGE("admin:match:manage", "Manage mentor-mentee matches"); | ||
|
|
||
| private final String code; | ||
| private final String description; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.