Turn role management off by default
[vid.git] / vid-app-common / src / main / java / org / onap / vid / roles / RoleValidator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 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.onap.vid.roles;
22
23 import java.util.List;
24 import java.util.Map;
25 import org.onap.portalsdk.core.util.SystemProperties;
26 import org.onap.vid.mso.rest.RequestDetails;
27
28 /**
29  * Created by Oren on 7/12/17.
30  */
31 public class RoleValidator {
32
33     private boolean disableRoles;
34     private final List<Role> userRoles;
35
36     public RoleValidator(List<Role> roles) {
37         this.userRoles = roles;
38         disableRoles = SystemProperties.getProperty("role_management_activated").equals("false");
39     }
40
41     public boolean isSubscriberPermitted(String subscriberName) {
42         if (this.disableRoles) {
43             return true;
44         }
45
46         for (Role role : userRoles) {
47             if (role.getSubscribeName().equals(subscriberName)) {
48                 return true;
49             }
50         }
51         return false;
52     }
53
54     public boolean isServicePermitted(String subscriberName, String serviceType) {
55         if (this.disableRoles) {
56             return true;
57         }
58
59         for (Role role : userRoles) {
60             if (role.getSubscribeName().equals(subscriberName) && role.getServiceType().equals(serviceType)) {
61                 return true;
62             }
63         }
64         return false;
65     }
66
67     boolean isMsoRequestValid(RequestDetails msoRequest) {
68         if (this.disableRoles) {
69             return true;
70         }
71
72         try {
73             String globalSubscriberIdRequested = (String) ((Map) ((Map) msoRequest.getAdditionalProperties()
74                 .get("requestDetails")).get("subscriberInfo")).get("globalSubscriberId");
75             String serviceType = (String) ((Map) ((Map) msoRequest.getAdditionalProperties().get("requestDetails"))
76                 .get("requestParameters")).get("subscriptionServiceType");
77             return isServicePermitted(globalSubscriberIdRequested, serviceType);
78         } catch (Exception e) {
79             //Until we'll get the exact information regarding the tenants and the global customer id, we'll return true on unknown requests to mso
80             return true;
81         }
82     }
83
84     public boolean isTenantPermitted(String globalCustomerId, String serviceType, String tenantName) {
85         if (this.disableRoles) {
86             return true;
87         }
88
89         for (Role role : userRoles) {
90             if (role.getSubscribeName().equals(globalCustomerId)
91                 && role.getServiceType().equals(serviceType)
92                 && (role.getTenant() == null || role.getTenant().equalsIgnoreCase(tenantName))) {
93                 return true;
94             }
95         }
96         return false;
97     }
98
99     void enableRoles() {
100         this.disableRoles = false;
101     }
102 }