2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-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.onap.policy.utils;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.List;
30 import org.onap.policy.model.Roles;
32 public final class UserUtils {
38 public static class Pair<T, U> {
42 public Pair(T t, U u) {
49 * Check Role and its Scopes.
51 * @param userRoles list of UserRoles.
52 * @return return role and scope from UserRole Object.
54 public static Pair<Set<String>, List<String>> checkRoleAndScope(List<Object> userRoles) {
57 // Check if the Role and Scope Size are Null get the values from db.
58 roles = new ArrayList<>();
59 scopes = new HashSet<>();
60 for (Object role : userRoles) {
61 Roles userRole = (Roles) role;
62 roles.add(userRole.getRole());
63 if (userRole.getScope() != null) {
64 if (userRole.getScope().contains(",")) {
65 String[] multipleScopes = userRole.getScope().split(",");
66 for (int i = 0; i < multipleScopes.length; i++) {
67 scopes.add(trimScope(multipleScopes[i]));
70 scopes.add(trimScope(userRole.getScope()));
74 return new Pair<>(scopes, roles);
78 * Get Role by Scope based on UserRole Object.
80 * @param userRoles list of UserRoles.
81 * @return return the map<scope, role>.
83 public static Map<String, String> getRoleByScope(List<Object> userRoles) {
84 Map<String, String> rolesList = new HashMap<>();
85 for (Object role : userRoles) {
86 Roles userRole = (Roles) role;
87 if (!userRole.getRole().startsWith("super-")) {
88 rolesList = addNonSuperUserScopes(userRole, rolesList);
90 rolesList.put("@All@", userRole.getRole());
97 * Read non super role scopes and add to map.
99 * @param userRole Role Object.
100 * @param rolesList roleList Object.
101 * @return return the map<scope, role>.
103 private static Map<String, String> addNonSuperUserScopes(Roles userRole, Map<String, String> rolesList) {
104 if (userRole.getScope() != null && !(userRole.getScope().trim().isEmpty())) {
105 if (userRole.getScope().contains(",")) {
106 String[] multipleScopes = userRole.getScope().split(",");
107 for (int i = 0; i < multipleScopes.length; i++) {
108 rolesList.put(trimScope(multipleScopes[i]), userRole.getRole());
111 rolesList.put(trimScope(userRole.getScope()), userRole.getRole());
120 * @param scope string scope name.
121 * @return trim scope.
123 private static String trimScope(String scope) {
124 return scope.replace("[", "").replace("]", "").replace("\"", "").trim();