Fixed Get Info for shared Folder

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/widgets/workspace-sharing-widget@179445 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Giancarlo Panichi 2019-05-16 10:56:38 +00:00
parent c82d433772
commit 66b42cab91
3 changed files with 254 additions and 104 deletions

View File

@ -550,7 +550,7 @@ public class GWTWorkspaceSharingBuilder {
}
/**
* Gets the workspace acl from ac ls.
* Gets the workspace acl from list of acl.
*
* @param listACL
* the types
@ -568,8 +568,9 @@ public class GWTWorkspaceSharingBuilder {
switch (accessType) {
case ADMINISTRATOR:
// acls.add(new WorkspaceACL(acl.toString(),
// ACL_TYPE.ADMINISTRATOR, "Admin", false,
// USER_TYPE.ADMINISTRATOR, ""));
// ACL_TYPE.ADMINISTRATOR, "Administrator", false,
// USER_TYPE.ADMINISTRATOR, "Users are administrator and can
// update/delete any file"));
break;
case READ_ONLY:
acls.add(new WorkspaceACL(acl.toString(), ACL_TYPE.READ_ONLY, "Read Only", false, USER_TYPE.OTHER,
@ -600,6 +601,54 @@ public class GWTWorkspaceSharingBuilder {
}
/**
* Gets the workspace acl from list of acl.
*
* @param acl
* the types
* @return the workspace acl from acl
* @throws Exception
* the exception
*/
public List<WorkspaceACL> getWorkspaceACLForUser(ACL acl) throws Exception {
List<WorkspaceACL> acls = new ArrayList<WorkspaceACL>();
List<AccessType> listAccessType = acl.getAccessTypes();
for (AccessType accessType : listAccessType) {
switch (accessType) {
case ADMINISTRATOR:
acls.add(new WorkspaceACL(acl.toString(), ACL_TYPE.ADMINISTRATOR, "Admin", false,
USER_TYPE.ADMINISTRATOR, "Users are administrator and can update/delete any file"));
break;
case READ_ONLY:
acls.add(new WorkspaceACL(acl.toString(), ACL_TYPE.READ_ONLY, "Read Only", false, USER_TYPE.OTHER,
"Users can read any file but cannot update/delete"));
break;
case WRITE_OWNER:
acls.add(new WorkspaceACL(acl.toString(), ACL_TYPE.WRITE_OWNER, "Write Own", true, USER_TYPE.OTHER,
"Users can update/delete only their files"));
break;
case WRITE_ALL:
acls.add(new WorkspaceACL(acl.toString(), ACL_TYPE.WRITE_ANY, "Write Any", false, USER_TYPE.OTHER,
"Any user can update/delete any file"));
break;
default:
// acls.add(new WorkspaceACL(acl.toString(),
// ACL_TYPE.WRITE_OWNER,
// acl.toString(), false, USER_TYPE.OTHER, ""));
break;
}
}
if (acls.size() == 0)
throw new Exception("No ACLs rules found!");
return acls;
}
/**
* Gets the formatted html acl from ac ls.
*

View File

@ -11,10 +11,12 @@ import javax.servlet.http.HttpServletRequest;
import org.gcube.common.homelibary.model.items.type.WorkspaceItemType;
import org.gcube.common.homelibrary.home.HomeLibrary;
import org.gcube.common.homelibrary.home.exceptions.HomeNotFoundException;
import org.gcube.common.homelibrary.home.workspace.Workspace;
import org.gcube.common.homelibrary.home.workspace.WorkspaceItem;
import org.gcube.common.homelibrary.home.workspace.WorkspaceSharedFolder;
import org.gcube.common.homelibrary.home.workspace.accessmanager.ACLType;
import org.gcube.common.portal.PortalContext;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.storagehub.client.dsl.FileContainer;
import org.gcube.common.storagehub.client.dsl.FolderContainer;
@ -47,6 +49,11 @@ import org.gcube.portlets.widgets.workspacesharingwidget.shared.InfoContactModel
import org.gcube.portlets.widgets.workspacesharingwidget.shared.ReportAssignmentACL;
import org.gcube.portlets.widgets.workspacesharingwidget.shared.SessionExpiredException;
import org.gcube.portlets.widgets.workspacesharingwidget.shared.WorkspaceACL;
import org.gcube.vomanagement.usermanagement.GroupManager;
import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager;
import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager;
import org.gcube.vomanagement.usermanagement.model.GCubeGroup;
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -93,6 +100,36 @@ public class WorkspaceSharingServiceImpl extends RemoteServiceServlet implements
return !WsUtil.isWithinPortal(); // IS NOT INTO PORTAL
}
private Workspace getWorkspace(HttpServletRequest httpServletRequest)
throws org.gcube.common.homelibrary.home.workspace.exceptions.WorkspaceFolderNotFoundException,
org.gcube.common.homelibrary.home.exceptions.InternalErrorException, HomeNotFoundException {
logger.trace("Get Workspace");
PortalContext pContext = PortalContext.getConfiguration();
// USER
GCubeUser user = pContext.getCurrentUser(httpServletRequest);
String username = user.getUsername();
String fullName = user.getFullname();
String email = user.getEmail();
String avatarID = user.getUserAvatarId();
String avatarURL = user.getUserAvatarURL();
// SESSION
String currentScope = pContext.getCurrentScope(httpServletRequest);
String userToken = pContext.getCurrentUserToken(httpServletRequest);
long currGroupId = pContext.getCurrentGroupId(httpServletRequest);
PortalContextInfo info = new PortalContextInfo(username, fullName, email, avatarID, avatarURL, currentScope,
userToken, currGroupId);
logger.trace("PortalContextInfo: " + info);
ScopeProvider.instance.set(info.getCurrentScope());
logger.trace("Scope provider instancied");
Workspace workspace = HomeLibrary.getUserWorkspace(info.getUsername());
return workspace;
}
/*
* (non-Javadoc)
*
@ -655,68 +692,94 @@ public class WorkspaceSharingServiceImpl extends RemoteServiceServlet implements
try {
logger.info("Call addAdministratorsByFolderId(): [itemId=" + itemId + ", listContactLogins"
+ listContactLogins + "]");
logger.info("This function is not available in StorageHub!");
return false;
// if(folderId==null || listContactLogins==null ||
// listContactLogins.size()==0)
// return false;
// Workspace workspace = getWorkspace();
// WorkspaceItem item = workspace.getItem(folderId);
// workspaceLogger.info("Adding administator/s to folder:
// "+folderId);
//
// if(item!=null &&
// item.getType().equals(WorkspaceItemType.SHARED_FOLDER)){
// WorkspaceSharedFolder sharedFolder = (WorkspaceSharedFolder)
// item;
//
// //retrieving old administrators list
// GWTWorkspaceBuilder builder = getGWTWorkspaceBuilder();
// workspaceLogger.info("Retrieving old administator/s..");
// List<InfoContactModel> oldAdmins =
// builder.buildGxtInfoContactsFromPortalLogins(sharedFolder.getAdministrators());
// workspaceLogger.info("Retrieving old administator/s are/is:
// "+oldAdmins.size());
//
// workspaceLogger.info("Setting administators:");
// printList(listContactLogins);
// sharedFolder.setAdmins(listContactLogins);
//
// workspaceLogger.info("Converting new administator/s..");
// List<InfoContactModel> newAdmins =
// builder.buildGxtInfoContactsFromPortalLogins(listContactLogins);
// NotificationsProducer np = getNotificationProducer();
//
// workspaceLogger.info("Sending notifications downgrade/upgrade
// administator/s..");
// DifferenceBetweenInfoContactModel diff1 = new
// DifferenceBetweenInfoContactModel(oldAdmins, newAdmins);
// List<InfoContactModel> contactsDowngrade =
// diff1.getDifferentsContacts();
//
// for (InfoContactModel infoContactModel : contactsDowngrade) {
// np.notifyAdministratorDowngrade(infoContactModel, sharedFolder);
// }
//
// DifferenceBetweenInfoContactModel diff2 = new
// DifferenceBetweenInfoContactModel(newAdmins, oldAdmins);
// List<InfoContactModel> contactsUpgrade =
// diff2.getDifferentsContacts();
//
// for (InfoContactModel infoContactModel : contactsUpgrade) {
// np.notifyAdministratorUpgrade(infoContactModel, sharedFolder);
// }
//
// return true;
//
// }else
// throw new Exception("The item is null or not instanceof
// "+WorkspaceItemType.SHARED_FOLDER);
// logger.info("This function is not available in StorageHub!");
if (itemId == null || listContactLogins == null || listContactLogins.size() == 0)
return false;
StorageHubClient shc = new StorageHubClient();
OpenResolver openResolverForItem = shc.open(itemId);
FolderContainer folderContainer = openResolverForItem.asFolder();
FolderItem folderItem = folderContainer.get();
if (folderItem.isShared()) {
if (folderItem instanceof SharedFolder) {
return addAdminstratorByWorkspace(folderItem.getId(), listContactLogins);
} else {
String error = "Attention, the set administrators operation can only be done on the root shared folder. "
+ "Please, select the root shared folder if you want to set the administrators.";
logger.error(error);
throw new Exception(error);
}
} else {
logger.error("The item requested is not a valid shared folder : [itemId=" + itemId + "]");
throw new Exception("The item requested is not a valid shared folder. Impossible set the administrators.");
}
} catch (Exception e) {
logger.error("Error in addAdministratorsByFolderId(): " + e.getLocalizedMessage(), e);
String error = "Attention, administrators can not be added by StorageHub.";
throw new Exception(error);
throw e;
}
}
private boolean addAdminstratorByWorkspace(String itemId, List<String> listContactLogins) throws Exception {
try {
Workspace workspace = getWorkspace(this.getThreadLocalRequest());
WorkspaceItem item = workspace.getItem(itemId);
logger.info("Adding administator/s to folder: " + itemId);
if (item != null && item.getType().equals(WorkspaceItemType.SHARED_FOLDER)) {
WorkspaceSharedFolder sharedFolder = (WorkspaceSharedFolder) item;
// retrieving old administrators list
// GWTWorkspaceSharingBuilder builder = new
// GWTWorkspaceSharingBuilder();
// logger.info("Retrieving old administator/s..");
// List<InfoContactModel> oldAdmins = builder
// .buildGxtInfoContactsFromPortalLogins(sharedFolder.getAdministrators());
// logger.info("Retrieving old administator/s are/is:" +
// oldAdmins.size());
logger.info("Setting administators:");
sharedFolder.setAdmins(listContactLogins);
/*
* logger.info("Converting new administator/s..");
* List<InfoContactModel> newAdmins =
* builder.buildGxtInfoContactsFromPortalLogins(
* listContactLogins); NotificationsProducer np =
* getNotificationProducer();
*
* logger.
* info("Sending notifications downgrade/upgrade administator/s.."
* ); DifferenceBetweenInfoContactModel diff1 = new
* DifferenceBetweenInfoContactModel(oldAdmins, newAdmins);
* List<InfoContactModel> contactsDowngrade =
* diff1.getDifferentsContacts();
*
* for (InfoContactModel infoContactModel : contactsDowngrade) {
* np.notifyAdministratorDowngrade(infoContactModel,
* sharedFolder); }
*
* DifferenceBetweenInfoContactModel diff2 = new
* DifferenceBetweenInfoContactModel(newAdmins, oldAdmins);
* List<InfoContactModel> contactsUpgrade =
* diff2.getDifferentsContacts();
*
* for (InfoContactModel infoContactModel : contactsUpgrade) {
* np.notifyAdministratorUpgrade(infoContactModel,
* sharedFolder); }
*/
return true;
} else
throw new Exception("The item is null or not instanceof " + WorkspaceItemType.SHARED_FOLDER);
} catch (Exception e) {
logger.error("Error in addAdminstratorByWorkspace() : " + e.getLocalizedMessage(), e);
throw new Exception("Error in set administrators operation for this item.");
}
}
@ -749,38 +812,6 @@ public class WorkspaceSharingServiceImpl extends RemoteServiceServlet implements
}
}
// /**
// * DEBUG.
// *
// * @return the fake groups
// */
// private List<InfoContactModel> getFakeGroups() {
//
// List<InfoContactModel> listContactsModel = new
// ArrayList<InfoContactModel>();
//
// PortalContext context = PortalContext.getConfiguration();
// System.out.println("context.getInfrastructureName(): " +
// context.getInfrastructureName());
// // System.out.println("context.getVOsAsString():
// // "+context.getVOsAsString());
// List<VO> vos =
// WsUtil.getVresFromInfrastructure(context.getInfrastructureName(),
// "devsec");
//
// for (VO vo : vos) {
// System.out.println("vo name " + vo.getName());
//
// for (VRE vre : vo.getVres()) {
// System.out.println("vre name " + vre.getName());
// listContactsModel.add(new InfoContactModel(vre.getName(), vre.getName(),
// vre.getName(), true));
// }
// }
//
// return listContactsModel;
// }
/*
* (non-Javadoc)
*
@ -1086,7 +1117,7 @@ public class WorkspaceSharingServiceImpl extends RemoteServiceServlet implements
if (folderItem.isShared()) {
if (folderItem instanceof SharedFolder) {
logger.debug("Is a shared folder search ACL in it");
List<WorkspaceACL> acls = retrieveACLsFromFolderContainer(folderContainer);
List<WorkspaceACL> acls = retrieveUserACLsFromFolderContainer(folderContainer);
List<ExtendedWorkspaceACL> listEACL = new ArrayList<ExtendedWorkspaceACL>(acls.size());
for (WorkspaceACL workspaceACL : acls) {
boolean isBaseSharedFolder = true;
@ -1103,7 +1134,7 @@ public class WorkspaceSharingServiceImpl extends RemoteServiceServlet implements
FolderContainer rootSharedFolderContainer = folderContainer.getRootSharedFolder();
FolderItem rootSharedFolder = rootSharedFolderContainer.get();
if (rootSharedFolder instanceof SharedFolder) {
List<WorkspaceACL> acls = retrieveACLsFromFolderContainer(rootSharedFolderContainer);
List<WorkspaceACL> acls = retrieveUserACLsFromFolderContainer(rootSharedFolderContainer);
List<ExtendedWorkspaceACL> listEACL = new ArrayList<ExtendedWorkspaceACL>(acls.size());
for (WorkspaceACL workspaceACL : acls) {
boolean isBaseSharedFolder = true;
@ -1133,18 +1164,87 @@ public class WorkspaceSharingServiceImpl extends RemoteServiceServlet implements
}
}
private List<WorkspaceACL> retrieveACLsFromFolderContainer(FolderContainer folderContainer) throws Exception {
// TODO
private List<WorkspaceACL> retrieveUserACLsFromFolderContainer(FolderContainer folderContainer) throws Exception {
String currentUser = getMyLogin();
logger.debug("Current User: " + currentUser);
List<ACL> acls = folderContainer.getAcls();
logger.debug("Retrieved acls: " + acls);
GWTWorkspaceSharingBuilder builder = new GWTWorkspaceSharingBuilder();
List<WorkspaceACL> wsAcls = builder.getWorkspaceACLFromACLs(acls);
if (wsAcls == null || wsAcls.isEmpty()) {
logger.info("Converted ACLBySharedFolderId is null or empty, returning null");
return null;
ACL currentUserACL = null;
for (ACL acl : acls) {
if (acl.getPricipal().compareTo(currentUser) == 0) {
currentUserACL = acl;
break;
}
}
logger.info("Returning first acl with id: " + wsAcls.get(0).getId());
return wsAcls;
List<WorkspaceACL> wsAcls = null;
if (currentUserACL == null) {
List<GCubeGroup> listVRE = retrieveCurrentUserACLFromGroup(currentUser);
int i = 0;
while (currentUserACL == null && i < listVRE.size()) {
GCubeGroup vre = listVRE.get(i);
String vreName = vre.getGroupName();
for (ACL acl : acls) {
String principal = acl.getPricipal();
String rootVO = PortalContext.getConfiguration().getInfrastructureName();
if (principal.startsWith(rootVO)) {
int voIndex = principal.indexOf("-");
if (voIndex != -1) {
String tempScope = principal.substring(voIndex + 1);
int vreIndex = tempScope.indexOf("-");
if (vreIndex != -1) {
tempScope = tempScope.substring(vreIndex + 1);
if (tempScope.compareTo(vreName) == 0) {
currentUserACL = acl;
break;
}
}
}
}
}
i++;
}
if (currentUserACL != null) {
GWTWorkspaceSharingBuilder builder = new GWTWorkspaceSharingBuilder();
wsAcls = builder.getWorkspaceACLForUser(currentUserACL);
}
} else {
GWTWorkspaceSharingBuilder builder = new GWTWorkspaceSharingBuilder();
wsAcls = builder.getWorkspaceACLForUser(currentUserACL);
}
if (wsAcls == null || wsAcls.isEmpty()) {
logger.info("List of ACL is null or empty, returning null");
return null;
} else {
logger.info("List of ACL is retrieved. Use first acl with id: " + wsAcls.get(0).getId());
return wsAcls;
}
}
private List<GCubeGroup> retrieveCurrentUserACLFromGroup(String userName) throws Exception {
try {
// Instanciate the manager
GroupManager groupManager = new LiferayGroupManager();
// retrieve the groups to whom a given user belongs (given the user
// identifier)
long userId;
userId = new LiferayUserManager().getUserId(userName);
List<GCubeGroup> listOfGroups = groupManager.listGroupsByUser(userId);
List<GCubeGroup> vres = new ArrayList<GCubeGroup>();
for (GCubeGroup g : listOfGroups) {
if (groupManager.isVRE(g.getGroupId()))
vres.add(g);
}
logger.debug("Current User VREs: " + vres);
return vres;
} catch (Throwable e) {
logger.error("Error retrieving User group: " + e.getLocalizedMessage(), e);
throw new Exception("Error retrieving User group: " + e.getLocalizedMessage(), e);
}
}
/**

View File

@ -10,6 +10,7 @@ package org.gcube.portlets.widgets.workspacesharingwidget.shared;
*/
public enum ACL_TYPE {
ADMINISTRATOR, // "Users are administrators"
READ_ONLY, // "Users can read any file but cannot update/delete"
WRITE_OWNER, //"Users can update/delete only their files"
WRITE_ANY; //"Any user can update/delete any file"