removed the parts of JCR and LDAP as they are now in the hook

master r4.19.0
Massimiliano Assante 4 years ago
parent fe554a8098
commit 0f34bff319

@ -11,7 +11,7 @@
<groupId>org.gcube.portal</groupId>
<artifactId>remove-account-library</artifactId>
<version>1.0.0</version>
<version>1.2.0</version>
<name>Remove Account Library</name>
<packaging>jar</packaging>
@ -41,19 +41,6 @@
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.gcube.resources.discovery</groupId>
<artifactId>ic-client</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>storagehub-client-library</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-encryption</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gcube.dvos</groupId>
<artifactId>usermanagement-core</artifactId>

@ -1,7 +1,5 @@
package org.gcube.portal.removeaccount;
import org.gcube.portal.removeaccount.thread.RemovedUserAccountThread;
import org.gcube.portal.removeaccount.thread.RemovedUserFromLDAPThread;
import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager;
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
import org.slf4j.Logger;
@ -32,16 +30,13 @@ public class D4ScienceRemoveAccountManager {
e.printStackTrace();
return false;
}
_log.info("Trying to remove user from LDAP ...");
Thread removeFromLDAPThread = new Thread(new RemovedUserFromLDAPThread(username2Delete));
removeFromLDAPThread.start();
_log.info("Trying to remove user from Liferay DB and JCR and notify infra-managers ...");
Thread emailManagersThread = new Thread(new RemovedUserAccountThread(
_log.info("Trying to remove user from Liferay DB and notify infra-managers ...");
RemovedUserAccount remove = new RemovedUserAccount(
user.getUserId(),
user.getUsername(),
user.getFullname(),
user.getEmail()));
emailManagersThread.start();
user.getEmail());
remove.doUserRemoval();
return true;
}
}

@ -1,4 +1,4 @@
package org.gcube.portal.removeaccount.thread;
package org.gcube.portal.removeaccount;
import java.util.List;
@ -28,9 +28,9 @@ import com.liferay.portal.util.PortalUtil;
* @author Massimiliano Assante ISTI-CNR
*
*/
public class RemovedUserAccountThread implements Runnable {
public class RemovedUserAccount {
private static final Logger _log = LoggerFactory.getLogger(RemovedUserAccountThread.class);
private static final Logger _log = LoggerFactory.getLogger(RemovedUserAccount.class);
final String SUBJECT = "User account REMOVAL notification";
@ -41,7 +41,7 @@ public class RemovedUserAccountThread implements Runnable {
private GroupManager gm;
private UserManager uMan;
public RemovedUserAccountThread(long userId,String userName, String fullName, String emailAddress) {
public RemovedUserAccount(long userId,String userName, String fullName, String emailAddress) {
super();
this.userId = userId;
this.userName = userName;
@ -51,21 +51,7 @@ public class RemovedUserAccountThread implements Runnable {
this.gm = new LiferayGroupManager();
}
@Override
public void run() {
try {
// _log.info("Trying to remove user " + userName + " from JCR first, using storageHub with role: "+Constants.AUTORISED_INFRA_ROLE);
// RemoveUserFromJCR rmJCR = new RemoveUserFromJCR(userName, gm, uMan);
// boolean result = rmJCR.remove();
// _log.info("The user " + userName + " has been removed from JCR with success? " + result);
} catch (Exception e) {
_log.error("An error occurred during user workspace removal: ", e);
}
handleUserRemoval(userId, userName, fullName, emailAddress);
}
private void handleUserRemoval(long userId, String userName, String fullName, String emailAddress) {
protected void doUserRemoval() {
_log.info("trying removeUser account for " + userName);
//first remove the account
try {

@ -1,58 +0,0 @@
package org.gcube.portal.removeaccount.thread;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
/**
*
* @author Massimiliano Assante ISTI-CNR
*
*/
public class RemoveUserFromJCR {
private static Log _log = LogFactoryUtil.getLog(RemoveUserFromJCR.class);
// private String username2Delete;
// private GroupManager gm;
// private UserManager uMan;
//
// public RemoveUserFromJCR(String username2Delete, GroupManager gm, UserManager uMan) {
// this.username2Delete = username2Delete;
// this.gm = gm;
// this.uMan = uMan;
// }
//
// public boolean remove() {
// try {
// //get the super user
// String infraContext = "/"+PortalContext.getConfiguration().getInfrastructureName();
// long groupId = gm.getGroupIdFromInfrastructureScope(infraContext);
// RoleManager rm = new LiferayRoleManager();
// 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);
// UserManagerClient userClient = AbstractPlugin.users().build();
// userClient.removeUser(username2Delete);
// return true;
// }
// } catch (Exception e) {
// _log.error("Could not delete " + username2Delete + " from JCR ", e);
// return false;
// }
// }
}

@ -1,153 +0,0 @@
package org.gcube.portal.removeaccount.thread;
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
import java.util.List;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameNotFoundException;
import javax.naming.NamingException;
import org.gcube.common.encryption.encrypter.StringEncrypter;
import org.gcube.common.portal.PortalContext;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.Property;
import org.gcube.common.resources.gcore.utils.Group;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
/**
*
* @author Massimiliano Assante ISTI-CNR
*
*/
public class RemovedUserFromLDAPThread implements Runnable {
private static Log _log = LogFactoryUtil.getLog(RemovedUserFromLDAPThread.class);
private static final String LDAP_SERVER_NAME = "LDAPServer";
private static final String LDAP_SERVER_FILTER_NAME = "filter";
private static final String LDAP_SERVER_PRINCPAL_NAME = "ldapPrincipal";
private static final String USER_CONTEXT = ",ou=People,o=D4Science,ou=Organizations,dc=d4science,dc=org";
private String portalName;
private String ldapUrl;
private String principal;
private String ldapPassword;
private String username2Delete;
public RemovedUserFromLDAPThread(String username2Delete) {
this.username2Delete = username2Delete;
}
@SuppressWarnings("deprecation")
@Override
public void run() {
portalName = PortalContext.getPortalInstanceName();
PortalContext context = PortalContext.getConfiguration();
String scope = "/" + context.getInfrastructureName();
ScopeProvider.instance.set(scope);
SimpleQuery query = queryFor(ServiceEndpoint.class);
query.addCondition("$resource/Profile/Category/text() eq 'Portal'");
query.addCondition("$resource/Profile/Name/text() eq '" + portalName + "'");
DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
List<ServiceEndpoint> list = client.submit(query);
if (list == null || list.isEmpty()) {
_log.error("Could not find any Service endpoint registred in the infrastructure for this portal: " + portalName);
}
else if (list.size() > 1) {
_log.warn("Found more than one Service endpoint registred in the infrastructure for this portal: " + portalName);
}
else {
for (ServiceEndpoint res : list) {
Group<AccessPoint> apGroup = res.profile().accessPoints();
AccessPoint[] accessPoints = (AccessPoint[]) apGroup.toArray(new AccessPoint[apGroup.size()]);
for (int i = 0; i < accessPoints.length; i++) {
if (accessPoints[i].name().compareTo(LDAP_SERVER_NAME) == 0) {
_log.info("Found credentials for " + LDAP_SERVER_NAME);
AccessPoint found = accessPoints[i];
ldapUrl = found.address();
String encrPassword = found.password();
try {
ldapPassword = StringEncrypter.getEncrypter().decrypt( encrPassword);
} catch (Exception e) {
_log.error("Something went wrong while decrypting password for " + LDAP_SERVER_NAME);
e.printStackTrace();
}
Group<Property> propGroup = found.properties();
Property[] props = (Property[]) propGroup.toArray(new Property[propGroup.size()]);
for (int j = 0; j < props.length; j++) {
if (props[j].name().compareTo(LDAP_SERVER_PRINCPAL_NAME) == 0) {
_log.info("\tFound properties of " + LDAP_SERVER_PRINCPAL_NAME);
String encrValue = props[j].value();
try {
principal = StringEncrypter.getEncrypter().decrypt(encrValue);
} catch (Exception e) {
_log.error("Something went wrong while decrypting value for " + LDAP_SERVER_PRINCPAL_NAME);
e.printStackTrace();
}
}
}
}
}
}
_log.debug("Got LDAP connection info from IS Resource ...");
/*************** */
_log.debug("Initializing LDAP connection ...");
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, ldapUrl);
env.put(Context.SECURITY_PRINCIPAL, principal);
env.put(Context.SECURITY_CREDENTIALS, ldapPassword);
try {
Context ctx = new InitialContext(env);
String userCtx2Delete = getSubContext(username2Delete);
// Remove the binding
_log.debug("***** trying delete userCtx=" + userCtx2Delete);
ctx.unbind(userCtx2Delete);
// Check that it is gone
Object obj = null;
try {
obj = ctx.lookup(userCtx2Delete);
} catch (NameNotFoundException ne) {
_log.info("unbind successful for "+userCtx2Delete);
return;
}
_log.error("unbind failed; object still there: " + obj);
// Close the context when we're done
ctx.close();
} catch (NamingException e) {
_log.error("Something went Wrong during LDAP remove user");
e.printStackTrace();
} catch (Exception es) {
_log.error("Something went Wrong during LDAP remove user in retrieving Liferay Organization");
es.printStackTrace();
}
}
}
/**
*
* @param username
* @return the single user subContext
*/
private String getSubContext(String username) {
return "uid="+username+USER_CONTEXT;
}
}