Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First try to implement SPARQL query with acl #2

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ru.agentlab.rdf4j.ppo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
<packaging>bundle</packaging>

<dependencies>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>java-hamcrest</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,120 +1,123 @@
package ru.agentlab.rdf4j.ppo.policies;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import lombok.Getter;
import lombok.Setter;
import org.eclipse.rdf4j.common.iteration.Iterations;
import org.eclipse.rdf4j.model.IRI;

import org.eclipse.rdf4j.model.Namespace;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.query.BindingSet;
import org.eclipse.rdf4j.query.MalformedQueryException;
import org.eclipse.rdf4j.query.QueryEvaluationException;
import org.eclipse.rdf4j.query.QueryLanguage;
import org.eclipse.rdf4j.query.TupleQueryResult;
import org.eclipse.rdf4j.repository.Repository;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.eclipse.rdf4j.repository.RepositoryException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ru.agentlab.rdf4j.ppo.policies.model.PPAccessAllowed;
import ru.agentlab.rdf4j.ppo.policies.model.PrivacyPreference;
import ru.agentlab.rdf4j.ppo.policies.model.PrivacyPreferenceFactory;
import ru.agentlab.rdf4j.ppo.policies.model.accesscontrol.PPAccessControl;
import ru.agentlab.rdf4j.ppo.policies.model.accesscontrol.PPAccessControlFactory;

import java.util.ArrayList;
import java.util.List;

public class PPManagerImpl implements PPManager{
private static Logger log = LoggerFactory.getLogger(PPManagerImpl.class);

protected String policiesContext;
protected List<PrivacyPreference> ppList;
protected boolean isWhitelisting = true; // switch to false for blacklisting
public static String prefixes = "";
public static final String CUR_USER = "?cur_user";

public PPManagerImpl() {
ppList = new ArrayList<PrivacyPreference>();
}

@Override
public void setWhitelisting(boolean isWhitelisting) {
this.isWhitelisting = isWhitelisting;
}

@Override
public boolean isWhitelisting() {
return isWhitelisting;
}

@Override
public void setPoliciesContext(String policiesContext) {
this.policiesContext = policiesContext;
}

@Override
public String getPoliciesContext() {
return this.policiesContext;
}

@Override
public void loadPrivacyPreferences(Repository repo) throws IOException {
try {
RepositoryConnection connection = repo.getConnection();
/*
* store prefixes
*/
List<Namespace> prefixList = null;

try {
prefixList = connection.getNamespaces().asList();
} catch (RepositoryException e) {
e.printStackTrace();
}

for(Namespace p : prefixList){
prefixes += "PREFIX ";
prefixes += p.getPrefix();
prefixes += ": <";
prefixes += p.getName();
prefixes += "> ";
}

/*
* create ppList
*/
TupleQueryResult statements = connection.prepareTupleQuery(QueryLanguage.SPARQL, "SELECT ?s FROM <" + policiesContext + "> WHERE {?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://vocab.deri.ie/ppo#PrivacyPreference>} ").evaluate();

try {
while(statements.hasNext()) {
BindingSet bindingSet = statements.next();
Value valueOfS = bindingSet.getValue("s");

IRI ppIRI = connection.getValueFactory().createIRI(valueOfS.stringValue());

ppList.add(PrivacyPreferenceFactory.createPrivacyPreference(ppIRI, connection, policiesContext));
}
log.info("{} privacy preferences have been succesfully mapped", getPrivacyPreferences().size());
} catch (Exception e) {
e.printStackTrace();
}

finally {
Collections.sort(ppList, Collections.reverseOrder()); // sort by priority
}

} catch (RepositoryException e) {
log.error("Error loading policies", e);
throw new IllegalStateException(e);
} catch (QueryEvaluationException e) {
e.printStackTrace();
} catch (MalformedQueryException e) {
e.printStackTrace();
}
}

@Override
public List<PrivacyPreference> getPrivacyPreferences () {
return ppList;
}
@Getter
@Setter
public class PPManagerImpl implements PPManager {
private static Logger log = LoggerFactory.getLogger(PPManagerImpl.class);

protected String policiesContext;
protected List<PrivacyPreference> ppList;
protected boolean isWhitelisting = true; // switch to false for blacklisting
public static String prefixes = "";
public static final String CUR_USER = "?cur_user";

public PPManagerImpl() {
ppList = new ArrayList<>();
}

@Override
public void loadPrivacyPreferences(Repository repo) {
try {
RepositoryConnection connection = repo.getConnection();
/*
* store prefixes
*/
List<Namespace> prefixList;

prefixList = Iterations.asList(connection.getNamespaces());

for (Namespace p : prefixList) {
prefixes += "PREFIX ";
prefixes += p.getPrefix();
prefixes += ": <";
prefixes += p.getName();
prefixes += "> ";
}

/*
* create ppList
*/
TupleQueryResult statements = connection.prepareTupleQuery(QueryLanguage.SPARQL,
"SELECT ?s FROM <" + policiesContext + "> " +
"WHERE {?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> " +
"<http://vocab.deri.ie/ppo#PrivacyPreference>} ")
.evaluate();

while (statements.hasNext()) {
BindingSet bindingSet = statements.next();
Value valueOfS = bindingSet.getValue("s");

IRI ppIRI = connection.getValueFactory().createIRI(valueOfS.stringValue());

ppList.add(PrivacyPreferenceFactory.createPrivacyPreference(ppIRI, connection, policiesContext));
}
log.info("{} privacy preferences have been succesfully mapped", getPrivacyPreferences().size());

} catch (RepositoryException e) {
log.error("Error loading policies", e);
throw new IllegalStateException(e);
} catch (Exception e) {
e.printStackTrace();
}
}

public void setUserExpertGroupOne(RepositoryConnection connection, IRI webid, boolean presence) {
IRI adminIri = connection.getValueFactory().createIRI("http://cpgu.kbpm.ru/ns/rm/users#expertGroup1");
editUserRole(connection, webid, adminIri, presence);
}

public void setUserExpertUsersTwo(RepositoryConnection connection, IRI webid, boolean presence) {
IRI adminIri = connection.getValueFactory().createIRI("http://cpgu.kbpm.ru/ns/rm/users#expertUsers2");
editUserRole(connection, webid, adminIri, presence);
}

public void setUserAdminGroup(RepositoryConnection connection, IRI webid, boolean presence) {
IRI adminIri = connection.getValueFactory().createIRI("http://cpgu.kbpm.ru/ns/rm/users#adminUsers");
editUserRole(connection, webid, adminIri, presence);
}

private void editUserRole(RepositoryConnection connection, IRI webid, IRI userRole, boolean presence) {
String memberOf = "http://xmlns.com/foaf/0.1/member";
IRI predicateForRole = connection.getValueFactory().createIRI(memberOf);
Statement statement = connection.getValueFactory().createStatement(
userRole,
predicateForRole,
webid);
if (presence) {
connection.add(statement);
} else {
connection.remove(statement);
}
connection.commit();
}

@Override
public List<PrivacyPreference> getPrivacyPreferences() {
return ppList;
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package ru.agentlab.rdf4j.ppo.policies.model;

/**
*
* @author Franz Brandstätter
*
* <p>
* States a pp can have according to verification
*
*/
public enum PPAccessAllowed {
ALLOWED, DENIED, NOT_HANDLED;
ALLOWED, DENIED, NOT_HANDLED;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import org.eclipse.rdf4j.model.IRI;

public interface PPComponent {
/**
* checks whether the statement is handled by the
* current PrivacyPreference
* @param webid ID of the current user
* @param statement Statement which has to be checked
* @return true if the current PP handles the statement, false otherwise
*/
boolean handlesAccess(IRI webid, Statement statement);
/**
* checks whether the statement is handled by the
* current PrivacyPreference
*
* @param webid ID of the current user
* @param statement Statement which has to be checked
* @return true if the current PP handles the statement, false otherwise
*/
boolean handlesAccess(IRI webid, Statement statement);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public interface PrivacyPreference extends Comparable<PrivacyPreference> {
* gets the priority of the PP
* @return the priority
*/
public float getPriority();
float getPriority();
/**
* Verifies whether the current PP allows read-access for the submitted
* statement and webid
Expand Down
Loading