1 package org.onap.vid.roles;
3 import com.fasterxml.jackson.core.JsonProcessingException;
4 import com.fasterxml.jackson.databind.ObjectMapper;
5 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
6 import org.onap.portalsdk.core.web.support.UserUtils;
7 import org.onap.vid.aai.AaiResponse;
8 import org.onap.vid.aai.exceptions.RoleParsingException;
9 import org.onap.vid.model.ModelConstants;
10 import org.onap.vid.model.Subscriber;
11 import org.onap.vid.model.SubscriberList;
12 import org.onap.vid.services.AaiService;
13 import org.springframework.beans.factory.annotation.Autowired;
14 import org.springframework.stereotype.Component;
16 import javax.servlet.http.HttpServletRequest;
19 //import org.codehaus.jackson.map.ObjectMapper;
22 * Created by Oren on 7/1/17.
26 public class RoleProvider {
28 private static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(RoleProvider.class);
29 final String readPermissionString = "read";
30 SubscriberList subscribers;
31 ObjectMapper om = new ObjectMapper();
33 private AaiService aaiService;
35 public static List<String> extractRoleFromSession(HttpServletRequest request) {
37 return new ArrayList<String>();
42 LOG.debug(EELFLoggerDelegate.debugLogger, "Role provider => init method started");
43 AaiResponse<SubscriberList> subscribersResponse = aaiService.getFullSubscriberList();
44 subscribers = subscribersResponse.getT();
45 LOG.debug(EELFLoggerDelegate.debugLogger, "Role provider => init method finished");
48 public List<Role> getUserRoles(HttpServletRequest request) throws JsonProcessingException {
49 String logPrefix = "Role Provider (" + UserUtils.getUserId(request) + ") ==>";
51 LOG.debug(EELFLoggerDelegate.debugLogger, logPrefix + "Entering to get user role for user " + UserUtils.getUserId(request));
53 List<Role> roleList = new ArrayList<>();
54 //Disable roles until AAF integration finishes
55 /*HashMap roles = UserUtils.getRoles(request);
56 for (Object role : roles.keySet()) {
57 org.openecomp.portalsdk.core.domain.Role sdkRol = (org.openecomp.portalsdk.core.domain.Role) roles.get(role);
59 LOG.debug(EELFLoggerDelegate.debugLogger, logPrefix + "Role " + sdkRol.getName() + " is being proccessed");
61 if (sdkRol.getName().contains(readPermissionString)) {
62 LOG.debug(EELFLoggerDelegate.debugLogger, logPrefix + " Role " + sdkRol.getName() + " contain " + readPermissionString);
66 String[] roleParts = splitRole((sdkRol.getName()), logPrefix);
67 roleList.add(createRoleFromStringArr(roleParts, logPrefix));
68 String msg = String.format(logPrefix + " User %s got permissions %s", UserUtils.getUserId(request), Arrays.toString(roleParts));
69 LOG.debug(EELFLoggerDelegate.debugLogger, msg);
70 } catch (RoleParsingException e) {
71 LOG.error(logPrefix + " Failed to parse permission");
79 public String[] splitRole(String roleAsString, String logPrefix) {
80 LOG.debug(EELFLoggerDelegate.debugLogger, logPrefix + "Spliting role = " + roleAsString + "With delimeter = " + ModelConstants.ROLE_DELIMITER);
81 return roleAsString.split(ModelConstants.ROLE_DELIMITER);
84 public boolean userPermissionIsReadOnly(List<Role> roles) {
86 return (!(roles.size() > 0));
89 public boolean userPermissionIsReadLogs(List<Role> roles){
90 for(Role role: roles){
91 if(role.getServiceType().equals("LOGS")){
92 if(role.getTenant().equals("PERMITTED")){
100 private String replaceSubscriberNameToGlobalCustomerID(String subscriberName, String logPrefix) throws JsonProcessingException {
101 if (subscribers == null) {
102 LOG.debug(EELFLoggerDelegate.debugLogger, "replaceSubscriberNameToGlobalCustomerID calling init method");
105 LOG.debug(EELFLoggerDelegate.debugLogger, logPrefix + "subscribers list size is " + subscribers.customer.size() + " with the values " + om.writeValueAsString(subscribers.customer));
106 LOG.debug(EELFLoggerDelegate.debugLogger, logPrefix + "subscribers list size is " + subscribers.customer.size() + " with the values " + om.writeValueAsString(subscribers.customer));
109 Optional<Subscriber> s = subscribers.customer.stream().filter(x -> x.subscriberName.equals(subscriberName)).findFirst();
110 //Fixing bug of logging "optional get" before isPresent
111 String replacement = s.isPresent() ? s.get().globalCustomerId : "";
112 LOG.debug(EELFLoggerDelegate.debugLogger, logPrefix + "Subscribername " + subscriberName + " changed to " + replacement);
116 public Role createRoleFromStringArr(String[] roleParts, String rolePrefix) throws JsonProcessingException, RoleParsingException {
117 String globalCustomerID = replaceSubscriberNameToGlobalCustomerID(roleParts[0], rolePrefix);
119 if (roleParts.length > 2) {
120 return new Role(EcompRole.READ, globalCustomerID, roleParts[1], roleParts[2]);
122 return new Role(EcompRole.READ, globalCustomerID, roleParts[1], null);
124 } catch (ArrayIndexOutOfBoundsException e) {
125 if (roleParts.length > 0)
126 LOG.debug(EELFLoggerDelegate.debugLogger, "Could not parse role ", roleParts[0]);
128 LOG.debug(EELFLoggerDelegate.debugLogger, "Got empty role, Could not parse it ");
131 throw new RoleParsingException();