1f68ffc0e0de30fc69752d102dc7f9a05396337e
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / utils / UserUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2018 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 package org.onap.policy.utils;
21
22 import java.util.ArrayList;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Set;
26
27 import org.onap.policy.model.Roles;
28
29 public final class UserUtils {
30         
31         private UserUtils () {
32                 // Empty Constructor
33         }
34         
35         public static class Pair<T, U> {
36                 public final T t;
37                 public final U u;
38                 
39                 public Pair (T t, U u) {
40                         this.t = t;
41                         this.u = u;
42                 }
43         }
44         
45         public static Pair<Set<String>, List<String>> checkRoleAndScope(List<Object> userRoles) {
46                 Set<String> scopes;
47                 List<String> roles;
48                 //Check if the Role and Scope Size are Null get the values from db. 
49                 roles = new ArrayList<>();
50                 scopes = new HashSet<>();
51                 for(Object role: userRoles){
52                         Roles userRole = (Roles) role;
53                         roles.add(userRole.getRole());
54                         if(userRole.getScope() != null){
55                                 if(userRole.getScope().contains(",")){
56                                         String[] multipleScopes = userRole.getScope().split(",");
57                                         for(int i =0; i < multipleScopes.length; i++){
58                                                 scopes.add(multipleScopes[i]);
59                                         }
60                                 }else{
61                                         scopes.add(userRole.getScope());
62                                 }               
63                         }
64                 }
65                 return new Pair<>(scopes, roles);
66         }
67
68 }