use of io.gravitee.management.rest.security.Permission in project gravitee-management-rest-api by gravitee-io.
the class PermissionFilterTest method initManagementMocks.
/**
* MANAGEMENT Tests
*/
private void initManagementMocks() {
Principal user = () -> USERNAME;
when(securityContext.getUserPrincipal()).thenReturn(user);
Permission perm = mock(Permission.class);
when(perm.value()).thenReturn(RolePermission.MANAGEMENT_API);
when(perm.acls()).thenReturn(new RolePermissionAction[] { RolePermissionAction.UPDATE });
when(permissions.value()).thenReturn(new Permission[] { perm });
UriInfo uriInfo = mock(UriInfo.class);
when(containerRequestContext.getUriInfo()).thenReturn(uriInfo);
when(membershipService.getRole(any(), any(), any(), any())).thenReturn(mock(RoleEntity.class));
}
use of io.gravitee.management.rest.security.Permission in project gravitee-management-rest-api by gravitee-io.
the class PermissionFilterTest method initApplicationMocks.
/**
* APPLICATION Tests
*/
private ApplicationEntity initApplicationMocks() {
ApplicationEntity application = new ApplicationEntity();
application.setId(APPLICATION_ID);
Principal user = () -> USERNAME;
when(applicationService.findById(application.getId())).thenReturn(application);
when(securityContext.getUserPrincipal()).thenReturn(user);
Permission perm = mock(Permission.class);
when(perm.value()).thenReturn(RolePermission.APPLICATION_ANALYTICS);
when(perm.acls()).thenReturn(new RolePermissionAction[] { RolePermissionAction.UPDATE });
when(permissions.value()).thenReturn(new Permission[] { perm });
UriInfo uriInfo = mock(UriInfo.class);
MultivaluedHashMap<String, String> map = new MultivaluedHashMap<>();
map.put("application", Collections.singletonList(application.getId()));
when(uriInfo.getPathParameters()).thenReturn(map);
when(containerRequestContext.getUriInfo()).thenReturn(uriInfo);
return application;
}
use of io.gravitee.management.rest.security.Permission in project gravitee-management-rest-api by gravitee-io.
the class PermissionFilterTest method initApiMocks.
/**
* API Tests
*/
private ApiEntity initApiMocks() {
ApiEntity api = new ApiEntity();
api.setId(API_ID);
Principal user = () -> USERNAME;
when(apiService.findById(api.getId())).thenReturn(api);
when(securityContext.getUserPrincipal()).thenReturn(user);
Permission perm = mock(Permission.class);
when(perm.value()).thenReturn(RolePermission.API_ANALYTICS);
when(perm.acls()).thenReturn(new RolePermissionAction[] { RolePermissionAction.UPDATE });
when(permissions.value()).thenReturn(new Permission[] { perm });
UriInfo uriInfo = mock(UriInfo.class);
MultivaluedHashMap<String, String> map = new MultivaluedHashMap<>();
map.put("api", Collections.singletonList(api.getId()));
when(uriInfo.getPathParameters()).thenReturn(map);
when(containerRequestContext.getUriInfo()).thenReturn(uriInfo);
return api;
}
use of io.gravitee.management.rest.security.Permission in project gravitee-management-rest-api by gravitee-io.
the class PermissionFilterTest method initPortalMocks.
/**
* PORTAL Tests
*/
private void initPortalMocks() {
Principal user = () -> USERNAME;
when(securityContext.getUserPrincipal()).thenReturn(user);
Permission perm = mock(Permission.class);
when(perm.value()).thenReturn(RolePermission.PORTAL_METADATA);
when(perm.acls()).thenReturn(new RolePermissionAction[] { RolePermissionAction.UPDATE });
when(permissions.value()).thenReturn(new Permission[] { perm });
UriInfo uriInfo = mock(UriInfo.class);
when(containerRequestContext.getUriInfo()).thenReturn(uriInfo);
when(membershipService.getRole(any(), any(), any(), any())).thenReturn(mock(RoleEntity.class));
}
use of io.gravitee.management.rest.security.Permission in project gravitee-management-rest-api by gravitee-io.
the class PermissionsFilter method filter.
protected void filter(Permissions permissions, ContainerRequestContext requestContext) {
if (permissions != null && permissions.value().length > 0) {
Principal principal = securityContext.getUserPrincipal();
if (principal != null) {
String username = principal.getName();
for (Permission permission : permissions.value()) {
RoleEntity role;
Map<String, char[]> memberPermissions;
switch(permission.value().getScope()) {
case MANAGEMENT:
role = membershipService.getRole(MembershipReferenceType.MANAGEMENT, MembershipDefaultReferenceId.DEFAULT.name(), username, RoleScope.MANAGEMENT);
if (roleService.hasPermission(role.getPermissions(), permission.value().getPermission(), permission.acls())) {
return;
}
break;
case PORTAL:
role = membershipService.getRole(MembershipReferenceType.PORTAL, MembershipDefaultReferenceId.DEFAULT.name(), username, RoleScope.PORTAL);
if (roleService.hasPermission(role.getPermissions(), permission.value().getPermission(), permission.acls())) {
return;
}
break;
case APPLICATION:
ApplicationEntity application = getApplication(requestContext);
memberPermissions = membershipService.getMemberPermissions(application, username);
if (roleService.hasPermission(memberPermissions, permission.value().getPermission(), permission.acls())) {
return;
}
break;
case API:
ApiEntity api = getApi(requestContext);
memberPermissions = membershipService.getMemberPermissions(api, username);
if (roleService.hasPermission(memberPermissions, permission.value().getPermission(), permission.acls())) {
return;
}
break;
default:
sendSecurityError();
}
}
}
sendSecurityError();
}
}
Aggregations