From 35a9debcc64b05163612165043690ddb128b1293 Mon Sep 17 00:00:00 2001 From: Alexey Sandler Date: Tue, 28 Jan 2020 16:57:39 +0200 Subject: create RoleValidatorFactory component. Issue-ID: VID-758 Signed-off-by: Alexey Sandler Change-Id: Id444ddbe74b6d28d697e130caa73bd7bfae9ce52 --- .../org/onap/vid/controller/AaiController.java | 14 ++---- .../main/java/org/onap/vid/roles/RoleProvider.java | 10 ++-- .../java/org/onap/vid/roles/RoleValidator.java | 10 ---- .../org/onap/vid/roles/RoleValidatorFactory.java | 53 ++++++++++++++++++++++ 4 files changed, 65 insertions(+), 22 deletions(-) create mode 100644 vid-app-common/src/main/java/org/onap/vid/roles/RoleValidatorFactory.java (limited to 'vid-app-common/src/main/java/org') diff --git a/vid-app-common/src/main/java/org/onap/vid/controller/AaiController.java b/vid-app-common/src/main/java/org/onap/vid/controller/AaiController.java index 563c9ff20..a9ce40bba 100644 --- a/vid-app-common/src/main/java/org/onap/vid/controller/AaiController.java +++ b/vid-app-common/src/main/java/org/onap/vid/controller/AaiController.java @@ -49,7 +49,6 @@ import org.onap.vid.aai.model.AaiGetTenatns.GetTenantsResponse; import org.onap.vid.aai.util.AAIRestInterface; import org.onap.vid.model.VersionByInvariantIdsRequest; import org.onap.vid.properties.Features; -import org.onap.vid.roles.Role; import org.onap.vid.roles.RoleProvider; import org.onap.vid.roles.RoleValidator; import org.onap.vid.services.AaiService; @@ -137,7 +136,7 @@ public class AaiController extends RestrictedBaseController { @RequestMapping(value = "/aai_get_services", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity doGetServices(HttpServletRequest request) throws IOException { - RoleValidator roleValidator = RoleValidator.by(roleProvider.getUserRoles(request)); + RoleValidator roleValidator = roleProvider.getUserRolesValidator(request); AaiResponse subscriberList = aaiService.getServices(roleValidator); return aaiResponseToResponseEntity(subscriberList); @@ -225,7 +224,7 @@ public class AaiController extends RestrictedBaseController { @RequestMapping(value = "/aai_get_full_subscribers", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity getFullSubscriberList(HttpServletRequest request) throws IOException { ResponseEntity responseEntity; - RoleValidator roleValidator = RoleValidator.by(roleProvider.getUserRoles(request)); + RoleValidator roleValidator = roleProvider.getUserRolesValidator(request); SubscriberFilteredResults subscriberList = aaiService.getFullSubscriberList(roleValidator); if (subscriberList.getHttpCode() == 200) { responseEntity = new ResponseEntity<>(objectMapper.writeValueAsString(subscriberList.getSubscriberList()), @@ -256,8 +255,7 @@ public class AaiController extends RestrictedBaseController { @RequestMapping(value = "/aai_sub_details/{subscriberId}", method = RequestMethod.GET) public ResponseEntity getSubscriberDetails(HttpServletRequest request, @PathVariable("subscriberId") String subscriberId, @RequestParam(value="omitServiceInstances", required = false, defaultValue = "false") boolean omitServiceInstances) throws IOException { - List roles = roleProvider.getUserRoles(request); - RoleValidator roleValidator = RoleValidator.by(roles); + RoleValidator roleValidator = roleProvider.getUserRolesValidator(request); AaiResponse subscriberData = aaiService.getSubscriberData(subscriberId, roleValidator, featureManager.isActive(Features.FLAG_1906_AAI_SUB_DETAILS_REDUCE_DEPTH) && omitServiceInstances); String httpMessage = subscriberData.getT() != null ? objectMapper.writeValueAsString(subscriberData.getT()) : subscriberData.getErrorMessage(); @@ -274,8 +272,7 @@ public class AaiController extends RestrictedBaseController { @RequestParam(value = "owningEntity", required = false) List owningEntities) throws IOException { ResponseEntity responseEntity; - List roles = roleProvider.getUserRoles(request); - RoleValidator roleValidator = RoleValidator.by(roles); + RoleValidator roleValidator = roleProvider.getUserRolesValidator(request); AaiResponse searchResult = aaiService .getServiceInstanceSearchResults(subscriberId, instanceIdentifier, roleValidator, owningEntities, projects); @@ -404,8 +401,7 @@ public class AaiController extends RestrictedBaseController { ResponseEntity responseEntity; try { - List roles = roleProvider.getUserRoles(request); - RoleValidator roleValidator = RoleValidator.by(roles); + RoleValidator roleValidator = roleProvider.getUserRolesValidator(request); AaiResponse response = aaiService .getTenants(globalCustomerId, serviceType, roleValidator); if (response.getHttpCode() == 200) { diff --git a/vid-app-common/src/main/java/org/onap/vid/roles/RoleProvider.java b/vid-app-common/src/main/java/org/onap/vid/roles/RoleProvider.java index 898db332c..d9f2fdedf 100644 --- a/vid-app-common/src/main/java/org/onap/vid/roles/RoleProvider.java +++ b/vid-app-common/src/main/java/org/onap/vid/roles/RoleProvider.java @@ -58,16 +58,20 @@ public class RoleProvider { private Function getUserIdFunction; private Function getRolesFunction; + private final RoleValidatorFactory roleValidatorFactory; @Autowired - public RoleProvider(AaiService aaiService) { + public RoleProvider(AaiService aaiService, RoleValidatorFactory roleValidatorFactory) { this.aaiService=aaiService; + this.roleValidatorFactory = roleValidatorFactory; getUserIdFunction = UserUtils::getUserId; getRolesFunction = UserUtils::getRoles; } - RoleProvider(AaiService aaiService, Function getUserIdFunction, Function getRolesFunction) { + RoleProvider(AaiService aaiService, RoleValidatorFactory roleValidatorFactory, + Function getUserIdFunction, Function getRolesFunction) { this.aaiService = aaiService; + this.roleValidatorFactory = roleValidatorFactory; this.getRolesFunction = getRolesFunction; this.getUserIdFunction = getUserIdFunction; } @@ -162,7 +166,7 @@ public class RoleProvider { } public RoleValidator getUserRolesValidator(HttpServletRequest request) { - return RoleValidator.by(getUserRoles(request)); + return roleValidatorFactory.by(getUserRoles(request)); } } diff --git a/vid-app-common/src/main/java/org/onap/vid/roles/RoleValidator.java b/vid-app-common/src/main/java/org/onap/vid/roles/RoleValidator.java index ed280c8b8..7b7401a01 100644 --- a/vid-app-common/src/main/java/org/onap/vid/roles/RoleValidator.java +++ b/vid-app-common/src/main/java/org/onap/vid/roles/RoleValidator.java @@ -27,16 +27,6 @@ import org.onap.portalsdk.core.util.SystemProperties; public interface RoleValidator { - static RoleValidator by(List roles) { - final boolean disableRoles = StringUtils.equals(SystemProperties.getProperty("role_management_activated"), "false"); - return by(roles, disableRoles); - } - - static RoleValidator by(List roles, boolean disableRoles) { - return disableRoles - ? new AlwaysValidRoleValidator() - : new RoleValidatorBySubscriberAndServiceType(roles); - } boolean isSubscriberPermitted(String subscriberId); diff --git a/vid-app-common/src/main/java/org/onap/vid/roles/RoleValidatorFactory.java b/vid-app-common/src/main/java/org/onap/vid/roles/RoleValidatorFactory.java new file mode 100644 index 000000000..12865401d --- /dev/null +++ b/vid-app-common/src/main/java/org/onap/vid/roles/RoleValidatorFactory.java @@ -0,0 +1,53 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2018 - 2019 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.vid.roles; + + +import java.util.List; +import org.apache.commons.lang3.StringUtils; +import org.onap.portalsdk.core.util.SystemProperties; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.togglz.core.manager.FeatureManager; + +@Component +public class RoleValidatorFactory { + private final FeatureManager featureManager; + + @Autowired + public RoleValidatorFactory(FeatureManager featureManager) { + this.featureManager = featureManager; + } + + + public RoleValidator by(List roles) { + final boolean disableRoles = StringUtils + .equals(SystemProperties.getProperty("role_management_activated"), "false"); + return by(roles, disableRoles); + } + + public RoleValidator by(List roles, boolean disableRoles) { + return disableRoles + ? new AlwaysValidRoleValidator() + : new RoleValidatorBySubscriberAndServiceType(roles); + } +} -- cgit 1.2.3-korg