Added oparent to sdc main
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / validation / AccessValidations.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.validation;
22
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;
27
28 import java.util.ArrayList;
29 import java.util.List;
30
31 @org.springframework.stereotype.Component
32 public class AccessValidations {
33
34     private final UserValidations userValidations;
35     private final ComponentValidations componentValidations;
36
37     public AccessValidations(UserValidations userValidations, ComponentValidations componentValidations) {
38         this.userValidations = userValidations;
39         this.componentValidations = componentValidations;
40     }
41
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);
46     }
47
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);
52     }
53
54
55     public void validateUserCanWorkOnComponent(Component component, String userId, String actionContext) {
56         User user = retrieveUser(userId, actionContext);
57         validateUserIsAdminOrDesigner(user);
58         componentValidations.validateComponentIsCheckedOutByUser(component, userId);
59     }
60
61     public void validateUserExists(String userId, String context) {
62         retrieveUser(userId, context);
63     }
64
65     public void validateUserExist(String userId, String actionContext) {
66         userValidations.validateUserExists(userId, actionContext, false);
67     }
68
69     public User userIsAdminOrDesigner(String userId, String actionContext){
70         User user = retrieveUser(userId, actionContext);
71         validateUserIsAdminOrDesigner(user);
72         return user;
73     }
74
75     private User retrieveUser(String userId, String actionContext) {
76         return userValidations.validateUserExists(userId, actionContext, true);
77     }
78
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);
84     }
85
86 }