Replaced tests for roles
[vid.git] / vid-app-common / src / main / java / org / onap / vid / roles / RoleValidator.java
1 package org.onap.vid.roles;
2
3 import org.onap.vid.mso.rest.RequestDetails;
4
5 import java.util.List;
6 import java.util.Map;
7
8 /**
9  * Created by Oren on 7/12/17.
10  */
11 public class RoleValidator {
12
13     private boolean disableRoles = true;
14     private List<Role> userRoles;
15
16     public RoleValidator(List<Role> roles) {
17         this.userRoles = roles;
18     }
19
20     public boolean isSubscriberPermitted(String subscriberName) {
21         if (this.disableRoles) return true;
22
23         for (Role role : userRoles) {
24             if (role.getSubscribeName().equals(subscriberName))
25                 return true;
26         }
27         return false;
28     }
29
30     public boolean isServicePermitted(String subscriberName, String serviceType) {
31         if (this.disableRoles) return true;
32
33         for (Role role : userRoles) {
34             if (role.getSubscribeName().equals(subscriberName) && role.getServiceType().equals(serviceType))
35                 return true;
36         }
37         return false;
38     }
39
40     public boolean isMsoRequestValid(RequestDetails mso_request) {
41         if (this.disableRoles) return true;
42
43         try {
44             String globalSubscriberIdRequested = (String) ((Map) ((Map) mso_request.getAdditionalProperties().get("requestDetails")).get("subscriberInfo")).get("globalSubscriberId");
45             String serviceType = (String) ((Map) ((Map) mso_request.getAdditionalProperties().get("requestDetails")).get("requestParameters")).get("subscriptionServiceType");
46             return isServicePermitted(globalSubscriberIdRequested, serviceType);
47         } catch (Exception e) {
48             //Until we'll get the exact information regarding the tenants and the global customer id, we'll return true on unknown requests to mso
49             return true;
50         }
51     }
52
53     public boolean isTenantPermitted(String globalCustomerId, String serviceType, String tenantName) {
54         if (this.disableRoles) return true;
55
56         for (Role role : userRoles) {
57             if (role.getSubscribeName().equals(globalCustomerId)
58                     && role.getServiceType().equals(serviceType)
59                     && (role.getTenant() == null || role.getTenant().equalsIgnoreCase(tenantName))) {
60                 return true;
61             }
62         }
63         return false;
64     }
65
66     void enableRoles() {
67         this.disableRoles = false;
68     }
69 }