Skip to content

Commit fc756c5

Browse files
committed
OAK-11498: Expose Session-bound principals via JackrabbitSession
1 parent 64fd6ad commit fc756c5

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

oak-jackrabbit-api/src/main/java/org/apache/jackrabbit/api/JackrabbitSession.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,20 @@
1616
*/
1717
package org.apache.jackrabbit.api;
1818

19-
import org.apache.jackrabbit.api.security.user.UserManager;
20-
import org.apache.jackrabbit.api.security.principal.PrincipalManager;
19+
import java.security.Principal;
20+
import java.util.Set;
2121

22+
import javax.jcr.AccessDeniedException;
2223
import javax.jcr.Item;
2324
import javax.jcr.ItemNotFoundException;
2425
import javax.jcr.Node;
2526
import javax.jcr.Property;
26-
import javax.jcr.Session;
27-
import javax.jcr.AccessDeniedException;
2827
import javax.jcr.RepositoryException;
28+
import javax.jcr.Session;
2929
import javax.jcr.UnsupportedRepositoryOperationException;
3030

31+
import org.apache.jackrabbit.api.security.principal.PrincipalManager;
32+
import org.apache.jackrabbit.api.security.user.UserManager;
3133
import org.jetbrains.annotations.NotNull;
3234
import org.jetbrains.annotations.Nullable;
3335
import org.osgi.annotation.versioning.ProviderType;
@@ -277,4 +279,12 @@ default Node getParentOrNull(@NotNull Item item) throws RepositoryException {
277279
return null;
278280
}
279281
}
282+
283+
/**
284+
* Returns the set of principals associated with this session.
285+
* @return the set of principals associated with this session.
286+
* @throws RepositoryException in case principal information cannot be retrieved.
287+
* @since 1.78
288+
*/
289+
@NotNull Set<Principal> getPrincipals() throws RepositoryException;
280290
}

oak-jackrabbit-api/src/main/java/org/apache/jackrabbit/api/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818
/**
1919
* Jackrabbit extensions for JCR core interfaces
2020
*/
21-
@org.osgi.annotation.versioning.Version("2.9.0")
21+
@org.osgi.annotation.versioning.Version("2.10.0")
2222
package org.apache.jackrabbit.api;
2323

oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/session/SessionImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.InputStream;
2525
import java.io.OutputStream;
2626
import java.security.AccessControlException;
27+
import java.security.Principal;
2728
import java.util.Collections;
2829
import java.util.Set;
2930
import java.util.TreeSet;
@@ -845,6 +846,12 @@ public UserManager getUserManager() throws RepositoryException {
845846
return sessionContext.getUserManager();
846847
}
847848

849+
@Override
850+
@NotNull
851+
public Set<Principal> getPrincipals() throws RepositoryException {
852+
return sd.getAuthInfo().getPrincipals();
853+
}
854+
848855
@Override
849856
public String toString() {
850857
if (isLive()) {

oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/session/JackrabbitSessionTest.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030

3131
import static org.mockito.Mockito.mock;
3232

33+
import java.security.Principal;
34+
import java.util.Set;
35+
3336
public class JackrabbitSessionTest extends AbstractJCRTest {
3437

3538
private JackrabbitSession s;
@@ -81,6 +84,23 @@ public void testGetParentOrNullImplMismatch() {
8184
// success
8285
}
8386
}
84-
85-
87+
88+
public void testGetPrincipalsForAdminSession() throws RepositoryException {
89+
Set<Principal> principals = s.getPrincipals();
90+
assertNotNull(principals);
91+
assertTrue("Admin principal expected", principals.contains(s.getPrincipalManager().getPrincipal("admin")));
92+
assertTrue("Everyone principal expected", principals.contains(s.getPrincipalManager().getEveryone()));
93+
}
94+
95+
public void testGetPrincipalsForGuestSession() throws RepositoryException {
96+
JackrabbitSession guest = (JackrabbitSession) getHelper().getRepository().login(new GuestCredentials());
97+
try {
98+
Set<Principal> principals = guest.getPrincipals();
99+
assertNotNull(principals);
100+
assertFalse("Admin principal not expected", principals.contains(s.getPrincipalManager().getPrincipal("admin")));
101+
assertTrue("Everyone principal expected", principals.contains(s.getPrincipalManager().getEveryone()));
102+
} finally {
103+
guest.logout();
104+
}
105+
}
86106
}

0 commit comments

Comments
 (0)