The methods isAlreadyRoleSet, getOrganizationsByUser and getGroupsAndRolesByUser no longer use the database connection. They now use the jackan client

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/ckan-util-library@129444 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2016-06-26 19:59:26 +00:00
parent 29950eff98
commit 43b4e938c0
2 changed files with 69 additions and 122 deletions

View File

@ -80,12 +80,13 @@ public class CKanUtilsImpl implements CKanUtilsInterface{
CKAN_DB_NAME = runningInstance.getDataBaseName();
CKAN_DB_USER = runningInstance.getDataBaseUser();
CKAN_DB_PASSWORD = runningInstance.getDataBasePassword();
logger.debug("Plain password first 3 chars are " + CKAN_DB_PASSWORD.substring(0, 3));
CKAN_TOKEN_SYS = runningInstance.getSysAdminToken();
logger.debug("Plain sys admin token first 3 chars are " + CKAN_TOKEN_SYS.substring(0, 3));
CKAN_DB_PORT = runningInstance.getDatabasePorts().get(0);
CKAN_CATALOGUE_URL = runningInstance.getDataCatalogueUrl().get(0);
logger.debug("Plain sys admin token first 3 chars are " + CKAN_TOKEN_SYS.substring(0, 3));
logger.debug("Plain db password first 3 chars are " + CKAN_DB_PASSWORD.substring(0, 3));
// create connection pool
String url = "jdbc:postgresql://" + CKAN_DB_URL + ":" + CKAN_DB_PORT + "/" + CKAN_DB_NAME;
connectionPool = new BasicDataSource();
@ -124,8 +125,7 @@ public class CKanUtilsImpl implements CKanUtilsInterface{
}catch(Exception e){
logger.error("Unable to close this connection ", e);
}
}else
logger.debug("CONNECTION WAS NULL");
}
}
@ -219,38 +219,33 @@ public class CKanUtilsImpl implements CKanUtilsInterface{
// in order to avoid errors, the username is always converted
String ckanUsername = UtilMethods.fromUsernameToCKanUsername(username);
String userId = getUserIdByUsername(ckanUsername);
// list to return
List<CkanOrganization> toReturn = new ArrayList<CkanOrganization>();
// the connection
Connection connection = null;
try{
connection = getConnection();
List<String> organizationIds = getOrganizationsIds();
// get the list of all organizations
List<CkanOrganization> organizations = client.getOrganizationList();
// for each org id, check if the user is included
for (String orgId : organizationIds) {
String query = "SELECT * FROM \"member\" WHERE \"table_id\"=? and \"group_id\"=? and \"table_name\"=? and \"state\"=?;";
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, userId);
preparedStatement.setString(2, orgId);
preparedStatement.setString(3, "user");
preparedStatement.setString(4, State.ACTIVE.toString().toLowerCase());
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()) {
// the role within the organization doesn't matter
logger.debug("User " + ckanUsername + " belongs to organization with id " + orgId);
toReturn.add(client.getOrganization(orgId));
// iterate over them
for (CkanOrganization ckanOrganization : organizations) {
// get the list of users in it
List<CkanUser> users = ckanOrganization.getUsers();
// check if the current user is among them
for (CkanUser ckanUser : users) {
if(ckanUser.getName().equals(ckanUsername)){
logger.debug("User " + ckanUsername + " is into " + ckanOrganization.getName());
toReturn.add(ckanOrganization);
break;
}
}
}
}catch(Exception e){
logger.error("Unable to get user's organizations", e);
}finally{
closeConnection(connection);
}
return toReturn;
}
@ -262,53 +257,48 @@ public class CKanUtilsImpl implements CKanUtilsInterface{
logger.debug("Requested roles that the user " + username + " has into his organizations");
logger.debug("Roles to check are " + rolesToMatch);
Map<String, List<RolesIntoOrganization>> toReturn = new HashMap<String, List<RolesIntoOrganization>>();
// the connection
Connection connection = null;
try{
// get connection
connection = getConnection();
// in order to avoid errors, the username is always converted
String ckanUsername = UtilMethods.fromUsernameToCKanUsername(username);
// get id from the user
String userId = getUserIdByUsername(ckanUsername);
// get the list of organizations in which the user is present
List<CkanOrganization> organizationsByUser = getOrganizationsByUser(ckanUsername);
// get the id of all the organizations
List<String> organizationIds = getOrganizationsIds();
// iterate over them
for (CkanOrganization ckanOrganization : organizationsByUser) {
for (String orgId : organizationIds) {
// get the list of users in it
List<CkanUser> users = ckanOrganization.getUsers();
// go to the member table, that says which role has this user into the org
String query = "SELECT * FROM \"member\" WHERE \"table_id\"=? and \"group_id\"=? and \"table_name\"=? and \"state\"=?;";
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, userId);
preparedStatement.setString(2, orgId);
preparedStatement.setString(3, "user");
preparedStatement.setString(4, State.ACTIVE.toString().toLowerCase());
ResultSet rs = preparedStatement.executeQuery();
// check if the current user is among them
for (CkanUser ckanUser : users) {
if(ckanUser.getName().equals(ckanUsername)){
// prepare the data to put into the hashmap
List<RolesIntoOrganization> rolesIntoOrg = new ArrayList<RolesIntoOrganization>();
// the role (admin, editor, member)
String capacity = ckanUser.getCapacity();
// list of roles
List<RolesIntoOrganization> rolesIntoOrg = new ArrayList<RolesIntoOrganization>();
if(rolesToMatch.contains(RolesIntoOrganization.valueOf(capacity.toUpperCase()))){
RolesIntoOrganization enumRole = RolesIntoOrganization.valueOf(capacity.toUpperCase());
rolesIntoOrg.add(enumRole);
logger.debug("User " + ckanUsername + " has role " + enumRole +
" into organization with name " + ckanOrganization.getName());
}
// save it
if(!rolesIntoOrg.isEmpty()){
String orgName = ckanOrganization.getName();
toReturn.put(orgName, rolesIntoOrg);
}
break;
while(rs.next()){
// check
String role = rs.getString("capacity"); // editor, admin, member
if(rolesToMatch.contains(RolesIntoOrganization.valueOf(role.toUpperCase()))){
RolesIntoOrganization enumRole = RolesIntoOrganization.valueOf(role.toUpperCase());
rolesIntoOrg.add(enumRole);
logger.debug("User " + ckanUsername + " has role " + enumRole + " into organization with id " + orgId);
}
break;
}
if(!rolesIntoOrg.isEmpty()){
String orgName = client.getOrganization(orgId).getName();
toReturn.put(orgName, rolesIntoOrg);
}
}
@ -317,35 +307,11 @@ public class CKanUtilsImpl implements CKanUtilsInterface{
return toReturn;
}catch(Exception e){
logger.error("Unable to analyze user's roles", e);
}finally{
closeConnection(connection);
}
return null;
}
/**
* Returns the user id given his username
* @param username
* @return the id on success, null otherwise
*/
private String getUserIdByUsername(String username) {
logger.debug("Request user id whose username is = " + username);
// in order to avoid errors, the username is always converted
String ckanUsername = UtilMethods.fromUsernameToCKanUsername(username);
String userId = null;
try{
userId = client.getUser(ckanUsername).getId();
logger.debug("User id retrieved for " + ckanUsername + " "+ userId);
}catch(Exception e){
logger.error("Unable to retrieve user with name " + ckanUsername, e);
}
return userId;
}
@Override
public List<String> getOrganizationsIds(){
@ -390,6 +356,7 @@ public class CKanUtilsImpl implements CKanUtilsInterface{
List<CkanOrganization> orgs = getOrganizationsByUser(ckanUsername);
List<String> orgsName = new ArrayList<String>();
for (CkanOrganization ckanOrganization : orgs) {
orgsName.add(ckanOrganization.getName());
logger.debug("Organization name is " + ckanOrganization.getName());
@ -700,7 +667,8 @@ public class CKanUtilsImpl implements CKanUtilsInterface{
// the url of the dataset looks like "getCatalogueUrl() + /dataset/ + dataset name"
try{
CheckedCkanClient client = new CheckedCkanClient(CKAN_CATALOGUE_URL, apiKey);
// get the dataset from name
CkanDataset dataset = client.getDataset(datasetIdOrName);
if(dataset != null){
@ -773,40 +741,21 @@ public class CKanUtilsImpl implements CKanUtilsInterface{
String organizationName,
RolesIntoOrganization correspondentRoleToCheck) {
//the connection
Connection connection = null;
try{
connection = getConnection();
// get this organization
CkanOrganization organization = client.getOrganization(organizationName);
// user id
String userId = getUserIdByUsername(ckanUsername);
// get the users
List<CkanUser> users = organization.getUsers();
// we need a checked client
CheckedCkanClient client = new CheckedCkanClient(CKAN_CATALOGUE_URL, CKAN_TOKEN_SYS);
// get the CkanClient to retrieve the organization id from the name
String orgId = client.getOrganization(organizationName).getId();
logger.debug("Organization with name " + organizationName + " has id " + orgId);
String query =
"SELECT * FROM \"member\" WHERE \"table_id\"=? and \"group_id\"=? and \"table_name\"=? and \"state\"=? and \"capacity\"=?;";
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, userId);
preparedStatement.setString(2, orgId);
preparedStatement.setString(3, "user");
preparedStatement.setString(4, State.ACTIVE.toString().toLowerCase());
preparedStatement.setString(5, correspondentRoleToCheck.toString().toLowerCase());
ResultSet rs = preparedStatement.executeQuery();
if(rs.next()) // ok, there is this row
return true;
for (CkanUser ckanUser : users) {
if(ckanUser.getName().equals(ckanUsername) && ckanUser.getCapacity().equals(correspondentRoleToCheck.toString().toLowerCase()))
return true;
}
}catch(Exception e){
logger.error("Unable to check if this role was already set", e);
}finally{
closeConnection(connection);
}
return false;
@ -818,9 +767,7 @@ public class CKanUtilsImpl implements CKanUtilsInterface{
// in order to avoid errors, the username is always converted
String ckanUsername = UtilMethods.fromUsernameToCKanUsername(username);
try{
CheckedCkanClient checkedClient = new CheckedCkanClient(CKAN_CATALOGUE_URL, apiKey);
CkanUser user = checkedClient.getUser(ckanUsername);
return user.isSysadmin();
return client.getUser(ckanUsername).isSysadmin();
}catch(Exception e){
logger.error("Failed to check if the user " + username + " has role sysadmin", e);
}
@ -828,12 +775,6 @@ public class CKanUtilsImpl implements CKanUtilsInterface{
return false;
}
@Override
protected void finalize() throws Throwable {
logger.debug("Closing connection pool on finalize()");
connectionPool.close();
}
@Override
public boolean createOrganization(String orgName, String token)throws Exception{
logger.debug("Create organization method call for creation of an organization named " + orgName);
@ -868,4 +809,10 @@ public class CKanUtilsImpl implements CKanUtilsInterface{
return result;
}
@Override
protected void finalize() throws Throwable {
logger.debug("Closing connection pool on finalize()");
connectionPool.close();
}
}

View File

@ -157,7 +157,7 @@ public interface CKanUtilsInterface {
*/
public boolean checkRole(String username, String organizationName,
RolesIntoOrganization correspondentRoleToCheck);
/**
* The method tries to create an organization with name orgName
* @param orgName the name of the organization