2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.components.validation;
23 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
24 import org.openecomp.sdc.be.model.Component;
25 import org.openecomp.sdc.be.model.User;
26 import org.openecomp.sdc.be.user.Role;
28 import java.util.ArrayList;
29 import java.util.List;
31 @org.springframework.stereotype.Component
32 public class AccessValidations {
34 private final UserValidations userValidations;
35 private final ComponentValidations componentValidations;
37 public AccessValidations(UserValidations userValidations, ComponentValidations componentValidations) {
38 this.userValidations = userValidations;
39 this.componentValidations = componentValidations;
42 public Component validateUserCanRetrieveComponentData(String componentId, String componentType, String userId, String actionContext) {
43 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
44 retrieveUser(userId, actionContext);
45 return componentValidations.getComponent(componentId, componentTypeEnum);
48 public Component validateUserCanWorkOnComponent(String componentId, ComponentTypeEnum componentType, String userId, String actionContext) {
49 User user = retrieveUser(userId, actionContext);
50 validateUserIsAdminOrDesigner(user);
51 return componentValidations.validateComponentIsCheckedOutByUser(componentId, componentType, userId);
54 public void validateUserCanWorkOnComponent(Component component, String userId, String actionContext) {
55 User user = retrieveUser(userId, actionContext);
56 validateUserIsAdminOrDesigner(user);
57 componentValidations.validateComponentIsCheckedOutByUser(component, userId);
60 public void validateUserExists(String userId, String context) {
61 retrieveUser(userId, context);
65 public void validateUserExist(String userId, String actionContext) {
66 userValidations.validateUserExists(userId);
69 public User userIsAdminOrDesigner(String userId, String actionContext){
70 User user = retrieveUser(userId, actionContext);
71 validateUserIsAdminOrDesigner(user);
75 private User retrieveUser(String userId, String actionContext) {
76 return userValidations.validateUserExists(userId);
79 private void validateUserIsAdminOrDesigner(User user) {
80 List<Role> roles = new ArrayList<>(2);
81 roles.add(Role.ADMIN);
82 roles.add(Role.DESIGNER);
83 userValidations.validateUserRole(user, roles);