Skip to content
This repository was archived by the owner on Apr 22, 2022. It is now read-only.

Commit a5d7806

Browse files
author
taleksashina
committed
Merge remote-tracking branch 'remotes/origin/master' into geoknow (roles, intial setup)
2 parents 5e119e5 + bc51839 commit a5d7806

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2063
-544
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@
1818
/src/main/webapp/VAADIN/gwt-unitCache
1919
/src/main/webapp/VAADIN/widgetsets
2020
/target
21+
/bin

src/main/java/ImportRDF.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ private int httpUpdate(String endpoint, String graph, Model model) throws Except
146146
RdfStoreManager rdfStoreManager = null;
147147
if (username != null && !username.isEmpty() && token != null && !token.isEmpty()) {
148148
FrameworkUserManager frameworkUserManager = FrameworkConfiguration.getInstance(
149-
getServletContext(), false).getFrameworkUserManager();
149+
getServletContext()).getFrameworkUserManager();
150150
if (frameworkUserManager.checkToken(username, token))
151151
rdfStoreManager = frameworkUserManager.getRdfStoreManager(username);
152152
}

src/main/java/accounts/FrameworkUserManager.java

Lines changed: 131 additions & 71 deletions
Large diffs are not rendered by default.

src/main/java/accounts/UserManager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ public interface UserManager {
88
public void setRdfGraphPermissions(String user, String graph, int permissions) throws Exception;
99
public void deleteRdfGraphPermissions(String user, String graph) throws Exception;
1010
public void setDefaultGraphPermissions(String graph, int permissions) throws Exception;
11+
public boolean checkUserExists(String username, String email) throws Exception;
1112
}

src/main/java/accounts/UserProfile.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public class UserProfile {
55
private String settingsGraph;
66
private String accountURI;
77
private String email;
8-
private boolean admin;
8+
private UserRole role;
99

1010
public String getUsername() {
1111
return username;
@@ -39,12 +39,12 @@ public void setEmail(String email) {
3939
this.email = email;
4040
}
4141

42-
public boolean isAdmin() {
43-
return admin;
42+
public UserRole getRole() {
43+
return role;
4444
}
4545

46-
public void setAdmin(boolean admin) {
47-
this.admin = admin;
46+
public void setRole(UserRole role) {
47+
this.role = role;
4848
}
4949

5050
@Override
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package accounts;
2+
3+
import java.util.Collection;
4+
5+
/**
6+
* Created by taleksaschina on 24.06.2014.
7+
*/
8+
public class UserRole {
9+
private String uri;
10+
private String name;
11+
private Collection<String> services;
12+
13+
public String getUri() {
14+
return uri;
15+
}
16+
17+
public void setUri(String uri) {
18+
this.uri = uri;
19+
}
20+
21+
public String getName() {
22+
return name;
23+
}
24+
25+
public void setName(String name) {
26+
this.name = name;
27+
}
28+
29+
public Collection<String> getServices() {
30+
return services;
31+
}
32+
33+
public void setServices(Collection<String> services) {
34+
this.services = services;
35+
}
36+
}

src/main/java/accounts/VirtuosoUserManager.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package accounts;
22

3-
import java.sql.Connection;
4-
import java.sql.DriverManager;
5-
import java.sql.SQLException;
6-
import java.sql.Statement;
3+
import java.sql.*;
74

85
public class VirtuosoUserManager implements UserManager {
96
private static final String jdbcDriver = "virtuoso.jdbc4.Driver";
@@ -29,8 +26,10 @@ public VirtuosoUserManager(String connectionString, String user, String password
2926
}
3027

3128
@Override
32-
public void createUser(String name, String password) throws ClassNotFoundException, SQLException {
33-
executeUpdate(getConnection(), "DB.DBA.USER_CREATE('" + name + "', '" + password + "')");
29+
public void createUser(String name, String password) throws Exception {
30+
if (checkUserExists(name, null))
31+
throw new Exception("User " + name + " already exists");
32+
executeUpdate(getConnection(), "DB.DBA.USER_CREATE('" + name + "', '" + password + "')"); //NB! this function doesn't throw exception if user already exists
3433
// executeUpdate(getConnection(), "USER_SET_OPTION('" + name +
3534
// "', 'DAV_ENABLE', 1)");
3635
}
@@ -42,7 +41,7 @@ public void dropUser(String name) throws ClassNotFoundException, SQLException {
4241

4342
@Override
4443
public void grantRole(String user, String role) throws ClassNotFoundException, SQLException {
45-
executeUpdate(getConnection(), "GRANT " + role + " TO \"" + user + "\"");
44+
executeUpdate(getConnection(), "GRANT " + role + " TO \"" + user + "\""); //NB! this function throws exception if the role is already granted
4645
}
4746

4847
@Override
@@ -82,9 +81,23 @@ public void setDefaultGraphPermissions(String graph, int permissions)
8281
* @throws SQLException
8382
*/
8483
public void grantLOLook(String user) throws ClassNotFoundException, SQLException {
85-
executeUpdate(getConnection(), "GRANT EXECUTE ON DB.DBA.L_O_LOOK TO '" + user + "'");
84+
executeUpdate(getConnection(), "GRANT EXECUTE ON DB.DBA.L_O_LOOK TO \"" + user + "\"");
8685
}
8786

87+
@Override
88+
public boolean checkUserExists(String username, String email) throws Exception {
89+
//todo is there any simpler way to check if the user exists? some function?
90+
String query = "select * from DB.DBA.SYS_USERS where U_NAME='" + username + "'";
91+
Connection conn = getConnection();
92+
Statement stmt = conn.createStatement();
93+
try {
94+
ResultSet resultSet = stmt.executeQuery(query);
95+
return resultSet.next();
96+
} finally {
97+
stmt.close();
98+
}
99+
}
100+
88101
private Connection getConnection() throws ClassNotFoundException, SQLException {
89102
if (connection == null || connection.isClosed()) {
90103
Class.forName(jdbcDriver);

src/main/java/authentication/FrameworkConfiguration.java

Lines changed: 1 addition & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package authentication;
22

3-
import java.io.ByteArrayOutputStream;
43
import java.io.IOException;
54

65
import javax.servlet.ServletContext;
@@ -31,7 +30,6 @@ public class FrameworkConfiguration {
3130
private String emailUsername = "";
3231
private String emailPassword = "";
3332

34-
private String accountsOntologyNS = "";
3533
private String resourceNS = "";
3634
private String frameworkOntologyNS = "";
3735

@@ -55,31 +53,21 @@ public class FrameworkConfiguration {
5553
/**
5654
*
5755
* @param context
58-
* @param reset
5956
* @return
6057
* @throws Exception
6158
*/
62-
// TODO: replace System.out.println with a logging implementation
63-
public static synchronized FrameworkConfiguration getInstance(ServletContext context,
64-
boolean reset) throws Exception {
59+
public static synchronized FrameworkConfiguration getInstance(ServletContext context) throws Exception {
6560

6661
if (instance == null) {
6762

68-
System.out.println("[INFO] System Initialization ");
69-
7063
instance = new FrameworkConfiguration();
7164

7265
String configurationFile = "framework-configuration.ttl";
73-
String datasetsFile = "framework-datasets.ttl";
74-
String componentsFile = "framework-components.ttl";
75-
String ontologyFile = "framework-ontology.ttl";
76-
String accountsOntologyFile = "framework-accounts-ontology.ttl";
7766

7867
// initialize parameters from context
7968
instance.frameworkUri = context.getInitParameter("framework-uri");
8069

8170
instance.setFrameworkOntologyNS(context.getInitParameter("framework-ontology-ns"));
82-
instance.setAccountsOntologyNamespace(context.getInitParameter("accounts-ns"));
8371
instance.setResourceNamespace(context.getInitParameter("framework-ns"));
8472

8573
instance.setSmtpHost(context.getInitParameter("smtp-host"));
@@ -174,145 +162,6 @@ else if ("groups".equals(soln.get("label").asLiteral().getString()))
174162
instance.setGroupsGraph(soln.get("name").toString());
175163
}
176164
qexec.close();
177-
178-
// creates the system user exist for the application in virtuoso
179-
VirtuosoUserManager userManager = instance.getVirtuosoUserManager();
180-
181-
// if the flag to reinstall is true
182-
if (reset) {
183-
try {
184-
userManager.dropUser(instance.getAuthSparqlUser());
185-
} catch (Exception e) {
186-
// catches the error in case the user do not exist
187-
}
188-
// TODO: we may need to delete all users before to clean the store?
189-
}
190-
191-
try {
192-
193-
userManager.createUser(instance.getAuthSparqlUser(), instance.getAuthSparqlPassword());
194-
userManager.setDefaultRdfPermissions(instance.getAuthSparqlUser(), 3);
195-
userManager.grantRole(instance.getAuthSparqlUser(), "SPARQL_UPDATE");
196-
userManager.grantLOLook(instance.getAuthSparqlUser());
197-
// TODO: check if we still need to grant these to SPARQL user
198-
userManager.grantRole("SPARQL", "SPARQL_UPDATE");
199-
userManager.grantLOLook("SPARQL");
200-
201-
System.out.println("[INFO] System User was created ");
202-
} catch (Exception e) {
203-
if ("virtuoso.jdbc4.VirtuosoException".equals(e.getClass().getCanonicalName()))
204-
// TODO: replace with a logging implementation
205-
System.out.println("Seems that the user is already there");
206-
else
207-
throw e;
208-
}
209-
210-
SecureRdfStoreManagerImpl frameworkRdfStoreManager = new SecureRdfStoreManagerImpl(instance
211-
.getAuthSparqlEndpoint(), instance.getAuthSparqlUser(), instance.getAuthSparqlPassword());
212-
// delete all graphs if reinstall is requested
213-
if (reset) {
214-
try {
215-
frameworkRdfStoreManager.dropGraph(instance.getSettingsGraph());
216-
frameworkRdfStoreManager.dropGraph(instance.getAccountsGraph());
217-
frameworkRdfStoreManager.dropGraph(instance.getGroupsGraph());
218-
frameworkRdfStoreManager.dropGraph(instance.getInitialSettingsGraph());
219-
} catch (Exception e) {
220-
}
221-
}
222-
223-
// check if settingsGraph exist do not overwrite
224-
String queryString = " ASK { GRAPH <" + instance.getSettingsGraph() + "> {?s a ?o} }";
225-
String response = frameworkRdfStoreManager.execute(queryString, "text/plain");
226-
if (response.toLowerCase().indexOf("true") < 0) {
227-
228-
// TODO: replace with a logging implementation
229-
System.out.println("[INFO] Default Graphs creation/configuration ");
230-
231-
// Read configuration files
232-
Model datasetModel = ModelFactory.createDefaultModel();
233-
Model componentsModel = ModelFactory.createDefaultModel();
234-
Model ontologyModel = ModelFactory.createDefaultModel();
235-
Model ontologyAccountsModel = ModelFactory.createDefaultModel();
236-
237-
try {
238-
datasetModel.read(datasetsFile);
239-
componentsModel.read(componentsFile);
240-
ontologyModel.read(ontologyFile);
241-
ontologyAccountsModel.read(accountsOntologyFile);
242-
} catch (RiotException e) {
243-
throw new IOException("Malformed configuration files");
244-
}
245-
246-
// create required named graphs and load the configuration files
247-
// using framework default user
248-
frameworkRdfStoreManager.createGraph(instance.getSettingsGraph());
249-
frameworkRdfStoreManager.createGraph(instance.getAccountsGraph());
250-
frameworkRdfStoreManager.createGraph(instance.getGroupsGraph());
251-
frameworkRdfStoreManager.createGraph(instance.getInitialSettingsGraph());
252-
253-
// Make graphs accessible to framework user only
254-
userManager.setDefaultRdfPermissions("nobody", 0);
255-
userManager.setRdfGraphPermissions(instance.getAuthSparqlUser(), instance
256-
.getSettingsGraph(), 3);
257-
userManager.setRdfGraphPermissions(instance.getAuthSparqlUser(), instance
258-
.getAccountsGraph(), 3);
259-
userManager.setRdfGraphPermissions(instance.getAuthSparqlUser(), instance.getGroupsGraph(),
260-
3);
261-
userManager.setRdfGraphPermissions(instance.getAuthSparqlUser(), instance
262-
.getInitialSettingsGraph(), 3);
263-
264-
// join the settings files
265-
Model settingsModel = ModelFactory.createDefaultModel();
266-
settingsModel.add(datasetModel);
267-
settingsModel.add(componentsModel);
268-
settingsModel.add(ontologyModel);
269-
270-
// add to settings virtuoso component without users/passwords
271-
queryString = "PREFIX foaf:<http://xmlns.com/foaf/0.1/> "
272-
+ "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
273-
+ "PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> "
274-
+ "PREFIX lds:<http://stack.linkeddata.org/ldis-schema/>" + " CONSTRUCT { <"
275-
+ instance.getFrameworkUri() + "> ?p ?o . " + "<" + instance.getFrameworkUri()
276-
+ "> lds:integrates ?component ."
277-
+ "?component rdfs:label ?label . ?component rdf:type ?type . "
278-
+ "?component lds:providesService ?service . ?service rdf:type ?servicetype ."
279-
+ "?service lds:serviceUrl ?serviceUrl .} " + " WHERE { <"
280-
+ instance.getFrameworkUri() + "> ?p ?o ." + "<" + instance.getFrameworkUri()
281-
+ "> lds:integrates ?component ."
282-
+ "?component rdfs:label ?label . ?component rdf:type ?type . "
283-
+ "?component lds:providesService ?service . ?service rdf:type ?servicetype ."
284-
+ "?service lds:serviceUrl ?serviceUrl .}";
285-
qexec = QueryExecutionFactory.create(queryString, configurationModel);
286-
Model triples = qexec.execConstruct();
287-
settingsModel.add(triples);
288-
qexec.close();
289-
290-
// write the initial settings model (default settings for new
291-
// users)
292-
ByteArrayOutputStream os = new ByteArrayOutputStream();
293-
settingsModel.write(os, "N-TRIPLES");
294-
queryString = "INSERT DATA { GRAPH <" + instance.getInitialSettingsGraph() + "> { "
295-
+ os.toString() + " } }";
296-
os.close();
297-
frameworkRdfStoreManager.execute(queryString, null);
298-
299-
// write the system settings model (include system graphs data)
300-
// settingsModel.add(graphsModel);
301-
os = new ByteArrayOutputStream();
302-
settingsModel.write(os, "N-TRIPLES");
303-
queryString = "INSERT DATA { GRAPH <" + instance.getSettingsGraph() + "> { "
304-
+ os.toString() + " } }";
305-
os.close();
306-
frameworkRdfStoreManager.execute(queryString, null);
307-
308-
// create and add accounts ontology to the accounts graph
309-
os = new ByteArrayOutputStream();
310-
ontologyAccountsModel.write(os, "N-TRIPLES");
311-
queryString = "INSERT DATA { GRAPH <" + instance.getAccountsGraph() + "> { "
312-
+ os.toString() + " } }";
313-
os.close();
314-
frameworkRdfStoreManager.execute(queryString, null);
315-
}
316165
}
317166

318167
return instance;
@@ -439,14 +288,6 @@ public void setAuthSparqlPassword(String authSparqlPassword) {
439288
// this.accountsNamespace = accountsNamespace;
440289
// }
441290

442-
public String getAccountsOntologyNamespace() {
443-
return accountsOntologyNS;
444-
}
445-
446-
public void setAccountsOntologyNamespace(String accountsNamespace) {
447-
this.accountsOntologyNS = accountsNamespace;
448-
}
449-
450291
public String getAccountsGraph() {
451292
return accountsGraph;
452293
}

0 commit comments

Comments
 (0)