Skip to content

Commit

Permalink
Create new identifier translation framework with unit tests
Browse files Browse the repository at this point in the history
- Deprecate GraphSubjects...
- Remove deprecated methods from GraphSubjects
- Integrate new identifier translation system into HTTP layer
- Add reverse translation to HTTP API

Resolves: https://www.pivotaltracker.com/story/show/67618364
  • Loading branch information
ajs6f authored and Andrew Woods committed Apr 14, 2014
1 parent 95a825e commit 6c9cdca
Show file tree
Hide file tree
Showing 137 changed files with 1,703 additions and 891 deletions.
Expand Up @@ -40,7 +40,7 @@ protected static class HttpHeaderPrincipal implements Principal {

private final String name;

HttpHeaderPrincipal(String name) {
HttpHeaderPrincipal(final String name) {
this.name = name;
}

Expand All @@ -55,22 +55,20 @@ public String toString() {
}

@Override
public boolean equals(Object o) {
public boolean equals(final Object o) {
if (o instanceof HttpHeaderPrincipal) {
return ((HttpHeaderPrincipal) o).getName().equals(
this.getName());
} else {
return false;
}
return false;
}

@Override
public int hashCode() {
if (name == null) {
return 0;
} else {
return name.hashCode();
}
return name.hashCode();
}

}
Expand All @@ -82,14 +80,14 @@ public int hashCode() {
/**
* @param headerName The name of the header from which to extract principals
*/
public void setHeaderName(String headerName) {
public void setHeaderName(final String headerName) {
this.headerName = headerName;
}

/**
* @param separator The string by which to split header values
*/
public void setSeparator(String separator) {
public void setSeparator(final String separator) {
this.separator = separator;
}

Expand Down
Expand Up @@ -46,8 +46,6 @@
import javax.jcr.security.Privilege;
import javax.servlet.http.HttpServletRequest;

import java.io.IOException;

/**
* @author Gregory Jansen
*/
Expand All @@ -67,7 +65,7 @@ public class ModeShapeHonorsFADResponseIT {
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);

@Before
public void setUp() throws RepositoryException, IOException {
public void setUp() {
// final Map<String, String> config = new HashMap<String, String>();
// config.put(JcrRepositoryFactory.URL,
// "file:src/test/resources/repository.json");
Expand Down
Expand Up @@ -16,10 +16,6 @@

package org.fcrepo.auth.integration;

import java.security.Principal;
import java.util.Iterator;
import java.util.Set;

import javax.jcr.Session;

import org.fcrepo.auth.common.FedoraAuthorizationDelegate;
Expand Down
6 changes: 6 additions & 0 deletions fcrepo-auth-common/src/test/resources/spring-test/rest.xml
Expand Up @@ -7,8 +7,14 @@
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<bean class="org.fcrepo.http.commons.session.SessionFactory"/>

<!-- Mints PIDs-->
<bean class="org.fcrepo.kernel.identifiers.UUIDPidMinter"/>

<!-- Identifier translation chain -->
<util:list id="translationChain" value-type="org.fcrepo.kernel.identifiers.InternalIdentifierConverter">
<bean class="org.fcrepo.kernel.identifiers.NamespaceConverter"/>
</util:list>

<context:component-scan base-package="org.fcrepo.http, org.fcrepo.kernel.services, org.fcrepo.serialization" />
</beans>
6 changes: 6 additions & 0 deletions fcrepo-auth-oauth/src/test/resources/spring-test/rest.xml
Expand Up @@ -10,6 +10,12 @@

<!-- Mints PIDs-->
<bean class="org.fcrepo.kernel.identifiers.UUIDPidMinter"/>

<!-- Identifier translation chain -->
<util:list id="translationChain" value-type="org.fcrepo.kernel.identifiers.InternalIdentifierConverter">
<bean class="org.fcrepo.kernel.identifiers.NamespaceConverter"/>
</util:list>


<context:annotation-config/>

Expand Down
Expand Up @@ -7,8 +7,13 @@
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<bean class="org.fcrepo.http.commons.session.SessionFactory"/>

<!-- Mints PIDs-->
<bean class="org.fcrepo.kernel.identifiers.UUIDPidMinter"/>

<!-- Identifier translation chain -->
<util:list id="translationChain" value-type="org.fcrepo.kernel.identifiers.InternalIdentifierConverter">
<bean class="org.fcrepo.kernel.identifiers.NamespaceConverter"/>
</util:list>
<context:component-scan base-package="org.fcrepo.kernel.services, org.fcrepo.http, org.fcrepo.serialization, org.fcrepo.auth.roles.common" />
</beans>
Expand Up @@ -78,7 +78,7 @@ public abstract class AbstractRolesAuthorizationDelegate implements FedoraAuthor
}

@Override
public boolean hasPermission(Session session, final Path absPath,
public boolean hasPermission(final Session session, final Path absPath,
final String[] actions) {
final Set<String> roles;

Expand Down Expand Up @@ -131,23 +131,21 @@ public boolean hasPermission(Session session, final Path absPath,
return true;
}

private Principal getUserPrincipal(Session session) {
private static Principal getUserPrincipal(final Session session) {
final Object value = session.getAttribute(FEDORA_USER_PRINCIPAL);
if (value instanceof Principal) {
return (Principal) value;
} else {
return null;
}
return null;
}

@SuppressWarnings("unchecked")
private Set<Principal> getPrincipals(Session session) {
private static Set<Principal> getPrincipals(final Session session) {
final Object value = session.getAttribute(FEDORA_ALL_PRINCIPALS);
if (value instanceof Set<?>) {
return (Set<Principal>) value;
} else {
return null;
}
return null;
}

/**
Expand Down
Expand Up @@ -17,7 +17,6 @@
package org.fcrepo.auth.roles.common;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -34,13 +33,19 @@
import org.fcrepo.auth.roles.common.Constants.JcrName;
import org.modeshape.jcr.value.Path;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import static com.google.common.collect.Iterables.toArray;
import static java.util.Collections.emptyMap;
import static org.fcrepo.auth.roles.common.Constants.registerPrefixes;
import static org.fcrepo.auth.roles.common.Constants.JcrName.Assignment;
import static org.fcrepo.auth.roles.common.Constants.JcrName.Rbacl;
import static org.fcrepo.auth.roles.common.Constants.JcrName.assignment;
import static org.fcrepo.auth.roles.common.Constants.JcrName.principal;
import static org.fcrepo.auth.roles.common.Constants.JcrName.rbacl;
import static org.fcrepo.auth.roles.common.Constants.JcrName.rbaclAssignable;
import static org.fcrepo.auth.roles.common.Constants.JcrName.role;
import static org.slf4j.LoggerFactory.getLogger;

/**
* Provides the effective access roles for authorization.
Expand All @@ -50,11 +55,9 @@
@Component
public class AccessRolesProvider {

private static final Logger LOGGER = LoggerFactory
.getLogger(AccessRolesProvider.class);
private static final Logger LOGGER = getLogger(AccessRolesProvider.class);

public static final Map<String, List<String>> DEFAULT_ACCESS_ROLES =
Collections.emptyMap();
public static final Map<String, List<String>> DEFAULT_ACCESS_ROLES = emptyMap();

/**
* Get the roles assigned to this Node. Optionally search up the tree for
Expand All @@ -64,23 +67,19 @@ public class AccessRolesProvider {
* @param effective if true then search for effective roles
* @return a set of roles for each principal
*/
public Map<String, List<String>>
getRoles(Node node, final boolean effective)
throws RepositoryException {
final Map<String, List<String>> data =
new HashMap<>();
public Map<String, List<String>> getRoles(Node node, final boolean effective) throws RepositoryException {
final Map<String, List<String>> data = new HashMap<>();
final Session session = node.getSession();
registerPrefixes(session);
if (node.isNodeType(JcrName.rbaclAssignable.getQualified())) {
if (node.isNodeType(rbaclAssignable.getQualified())) {
getAssignments(node, data);
return data;
}
if (effective) { // look up the tree
try {
for (node = node.getParent(); node != null; node =
node.getParent()) {
if (node.isNodeType(JcrName.rbaclAssignable
.getQualified())) {
if (node.isNodeType(rbaclAssignable.getQualified())) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("effective roles are assigned at node: {}",
node.getPath());
Expand Down Expand Up @@ -130,7 +129,7 @@ private void getAssignments(final Node node, final Map<String, List<String>> dat
data.put(principalName, roles);
}
for (final Value v : assign.getProperty(
JcrName.role.getQualified()).getValues()) {
role.getQualified()).getValues()) {
if (v == null || v.toString().trim().length() == 0) {
LOGGER.warn("found empty role name on node {}",
node.getPath());
Expand Down Expand Up @@ -158,30 +157,27 @@ private void getAssignments(final Node node, final Map<String, List<String>> dat
public void postRoles(final Node node, final Map<String, Set<String>> data)
throws RepositoryException {
final Session session = node.getSession();
Constants.registerPrefixes(session);
if (!node.isNodeType(JcrName.rbaclAssignable.getQualified())) {
node.addMixin(JcrName.rbaclAssignable.getQualified());
registerPrefixes(session);
if (!node.isNodeType(rbaclAssignable.getQualified())) {
node.addMixin(rbaclAssignable.getQualified());
LOGGER.debug("added rbaclAssignable type");
}

Node acl;

if (node.hasNode(JcrName.rbacl.getQualified())) {
acl = node.getNode(JcrName.rbacl.getQualified());
if (node.hasNode(rbacl.getQualified())) {
acl = node.getNode(rbacl.getQualified());
for (final NodeIterator ni = acl.getNodes(); ni.hasNext();) {
ni.nextNode().remove();
}
} else {
acl = node.addNode(JcrName.rbacl.getQualified(), JcrName.Rbacl
.getQualified());
acl = node.addNode(rbacl.getQualified(), Rbacl.getQualified());
}

for (final Map.Entry<String, Set<String>> entry : data.entrySet()) {
final Node assign =
acl.addNode(JcrName.assignment.getQualified(),
JcrName.Assignment.getQualified());
assign.setProperty(JcrName.principal.getQualified(), entry.getKey());
assign.setProperty(JcrName.role.getQualified(), toArray(entry.getValue(), String.class));
final Node assign = acl.addNode(assignment.getQualified(), Assignment.getQualified());
assign.setProperty(principal.getQualified(), entry.getKey());
assign.setProperty(role.getQualified(), toArray(entry.getValue(), String.class));
}
}

Expand All @@ -192,8 +188,8 @@ public void postRoles(final Node node, final Map<String, Set<String>> data)
*/
public void deleteRoles(final Node node) throws RepositoryException {
final Session session = node.getSession();
Constants.registerPrefixes(session);
if (node.isNodeType(JcrName.rbaclAssignable.getQualified())) {
registerPrefixes(session);
if (node.isNodeType(rbaclAssignable.getQualified())) {
// remove rbacl child
try {
final Node rbacl = node.getNode(JcrName.rbacl.getQualified());
Expand All @@ -202,7 +198,7 @@ public void deleteRoles(final Node node) throws RepositoryException {
LOGGER.debug("Cannot find node: {}", node, e);
}
// remove mixin
node.removeMixin(JcrName.rbaclAssignable.getQualified());
node.removeMixin(rbaclAssignable.getQualified());
}
}

Expand Down
Expand Up @@ -27,7 +27,7 @@
import org.fcrepo.jcr.FedoraJcrTypes;
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.RdfLexicon;
import org.fcrepo.kernel.rdf.GraphSubjects;
import org.fcrepo.kernel.rdf.IdentifierTranslator;
import org.springframework.stereotype.Component;

import com.hp.hpl.jena.rdf.model.Model;
Expand All @@ -46,14 +46,14 @@ public class AccessRolesResources implements UriAwareResourceModelFactory {
* (non-Javadoc)
* @see org.fcrepo.http.commons.api.rdf.UriAwareResourceModelFactory#
* createModelForResource( org.fcrepo.kernel.FedoraResourceImpl,
* javax.ws.rs.core.UriInfo, org.fcrepo.kernel.rdf.GraphSubjects)
* javax.ws.rs.core.UriInfo, org.fcrepo.kernel.rdf.IdentifierTranslator)
*/
@Override
public Model createModelForResource(final FedoraResource resource,
final UriInfo uriInfo, final GraphSubjects graphSubjects)
final UriInfo uriInfo, final IdentifierTranslator graphSubjects)
throws RepositoryException {
final Model model = ModelFactory.createDefaultModel();
final Resource s = graphSubjects.getGraphSubject(resource.getNode());
final Resource s = graphSubjects.getSubject(resource.getNode().getPath());

if (resource.getNode().isNodeType(
FedoraJcrTypes.FEDORA_RESOURCE)) {
Expand Down
Expand Up @@ -20,7 +20,6 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
Expand All @@ -32,9 +31,10 @@
import org.fcrepo.jcr.FedoraJcrTypes;
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.RdfLexicon;
import org.fcrepo.kernel.rdf.GraphSubjects;
import org.fcrepo.kernel.rdf.IdentifierTranslator;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Matchers;
import org.mockito.Mock;

import com.hp.hpl.jena.rdf.model.Model;
Expand All @@ -49,7 +49,7 @@
public class AccessRolesResourcesTest {

@Mock
private GraphSubjects graphSubjects;
private IdentifierTranslator graphSubjects;

@Mock
private FedoraResource fedoraResource;
Expand Down Expand Up @@ -77,7 +77,7 @@ public void setUp() throws RepositoryException {
model = ModelFactory.createDefaultModel();
graphResource = model.createResource("/" + pathString);

when(graphSubjects.getGraphSubject(any(Node.class))).thenReturn(
when(graphSubjects.getSubject(Matchers.anyString())).thenReturn(
graphResource);
when(fedoraResource.getNode()).thenReturn(resourceNode);

Expand Down
Expand Up @@ -69,8 +69,7 @@ public class AccessRolesTypesTest {
private NodeType mockNodeType;

@Before
public void setUp() throws RepositoryException, NoSuchFieldException,
IOException {
public void setUp() throws RepositoryException, IOException {
initMocks(this);

when(session.getWorkspace()).thenReturn(workspace);
Expand Down
Expand Up @@ -7,8 +7,14 @@
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<bean class="org.fcrepo.http.commons.session.SessionFactory"/>

<!-- Mints PIDs-->
<bean class="org.fcrepo.kernel.identifiers.UUIDPidMinter"/>

<!-- Identifier translation chain -->
<util:list id="translationChain" value-type="org.fcrepo.kernel.identifiers.InternalIdentifierConverter">
<bean class="org.fcrepo.kernel.identifiers.NamespaceConverter"/>
</util:list>

<context:component-scan base-package="org.fcrepo.kernel.services, org.fcrepo.http, org.fcrepo.serialization, org.fcrepo.auth.roles.common" />
</beans>

0 comments on commit 6c9cdca

Please sign in to comment.