package org.gcube.portlets.user.geoportaldataentry.server.config; import java.io.File; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.gcube.portlets.user.geoportaldataentry.shared.ACTION_ON_ITEM; import org.gcube.portlets.user.geoportaldataentry.shared.GNAUserRightsConfigException; import org.gcube.portlets.user.geoportaldataentry.shared.GcubeUserRole; import org.gcube.portlets.user.geoportaldataentry.shared.RoleRights; import org.gcube.portlets.user.geoportaldataentry.shared.RoleRights.OPERATION_TYPE; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.allen_sauer.gwt.log.client.Log; /** * The Class GNARoleRitghtsConfigReader. * * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it * * Nov 25, 2021 */ public class GNARoleRitghtsConfigReader { private static final String USER_RIGHTS_CONFIG_FILENAME = "GNA_RoleRights_Configurations.csv"; private static Logger LOG = LoggerFactory.getLogger(GNARoleRitghtsConfigReader.class); public static final String WRITE_OWN_CONFIG = "WRITE_OWN"; public static final String WRITE_ANY_CONFIG = "WRITE_ANY"; /** * Read user rights config. * * @return the list * @throws GNAUserRightsConfigException the GNA user rights config not * found exception */ public static List readRoleRightsConfig() throws GNAUserRightsConfigException { LOG.debug("readRoleRightsConfig called"); File configurationFile = null; try { InputStream in = (InputStream) GNARoleRitghtsConfigReader.class.getResourceAsStream(USER_RIGHTS_CONFIG_FILENAME); configurationFile = FileUtil.inputStreamToTempFile(in, USER_RIGHTS_CONFIG_FILENAME); CSVReader reader = new CSVReader(configurationFile); CSVFile csvFile = reader.getCsvFile(); List listUserRights = new ArrayList(); List headerKeys = csvFile.getHeaderRow().getListValues(); List rows = csvFile.getValueRows(); // MAPPING OPERATION TYPE AS READ, WRITE, etc. Map mapOperationTypes = new HashMap(); CSVRow operationTypeRow = rows.get(0); List rowValues = operationTypeRow.getListValues(); for (int j = 1; j < rowValues.size(); j++) { String operationType = rowValues.get(j); RoleRights.OPERATION_TYPE ot = RoleRights.OPERATION_TYPE.UNKNOWN; if (operationType.equalsIgnoreCase("R")) { ot = RoleRights.OPERATION_TYPE.READ; } else if (operationType.equalsIgnoreCase("RW")) { ot = RoleRights.OPERATION_TYPE.READ_WRITE; } else if (operationType.equalsIgnoreCase("W")) { ot = RoleRights.OPERATION_TYPE.WRITE; } mapOperationTypes.put(headerKeys.get(j), ot); } LOG.debug("Map of operation types: " + mapOperationTypes); //Starting from index 1 (means the second row in the CSV) for (int i = 1; i < rows.size(); i++) { LOG.trace(i + " row"); RoleRights useRights = new RoleRights(); CSVRow row = rows.get(i); // to map properties rowValues = row.getListValues(); LOG.debug("rowValues: " + rowValues); Map mapUserRolePermissions = new HashMap(); GcubeUserRole gCubeUserRole = toGcubeUserRole(rowValues.get(0)); if (gCubeUserRole == null) { LOG.warn("The Role " + rowValues.get(0) + " not found into roleName of: " + GcubeUserRole.values()); continue; } useRights.setUserRole(gCubeUserRole); for (int j = 1; j < rowValues.size(); j++) { mapUserRolePermissions.put(headerKeys.get(j), rowValues.get(j)); } LOG.debug("Role: " + useRights.getUserRole()); LOG.debug("Permissions read: " + mapUserRolePermissions); Map listPermessions = new HashMap(); for (ACTION_ON_ITEM value : ACTION_ON_ITEM.values()) { String yesno = mapUserRolePermissions.get(value.name()); if (yesno != null && yesno.equalsIgnoreCase("yes")) { listPermessions.put(value, mapOperationTypes.get(value.name())); } } useRights.setListPermessions(listPermessions); // String writeOwn = mapUserRolePermissions.get(WRITE_OWN_CONFIG); // if (writeOwn != null && writeOwn.equalsIgnoreCase("yes")) { // useRights.setWriteOwn(true); // } // // String writeAny = mapUserRolePermissions.get(WRITE_ANY_CONFIG); // if (writeAny != null && writeAny.equalsIgnoreCase("yes")) { // useRights.setWriteAny(true); // } listUserRights.add(useRights); } Log.info("Returning user rights config: " + listUserRights); return listUserRights; } catch (Exception e) { LOG.error("An error occurred on reading the configuration file " + USER_RIGHTS_CONFIG_FILENAME, e); throw new GNAUserRightsConfigException("Error on reading the configuration file. Is the file '" + USER_RIGHTS_CONFIG_FILENAME + "' in the application path?"); } finally { if (configurationFile != null) { try { configurationFile.delete(); } catch (Exception e) { // silent } } } } /** * To gcube user role. * * @param name the name * @return the gcube user role */ public static GcubeUserRole toGcubeUserRole(String name) { for (GcubeUserRole gCubeUserRole : GcubeUserRole.values()) { if (gCubeUserRole.getName().equalsIgnoreCase(name)) return gCubeUserRole; } return null; } }