From bdb154935cae9aa7113e31cde06de73b6f3c8366 Mon Sep 17 00:00:00 2001 From: jeb228 Date: Tue, 23 Feb 2010 18:48:48 +0000 Subject: [PATCH] NIHVIVO-119 If there are no users in the model, and none from files in ontologies/auth, create one from deploy.properties "initialAdminUser". --- .../servlet/setup/JenaDataSourceSetup.java | 3 + .../setup/JenaDataSourceSetupBase.java | 63 +++++++++++++++---- .../setup/JenaPersistentDataSourceSetup.java | 10 ++- 3 files changed, 60 insertions(+), 16 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaDataSourceSetup.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaDataSourceSetup.java index c6a1db7e5..c068a4a24 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaDataSourceSetup.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaDataSourceSetup.java @@ -81,6 +81,9 @@ public class JenaDataSourceSetup extends JenaDataSourceSetupBase implements java } if (userAccountsModel.size() == 0) { readOntologyFilesInPathSet(AUTHPATH, sce.getServletContext(), userAccountsModel); + if (userAccountsModel.size() == 0) { + createInitialAdminUser(userAccountsModel); + } } ensureEssentialInterfaceData(memModel, sce, wadf); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaDataSourceSetupBase.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaDataSourceSetupBase.java index db1c0cc2d..c171aa434 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaDataSourceSetupBase.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaDataSourceSetupBase.java @@ -15,8 +15,10 @@ import com.hp.hpl.jena.graph.Graph; import com.hp.hpl.jena.ontology.OntModelSpec; import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.ModelFactory; +import com.hp.hpl.jena.rdf.model.Resource; import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties; +import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary; import edu.cornell.mannlib.vitro.webapp.dao.jena.RDBGraphGenerator; import edu.cornell.mannlib.vitro.webapp.dao.jena.RegeneratingGraph; @@ -126,18 +128,53 @@ public class JenaDataSourceSetupBase { return dbModel; } - public static void readOntologyFilesInPathSet(String path, ServletContext ctx, Model model) { - Set paths = ctx.getResourcePaths(path); - for(String p : paths) { - log.debug("Loading ontology file at " + p); - InputStream ontologyInputStream = ctx.getResourceAsStream(p); - try { - model.read(ontologyInputStream,null); - log.debug("...successful"); - } catch (Throwable t) { - log.debug("...unsuccessful"); - } - } - } + public static void readOntologyFilesInPathSet(String path, + ServletContext ctx, Model model) { + Set paths = ctx.getResourcePaths(path); + if (paths != null) { + for (String p : paths) { + log.debug("Loading ontology file at " + p); + InputStream ontologyInputStream = ctx.getResourceAsStream(p); + try { + model.read(ontologyInputStream, null); + log.debug("...successful"); + } catch (Throwable t) { + log.debug("...unsuccessful"); + } + } + } + } + /** + * If the {@link ConfigurationProperties} has a name for the initial admin + * user, create the user and add it to the model. + */ + protected void createInitialAdminUser(Model model) { + String initialAdminUsername = ConfigurationProperties + .getProperty("initialAdminUser"); + if (initialAdminUsername == null) { + return; + } + + // A hard-coded MD5 encryption of "defaultAdmin" + String initialAdminPassword = "22BA075EC8951A70960A0A95C0BC2294"; + + String vitroDefaultNs = "http://vitro.mannlib.cornell.edu/ns/vitro/default#"; + + Resource user = model.createResource(vitroDefaultNs + + "defaultAdminUser"); + model.add(model.createStatement(user, model + .createProperty(VitroVocabulary.RDF_TYPE), model + .createLiteral(VitroVocabulary.USER))); + model.add(model.createStatement(user, model + .createProperty(VitroVocabulary.USER_USERNAME), model + .createTypedLiteral(initialAdminUsername))); + model.add(model.createStatement(user, model + .createProperty(VitroVocabulary.USER_MD5PASSWORD), model + .createTypedLiteral(initialAdminPassword))); + model.add(model.createStatement(user, model + .createProperty(VitroVocabulary.USER_ROLE), model + .createTypedLiteral("role:/50"))); + } + } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaPersistentDataSourceSetup.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaPersistentDataSourceSetup.java index 5ee7f70a2..b5b68ada6 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaPersistentDataSourceSetup.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaPersistentDataSourceSetup.java @@ -89,9 +89,13 @@ public class JenaPersistentDataSourceSetup extends JenaDataSourceSetupBase imple // user accounts Model try { Model userAccountsDbModel = makeDBModelFromConfigurationProperties(JENA_USER_ACCOUNTS_MODEL, DB_ONT_MODEL_SPEC); - if (firstStartup) { - readOntologyFilesInPathSet(AUTHPATH, sce.getServletContext(), userAccountsDbModel); - } + if (userAccountsDbModel.size() == 0) { + readOntologyFilesInPathSet(AUTHPATH, sce.getServletContext(), + userAccountsDbModel); + if (userAccountsDbModel.size() == 0) { + createInitialAdminUser(userAccountsDbModel); + } + } OntModel userAccountsModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC); userAccountsModel.add(userAccountsDbModel); userAccountsModel.getBaseModel().register(new ModelSynchronizer(userAccountsDbModel));