@Test public void testIsAuthorizedForSpecificView() { RoleAuthorizationEntity readOnlyRoleAuthorizationEntity = new RoleAuthorizationEntity(); readOnlyRoleAuthorizationEntity.setAuthorizationId(RoleAuthorization.CLUSTER_VIEW_METRICS.getId()); RoleAuthorizationEntity viewUseRoleAuthorizationEntity = new RoleAuthorizationEntity(); viewUseRoleAuthorizationEntity.setAuthorizationId(RoleAuthorization.VIEW_USE.getId()); RoleAuthorizationEntity administratorRoleAuthorizationEntity = new RoleAuthorizationEntity(); administratorRoleAuthorizationEntity.setAuthorizationId(RoleAuthorization.AMBARI_MANAGE_USERS.getId()); ResourceTypeEntity ambariResourceTypeEntity = new ResourceTypeEntity(); ambariResourceTypeEntity.setId(1); ambariResourceTypeEntity.setName(ResourceType.AMBARI.name()); ResourceTypeEntity clusterResourceTypeEntity = new ResourceTypeEntity(); clusterResourceTypeEntity.setId(1); clusterResourceTypeEntity.setName(ResourceType.CLUSTER.name()); ResourceTypeEntity viewResourceTypeEntity = new ResourceTypeEntity(); viewResourceTypeEntity.setId(30); viewResourceTypeEntity.setName(ResourceType.VIEW.name()); ResourceEntity ambariResourceEntity = new ResourceEntity(); ambariResourceEntity.setResourceType(ambariResourceTypeEntity); ambariResourceEntity.setId(1L); ResourceEntity clusterResourceEntity = new ResourceEntity(); clusterResourceEntity.setResourceType(clusterResourceTypeEntity); clusterResourceEntity.setId(1L); ResourceEntity viewResourceEntity = new ResourceEntity(); viewResourceEntity.setResourceType(viewResourceTypeEntity); viewResourceEntity.setId(53L); PermissionEntity readOnlyPermissionEntity = new PermissionEntity(); readOnlyPermissionEntity.addAuthorization(readOnlyRoleAuthorizationEntity); PermissionEntity viewUsePermissionEntity = new PermissionEntity(); viewUsePermissionEntity.addAuthorization(readOnlyRoleAuthorizationEntity); viewUsePermissionEntity.addAuthorization(viewUseRoleAuthorizationEntity); PermissionEntity administratorPermissionEntity = new PermissionEntity(); administratorPermissionEntity.addAuthorization(readOnlyRoleAuthorizationEntity); administratorPermissionEntity.addAuthorization(viewUseRoleAuthorizationEntity); administratorPermissionEntity.addAuthorization(administratorRoleAuthorizationEntity); PrivilegeEntity readOnlyPrivilegeEntity = new PrivilegeEntity(); readOnlyPrivilegeEntity.setPermission(readOnlyPermissionEntity); readOnlyPrivilegeEntity.setResource(clusterResourceEntity); PrivilegeEntity viewUsePrivilegeEntity = new PrivilegeEntity(); viewUsePrivilegeEntity.setPermission(viewUsePermissionEntity); viewUsePrivilegeEntity.setResource(viewResourceEntity); PrivilegeEntity administratorPrivilegeEntity = new PrivilegeEntity(); administratorPrivilegeEntity.setPermission(administratorPermissionEntity); administratorPrivilegeEntity.setResource(ambariResourceEntity); GrantedAuthority readOnlyAuthority = new AmbariGrantedAuthority(readOnlyPrivilegeEntity); GrantedAuthority viewUseAuthority = new AmbariGrantedAuthority(viewUsePrivilegeEntity); GrantedAuthority administratorAuthority = new AmbariGrantedAuthority(administratorPrivilegeEntity); Authentication readOnlyUser = new TestAuthentication(Collections.singleton(readOnlyAuthority)); Authentication viewUser = new TestAuthentication(Arrays.asList(readOnlyAuthority, viewUseAuthority)); Authentication administratorUser = new TestAuthentication(Collections.singleton(administratorAuthority)); SecurityContext context = SecurityContextHolder.getContext(); Set<RoleAuthorization> permissionsViewUse = EnumSet.of(RoleAuthorization.VIEW_USE); context.setAuthentication(readOnlyUser); assertFalse(AuthorizationHelper.isAuthorized(ResourceType.VIEW, 53L, permissionsViewUse)); assertFalse(AuthorizationHelper.isAuthorized(ResourceType.VIEW, 50L, permissionsViewUse)); context.setAuthentication(viewUser); assertTrue(AuthorizationHelper.isAuthorized(ResourceType.VIEW, 53L, permissionsViewUse)); assertFalse(AuthorizationHelper.isAuthorized(ResourceType.VIEW, 50L, permissionsViewUse)); context.setAuthentication(administratorUser); assertTrue(AuthorizationHelper.isAuthorized(ResourceType.VIEW, 53L, permissionsViewUse)); assertTrue(AuthorizationHelper.isAuthorized(ResourceType.VIEW, 50L, permissionsViewUse)); }
public static boolean isAuthorized(ResourceType resourceType, Long resourceId, RoleAuthorization requiredAuthorization) { return isAuthorized(getAuthentication(), resourceType, resourceId, EnumSet.of(requiredAuthorization)); }
AuthorizationHelper { public static boolean isAuthorized(ResourceType resourceType, Long resourceId, RoleAuthorization requiredAuthorization) { return isAuthorized(getAuthentication(), resourceType, resourceId, EnumSet.of(requiredAuthorization)); } }
AuthorizationHelper { public static boolean isAuthorized(ResourceType resourceType, Long resourceId, RoleAuthorization requiredAuthorization) { return isAuthorized(getAuthentication(), resourceType, resourceId, EnumSet.of(requiredAuthorization)); }  }
AuthorizationHelper { public static boolean isAuthorized(ResourceType resourceType, Long resourceId, RoleAuthorization requiredAuthorization) { return isAuthorized(getAuthentication(), resourceType, resourceId, EnumSet.of(requiredAuthorization)); }  Collection<GrantedAuthority> convertPrivilegesToAuthorities(Collection<PrivilegeEntity> privilegeEntities); static String getAuthenticatedName(); static String getAuthenticatedName(String defaultUsername); static int getAuthenticatedId(); static boolean isAuthorized(ResourceType resourceType, Long resourceId, 
                                     RoleAuthorization requiredAuthorization); static boolean isAuthorized(ResourceType resourceType, Long resourceId, 
                                     Set<RoleAuthorization> requiredAuthorizations); static boolean isAuthorized(Authentication authentication, ResourceType resourceType,
                                     Long resourceId, RoleAuthorization requiredAuthorization); static boolean isAuthorized(Authentication authentication, ResourceType resourceType,
                                     Long resourceId, Set<RoleAuthorization> requiredAuthorizations); static void verifyAuthorization(ResourceType resourceType,
                                         Long resourceId,
                                         Set<RoleAuthorization> requiredAuthorizations); static void verifyAuthorization(Authentication authentication,
                                         ResourceType resourceType,
                                         Long resourceId,
                                         Set<RoleAuthorization> requiredAuthorizations); static Authentication getAuthentication(); static void addLoginNameAlias(String ambariUserName, String loginAlias); static String resolveLoginAliasToUserName(String loginAlias); static List<String> getAuthorizationNames(Authentication authentication); }
AuthorizationHelper { public static boolean isAuthorized(ResourceType resourceType, Long resourceId, RoleAuthorization requiredAuthorization) { return isAuthorized(getAuthentication(), resourceType, resourceId, EnumSet.of(requiredAuthorization)); }  Collection<GrantedAuthority> convertPrivilegesToAuthorities(Collection<PrivilegeEntity> privilegeEntities); static String getAuthenticatedName(); static String getAuthenticatedName(String defaultUsername); static int getAuthenticatedId(); static boolean isAuthorized(ResourceType resourceType, Long resourceId, 
                                     RoleAuthorization requiredAuthorization); static boolean isAuthorized(ResourceType resourceType, Long resourceId, 
                                     Set<RoleAuthorization> requiredAuthorizations); static boolean isAuthorized(Authentication authentication, ResourceType resourceType,
                                     Long resourceId, RoleAuthorization requiredAuthorization); static boolean isAuthorized(Authentication authentication, ResourceType resourceType,
                                     Long resourceId, Set<RoleAuthorization> requiredAuthorizations); static void verifyAuthorization(ResourceType resourceType,
                                         Long resourceId,
                                         Set<RoleAuthorization> requiredAuthorizations); static void verifyAuthorization(Authentication authentication,
                                         ResourceType resourceType,
                                         Long resourceId,
                                         Set<RoleAuthorization> requiredAuthorizations); static Authentication getAuthentication(); static void addLoginNameAlias(String ambariUserName, String loginAlias); static String resolveLoginAliasToUserName(String loginAlias); static List<String> getAuthorizationNames(Authentication authentication);  }