diff --git a/CHANGELOG.md b/CHANGELOG.md index 1459060..64d3ffa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,13 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [v1.4.0-SNAPSHOT] + +- Removed old code which set the user role in ckan [#23310] +- Fixed gcat discovery [#23309] +- Supporting new authorization [#23306] + + ## [v1.3.0] - updated gcat-client version [#21530] diff --git a/pom.xml b/pom.xml index 2bbeb4a..08526af 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ org.gcube.data.access ckan-connector - 1.3.0 + 1.4.0-SNAPSHOT war ckan connector a ckan connector for automatic login @@ -32,7 +32,7 @@ org.gcube.distribution gcube-smartgears-bom - 2.1.0 + 2.1.1 pom import diff --git a/src/main/java/org/gcube/data/access/ckanconnector/ConnectorManager.java b/src/main/java/org/gcube/data/access/ckanconnector/ConnectorManager.java index 2548d1c..3e49de1 100644 --- a/src/main/java/org/gcube/data/access/ckanconnector/ConnectorManager.java +++ b/src/main/java/org/gcube/data/access/ckanconnector/ConnectorManager.java @@ -78,41 +78,44 @@ public class ConnectorManager { @GET public Response connect(@PathParam(value = "pathInfo") String path, @Context HttpServletRequest req, @QueryParam(value="listOfVres") String vres ) { try{ - if (AuthorizationProvider.instance.get()==null || AuthorizationProvider.instance.get().getClient() == null ) return Response.status(Status.UNAUTHORIZED).build(); + +// This is done by smartgears +// if (AuthorizationProvider.instance.get()==null || AuthorizationProvider.instance.get().getClient() == null ) return Response.status(Status.UNAUTHORIZED).build(); + log.info("passed path is {}",path); String ckanKey = context.getInitParameter("ckanKey"); String originalUserName = AuthorizationProvider.instance.get().getClient().getId(); String changedUserName = originalUserName.replace(".", "_"); - + User user = new User(); + user.read(changedUserName); int internalPort = Integer.parseInt(context.getInitParameter("internalPort")); String localhostName = "http://127.0.0.1:"+internalPort; long startCheckUser = System.currentTimeMillis(); CkanClient ckanClient = new CkanClient(localhostName, ckanKey); - - try { - User user = new User(); - user.read(changedUserName); - }catch (Throwable tr) { - log.error("Error while contacting gCat. The old code will made the work", tr); - CkanUser user = null; - try{ - user = ckanClient.getUser(changedUserName); - }catch(Exception e){ - log.warn("user {} doesn't exist, the system will create it",originalUserName, e); - } - log.info("checking user took {}",(System.currentTimeMillis()-startCheckUser)); - if (user==null){ - long startCreateUser = System.currentTimeMillis(); - user = ckanClient.createUser(new CkanUser(changedUserName, originalUserName+"@gcube.ckan.org" , randomString.nextString() )); - log.info("create user took {}",(System.currentTimeMillis()-startCreateUser)); - } - } - - addUserToVres(vres, changedUserName, ckanClient, ckanKey, localhostName); - +// try { +// User user = new User(); +// user.read(changedUserName); +// }catch (Throwable tr) { +// log.error("Error while contacting gCat. The old code will made the work", tr); +// CkanUser user = null; +// try{ +// user = ckanClient.getUser(changedUserName); +// }catch(Exception e){ +// log.warn("user {} doesn't exist, the system will create it",originalUserName, e); +// } +// log.info("checking user took {}",(System.currentTimeMillis()-startCheckUser)); +// if (user==null){ +// long startCreateUser = System.currentTimeMillis(); +// user = ckanClient.createUser(new CkanUser(changedUserName, originalUserName+"@gcube.ckan.org" , randomString.nextString() )); +// log.info("create user took {}",(System.currentTimeMillis()-startCreateUser)); +// } +// } +// +// addUserToVres(vres, changedUserName, ckanClient, ckanKey, localhostName); +// log.info("logging {} in scope {}",originalUserName, ScopeProvider.instance.get()); return createResponse(changedUserName, path, req.getQueryString()); }catch(Exception e){ @@ -122,17 +125,17 @@ public class ConnectorManager { } - private void addUserToVres(String vres, String changedUserName, - CkanClient ckanClient, String ckanKey, String localhostName) { - if (vres!=null && !vres.isEmpty()) - for (String vreAndRole: vres.split(",")){ - String[] splitVRE = vreAndRole.split("\\|"); - String vre = splitVRE[0]; - String role = splitVRE[1]; - boolean added = addUserToOrganization(changedUserName, vre, role, ckanKey, localhostName); - log.info("{} {} added to vre {}",changedUserName, added?"":"not", vre); - } - } +// private void addUserToVres(String vres, String changedUserName, +// CkanClient ckanClient, String ckanKey, String localhostName) { +// if (vres!=null && !vres.isEmpty()) +// for (String vreAndRole: vres.split(",")){ +// String[] splitVRE = vreAndRole.split("\\|"); +// String vre = splitVRE[0]; +// String role = splitVRE[1]; +// boolean added = addUserToOrganization(changedUserName, vre, role, ckanKey, localhostName); +// log.info("{} {} added to vre {}",changedUserName, added?"":"not", vre); +// } +// } private Response createResponse(String userName, String path, String query){ try{ @@ -182,38 +185,38 @@ public class ConnectorManager { } - private boolean addUserToOrganization(String ckanUsername, String organizationName, String role, String ckanKey, String hostAddress ){ - // we need to use the apis to make it - String path = "/api/3/action/organization_member_create"; - - // Request parameters to be replaced - String parameter = "{" - + "\"id\":\"ORGANIZATION_ID_NAME\"," - + "\"username\":\"USERNAME_ID_NAME\"," - + "\"role\":\"ROLE\"" - + "}"; - - // replace those values - parameter = parameter.replace("ORGANIZATION_ID_NAME", organizationName.toLowerCase()); - parameter = parameter.replace("USERNAME_ID_NAME", ckanUsername); - parameter = parameter.replace("ROLE", role); - - log.debug("API request for organization membership is going to be " + parameter); - - try(CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { - HttpPost request = new HttpPost(hostAddress + path); - request.addHeader("Authorization", ckanKey); // sys token - StringEntity params = new StringEntity(parameter); - request.setEntity(params); - HttpResponse response = httpClient.execute(request); - log.debug("Response code is " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase()); - - return (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK); - - }catch (Exception ex) { - log.error("Error while trying to change the role for this user ", ex); - return false; - } - } +// private boolean addUserToOrganization(String ckanUsername, String organizationName, String role, String ckanKey, String hostAddress ){ +// // we need to use the apis to make it +// String path = "/api/3/action/organization_member_create"; +// +// // Request parameters to be replaced +// String parameter = "{" +// + "\"id\":\"ORGANIZATION_ID_NAME\"," +// + "\"username\":\"USERNAME_ID_NAME\"," +// + "\"role\":\"ROLE\"" +// + "}"; +// +// // replace those values +// parameter = parameter.replace("ORGANIZATION_ID_NAME", organizationName.toLowerCase()); +// parameter = parameter.replace("USERNAME_ID_NAME", ckanUsername); +// parameter = parameter.replace("ROLE", role); +// +// log.debug("API request for organization membership is going to be " + parameter); +// +// try(CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { +// HttpPost request = new HttpPost(hostAddress + path); +// request.addHeader("Authorization", ckanKey); // sys token +// StringEntity params = new StringEntity(parameter); +// request.setEntity(params); +// HttpResponse response = httpClient.execute(request); +// log.debug("Response code is " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase()); +// +// return (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK); +// +// }catch (Exception ex) { +// log.error("Error while trying to change the role for this user ", ex); +// return false; +// } +// } } diff --git a/src/main/java/org/gcube/data/access/ckanconnector/OrganizationManager.java b/src/main/java/org/gcube/data/access/ckanconnector/OrganizationManager.java index 9377116..5b2fbbb 100644 --- a/src/main/java/org/gcube/data/access/ckanconnector/OrganizationManager.java +++ b/src/main/java/org/gcube/data/access/ckanconnector/OrganizationManager.java @@ -35,7 +35,9 @@ public class OrganizationManager { public Response create(@PathParam("name") String organizationName) { log.info("create called"); try{ - if (AuthorizationProvider.instance.get()==null || AuthorizationProvider.instance.get().getClient() == null ) return Response.status(Status.UNAUTHORIZED).build(); + +// This is done by smartgears +// if (AuthorizationProvider.instance.get()==null || AuthorizationProvider.instance.get().getClient() == null ) return Response.status(Status.UNAUTHORIZED).build(); String ckanKey = context.getInitParameter("ckanKey"); int internalPort = Integer.parseInt(context.getInitParameter("internalPort")); @@ -60,7 +62,8 @@ public class OrganizationManager { public String get(@PathParam("name") String organizationName) { log.info("get called with name {}", organizationName); - if (AuthorizationProvider.instance.get()==null || AuthorizationProvider.instance.get().getClient() == null ) throw new WebApplicationException("user authentication needed",Response.Status.UNAUTHORIZED); +// This is done by smartgears +// if (AuthorizationProvider.instance.get()==null || AuthorizationProvider.instance.get().getClient() == null ) throw new WebApplicationException("user authentication needed",Response.Status.UNAUTHORIZED); String ckanKey = context.getInitParameter("ckanKey"); int internalPort = Integer.parseInt(context.getInitParameter("internalPort"));