@Test public void hasPermission_UserIsLoggedIn_ShouldReturnTrue() { Authentication loggedInUser = createAuthenticationForLoggedInUser(SecurityRole.ROLE_USER.name()); boolean hasPermission = permissionEvaluator.hasPermission(loggedInUser, DOMAIN_OBJECT_TODO, PERMISSION_ADD); assertTrue(hasPermission); }
@Override public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) { LOGGER.debug("Evaluation permission: {} for domain object: {}", permission, targetDomainObject); LOGGER.debug("Received authentication: {}", authentication); boolean hasPermission = false; if (targetDomainObject.equals("Todo")) { Object principal = authentication.getPrincipal(); if (principal instanceof UserDetails) { LOGGER.debug("User is not anonymous. Evaluation permission"); UserDetails userDetails = (UserDetails) principal; String principalRole = getRole(userDetails.getAuthorities()); if (principalRole.equals(SecurityRole.ROLE_USER.name())) { LOGGER.debug("Principal: {} has permission to perform requested operation", userDetails); hasPermission = true; } } else { LOGGER.debug("User is anonymous. Permission denied."); } } else { LOGGER.debug("Unknown class: {} for target domain Object: {}", targetDomainObject.getClass(), targetDomainObject); } LOGGER.debug("Returning: {}", hasPermission); return hasPermission; }
TodoPermissionEvaluator implements PermissionEvaluator { @Override public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) { LOGGER.debug("Evaluation permission: {} for domain object: {}", permission, targetDomainObject); LOGGER.debug("Received authentication: {}", authentication); boolean hasPermission = false; if (targetDomainObject.equals("Todo")) { Object principal = authentication.getPrincipal(); if (principal instanceof UserDetails) { LOGGER.debug("User is not anonymous. Evaluation permission"); UserDetails userDetails = (UserDetails) principal; String principalRole = getRole(userDetails.getAuthorities()); if (principalRole.equals(SecurityRole.ROLE_USER.name())) { LOGGER.debug("Principal: {} has permission to perform requested operation", userDetails); hasPermission = true; } } else { LOGGER.debug("User is anonymous. Permission denied."); } } else { LOGGER.debug("Unknown class: {} for target domain Object: {}", targetDomainObject.getClass(), targetDomainObject); } LOGGER.debug("Returning: {}", hasPermission); return hasPermission; } }
TodoPermissionEvaluator implements PermissionEvaluator { @Override public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) { LOGGER.debug("Evaluation permission: {} for domain object: {}", permission, targetDomainObject); LOGGER.debug("Received authentication: {}", authentication); boolean hasPermission = false; if (targetDomainObject.equals("Todo")) { Object principal = authentication.getPrincipal(); if (principal instanceof UserDetails) { LOGGER.debug("User is not anonymous. Evaluation permission"); UserDetails userDetails = (UserDetails) principal; String principalRole = getRole(userDetails.getAuthorities()); if (principalRole.equals(SecurityRole.ROLE_USER.name())) { LOGGER.debug("Principal: {} has permission to perform requested operation", userDetails); hasPermission = true; } } else { LOGGER.debug("User is anonymous. Permission denied."); } } else { LOGGER.debug("Unknown class: {} for target domain Object: {}", targetDomainObject.getClass(), targetDomainObject); } LOGGER.debug("Returning: {}", hasPermission); return hasPermission; }  }
TodoPermissionEvaluator implements PermissionEvaluator { @Override public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) { LOGGER.debug("Evaluation permission: {} for domain object: {}", permission, targetDomainObject); LOGGER.debug("Received authentication: {}", authentication); boolean hasPermission = false; if (targetDomainObject.equals("Todo")) { Object principal = authentication.getPrincipal(); if (principal instanceof UserDetails) { LOGGER.debug("User is not anonymous. Evaluation permission"); UserDetails userDetails = (UserDetails) principal; String principalRole = getRole(userDetails.getAuthorities()); if (principalRole.equals(SecurityRole.ROLE_USER.name())) { LOGGER.debug("Principal: {} has permission to perform requested operation", userDetails); hasPermission = true; } } else { LOGGER.debug("User is anonymous. Permission denied."); } } else { LOGGER.debug("Unknown class: {} for target domain Object: {}", targetDomainObject.getClass(), targetDomainObject); } LOGGER.debug("Returning: {}", hasPermission); return hasPermission; }  @Override boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission); @Override boolean hasPermission(Authentication authentication, Serializable targetId, String targetType, Object permission); }
TodoPermissionEvaluator implements PermissionEvaluator { @Override public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) { LOGGER.debug("Evaluation permission: {} for domain object: {}", permission, targetDomainObject); LOGGER.debug("Received authentication: {}", authentication); boolean hasPermission = false; if (targetDomainObject.equals("Todo")) { Object principal = authentication.getPrincipal(); if (principal instanceof UserDetails) { LOGGER.debug("User is not anonymous. Evaluation permission"); UserDetails userDetails = (UserDetails) principal; String principalRole = getRole(userDetails.getAuthorities()); if (principalRole.equals(SecurityRole.ROLE_USER.name())) { LOGGER.debug("Principal: {} has permission to perform requested operation", userDetails); hasPermission = true; } } else { LOGGER.debug("User is anonymous. Permission denied."); } } else { LOGGER.debug("Unknown class: {} for target domain Object: {}", targetDomainObject.getClass(), targetDomainObject); } LOGGER.debug("Returning: {}", hasPermission); return hasPermission; }  @Override boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission); @Override boolean hasPermission(Authentication authentication, Serializable targetId, String targetType, Object permission);  }