+ return new ModelAndView("policy_Editor", "model", model);
+ }
+
+ /**
+ * Read the role from session.
+ *
+ * @param request Request input.
+ */
+ public void getUserRoleFromSession(HttpServletRequest request) {
+ // While user landing on Policy page, fetch the userId and Role from
+ // session.
+ // And, Query the Roles table and if user not exists or else modified
+ // update the Roles table.
+ List<String> roles;
+ List<String> newRoles = new ArrayList<>();
+ String userId = UserUtils.getUserSession(request).getOrgUserId();
+ String name = UserUtils.getUserSession(request).getFullName();
+ @SuppressWarnings("unchecked")
+ Set<UserApp> userApps = UserUtils.getUserSession(request).getUserApps();
+ for (UserApp userApp : userApps) {
+ newRoles.add(userApp.getRole().getName());
+ }
+ List<Object> userRoles = getRoles(userId);
+ String filteredRole = filterRole(newRoles);
+ if (userRoles == null || userRoles.isEmpty()) {
+ savePolicyRoles(name, filteredRole, userId);
+ } else {
+ Pair<Set<String>, List<String>> pair = org.onap.policy.utils.UserUtils.checkRoleAndScope(userRoles);
+ roles = pair.u;
+ if (!roles.contains(filteredRole)) {
+ String query = "delete from Roles where loginid='" + userId + "'";
+ commonClassDao.updateQuery(query);
+ savePolicyRoles(name, filteredRole, userId);
+ }
+ }
+ }
+
+ /**
+ * Save the Role to DB.
+ *
+ * @param name User Name.
+ * @param filteredRole Role Name.
+ * @param userId User LoginID.
+ */
+ private void savePolicyRoles(String name, String filteredRole, String userId) {
+ UserInfo userInfo = new UserInfo();
+ userInfo.setUserLoginId(userId);
+ userInfo.setUserName(name);
+ commonClassDao.save(userInfo);
+ Roles role = new Roles();
+ role.setName(name);
+ role.setRole(filteredRole);
+ role.setLoginId(userId);
+ commonClassDao.save(role);
+ }
+
+ /**
+ * Filter the list of roles hierarchy wise.
+ *
+ * @param newRoles list of roles from request.
+ * @return
+ */
+ private String filterRole(List<String> newRoles) {
+ Map<Integer, String> roleMap = new TreeMap<>();
+ roleMap.put(6, "guest");
+ for (String role : newRoles) {
+ if ("Policy Super Admin".equalsIgnoreCase(role.trim())
+ || "System Administrator".equalsIgnoreCase(role.trim())
+ || "Standard User".equalsIgnoreCase(role.trim())) {
+ roleMap.put(1, "super-admin");
+ } else if ("Policy Super Editor".equalsIgnoreCase(role.trim())) {
+ roleMap.put(2, "super-editor");
+ } else if ("Policy Super Guest".equalsIgnoreCase(role.trim())) {
+ roleMap.put(3, "super-guest");
+ } else if ("Policy Admin".equalsIgnoreCase(role.trim())) {
+ roleMap.put(4, "admin");
+ } else if ("Policy Editor".equalsIgnoreCase(role.trim())) {
+ roleMap.put(5, "editor");
+ }
+ }
+ return roleMap.entrySet().iterator().next().getValue();