fixed bug that could ask liferay db data after the user was cancelled

This commit is contained in:
Massimiliano Assante 2020-01-30 12:58:46 +01:00
parent 6e7329dbd7
commit a5cac5b109
3 changed files with 76 additions and 75 deletions

View File

@ -2,19 +2,10 @@ package org.gcube.portal.removeaccount.thread;
import static org.gcube.common.authorization.client.Constants.authorizationService; import static org.gcube.common.authorization.client.Constants.authorizationService;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.portal.PortalContext;
import org.gcube.common.storagehub.client.dsl.StorageHubClient; import org.gcube.common.storagehub.client.dsl.StorageHubClient;
import org.gcube.portal.usersaccount.Constants;
import org.gcube.vomanagement.usermanagement.GroupManager;
import org.gcube.vomanagement.usermanagement.RoleManager;
import org.gcube.vomanagement.usermanagement.UserManager;
import org.gcube.vomanagement.usermanagement.impl.LiferayRoleManager;
import org.gcube.vomanagement.usermanagement.model.GCubeRole;
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
import com.liferay.portal.kernel.log.Log; import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil; import com.liferay.portal.kernel.log.LogFactoryUtil;
@ -28,46 +19,27 @@ public class RemoveUserFromJCR {
private static Log _log = LogFactoryUtil.getLog(RemoveUserFromJCR.class); private static Log _log = LogFactoryUtil.getLog(RemoveUserFromJCR.class);
private String username2Delete; private String username2Delete;
private GroupManager gm; private String theAdminToken;
private UserManager uMan; private List<String> theAdminRolesString;
private RoleManager rm;
public RemoveUserFromJCR(String username2Delete, GroupManager gm, UserManager uMan) {
this.username2Delete = username2Delete; public RemoveUserFromJCR(String userNameToDelete, String theAdminToken, List<String> theAdminRolesString) {
this.gm = gm; super();
this.uMan = uMan; this.username2Delete = userNameToDelete;
this.rm = new LiferayRoleManager(); this.theAdminRolesString = theAdminRolesString;
this.theAdminToken = theAdminToken;
} }
public boolean remove() { public boolean remove() {
try { try {
_log.debug("in RemoveUserFromJCR remove() for " + username2Delete ); authorizationService().setTokenRoles(theAdminToken, theAdminRolesString);
//get the super user
String infraContext = "/"+PortalContext.getConfiguration().getInfrastructureName();
long groupId = gm.getGroupIdFromInfrastructureScope(infraContext);
long roleId = rm.getRoleId(Constants.AUTORISED_INFRA_ROLE, groupId);
List<GCubeUser> users = uMan.listUsersByGroupAndRole(groupId, roleId);
if (users.isEmpty()) {
_log.error("Cannot delete the user: there is no user having role " + Constants.AUTORISED_INFRA_ROLE + " on context: " + infraContext);
return false;
}
else {
GCubeUser theAdmin = users.get(0);
String adminUsername = theAdmin.getUsername();
String theAdminToken = PortalContext.getConfiguration().getCurrentUserToken(infraContext, adminUsername);
List<GCubeRole> theAdminRoles = rm.listRolesByUserAndGroup(theAdmin.getUserId(), groupId);
List<String> rolesString = new ArrayList<String>();
for (GCubeRole gCubeRole : theAdminRoles) {
rolesString.add(gCubeRole.getRoleName());
}
authorizationService().setTokenRoles(theAdminToken, rolesString);
SecurityTokenProvider.instance.set(theAdminToken); SecurityTokenProvider.instance.set(theAdminToken);
_log.debug("Autorising drop workspace with infra manager token of " + theAdminToken); _log.debug("Autorising drop workspace with infra manager token of " + theAdminToken);
StorageHubClient shc = new StorageHubClient(); StorageHubClient shc = new StorageHubClient();
_log.debug("BEFORE stohub.deleteUserAccount " + username2Delete); _log.debug("BEFORE stohub.deleteUserAccount " + username2Delete);
shc.deleteUserAccount(username2Delete); shc.deleteUserAccount(username2Delete);
return true; return true;
}
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
_log.error("Could not delete " + username2Delete + " from JCR an error occurred on the service"); _log.error("Could not delete " + username2Delete + " from JCR an error occurred on the service");

View File

@ -2,27 +2,10 @@ package org.gcube.portal.removeaccount.thread;
import java.util.List; import java.util.List;
import javax.portlet.PortletPreferences;
import org.gcube.common.portal.PortalContext;
import org.gcube.common.portal.mailing.EmailNotification;
import org.gcube.portal.usersaccount.Constants; import org.gcube.portal.usersaccount.Constants;
import org.gcube.vomanagement.usermanagement.GroupManager;
import org.gcube.vomanagement.usermanagement.RoleManager;
import org.gcube.vomanagement.usermanagement.UserManager;
import org.gcube.vomanagement.usermanagement.exception.RoleRetrievalFault;
import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager;
import org.gcube.vomanagement.usermanagement.impl.LiferayRoleManager;
import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager;
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
import org.gcube.vomanagement.usermanagement.model.GatewayRolesNames;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.liferay.portal.service.PortalPreferencesLocalServiceUtil;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.util.PortalUtil;
/** /**
* *
@ -35,24 +18,26 @@ public class RemovedUserAccountThread implements Runnable {
final String SUBJECT = "Removed account notification"; final String SUBJECT = "Removed account notification";
private String userName; private String userNameToDelete;
private GroupManager gm; private String theAdminToken;
private UserManager uMan; private List<String> theAdminRolesString;
public RemovedUserAccountThread(long userId,String userName) {
public RemovedUserAccountThread(String userNameToDelete, String theAdminToken, List<String> theAdminRolesString) {
super(); super();
this.userName = userName; this.userNameToDelete = userNameToDelete;
this.uMan = new LiferayUserManager(); this.theAdminRolesString = theAdminRolesString;
this.gm = new LiferayGroupManager(); this.theAdminToken = theAdminToken;
} }
@Override @Override
public void run() { public void run() {
try { try {
_log.info("Trying to remove user " + userName + " from JCR first, using storageHub with role: "+Constants.AUTORISED_INFRA_ROLE); _log.info("Trying to remove user " + userNameToDelete + " from JCR first, using storageHub with role: "+Constants.AUTORISED_INFRA_ROLE);
RemoveUserFromJCR rmJCR = new RemoveUserFromJCR(userName, gm, uMan); RemoveUserFromJCR rmJCR = new RemoveUserFromJCR(userNameToDelete, theAdminToken, theAdminRolesString);
boolean result = rmJCR.remove(); boolean result = rmJCR.remove();
_log.info("The user " + userName + " has been removed from JCR with success? " + result); _log.info("The user " + userNameToDelete + " has been removed from JCR with success? " + result);
} catch (Exception e) { } catch (Exception e) {
_log.error("An error occurred during user workspace removal: ", e); _log.error("An error occurred during user workspace removal: ", e);

View File

@ -1,9 +1,25 @@
package org.gcube.portal.usersaccount; package org.gcube.portal.usersaccount;
import static org.gcube.common.authorization.client.Constants.authorizationService;
import java.util.ArrayList;
import java.util.List;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.portal.PortalContext;
import org.gcube.common.storagehub.client.dsl.StorageHubClient;
import org.gcube.portal.notifications.thread.NewUserAccountNotificationThread; import org.gcube.portal.notifications.thread.NewUserAccountNotificationThread;
import org.gcube.portal.removeaccount.thread.RemoveUserTokenFromInfraThread; import org.gcube.portal.removeaccount.thread.RemoveUserTokenFromInfraThread;
import org.gcube.portal.removeaccount.thread.RemovedUserAccountThread; import org.gcube.portal.removeaccount.thread.RemovedUserAccountThread;
import org.gcube.portal.removeaccount.thread.RemovedUserFromLDAPThread; import org.gcube.portal.removeaccount.thread.RemovedUserFromLDAPThread;
import org.gcube.vomanagement.usermanagement.GroupManager;
import org.gcube.vomanagement.usermanagement.RoleManager;
import org.gcube.vomanagement.usermanagement.UserManager;
import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager;
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.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -23,6 +39,9 @@ import com.liferay.portal.model.User;
public class MyCreateUserAccountListener extends BaseModelListener<User> { public class MyCreateUserAccountListener extends BaseModelListener<User> {
private static final Logger _log = LoggerFactory.getLogger(MyCreateUserAccountListener.class); private static final Logger _log = LoggerFactory.getLogger(MyCreateUserAccountListener.class);
final String SUBJECT = "New user account notification"; final String SUBJECT = "New user account notification";
private GroupManager gm;
private UserManager uMan;
private RoleManager rm;
@Override @Override
public void onAfterCreate(User user) throws ModelListenerException { public void onAfterCreate(User user) throws ModelListenerException {
@ -36,12 +55,37 @@ public class MyCreateUserAccountListener extends BaseModelListener<User> {
@Override @Override
public void onBeforeRemove(User user) throws ModelListenerException { public void onBeforeRemove(User user) throws ModelListenerException {
gm = new LiferayGroupManager();
uMan = new LiferayUserManager();
rm = new LiferayRoleManager();
_log.info("onBeforeRemove userAccount listener for: " + user.getScreenName() + " / " + user.getFullName()); _log.info("onBeforeRemove userAccount listener for: " + user.getScreenName() + " / " + user.getFullName());
String username2Delete = user.getScreenName(); String username2Delete = user.getScreenName();
_log.info("Trying to remove user from JCR and not notify infra-managers ..."); _log.info("Trying to remove user from JCR and not notify infra-managers ...");
try { try {
Thread dropUserWorkspaceThread = new Thread(new RemovedUserAccountThread(user.getUserId(), username2Delete)); _log.debug("Getting super user with role {}", Constants.AUTORISED_INFRA_ROLE);
//get the super user
String infraContext = "/"+PortalContext.getConfiguration().getInfrastructureName();
long groupId = gm.getGroupIdFromInfrastructureScope(infraContext);
long roleId = rm.getRoleId(Constants.AUTORISED_INFRA_ROLE, groupId);
List<GCubeUser> users = uMan.listUsersByGroupAndRole(groupId, roleId);
if (users.isEmpty()) {
_log.error("Cannot delete the user: there is no user having role " + Constants.AUTORISED_INFRA_ROLE + " on context: " + infraContext);
return;
}
else {
GCubeUser theAdmin = users.get(0);
String adminUsername = theAdmin.getUsername();
String theAdminToken = PortalContext.getConfiguration().getCurrentUserToken(infraContext, adminUsername);
List<GCubeRole> theAdminRoles = rm.listRolesByUserAndGroup(theAdmin.getUserId(), groupId);
List<String> rolesString = new ArrayList<String>();
for (GCubeRole gCubeRole : theAdminRoles) {
rolesString.add(gCubeRole.getRoleName());
}
Thread dropUserWorkspaceThread = new Thread(new RemovedUserAccountThread(username2Delete, theAdminToken, rolesString));
dropUserWorkspaceThread.start(); dropUserWorkspaceThread.start();
}
_log.info("Trying to remove user from LDAP ..."); _log.info("Trying to remove user from LDAP ...");
Thread removeFromLDAPThread = new Thread(new RemovedUserFromLDAPThread(username2Delete)); Thread removeFromLDAPThread = new Thread(new RemovedUserFromLDAPThread(username2Delete));
removeFromLDAPThread.start(); removeFromLDAPThread.start();