diff --git a/pom.xml b/pom.xml index e5fa452..eefefde 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ org.gcube.portal.plugins VREFolder-hook VREFolder-hook Hook - 6.4.0-SNAPSHOT + 6.5.0-SNAPSHOT war VREFolder-hook handles the user adding/removal from the related Home Library VRE Folder diff --git a/src/main/java/org/gcube/portal/plugins/GCubeHookUserLocalService.java b/src/main/java/org/gcube/portal/plugins/GCubeHookUserLocalService.java index d6083f6..fa6a7a2 100644 --- a/src/main/java/org/gcube/portal/plugins/GCubeHookUserLocalService.java +++ b/src/main/java/org/gcube/portal/plugins/GCubeHookUserLocalService.java @@ -3,6 +3,7 @@ package org.gcube.portal.plugins; import org.gcube.common.homelibrary.home.HomeLibrary; import org.gcube.common.portal.PortalContext; import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.portal.plugins.thread.CheckShareLatexUserThread; import org.gcube.vomanagement.usermanagement.GroupManager; import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager; import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager; @@ -53,9 +54,9 @@ public class GCubeHookUserLocalService extends UserLocalServiceWrapper { addUserToHLVREFolder(groupId, user.getUserId()); } } - - - + + + /** USERS REMOVAL FROM GROUP **/ /** * this is the method used from Liferay Sites Membership Admin @@ -65,7 +66,7 @@ public class GCubeHookUserLocalService extends UserLocalServiceWrapper { super.unsetGroupUsers(groupId, userIds, serviceContext); removeUsersFromHLVREFolder(groupId, userIds); } - + @Override public void deleteGroupUser(long groupId, long userId) throws com.liferay.portal.kernel.exception.SystemException { super.deleteGroupUser(groupId, userId); @@ -119,6 +120,9 @@ public class GCubeHookUserLocalService extends UserLocalServiceWrapper { String username = um.getUserById(userId).getUsername(); org.gcube.common.homelibrary.home.workspace.usermanager.UserManager hlUm = HomeLibrary.getHomeManagerFactory().getUserManager(); hlUm.associateUserToGroup(scope, username); + //add the user to shareLatex + Thread t = new Thread(new CheckShareLatexUserThread(username, scope)); + t.start(); } else { _log.debug("Group is not a VRE, SKIP adding"); } @@ -166,5 +170,5 @@ public class GCubeHookUserLocalService extends UserLocalServiceWrapper { } ScopeProvider.instance.set(currScope); } - + } \ No newline at end of file diff --git a/src/main/java/org/gcube/portal/plugins/thread/CheckShareLatexUserThread.java b/src/main/java/org/gcube/portal/plugins/thread/CheckShareLatexUserThread.java new file mode 100644 index 0000000..1a73526 --- /dev/null +++ b/src/main/java/org/gcube/portal/plugins/thread/CheckShareLatexUserThread.java @@ -0,0 +1,122 @@ +package org.gcube.portal.plugins.thread; +import static org.gcube.common.authorization.client.Constants.authorizationService; +import static org.gcube.resources.discovery.icclient.ICFactory.clientFor; +import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.Iterator; + +import org.gcube.common.authorization.library.provider.UserInfo; +import org.gcube.common.resources.gcore.GCoreEndpoint; +import org.gcube.common.resources.gcore.GCoreEndpoint.Profile.Endpoint; +import org.gcube.common.scope.api.ScopeProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * @author Massimiliano Assante ISTI-CNR + * + */ +public class CheckShareLatexUserThread implements Runnable { + private static final Logger _log = LoggerFactory.getLogger(CheckShareLatexUserThread.class); + + private static final String SERVICE_NAME = "ShareLatex"; + private static final String SERVICE_CLASS = "DataAccess"; + private static final String ENTRY_NAME = "org.gcube.data.access.sharelatex.connector.Connector"; + private static final String USER_AGENT = "Mozilla/5.0"; + + private String username; + private String scope; + + public CheckShareLatexUserThread(String username, String scope) { + super(); + this.username = username; + this.scope = scope; + } + + @Override + public void run() { + String currScope = ScopeProvider.instance.get(); + ScopeProvider.instance.set(scope); + //construct the xquery + org.gcube.resources.discovery.client.queries.api.SimpleQuery query = queryFor(GCoreEndpoint.class); + query.addCondition("$resource/Profile/ServiceName/text() eq '" + SERVICE_NAME + "'"); + query.addCondition("$resource/Profile/ServiceClass/text() eq '" + SERVICE_CLASS + "'"); + + org.gcube.resources.discovery.client.api.DiscoveryClient client = clientFor( + GCoreEndpoint.class); + java.util.List conf = client.submit(query); + if (conf == null || conf.isEmpty()) { + _log.info("Not creating user in ShareLatex as no gcore endpoint named " + SERVICE_NAME + " is present in the scope " + scope + ""); + ScopeProvider.instance.set(currScope); + } else { + _log.debug("Getting token for " + username + " in " + scope); + String token = ""; + try { + java.util.List userRoles = new java.util.ArrayList(); + String DEFAULT_ROLE = "OrganizationMember"; + userRoles.add(DEFAULT_ROLE); + token = authorizationService().generateUserToken(new UserInfo(username, userRoles), scope); + } catch (Exception e) { + e.printStackTrace(); + ScopeProvider.instance.set(currScope); + } + ScopeProvider.instance.set(currScope); + GCoreEndpoint re2s = conf.get(0); + Iterator it = re2s.profile().endpoints().iterator(); + String uriService = ""; + while (it.hasNext()) { + Endpoint ep = it.next(); + if (ep.name().compareTo(ENTRY_NAME)==0) { + uriService = ep.uri().toString(); + _log.debug(" ** Found uriService for "+ENTRY_NAME+"="+uriService); + break; + } + } + createUserInShareLatex(uriService, token); + } + + } + + private void createUserInShareLatex(String uriService, String token) { + String connectURL = uriService+"/connect?gcube-token="+token; + String disconnectURL = uriService+"/disconnect?gcube-token="+token; + try { + sendGet(connectURL); + Thread.sleep(1000); + sendGet(disconnectURL); + } catch (Exception e) { + e.printStackTrace(); + } + } + + // HTTP GET request + private static void sendGet(String url) throws Exception { + + URL obj = new URL(url); + HttpURLConnection con = (HttpURLConnection) obj.openConnection(); + con.setRequestProperty("User-Agent", USER_AGENT); + + int responseCode = con.getResponseCode(); + _log.debug("\nSending 'GET' request to URL : " + url); + _log.debug("Response Code : " + responseCode); + + BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); + String inputLine; + StringBuffer response = new StringBuffer(); + + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } + in.close(); + + _log.debug(response.toString()); + + } +} + + diff --git a/src/main/webapp/WEB-INF/liferay-plugin-package.properties b/src/main/webapp/WEB-INF/liferay-plugin-package.properties index 4b2043e..b154ce5 100644 --- a/src/main/webapp/WEB-INF/liferay-plugin-package.properties +++ b/src/main/webapp/WEB-INF/liferay-plugin-package.properties @@ -1,9 +1,9 @@ name=VREFolder-hook module-group-id=liferay -module-incremental-version=2 +module-incremental-version=3 tags= short-description= change-log= -page-url=http://www.liferay.com -author=Liferay, Inc. -licenses=LGPL +page-url=http://www.gcube-system.org +author=M. Assante +licenses=EUPL