From 4714974231f5bcbcd8f74c6309b6b8b26d13da87 Mon Sep 17 00:00:00 2001 From: Costantino Perciante Date: Thu, 16 Feb 2017 17:32:19 +0000 Subject: [PATCH] moved isAdmin check in the widget. Changed Product/Record term with Item for consistency with the portlet/widgets of the catalogue git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portlets/widgets/grsf-manage-widget@142657 82a268e6-3cf1-43bd-a215-b396298e98cf --- .project | 10 +- pom.xml | 2 +- .../client/GRSFManageWidgetService.java | 5 + .../client/GRSFManageWidgetServiceAsync.java | 2 + .../client/view/ManageProductWidget.java | 104 ++++++++++++------ .../client/view/ManageProductWidget.ui.xml | 28 ++--- .../manage/GRSFNotificationService.java | 45 +++++++- 7 files changed, 137 insertions(+), 59 deletions(-) diff --git a/.project b/.project index 072a4cd..4f283c0 100644 --- a/.project +++ b/.project @@ -25,11 +25,6 @@ - - org.eclipse.m2e.core.maven2Builder - - - com.google.gdt.eclipse.core.webAppProjectValidator @@ -40,6 +35,11 @@ + + org.eclipse.m2e.core.maven2Builder + + + org.eclipse.jem.workbench.JavaEMFNature diff --git a/pom.xml b/pom.xml index 0cd7b9d..d56dc9c 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ org.gcube.portlets.widgets grsf-manage-widget - 1.0.0-SNAPSHOT + 1.1.0-SNAPSHOT gCube GRSF Manage widget diff --git a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/GRSFManageWidgetService.java b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/GRSFManageWidgetService.java index 627aa2a..cbc30ba 100644 --- a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/GRSFManageWidgetService.java +++ b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/GRSFManageWidgetService.java @@ -21,4 +21,9 @@ public interface GRSFManageWidgetService extends RemoteService { * @throws Exception */ ManageProductBean getProductBeanById(String identifier) throws Exception; + + /** + * check if the user has the role to manage the item + */ + boolean isAdminUser(); } diff --git a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/GRSFManageWidgetServiceAsync.java b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/GRSFManageWidgetServiceAsync.java index cb93d22..d557599 100644 --- a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/GRSFManageWidgetServiceAsync.java +++ b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/GRSFManageWidgetServiceAsync.java @@ -16,4 +16,6 @@ public interface GRSFManageWidgetServiceAsync { void getProductBeanById(String identifier, AsyncCallback callback); + void isAdminUser(AsyncCallback callback); + } diff --git a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/ManageProductWidget.java b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/ManageProductWidget.java index c9d1312..d35810e 100644 --- a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/ManageProductWidget.java +++ b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/ManageProductWidget.java @@ -51,7 +51,7 @@ public class ManageProductWidget extends Composite{ AlertBlock infoBlock; @UiField - TextBox nameTextBox; + TextArea nameTextArea; @UiField TextBox currentStatus; @@ -85,75 +85,111 @@ public class ManageProductWidget extends Composite{ public static final String LOADING_IMAGE_URL = GWT.getModuleBaseURL() + "../images/loader.gif"; private final static List STATUS = new ArrayList(Arrays.asList(GRSFStatus.values())); - private final static String STATUS_UPDATE_SUCCESS = "The product was correctly updated. Thanks for your collaboration!"; - private final static String STATUS_UPDATE_ERROR = "Sorry, there was a problem while trying to update the status of this product"; + private final static String STATUS_UPDATE_SUCCESS = "The item has been correctly updated. Thanks for your collaboration!"; + private final static String STATUS_UPDATE_ERROR = "Sorry, there was a problem while trying to update the status of this item"; protected static final String ERROR_ON_RETRIEVING_BEAN = "It seems there was a problem while contacting the service..."; - protected static final String NO_GRSF_RECORD_BEAN = "This record is not a GRSF record, thus it cannot be managed"; + protected static final String NO_GRSF_RECORD_BEAN = "This item is not a GRSF record, thus it cannot be managed"; + + protected static final String ERROR_ON_ROLE_CHECK = "Sorry but the service was not able to check if you have the rights to manage an item." + + " You are suggested to contact the VRE Manager."; private ManageProductBean bean; public ManageProductWidget(String productIdentifier) { initWidget(uiBinder.createAndBindUi(this)); if(productIdentifier == null || productIdentifier.isEmpty()){ - GWT.log("The received product identifier is null.."); + GWT.log("The received item identifier is null.."); return; } - GWT.log("Product identifier is " + productIdentifier); + GWT.log("item identifier is " + productIdentifier); // start loader service loadingImage.setUrl(LOADING_IMAGE_URL); loadingImage.setVisible(true); + manageProductModal.show(); + // async request to fetch the product retrieveProductBean(productIdentifier); - manageProductModal.show(); } - private void retrieveProductBean(String productIdentifier) { + private void retrieveProductBean(final String productIdentifier) { - service.getProductBeanById(productIdentifier, new AsyncCallback() { + // check if it is an administrator + service.isAdminUser(new AsyncCallback() { @Override - public void onSuccess(ManageProductBean result) { + public void onSuccess(Boolean result) { - if(result != null){ - bean = result; - annotationArea.setText(""); - infoBlock.setVisible(false); - nameTextBox.setText(bean.getProductName()); - currentStatus.setText(bean.getCurrentStatus().toString()); - productType.setText(bean.getProductType()); - List statusToShow = new ArrayList(STATUS); - statusToShow.remove(bean.getCurrentStatus()); - listBoxStatus.addItem("Select the new status"); - listBoxStatus.getElement().cast().getOptions().getItem(0).setDisabled(true); - for (GRSFStatus availableStatus : statusToShow) { - listBoxStatus.addItem(availableStatus.toString()); - } - listBoxStatus.setSelectedIndex(0); - } - else{ - showInfo(ERROR_ON_RETRIEVING_BEAN, AlertType.ERROR); + if(!result){ + + showInfo(ERROR_ON_ROLE_CHECK, AlertType.ERROR); + + // hide the form and disable the send button formUpdate.setVisible(false); confirmButton.setEnabled(false); + loadingImage.setVisible(false); + + }else{ + service.getProductBeanById(productIdentifier, new AsyncCallback() { + + @Override + public void onSuccess(ManageProductBean result) { + + if(result != null){ + bean = result; + annotationArea.setText(""); + infoBlock.setVisible(false); + nameTextArea.setText(bean.getProductName()); + currentStatus.setText(bean.getCurrentStatus().toString()); + productType.setText(bean.getProductType()); + List statusToShow = new ArrayList(STATUS); + statusToShow.remove(bean.getCurrentStatus()); + listBoxStatus.addItem("Select the new status"); + listBoxStatus.getElement().cast().getOptions().getItem(0).setDisabled(true); + for (GRSFStatus availableStatus : statusToShow) { + listBoxStatus.addItem(availableStatus.toString()); + } + listBoxStatus.setSelectedIndex(0); + } + else{ + showInfo(ERROR_ON_RETRIEVING_BEAN, AlertType.ERROR); + formUpdate.setVisible(false); + confirmButton.setEnabled(false); + } + + loadingImage.setVisible(false); + } + + @Override + public void onFailure(Throwable caught) { + + if(caught instanceof NoGRSFRecordException) + showInfo(NO_GRSF_RECORD_BEAN, AlertType.WARNING); + else + showInfo(caught.getMessage(), AlertType.ERROR); + + // hide the form and disable the send button + formUpdate.setVisible(false); + confirmButton.setEnabled(false); + loadingImage.setVisible(false); + } + }); } - loadingImage.setVisible(false); } @Override public void onFailure(Throwable caught) { - - if(caught instanceof NoGRSFRecordException) - showInfo(NO_GRSF_RECORD_BEAN, AlertType.WARNING); - else - showInfo(caught.getMessage(), AlertType.ERROR); + + showInfo(ERROR_ON_ROLE_CHECK, AlertType.ERROR); // hide the form and disable the send button formUpdate.setVisible(false); confirmButton.setEnabled(false); loadingImage.setVisible(false); + } }); diff --git a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/ManageProductWidget.ui.xml b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/ManageProductWidget.ui.xml index 5ffd5d2..be8e9ba 100644 --- a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/ManageProductWidget.ui.xml +++ b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/ManageProductWidget.ui.xml @@ -8,7 +8,7 @@ } - @@ -23,23 +23,23 @@ - - Product Name: + + Item Name: - + - Product Current Status: + title="The current status of this Item"> + Item Current Status: @@ -47,8 +47,8 @@ - Product New Status: + title="The new status of this Item"> + Item New Status: - Product Type: + title="The Item type"> + Item Type: + width="97%" title="The Item type" enabled="false" ui:field="productType"> diff --git a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/server/manage/GRSFNotificationService.java b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/server/manage/GRSFNotificationService.java index 2b601c3..94b32fd 100644 --- a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/server/manage/GRSFNotificationService.java +++ b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/server/manage/GRSFNotificationService.java @@ -26,7 +26,9 @@ import org.gcube.datacatalogue.grsf_manage_widget.shared.ManageProductBean; import org.gcube.datacatalogue.grsf_manage_widget.shared.ex.NoGRSFRecordException; import org.gcube.resources.discovery.client.api.DiscoveryClient; import org.gcube.resources.discovery.client.queries.api.SimpleQuery; +import org.gcube.vomanagement.usermanagement.impl.LiferayRoleManager; import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager; +import org.gcube.vomanagement.usermanagement.model.GCubeRole; import org.gcube.vomanagement.usermanagement.model.GCubeUser; import org.json.simple.JSONArray; import org.json.simple.JSONObject; @@ -54,7 +56,6 @@ import eu.trentorise.opendata.jackan.model.CkanTag; public class GRSFNotificationService extends RemoteServiceServlet implements GRSFManageWidgetService{ private static final long serialVersionUID = -4534905087994875893L; - //private static Logger logger = LoggerFactory.getLogger(GRSFNotificationService.class); private static final Log logger = LogFactoryUtil.getLog(GRSFNotificationService.class); private static final String SERVICE_POST_METHOD = "/service/updater/post"; private static final String ANNOTATION_KEY = "Annotation on update"; @@ -62,6 +63,7 @@ public class GRSFNotificationService extends RemoteServiceServlet implements GRS private static final String STATUS_CUSTOM_FIELD_KEY = "Status"; private static final int MAX_TRIAL = 5; public static final String GRSF_UPDATER_SERVICE = "GRSFUpdaterEndPoint"; + private static final String GRSF_ADMIN_ROLE = "GRSF-Admin"; // request post fields private static final String CATALOGUE_ID = "catalog_id"; private static final String KB_ID = "record_id"; @@ -80,6 +82,9 @@ public class GRSFNotificationService extends RemoteServiceServlet implements GRS // request url public static final String GCUBE_REQUEST_URL = "gcube-request-url"; + // session info for user + public static final String GRSF_ADMIN_SESSION_KEY = "IS_GRSF_ADMIN"; + /** * Instanciate the ckan util library. * Since it needs the scope, we need to check if it is null or not @@ -145,7 +150,7 @@ public class GRSFNotificationService extends RemoteServiceServlet implements GRS // it cannot be enabled in this case ... if(recordType == null || recordType.equals("Source")) - throw new NoGRSFRecordException("This is not a GRSF record"); + throw new NoGRSFRecordException("This is not a GRSF Item"); if(status == null || uuidKB == null || productType == null) throw new Exception("Some information is missing in this record: Status = " + status + ", knowledge_base_uuid = " + uuidKB + @@ -158,7 +163,7 @@ public class GRSFNotificationService extends RemoteServiceServlet implements GRS toReturn.setProductName(title); toReturn.setProductType(productType); - logger.info("Returning product bean " + toReturn); + logger.info("Returning item bean " + toReturn); return toReturn; @@ -225,7 +230,7 @@ public class GRSFNotificationService extends RemoteServiceServlet implements GRS throw new IllegalArgumentException("GRSF Updater service url cannot be null"); if(bean == null) - throw new IllegalArgumentException("Product bean to manage cannot be null"); + throw new IllegalArgumentException("Item bean to manage cannot be null"); try(CloseableHttpClient httpClient = HttpClientBuilder.create().build();){ @@ -263,7 +268,7 @@ public class GRSFNotificationService extends RemoteServiceServlet implements GRS return patchProduct(catalogue, bean, username); }catch(Exception e){ - logger.error("Unable to update this record" + e.getMessage()); + logger.error("Unable to update this Item " + e.getMessage()); return e.getMessage(); } @@ -459,4 +464,34 @@ public class GRSFNotificationService extends RemoteServiceServlet implements GRS logger.debug("Returning user " + user); return user; } + + + @Override + public boolean isAdminUser() { + try{ + + Boolean inSession = (Boolean)getThreadLocalRequest().getSession().getAttribute(GRSF_ADMIN_SESSION_KEY); + + if(inSession != null) + return inSession; + else{ + PortalContext pContext = PortalContext.getConfiguration(); + List userRoles = new LiferayRoleManager().listRolesByUserAndGroup( + pContext.getCurrentUser(getThreadLocalRequest()).getUserId(), + pContext.getCurrentGroupId(getThreadLocalRequest())); + boolean toSetInSession = false; + for (GCubeRole gCubeRole : userRoles) { + if(gCubeRole.getRoleName().equals(GRSF_ADMIN_ROLE)){ + toSetInSession = true; + break; + } + } + getThreadLocalRequest().getSession().setAttribute(GRSF_ADMIN_SESSION_KEY, toSetInSession); + return toSetInSession; + } + }catch(Exception e){ + logger.error("Failed to check if the user has role " + GRSF_ADMIN_ROLE, e); + } + return false; + } }