diff --git a/src/main/java/org/gcube/portlets/widgets/ckancontentmoderator/client/CkanContentModeratorService.java b/src/main/java/org/gcube/portlets/widgets/ckancontentmoderator/client/CkanContentModeratorService.java index cdad3db..8c69802 100644 --- a/src/main/java/org/gcube/portlets/widgets/ckancontentmoderator/client/CkanContentModeratorService.java +++ b/src/main/java/org/gcube/portlets/widgets/ckancontentmoderator/client/CkanContentModeratorService.java @@ -3,7 +3,9 @@ package org.gcube.portlets.widgets.ckancontentmoderator.client; import java.util.List; import org.gcube.datacatalogue.utillibrary.shared.ItemStatus; +import org.gcube.portlets.widgets.ckancontentmoderator.shared.CMSUserRole; import org.gcube.portlets.widgets.ckancontentmoderator.shared.CatalogueDataset; +import org.gcube.portlets.widgets.ckancontentmoderator.shared.ModerationUserRole; import org.gcube.portlets.widgets.ckancontentmoderator.shared.OperationReport; import org.gcube.portlets.widgets.ckancontentmoderator.shared.SearchedData; @@ -28,13 +30,13 @@ public interface CkanContentModeratorService extends RemoteService { public boolean isContentModeratorEnabled(); /** - * Sets the status. - * Currently, this only used to change the status from Rejected to Pending - * + * Sets the status. Currently, this only used to change the status from Rejected + * to Pending + * * @param theStatus the the status * @param itemNames the item names * @return the operation report - * @throws Exception + * @throws Exception the exception */ public OperationReport setStatus(ItemStatus theStatus, List itemNames) throws Exception; @@ -79,6 +81,7 @@ public interface CkanContentModeratorService extends RemoteService { * @param serverIndex the server index * @return the data for status * @throws Exception the exception + * @par@Override am lenght the lenght */ SearchedData getDataForStatus(ItemStatus status, int startIndex, int lenght, int serverIndex) throws Exception; @@ -92,4 +95,11 @@ public interface CkanContentModeratorService extends RemoteService { */ OperationReport approveItem(List itemNames, String moderatorMessage) throws Exception; + /** + * Gets the CMS roles for user in the context. + * + * @return the CMS roles for user in the context + * @throws Exception the exception + */ + ModerationUserRole getCMSRolesForUserInTheContext() throws Exception; } diff --git a/src/main/java/org/gcube/portlets/widgets/ckancontentmoderator/client/CkanContentModeratorServiceAsync.java b/src/main/java/org/gcube/portlets/widgets/ckancontentmoderator/client/CkanContentModeratorServiceAsync.java index 036a98f..e25aa77 100644 --- a/src/main/java/org/gcube/portlets/widgets/ckancontentmoderator/client/CkanContentModeratorServiceAsync.java +++ b/src/main/java/org/gcube/portlets/widgets/ckancontentmoderator/client/CkanContentModeratorServiceAsync.java @@ -4,6 +4,7 @@ import java.util.List; import org.gcube.datacatalogue.utillibrary.shared.ItemStatus; import org.gcube.portlets.widgets.ckancontentmoderator.shared.CatalogueDataset; +import org.gcube.portlets.widgets.ckancontentmoderator.shared.ModerationUserRole; import org.gcube.portlets.widgets.ckancontentmoderator.shared.OperationReport; import org.gcube.portlets.widgets.ckancontentmoderator.shared.SearchedData; @@ -87,4 +88,12 @@ public interface CkanContentModeratorServiceAsync { */ void setStatus(ItemStatus theStatus, List itemNames, AsyncCallback callback); + /** + * Gets the CMS roles for user in the context. + * + * @param callback the callback + * @return the CMS roles for user in the context + */ + void getCMSRolesForUserInTheContext(AsyncCallback callback); + } diff --git a/src/main/java/org/gcube/portlets/widgets/ckancontentmoderator/client/ui/HomeView.java b/src/main/java/org/gcube/portlets/widgets/ckancontentmoderator/client/ui/HomeView.java index be8bd1a..70c7637 100644 --- a/src/main/java/org/gcube/portlets/widgets/ckancontentmoderator/client/ui/HomeView.java +++ b/src/main/java/org/gcube/portlets/widgets/ckancontentmoderator/client/ui/HomeView.java @@ -114,7 +114,7 @@ public class HomeView extends Composite { } - setStatusOptions(); + setStatusOptions(status); bindEvents(); confirmPanel.add(confirmPanelContainer); @@ -123,7 +123,7 @@ public class HomeView extends Composite { /** * Sets the status options according to item status selected */ - private void setStatusOptions() { + private void setStatusOptions(ItemStatus selectedStatus) { // Fired when user clicks on CMS action for (final NavLink navLink : setStatusOptions) { @@ -132,6 +132,15 @@ public class HomeView extends Composite { if (navLink.getText().trim().equals(displayingItemStatus.getLabel())) { navLink.setVisible(false); } + + //Hiding the changing REJECT -> APPROVED + //One item REJECT must be only updated to return to PENDING status + if(selectedStatus.equals(ItemStatus.REJECTED)) { + if (navLink.getText().trim().equals(ItemStatus.APPROVED.getLabel())) { + navLink.setVisible(false); + } + } + } } @@ -366,7 +375,7 @@ public class HomeView extends Composite { setCheckedCheckboxSelectAll(false); setVisibleUpdateStatusAction(false); setVisiblePermanentlyDelete(false); - setStatusOptions(); + setStatusOptions(itemStatus); confirmPanelContainer.clear(); if (itemStatus.equals(ItemStatus.REJECTED)) setVisiblePermanentlyDelete(true); diff --git a/src/main/java/org/gcube/portlets/widgets/ckancontentmoderator/server/CkanContentModeratorServiceImpl.java b/src/main/java/org/gcube/portlets/widgets/ckancontentmoderator/server/CkanContentModeratorServiceImpl.java index b423f37..250c363 100644 --- a/src/main/java/org/gcube/portlets/widgets/ckancontentmoderator/server/CkanContentModeratorServiceImpl.java +++ b/src/main/java/org/gcube/portlets/widgets/ckancontentmoderator/server/CkanContentModeratorServiceImpl.java @@ -10,7 +10,9 @@ import org.gcube.datacatalogue.utillibrary.server.cms.CatalogueContentModeratorS import org.gcube.datacatalogue.utillibrary.shared.ItemStatus; import org.gcube.datacatalogue.utillibrary.shared.jackan.model.CkanDataset; import org.gcube.portlets.widgets.ckancontentmoderator.client.CkanContentModeratorService; +import org.gcube.portlets.widgets.ckancontentmoderator.shared.CMSUserRole; import org.gcube.portlets.widgets.ckancontentmoderator.shared.CatalogueDataset; +import org.gcube.portlets.widgets.ckancontentmoderator.shared.ModerationUserRole; import org.gcube.portlets.widgets.ckancontentmoderator.shared.OperationReport; import org.gcube.portlets.widgets.ckancontentmoderator.shared.SearchedData; import org.gcube.vomanagement.usermanagement.model.GCubeUser; @@ -52,15 +54,34 @@ public class CkanContentModeratorServiceImpl extends RemoteServiceServlet implem return isContentModeratorEnabled; } + + /** + * Gets the CMS roles for user in the context. + * + * @return the CMS roles for user in the context + * @throws Exception the exception + */ + @Override + public ModerationUserRole getCMSRolesForUserInTheContext() throws Exception { + LOG.info("called getCMSRolesForUserInTheContext"); + setContexts(); + List roles = GcubeContextUtil.getCMSRoleForUserInTheScope(getThreadLocalRequest()); + + GcubeContextUtil.getCurrentUser(getThreadLocalRequest()); + ModerationUserRole userRole = new ModerationUserRole(getServletInfo(), roles); + LOG.info("return: "+userRole); + return userRole; + } + /** * Sets the contexts. * * @return the scope operating in */ private String setContexts() { - String scope = WsUtil.getCurrentScope(this.getThreadLocalRequest()); - GCubeUser user = WsUtil.getCurrentUser(this.getThreadLocalRequest()); - String token = WsUtil.getCurrentToken(scope, user.getUsername()); + String scope = GcubeContextUtil.getCurrentScope(this.getThreadLocalRequest()); + GCubeUser user = GcubeContextUtil.getCurrentUser(this.getThreadLocalRequest()); + String token = GcubeContextUtil.getCurrentToken(scope, user.getUsername()); ScopeProvider.instance.set(scope); SecurityTokenProvider.instance.set(token); return scope; @@ -107,7 +128,6 @@ public class CkanContentModeratorServiceImpl extends RemoteServiceServlet implem LOG.info("returning " + datasetList.size() + " dataset"); return datasetList; } - /** * Sets the status. @@ -115,7 +135,7 @@ public class CkanContentModeratorServiceImpl extends RemoteServiceServlet implem * @param theStatus the the status * @param itemNames the item names * @return the operation report - * @throws Exception + * @throws Exception */ @Override public OperationReport setStatus(ItemStatus theStatus, List itemNames) throws Exception { @@ -125,26 +145,26 @@ public class CkanContentModeratorServiceImpl extends RemoteServiceServlet implem String scope = setContexts(); DataCatalogueImpl catalogueImpl = CatalogueCMSFactory.getFactory().getCatalogueImplPerScope(scope); List errorListItems = new ArrayList(); - List approvedListItems = new ArrayList(); + List changedStatusListItems = new ArrayList(); for (String itemName : itemNames) { try { catalogueImpl.refreshDataset(itemName); - approvedListItems.add(itemName); + changedStatusListItems.add(itemName); } catch (Exception e) { LOG.warn("Error when setting status (updating) the itemName: " + itemName, e); errorListItems.add(itemName); } } - return new OperationReport(theStatus.getLabel(), errorListItems, approvedListItems); + return new OperationReport(theStatus.getLabel(), errorListItems, changedStatusListItems); } catch (Exception e) { LOG.error(e.getMessage(), e); - throw new Exception("Error occurred on updating the status for item/s: " + itemNames + ". Caused by: " + e.getMessage()); + throw new Exception("Error occurred on updating the status for item/s: " + itemNames + ". Caused by: " + + e.getMessage()); } } - /** * Approve item. * diff --git a/src/main/java/org/gcube/portlets/widgets/ckancontentmoderator/server/GcubeContextUtil.java b/src/main/java/org/gcube/portlets/widgets/ckancontentmoderator/server/GcubeContextUtil.java new file mode 100644 index 0000000..65c3f4b --- /dev/null +++ b/src/main/java/org/gcube/portlets/widgets/ckancontentmoderator/server/GcubeContextUtil.java @@ -0,0 +1,163 @@ +/** + * + */ +package org.gcube.portlets.widgets.ckancontentmoderator.server; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import javax.servlet.http.HttpServletRequest; + +import org.gcube.common.portal.PortalContext; +import org.gcube.portlets.widgets.ckancontentmoderator.shared.CMSUserRole; +import org.gcube.vomanagement.usermanagement.RoleManager; +import org.gcube.vomanagement.usermanagement.exception.GroupRetrievalFault; +import org.gcube.vomanagement.usermanagement.exception.UserRetrievalFault; +import org.gcube.vomanagement.usermanagement.impl.LiferayRoleManager; +import org.gcube.vomanagement.usermanagement.model.GCubeRole; +import org.gcube.vomanagement.usermanagement.model.GCubeUser; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.liferay.portal.service.UserLocalServiceUtil; + +/** + * The Class GcubeContextUtil. + * + * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it + * + * Feb 21, 2022 + */ +public class GcubeContextUtil { + + protected static Logger LOG = LoggerFactory.getLogger(GcubeContextUtil.class); + + /** + * Checks if is within portal. + * + * @return true if you're running into the portal, false if in development + */ + public static boolean isWithinPortal() { + try { + UserLocalServiceUtil.getService(); + return true; + } catch (Exception ex) { + LOG.trace("Development Mode ON"); + return false; + } + } + + /** + * Gets the current user. + * + * @param httpServletRequest the http servlet request + * @return the current user + */ + public static GCubeUser getCurrentUser(HttpServletRequest httpServletRequest) { + return PortalContext.getConfiguration().getCurrentUser(httpServletRequest); + } + + /** + * Gets the current scope. + * + * @param httpServletRequest the http servlet request + * @return the current scope + */ + public static String getCurrentScope(HttpServletRequest httpServletRequest) { + return PortalContext.getConfiguration().getCurrentScope(httpServletRequest); + } + + /** + * Gets the current token. + * + * @param scope the scope + * @param username the username + * @return the current token + */ + public static String getCurrentToken(String scope, String username) { + return PortalContext.getConfiguration().getCurrentUserToken(scope, username); + } + + /** + * Gets the current group id. + * + * @param httpServletRequest the http servlet request + * @return the current group id + */ + public static long getCurrentGroupId(HttpServletRequest httpServletRequest) { + return PortalContext.getConfiguration().getCurrentGroupId(httpServletRequest); + } + + /** + * Gets the CMS user role for the logged user (in the current context) + * + * @param httpServletRequest the http servlet request + * @return the scopes with user roles for logged user + * @throws Exception the exception + */ + public static List getCMSRoleForUserInTheScope(HttpServletRequest httpServletRequest) throws Exception { + LOG.info("called getScopesWithThreddsRolesForLoggedUser"); + + // DEV MODE + if (!isWithinPortal()) { + CMSUserRole userRole = CMSUserRole.CATALOGUE_MODERATOR; + LOG.warn("\n\n\nDevelopment MODE is enabled, returning CMS user role: "+userRole); + return Arrays.asList(userRole); + } + + GCubeUser user = null; + String scope = null; + Long groupId = null; + try { + + user = getCurrentUser(httpServletRequest); + scope = getCurrentScope(httpServletRequest); + groupId = getCurrentGroupId(httpServletRequest); + List cmsRoles = getCMSRoleForUserInTheGroupId(user, groupId); + LOG.info("in the context: " + scope + ", returning the role: " + cmsRoles + " for user " + + user.getUsername()); + return cmsRoles; + } catch (Exception e) { + String errorMsg = "An error occurred on checking user roles. Refresh the page and try again."; + LOG.error("An error occurred on checking user roles for user: " + user, e); + throw new Exception(errorMsg); + } + } + + /** + * Gets the CMS role for user in the scope. + * + * @param user the user + * @param groupId the group id + * @return the CMS role for user in the scope + */ + private static List getCMSRoleForUserInTheGroupId(GCubeUser user, Long groupId) { + if (user == null || groupId == null) { + LOG.warn("called getCMSRoleForUserInTheScope with invalid parameter user: " + user + ", in the groupId: " + + groupId, ", returning null"); + return null; + } + + LOG.info("called getCMSRoleForUserInTheScope user: " + user.getUsername() + ", in the groupId: " + groupId); + try { + RoleManager roleManager = new LiferayRoleManager(); + List roles = roleManager.listRolesByUserAndGroup(user.getUserId(), groupId); + List toReturn = new ArrayList(); + if (roles != null) { + for (GCubeRole gCubeRole : roles) { + if (gCubeRole.getRoleName().equalsIgnoreCase(CMSUserRole.CATALOGUE_MODERATOR.getRoleName())) { + toReturn.add(CMSUserRole.CATALOGUE_MODERATOR); + } + } + } + LOG.info("For user: " + user.getUsername() + " in the groupId: " + groupId + + " read and returing the role/s: " + toReturn); + return toReturn; + } catch (UserRetrievalFault | GroupRetrievalFault e) { + LOG.error("An error occurred during getVreRoleForUser: " + user, e); + return null; + } + } + +} diff --git a/src/main/java/org/gcube/portlets/widgets/ckancontentmoderator/server/WsUtil.java b/src/main/java/org/gcube/portlets/widgets/ckancontentmoderator/server/WsUtil.java deleted file mode 100644 index 0381160..0000000 --- a/src/main/java/org/gcube/portlets/widgets/ckancontentmoderator/server/WsUtil.java +++ /dev/null @@ -1,75 +0,0 @@ -/** - * - */ -package org.gcube.portlets.widgets.ckancontentmoderator.server; - -import javax.servlet.http.HttpServletRequest; - -import org.gcube.common.portal.PortalContext; -import org.gcube.vomanagement.usermanagement.model.GCubeUser; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.liferay.portal.service.UserLocalServiceUtil; - - -// TODO: Auto-generated Javadoc -/** - * The Class WsUtil. - * - * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it - * - * Jun 1, 2021 - */ -public class WsUtil { - - protected static Logger logger = LoggerFactory.getLogger(WsUtil.class); - - /** - * Checks if is within portal. - * - * @return true if you're running into the portal, false if in development - */ - public static boolean isWithinPortal() { - try { - UserLocalServiceUtil.getService(); - return true; - } catch (Exception ex) { - logger.trace("Development Mode ON"); - return false; - } - } - - /** - * Gets the current user. - * - * @param httpServletRequest the http servlet request - * @return the current user - */ - public static GCubeUser getCurrentUser(HttpServletRequest httpServletRequest){ - return PortalContext.getConfiguration().getCurrentUser(httpServletRequest); - } - - /** - * Gets the current scope. - * - * @param httpServletRequest the http servlet request - * @return the current scope - */ - public static String getCurrentScope(HttpServletRequest httpServletRequest){ - return PortalContext.getConfiguration().getCurrentScope(httpServletRequest); - } - - - /** - * Gets the current token. - * - * @param scope the scope - * @param username the username - * @return the current token - */ - public static String getCurrentToken(String scope, String username){ - return PortalContext.getConfiguration().getCurrentUserToken(scope,username); - } - -} diff --git a/src/main/java/org/gcube/portlets/widgets/ckancontentmoderator/shared/CMSUserRole.java b/src/main/java/org/gcube/portlets/widgets/ckancontentmoderator/shared/CMSUserRole.java new file mode 100644 index 0000000..0c7a1dd --- /dev/null +++ b/src/main/java/org/gcube/portlets/widgets/ckancontentmoderator/shared/CMSUserRole.java @@ -0,0 +1,17 @@ +package org.gcube.portlets.widgets.ckancontentmoderator.shared; + + public enum CMSUserRole { + + CATALOGUE_MODERATOR("Catalogue-Moderator"); + + private String name; + + private CMSUserRole(String name) { + this.name = name; + } + + public String getRoleName() { + return this.name; + } + +} diff --git a/src/main/java/org/gcube/portlets/widgets/ckancontentmoderator/shared/ModerationUserRole.java b/src/main/java/org/gcube/portlets/widgets/ckancontentmoderator/shared/ModerationUserRole.java new file mode 100644 index 0000000..a57983c --- /dev/null +++ b/src/main/java/org/gcube/portlets/widgets/ckancontentmoderator/shared/ModerationUserRole.java @@ -0,0 +1,57 @@ +package org.gcube.portlets.widgets.ckancontentmoderator.shared; + +import java.io.Serializable; +import java.util.List; + +/** + * The Class ModerationUserRole. + * + * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it + * + * Feb 21, 2022 + */ +public class ModerationUserRole implements Serializable { + + /** + * + */ + private static final long serialVersionUID = 7002466635359684148L; + private String username; + private List roles; + + /** + * Instantiates a new moderation user role. + */ + public ModerationUserRole() { + } + + public ModerationUserRole(String username, List roles) { + super(); + this.username = username; + this.roles = roles; + } + + public static long getSerialversionuid() { + return serialVersionUID; + } + + public String getUsername() { + return username; + } + + public List getRoles() { + return roles; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("ModerationUserRole [username="); + builder.append(username); + builder.append(", roles="); + builder.append(roles); + builder.append("]"); + return builder.toString(); + } + +} diff --git a/src/test/java/org/gcube/portlets/widgets/ckancontentmoderator/server/CkanContentModeratorServiceTest.java b/src/test/java/org/gcube/portlets/widgets/ckancontentmoderator/server/CkanContentModeratorServiceTest.java index a0bbfd5..1c87a34 100644 --- a/src/test/java/org/gcube/portlets/widgets/ckancontentmoderator/server/CkanContentModeratorServiceTest.java +++ b/src/test/java/org/gcube/portlets/widgets/ckancontentmoderator/server/CkanContentModeratorServiceTest.java @@ -14,17 +14,10 @@ import org.slf4j.LoggerFactory; public class CkanContentModeratorServiceTest { - //devVRE //private String scope = "/gcube/devsec/devVRE"; - //Dorne private String scope = "/pred4s/preprod/Dorne"; - private String testUser = "francesco.mangiacrapa"; - //devVRE - //private String authorizationToken = "8e74a17c-92f1-405a-b591-3a6090066248-98187548"; - //Dorne private String authorizationToken = ""; - //debd432e-de72-4e1c-960c-fd2af916c5e8-980114272 Dorne private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(CkanContentModeratorServiceTest.class); @@ -33,7 +26,7 @@ public class CkanContentModeratorServiceTest { fail("Not yet implemented"); } - @Test + //@Test public void loadItemsForStatus() { ScopeProvider.instance.set(scope); SecurityTokenProvider.instance.set(authorizationToken);