Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Move FedoraUserSecurityContext instantiation to fad Partial resolution of: https://jira.duraspace.org/browse/FCREPO-1714
- Loading branch information
Showing
4 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/main/java/org/fcrepo/auth/webac/FedoraWebACUserSecurityContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* Copyright 2015 DuraSpace, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.fcrepo.auth.webac; | ||
|
||
import static org.fcrepo.auth.webac.URIConstants.WEBAC_MODE_APPEND_VALUE; | ||
import static org.fcrepo.auth.webac.URIConstants.WEBAC_MODE_CONTROL_VALUE; | ||
import static org.fcrepo.auth.webac.URIConstants.WEBAC_MODE_READ_VALUE; | ||
import static org.fcrepo.auth.webac.URIConstants.WEBAC_MODE_WRITE_VALUE; | ||
|
||
import java.security.Principal; | ||
|
||
import org.fcrepo.auth.common.FedoraAuthorizationDelegate; | ||
import org.fcrepo.auth.common.FedoraUserSecurityContext; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* The security context for Fedora WebAC servlet users. These users are not | ||
* necessarily authenticated by the container, i.e. users may include the | ||
* general public. This security context delegates all access decisions to the | ||
* configured authorization delegate. | ||
* | ||
* @author mohideen | ||
*/ | ||
public class FedoraWebACUserSecurityContext extends FedoraUserSecurityContext { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(FedoraWebACUserSecurityContext.class); | ||
|
||
/** | ||
* Constructs a new security context. | ||
* | ||
* @param userPrincipal the user principal associated with this security context | ||
* @param fad the authorization delegate | ||
*/ | ||
protected FedoraWebACUserSecurityContext(final Principal userPrincipal, | ||
final FedoraAuthorizationDelegate fad) { | ||
super(userPrincipal, fad); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @see org.modeshape.jcr.security.SecurityContext#hasRole(String) | ||
*/ | ||
@Override | ||
public final boolean hasRole(final String roleName) { | ||
LOGGER.debug("Checking hasRole({})", roleName); | ||
|
||
if (WEBAC_MODE_READ_VALUE.equals(roleName)) { | ||
return true; | ||
} else if (WEBAC_MODE_WRITE_VALUE.equals(roleName)) { | ||
return true; | ||
} else if (WEBAC_MODE_APPEND_VALUE.equals(roleName)) { | ||
return true; | ||
} else if (WEBAC_MODE_CONTROL_VALUE.equals(roleName)) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/test/java/org/fcrepo/auth/webac/FedoraWebACUserSecurityContextTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* Copyright 2015 DuraSpace, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.fcrepo.auth.webac; | ||
|
||
import static org.fcrepo.auth.webac.URIConstants.WEBAC_MODE_READ_VALUE; | ||
import static org.fcrepo.auth.webac.URIConstants.WEBAC_MODE_WRITE_VALUE; | ||
import static org.fcrepo.auth.webac.URIConstants.WEBAC_MODE_APPEND_VALUE; | ||
import static org.fcrepo.auth.webac.URIConstants.WEBAC_MODE_CONTROL_VALUE; | ||
|
||
import java.security.Principal; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
import org.fcrepo.auth.common.FedoraAuthorizationDelegate; | ||
import org.fcrepo.auth.common.FedoraUserSecurityContext; | ||
import org.fcrepo.auth.common.FedoraUserSecurityContextTest; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.mockito.Mock; | ||
|
||
/** | ||
* @author mohideen | ||
* @since 9/1/15. | ||
*/ | ||
public class FedoraWebACUserSecurityContextTest extends FedoraUserSecurityContextTest { | ||
|
||
@Mock | ||
private FedoraAuthorizationDelegate fad; | ||
@Mock | ||
private Principal principal; | ||
@Mock | ||
private HttpServletRequest request; | ||
|
||
@Test | ||
public void testHasRole() { | ||
final FedoraUserSecurityContext context = new FedoraWebACUserSecurityContext(this.principal, this.fad); | ||
Assert.assertTrue(context.hasRole(WEBAC_MODE_READ_VALUE)); | ||
Assert.assertTrue(context.hasRole(WEBAC_MODE_WRITE_VALUE)); | ||
Assert.assertTrue(context.hasRole(WEBAC_MODE_APPEND_VALUE)); | ||
Assert.assertTrue(context.hasRole(WEBAC_MODE_CONTROL_VALUE)); | ||
Assert.assertFalse(context.hasRole(null)); | ||
Assert.assertFalse(context.hasRole("other")); | ||
} | ||
|
||
} |