2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Modifications Copyright 2018 Nokia
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.vid.roles;
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
26 import org.onap.portalsdk.core.web.support.UserUtils;
27 import org.onap.vid.aai.AaiResponse;
28 import org.onap.vid.aai.exceptions.RoleParsingException;
29 import org.onap.vid.model.ModelConstants;
30 import org.onap.vid.model.Subscriber;
31 import org.onap.vid.model.SubscriberList;
32 import org.onap.vid.services.AaiService;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.stereotype.Component;
36 import javax.servlet.http.HttpServletRequest;
40 * Created by Oren on 7/1/17.
44 public class RoleProvider {
46 private static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(RoleProvider.class);
47 final String readPermissionString = "read";
48 SubscriberList subscribers;
49 ObjectMapper om = new ObjectMapper();
51 private AaiService aaiService;
53 public static List<String> extractRoleFromSession(HttpServletRequest request) {
55 return new ArrayList<String>();
60 LOG.debug(EELFLoggerDelegate.debugLogger, "Role provider => init method started");
61 AaiResponse<SubscriberList> subscribersResponse = aaiService.getFullSubscriberList();
62 subscribers = subscribersResponse.getT();
63 LOG.debug(EELFLoggerDelegate.debugLogger, "Role provider => init method finished");
66 public List<Role> getUserRoles(HttpServletRequest request) {
67 String logPrefix = "Role Provider (" + UserUtils.getUserId(request) + ") ==>";
69 LOG.debug(EELFLoggerDelegate.debugLogger, logPrefix + "Entering to get user role for user " + UserUtils.getUserId(request));
71 List<Role> roleList = new ArrayList<>();
73 Map roles = UserUtils.getRoles(request);
74 for (Object role : roles.keySet()) {
75 org.onap.portalsdk.core.domain.Role sdkRol = (org.onap.portalsdk.core.domain.Role) roles.get(role);
77 LOG.debug(EELFLoggerDelegate.debugLogger, logPrefix + "Role " + sdkRol.getName() + " is being proccessed");
79 if (sdkRol.getName().contains(readPermissionString)) {
80 LOG.debug(EELFLoggerDelegate.debugLogger, logPrefix + " Role " + sdkRol.getName() + " contain " + readPermissionString);
84 String[] roleParts = splitRole((sdkRol.getName()), logPrefix);
85 roleList.add(createRoleFromStringArr(roleParts, logPrefix));
86 String msg = String.format(logPrefix + " User %s got permissions %s", UserUtils.getUserId(request), Arrays.toString(roleParts));
87 LOG.debug(EELFLoggerDelegate.debugLogger, msg);
88 } catch (Exception e) {
89 LOG.error(logPrefix + " Failed to parse permission");
97 public String[] splitRole(String roleAsString, String logPrefix) {
98 LOG.debug(EELFLoggerDelegate.debugLogger, logPrefix + "Spliting role = " + roleAsString + "With delimeter = " + ModelConstants.ROLE_DELIMITER);
99 return roleAsString.split(ModelConstants.ROLE_DELIMITER);
102 public boolean userPermissionIsReadOnly(List<Role> roles) {
104 return (!(roles.size() > 0));
107 public boolean userPermissionIsReadLogs(List<Role> roles){
108 for(Role role: roles){
109 if(role.getServiceType().equals("LOGS")){
110 if(role.getTenant().equals("PERMITTED")){
118 private String replaceSubscriberNameToGlobalCustomerID(String subscriberName, String logPrefix) throws JsonProcessingException {
119 if (subscribers == null) {
120 LOG.debug(EELFLoggerDelegate.debugLogger, "replaceSubscriberNameToGlobalCustomerID calling init method");
123 LOG.debug(EELFLoggerDelegate.debugLogger, logPrefix + "subscribers list size is " + subscribers.customer.size() + " with the values " + om.writeValueAsString(subscribers.customer));
124 LOG.debug(EELFLoggerDelegate.debugLogger, logPrefix + "subscribers list size is " + subscribers.customer.size() + " with the values " + om.writeValueAsString(subscribers.customer));
127 Optional<Subscriber> s = subscribers.customer.stream().filter(x -> x.subscriberName.equals(subscriberName)).findFirst();
128 //Fixing bug of logging "optional get" before isPresent
129 String replacement = s.isPresent() ? s.get().globalCustomerId : "";
130 LOG.debug(EELFLoggerDelegate.debugLogger, logPrefix + "Subscribername " + subscriberName + " changed to " + replacement);
134 public Role createRoleFromStringArr(String[] roleParts, String rolePrefix) throws JsonProcessingException, RoleParsingException {
135 String globalCustomerID = replaceSubscriberNameToGlobalCustomerID(roleParts[0], rolePrefix);
137 if (roleParts.length > 2) {
138 return new Role(EcompRole.READ, globalCustomerID, roleParts[1], roleParts[2]);
140 return new Role(EcompRole.READ, globalCustomerID, roleParts[1], null);
142 } catch (ArrayIndexOutOfBoundsException e) {
143 if (roleParts.length > 0)
144 LOG.debug(EELFLoggerDelegate.debugLogger, "Could not parse role ", roleParts[0]);
146 LOG.debug(EELFLoggerDelegate.debugLogger, "Got empty role, Could not parse it ");
149 throw new RoleParsingException();