diff --git a/commons/pac-api-commons/src/main/java/com/tmobile/pacman/api/commons/service/SecurityService.java b/commons/pac-api-commons/src/main/java/com/tmobile/pacman/api/commons/service/SecurityService.java index 8a294ae3ed..76f2c0c599 100644 --- a/commons/pac-api-commons/src/main/java/com/tmobile/pacman/api/commons/service/SecurityService.java +++ b/commons/pac-api-commons/src/main/java/com/tmobile/pacman/api/commons/service/SecurityService.java @@ -15,6 +15,7 @@ ******************************************************************************/ package com.tmobile.pacman.api.commons.service; +import com.tmobile.pacman.api.commons.Constants; import com.tmobile.pacman.api.commons.config.RoleMappingLoader; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.Authentication; @@ -31,6 +32,7 @@ @Service public class SecurityService { + private static final String ADMIN_PERMISSION = "rule-admin"; @Autowired private RoleMappingLoader roleMappingLoader; @@ -56,4 +58,32 @@ public boolean hasPermission(Authentication authentication, String... permission .anyMatch( allowedPermissions.stream().map(String::toLowerCase) .collect(Collectors.toSet())::contains); } + + public boolean hasPermissionForIssueExemption(Authentication authentication, Constants.ExemptionActions action) { + List allowedPermissions = getAllowedPermissions(authentication); + switch (action) { + /*for admin*/ + case APPROVE_EXEMPTION_REQUEST: + case CANCEL_EXEMPTION_REQUEST: + return allowedPermissions.contains(ADMIN_PERMISSION); + /*for user*/ + case CREATE_EXEMPTION_REQUEST: + case REVOKE_EXEMPTION_REQUEST: + default: + return !allowedPermissions.contains(ADMIN_PERMISSION); + } + } + + private List getAllowedPermissions(Authentication authentication) { + final Set userRoles = authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toSet()); + Map> rolePermissionMappings = roleMappingLoader.getRoleList(); + List allowedPermissions = new ArrayList<>(); + userRoles.forEach(role -> { + List permissionList = rolePermissionMappings.get(role); + if (permissionList != null) { + allowedPermissions.addAll(permissionList); + } + }); + return allowedPermissions; + } }