Search in sources :

Example 6 with ArrayUtils.isEmpty

use of org.apache.commons.lang3.ArrayUtils.isEmpty in project alien4cloud by alien4cloud.

the class AbstractLocationResourcesBatchSecurityController method updateAuthorizedEnvironmentsPerApplication.

/**
 *****************************************************************************************************************************
 *
 * SECURITY ON APPLICATIONS
 *
 ******************************************************************************************************************************
 */
/**
 * Update applications, environments and environment types authorized to access the location resource.
 */
@ApiOperation(value = "Update applications, environments and environment type authorized to access the location resource", notes = "Only user with ADMIN role can update authorized applications/environments for the location.")
@RequestMapping(value = "/environmentsPerApplication", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAuthority('ADMIN')")
public synchronized RestResponse<Void> updateAuthorizedEnvironmentsPerApplication(@PathVariable String orchestratorId, @PathVariable String locationId, @RequestBody ApplicationEnvironmentAuthorizationUpdateRequest request) {
    if (ArrayUtils.isEmpty(request.getResources())) {
        return RestResponseBuilder.<Void>builder().build();
    }
    Location location = locationService.getLocation(orchestratorId, locationId);
    locationSecurityService.grantAuthorizationOnLocationIfNecessary(request.getApplicationsToAdd(), request.getEnvironmentsToAdd(), request.getEnvironmentTypesToAdd(), location);
    Arrays.stream(request.getResources()).forEach(resourceId -> {
        AbstractLocationResourceTemplate resourceTemplate = locationResourceService.getOrFail(resourceId);
        if (ArrayUtils.isNotEmpty(request.getApplicationsToDelete())) {
            resourcePermissionService.revokePermission(resourceTemplate, (resource -> locationResourceService.saveResource(location, (AbstractLocationResourceTemplate) resource)), Subject.APPLICATION, request.getApplicationsToDelete());
        }
        if (ArrayUtils.isNotEmpty(request.getEnvironmentsToDelete())) {
            resourcePermissionService.revokePermission(resourceTemplate, (resource -> locationResourceService.saveResource(location, (AbstractLocationResourceTemplate) resource)), Subject.ENVIRONMENT, request.getEnvironmentsToDelete());
        }
        if (ArrayUtils.isNotEmpty(request.getEnvironmentTypesToDelete())) {
            resourcePermissionService.revokePermission(resourceTemplate, (resource -> locationResourceService.saveResource(location, (AbstractLocationResourceTemplate) resource)), Subject.ENVIRONMENT_TYPE, request.getEnvironmentTypesToDelete());
        }
        Set<String> envIds = Sets.newHashSet();
        if (ArrayUtils.isNotEmpty(request.getApplicationsToAdd())) {
            resourcePermissionService.grantPermission(resourceTemplate, (resource -> locationResourceService.saveResource(location, (AbstractLocationResourceTemplate) resource)), Subject.APPLICATION, request.getApplicationsToAdd());
            // when an app is added, all eventual existing env authorizations are removed
            for (String applicationToAddId : request.getApplicationsToAdd()) {
                ApplicationEnvironment[] aes = applicationEnvironmentService.getByApplicationId(applicationToAddId);
                for (ApplicationEnvironment ae : aes) {
                    envIds.add(ae.getId());
                }
            }
            if (!envIds.isEmpty()) {
                resourcePermissionService.revokePermission(resourceTemplate, (resource -> locationResourceService.saveResource(location, (AbstractLocationResourceTemplate) resource)), Subject.ENVIRONMENT, envIds.toArray(new String[envIds.size()]));
            }
        }
        if (ArrayUtils.isNotEmpty(request.getEnvironmentsToAdd())) {
            List<String> envToAddSet = Arrays.stream(request.getEnvironmentsToAdd()).filter(env -> !envIds.contains(env)).collect(Collectors.toList());
            resourcePermissionService.grantPermission(resourceTemplate, (resource -> locationResourceService.saveResource(location, (AbstractLocationResourceTemplate) resource)), Subject.ENVIRONMENT, envToAddSet.toArray(new String[envToAddSet.size()]));
        }
        if (ArrayUtils.isNotEmpty(request.getEnvironmentTypesToAdd())) {
            List<String> envToAddSet = Arrays.stream(request.getEnvironmentTypesToAdd()).filter(env -> !envIds.contains(env)).collect(Collectors.toList());
            resourcePermissionService.grantPermission(resourceTemplate, (resource -> locationResourceService.saveResource(location, (AbstractLocationResourceTemplate) resource)), Subject.ENVIRONMENT_TYPE, envToAddSet.toArray(new String[envToAddSet.size()]));
        }
    });
    return RestResponseBuilder.<Void>builder().build();
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) Arrays(java.util.Arrays) ApplicationEnvironmentService(alien4cloud.application.ApplicationEnvironmentService) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Subject(alien4cloud.security.Subject) LocationService(alien4cloud.orchestrators.locations.services.LocationService) ResourcePermissionService(alien4cloud.authorization.ResourcePermissionService) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ArrayUtils(org.apache.commons.lang3.ArrayUtils) LocationSecurityService(alien4cloud.orchestrators.locations.services.LocationSecurityService) Location(alien4cloud.model.orchestrators.locations.Location) RequestBody(org.springframework.web.bind.annotation.RequestBody) ApiOperation(io.swagger.annotations.ApiOperation) Audit(alien4cloud.audit.annotation.Audit) RestResponseBuilder(alien4cloud.rest.model.RestResponseBuilder) RestResponse(alien4cloud.rest.model.RestResponse) ILocationResourceService(alien4cloud.orchestrators.locations.services.ILocationResourceService) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) MediaType(org.springframework.http.MediaType) Resource(javax.annotation.Resource) Set(java.util.Set) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) SubjectsAuthorizationRequest(alien4cloud.rest.orchestrator.model.SubjectsAuthorizationRequest) List(java.util.List) AbstractLocationResourceTemplate(alien4cloud.model.orchestrators.locations.AbstractLocationResourceTemplate) ApplicationEnvironmentAuthorizationUpdateRequest(alien4cloud.rest.orchestrator.model.ApplicationEnvironmentAuthorizationUpdateRequest) AbstractLocationResourceTemplate(alien4cloud.model.orchestrators.locations.AbstractLocationResourceTemplate) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) Location(alien4cloud.model.orchestrators.locations.Location) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ArrayUtils (org.apache.commons.lang3.ArrayUtils)6 List (java.util.List)5 Arrays (java.util.Arrays)4 Collectors (java.util.stream.Collectors)4 ApplicationEnvironmentService (alien4cloud.application.ApplicationEnvironmentService)3 Audit (alien4cloud.audit.annotation.Audit)3 ResourcePermissionService (alien4cloud.authorization.ResourcePermissionService)3 ApplicationEnvironment (alien4cloud.model.application.ApplicationEnvironment)3 AbstractLocationResourceTemplate (alien4cloud.model.orchestrators.locations.AbstractLocationResourceTemplate)3 Location (alien4cloud.model.orchestrators.locations.Location)3 ILocationResourceService (alien4cloud.orchestrators.locations.services.ILocationResourceService)3 LocationSecurityService (alien4cloud.orchestrators.locations.services.LocationSecurityService)3 LocationService (alien4cloud.orchestrators.locations.services.LocationService)3 RestResponse (alien4cloud.rest.model.RestResponse)3 RestResponseBuilder (alien4cloud.rest.model.RestResponseBuilder)3 ApplicationEnvironmentAuthorizationUpdateRequest (alien4cloud.rest.orchestrator.model.ApplicationEnvironmentAuthorizationUpdateRequest)3 SubjectsAuthorizationRequest (alien4cloud.rest.orchestrator.model.SubjectsAuthorizationRequest)3 Subject (alien4cloud.security.Subject)3 Sets (com.google.common.collect.Sets)3 ApiOperation (io.swagger.annotations.ApiOperation)3