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.annotation.PostConstruct;
17 import javax.servlet.http.HttpServletRequest;
20 //import org.codehaus.jackson.map.ObjectMapper;
23 * Created by Oren on 7/1/17.
27 public class RoleProvider {
29 private static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(RoleProvider.class);
30 final String readPermissionString = "read";
31 SubscriberList subscribers;
32 ObjectMapper om = new ObjectMapper();
34 private AaiService aaiService;
36 public static List<String> extractRoleFromSession(HttpServletRequest request) {
38 return new ArrayList<String>();
44 LOG.debug(EELFLoggerDelegate.debugLogger, "Role provider => init method started");
45 AaiResponse<SubscriberList> subscribersResponse = aaiService.getFullSubscriberList();
46 subscribers = subscribersResponse.getT();
47 LOG.debug(EELFLoggerDelegate.debugLogger, "Role provider => init method finished");
50 public List<Role> getUserRoles(HttpServletRequest request) throws JsonProcessingException {
51 String logPrefix = "Role Provider (" + UserUtils.getUserId(request) + ") ==>";
53 LOG.debug(EELFLoggerDelegate.debugLogger, logPrefix + "Entering to get user role for user " + UserUtils.getUserId(request));
55 List<Role> roleList = new ArrayList<>();
56 //Disable roles until AAF integration finishes
57 /*Map roles = UserUtils.getRoles(request);
58 for (Object role : roles.keySet()) {
59 org.onap.portalsdk.core.domain.Role sdkRol = (org.onap.portalsdk.core.domain.Role) roles.get(role);
61 LOG.debug(EELFLoggerDelegate.debugLogger, logPrefix + "Role " + sdkRol.getName() + " is being proccessed");
63 if (sdkRol.getName().contains(readPermissionString)) {
64 LOG.debug(EELFLoggerDelegate.debugLogger, logPrefix + " Role " + sdkRol.getName() + " contain " + readPermissionString);
68 String[] roleParts = splitRole((sdkRol.getName()), logPrefix);
69 roleList.add(createRoleFromStringArr(roleParts, logPrefix));
70 String msg = String.format(logPrefix + " User %s got permissions %s", UserUtils.getUserId(request), Arrays.toString(roleParts));
71 LOG.debug(EELFLoggerDelegate.debugLogger, msg);
72 } catch (RoleParsingException e) {
73 LOG.error(logPrefix + " Failed to parse permission");
81 public String[] splitRole(String roleAsString, String logPrefix) {
82 LOG.debug(EELFLoggerDelegate.debugLogger, logPrefix + "Spliting role = " + roleAsString + "With delimeter = " + ModelConstants.ROLE_DELIMITER);
83 return roleAsString.split(ModelConstants.ROLE_DELIMITER);
86 public boolean userPermissionIsReadOnly(List<Role> roles) {
88 return (!(roles.size() > 0));
91 public boolean userPermissionIsReadLogs(List<Role> roles){
92 for(Role role: roles){
93 if(role.getServiceType().equals("LOGS")){
94 if(role.getTenant().equals("PERMITTED")){
102 private String replaceSubscriberNameToGlobalCustomerID(String subscriberName, String logPrefix) throws JsonProcessingException {
103 if (subscribers == null) {
104 LOG.debug(EELFLoggerDelegate.debugLogger, "replaceSubscriberNameToGlobalCustomerID calling init method");
107 LOG.debug(EELFLoggerDelegate.debugLogger, logPrefix + "subscribers list size is " + subscribers.customer.size() + " with the values " + om.writeValueAsString(subscribers.customer));
108 LOG.debug(EELFLoggerDelegate.debugLogger, logPrefix + "subscribers list size is " + subscribers.customer.size() + " with the values " + om.writeValueAsString(subscribers.customer));
111 Optional<Subscriber> s = subscribers.customer.stream().filter(x -> x.subscriberName.equals(subscriberName)).findFirst();
112 //Fixing bug of logging "optional get" before isPresent
113 String replacement = s.isPresent() ? s.get().globalCustomerId : "";
114 LOG.debug(EELFLoggerDelegate.debugLogger, logPrefix + "Subscribername " + subscriberName + " changed to " + replacement);
118 public Role createRoleFromStringArr(String[] roleParts, String rolePrefix) throws JsonProcessingException, RoleParsingException {
119 String globalCustomerID = replaceSubscriberNameToGlobalCustomerID(roleParts[0], rolePrefix);
121 if (roleParts.length > 2) {
122 return new Role(EcompRole.READ, globalCustomerID, roleParts[1], roleParts[2]);
124 return new Role(EcompRole.READ, globalCustomerID, roleParts[1], null);
126 } catch (ArrayIndexOutOfBoundsException e) {
127 if (roleParts.length > 0)
128 LOG.debug(EELFLoggerDelegate.debugLogger, "Could not parse role ", roleParts[0]);
130 LOG.debug(EELFLoggerDelegate.debugLogger, "Got empty role, Could not parse it ");
133 throw new RoleParsingException();