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=========================================================
20 package org.onap.policy.utils;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.List;
29 import org.onap.policy.model.Roles;
31 public final class UserUtils {
37 public static class Pair<T, U> {
41 public Pair(T t, U u) {
48 * Check Role and its Scopes.
50 * @param userRoles list of UserRoles.
51 * @return return role and scope from UserRole Object.
53 public static Pair<Set<String>, List<String>> checkRoleAndScope(List<Object> userRoles) {
56 // Check if the Role and Scope Size are Null get the values from db.
57 roles = new ArrayList<>();
58 scopes = new HashSet<>();
59 for (Object role : userRoles) {
60 Roles userRole = (Roles) role;
61 roles.add(userRole.getRole());
62 if (userRole.getScope() != null) {
63 if (userRole.getScope().contains(",")) {
64 String[] multipleScopes = userRole.getScope().split(",");
65 for (int i = 0; i < multipleScopes.length; i++) {
66 scopes.add(trimScope(multipleScopes[i]));
69 scopes.add(trimScope(userRole.getScope()));
73 return new Pair<>(scopes, roles);
77 * Get Role by Scope based on UserRole Object.
79 * @param userRoles list of UserRoles.
80 * @return return the map<scope, role>.
82 public static Map<String, String> getRoleByScope(List<Object> userRoles) {
83 Map<String, String> rolesList = new HashMap<>();
84 for (Object role : userRoles) {
85 Roles userRole = (Roles) role;
86 if (!userRole.getRole().startsWith("super-")) {
87 rolesList = addNonSuperUserScopes(userRole, rolesList);
89 rolesList.put("@All@", userRole.getRole());
96 * Read non super role scopes and add to map.
98 * @param userRole Role Object.
99 * @param rolesList roleList Object.
100 * @return return the map<scope, role>.
102 private static Map<String, String> addNonSuperUserScopes(Roles userRole, Map<String, String> rolesList) {
103 if (userRole.getScope() != null && !(userRole.getScope().trim().isEmpty())) {
104 if (userRole.getScope().contains(",")) {
105 String[] multipleScopes = userRole.getScope().split(",");
106 for (int i = 0; i < multipleScopes.length; i++) {
107 rolesList.put(trimScope(multipleScopes[i]), userRole.getRole());
110 rolesList.put(trimScope(userRole.getScope()), userRole.getRole());
119 * @param scope string scope name.
120 * @return trim scope.
122 private static String trimScope(String scope) {
123 return scope.replace("[", "").replace("]", "").replace("\"", "").trim();