NIHVIVO-2279 Hard-coded loading for our four favorite PermissionSets.

This commit is contained in:
j2blake 2011-05-27 19:04:55 +00:00
parent 5c36325445
commit 93738cb8bf
4 changed files with 117 additions and 4 deletions

View file

@ -0,0 +1,97 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.auth.permissions;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.shared.Lock;
import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties;
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelContext;
import edu.cornell.mannlib.vitro.webapp.servlet.setup.AbortStartup;
/**
* Load the initial configuration of PermissionSets and Permissions.
*
* The UserAccounts model must be created before this runs.
*
* For now, we just use the four hard-coded "roles".
*/
public class PermissionSetsLoader implements ServletContextListener {
private static final Log log = LogFactory
.getLog(PermissionSetsLoader.class);
@Override
public void contextInitialized(ServletContextEvent sce) {
ServletContext ctx = sce.getServletContext();
if (AbortStartup.isStartupAborted(ctx)) {
return;
}
try {
String ns = ConfigurationProperties.getBean(ctx).getProperty(
"Vitro.defaultNamespace");
OntModel model = ModelContext.getBaseOntModelSelector(ctx)
.getUserAccountsModel();
ModelWrapper wrapper = new ModelWrapper(model, ns);
wrapper.createPermissionSet("1", "Self Editor");
wrapper.createPermissionSet("2", "Editor");
wrapper.createPermissionSet("3", "Curator");
wrapper.createPermissionSet("4", "Site Admin");
} catch (Exception e) {
log.error("could not run PermissionSetsLoader" + e);
AbortStartup.abortStartup(ctx);
throw new RuntimeException(e);
}
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
// Nothing to tear down.
}
private static class ModelWrapper {
private final OntModel model;
private final String defaultNamespace;
private final Property typeProperty;
private final Property labelProperty;
private final Resource permissionSet;
public ModelWrapper(OntModel model, String defaultNamespace) {
this.model = model;
this.defaultNamespace = defaultNamespace;
typeProperty = model.createProperty(VitroVocabulary.RDF_TYPE);
labelProperty = model.createProperty(VitroVocabulary.LABEL);
permissionSet = model.createResource(VitroVocabulary.PERMISSIONSET);
}
public void createPermissionSet(String uriSuffix, String label) {
String uri = defaultNamespace + "permissionSet-" + uriSuffix;
model.enterCriticalSection(Lock.WRITE);
try {
Resource r = model.createResource(uri);
model.add(r, typeProperty, permissionSet);
model.add(r, labelProperty, label);
log.debug("Created permission set: '" + uri + "', '" + label
+ "'");
} finally {
model.leaveCriticalSection();
}
}
}
}

View file

@ -75,7 +75,7 @@ public interface UserAccountsDao {
PermissionSet getPermissionSetByUri(String uri);
/**
* Get all of the PermissionSets in the model.
* Get all of the PermissionSets in the model, sorted by URI.
*
* @return a collection which might be empty, but is never null.
*/

View file

@ -4,6 +4,8 @@ package edu.cornell.mannlib.vitro.webapp.dao.jena;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
@ -79,10 +81,11 @@ public class UserAccountsDaoJena extends JenaBaseDao implements UserAccountsDao
}
String userUri = null;
getOntModel().enterCriticalSection(Lock.READ);
try {
StmtIterator stmts = getOntModel().listStatements(null, USERACCOUNT_EMAIL_ADDRESS,
StmtIterator stmts = getOntModel().listStatements(null,
USERACCOUNT_EMAIL_ADDRESS,
getOntModel().createLiteral(emailAddress));
if (stmts.hasNext()) {
userUri = stmts.next().getSubject().getURI();
@ -90,7 +93,7 @@ public class UserAccountsDaoJena extends JenaBaseDao implements UserAccountsDao
} finally {
getOntModel().leaveCriticalSection();
}
return getUserAccountByUri(userUri);
}
@ -263,6 +266,8 @@ public class UserAccountsDaoJena extends JenaBaseDao implements UserAccountsDao
getOntModel().leaveCriticalSection();
}
Collections.sort(list, new PermissionSetsByUri());
return list;
}
@ -286,4 +291,11 @@ public class UserAccountsDaoJena extends JenaBaseDao implements UserAccountsDao
+ errMsg);
}
private static class PermissionSetsByUri implements
Comparator<PermissionSet> {
@Override
public int compare(PermissionSet ps1, PermissionSet ps2) {
return ps1.getUri().compareTo(ps2.getUri());
}
}
}