Guest User

Untitled

a guest
Jan 20th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. /
  2. node [nt:unstructured]
  3. subnode [nt:unstructured]
  4.  
  5. SELECT * FROM [nt:unstructured]
  6.  
  7. public static void main(String[] args) throws Exception {
  8. Repository repository = new Jcr().with(new MySecurityProvider()).createRepository();
  9. Session session = repository.login(new UserIdCredentials("")); // principal is "SystemPrincipal.INSTANCE"
  10.  
  11. // Create nodes
  12. Node node = session.getRootNode().addNode("node", "nt:unstructured");
  13. node.addNode("subnode", "nt:unstructured");
  14.  
  15. // Add access control entry + restriction
  16. AccessControlManager acm = session.getAccessControlManager();
  17. JackrabbitAccessControlList acl = (JackrabbitAccessControlList) acm
  18. .getApplicablePolicies("/node").nextAccessControlPolicy();
  19. Privilege[] privileges = new Privilege[]{acm.privilegeFromName(Privilege.JCR_ALL)};
  20. Map<String, Value> restrictions = new HashMap<String, Value>() {{put("rep:glob", new StringValue(""));}};
  21. acl.addEntry(new PrincipalImpl("user"), privileges, true, restrictions);
  22. acm.setPolicy("/node", acl);
  23. session.save();
  24.  
  25. // executes query
  26. RowIterator rows = repository.login(new UserIdCredentials("user")).getWorkspace().getQueryManager()
  27. .createQuery("SELECT * FROM [nt:unstructured]", Query.JCR_SQL2).execute().getRows();
  28. System.out.println("Number of rows: " + rows.getSize()); //Prints 0
  29. }
  30.  
  31. class MyAuthenticationConfiguration extends AuthenticationConfigurationImpl {
  32. public MyAuthenticationConfiguration(SecurityProvider securityProvider) {
  33. super(securityProvider);
  34. }
  35.  
  36. @NotNull
  37. @Override
  38. public LoginContextProvider getLoginContextProvider(ContentRepository contentRepository) {
  39. return new LoginContextProvider() {
  40. @NotNull
  41. public LoginContext getLoginContext(Credentials credentials, String workspaceName) {
  42. String userId = ((UserIdCredentials) credentials).getUserId();
  43. Set<Principal> principalSets = new HashSet<>();
  44. if (userId.isEmpty()) {
  45. principalSets.add(SystemPrincipal.INSTANCE);
  46. } else {
  47. principalSets.add(new PrincipalImpl(userId));
  48. }
  49. Map<String, ? extends Principal> publicPrivileges = new HashMap<>();
  50. AuthInfoImpl authInfoImpl = new AuthInfoImpl(userId, publicPrivileges, principalSets);
  51. Subject subject = new Subject(true, principalSets, Collections.singleton(authInfoImpl), new HashSet<Principal>());
  52. return new PreAuthContext(subject);
  53. }
  54. };
  55. }
  56. }
Add Comment
Please, Sign In to add comment