use of javax.jcr.NodeIterator in project jackrabbit by apache.
the class VirtualNodeTypeStateManager method recursiveRemove.
/**
* Adds a subtree of itemstates as 'removed' to a list of events
*
* @param events
* @param parent
* @param node
* @throws RepositoryException
*/
private void recursiveRemove(List<EventState> events, NodeImpl parent, NodeImpl node) throws RepositoryException {
events.add(EventState.childNodeRemoved(parent.getNodeId(), parent.getPrimaryPath(), node.getNodeId(), node.getPrimaryPath().getLastElement(), ((NodeTypeImpl) parent.getPrimaryNodeType()).getQName(), parent.getMixinTypeNames(), node.getSession()));
NodeIterator niter = node.getNodes();
while (niter.hasNext()) {
NodeImpl n = (NodeImpl) niter.nextNode();
recursiveRemove(events, node, n);
}
}
use of javax.jcr.NodeIterator in project jackrabbit by apache.
the class VirtualNodeTypeStateManager method recursiveAdd.
/**
* Adds a subtree of itemstates as 'added' to a list of events
*
* @param events
* @param parent
* @param node
* @throws RepositoryException
*/
private void recursiveAdd(List<EventState> events, NodeImpl parent, NodeImpl node) throws RepositoryException {
events.add(EventState.childNodeAdded(parent.getNodeId(), parent.getPrimaryPath(), node.getNodeId(), node.getPrimaryPath().getLastElement(), ((NodeTypeImpl) parent.getPrimaryNodeType()).getQName(), parent.getMixinTypeNames(), node.getSession()));
PropertyIterator iter = node.getProperties();
while (iter.hasNext()) {
PropertyImpl prop = (PropertyImpl) iter.nextProperty();
events.add(EventState.propertyAdded((NodeId) node.getId(), node.getPrimaryPath(), prop.getPrimaryPath().getLastElement(), ((NodeTypeImpl) node.getPrimaryNodeType()).getQName(), node.getMixinTypeNames(), node.getSession()));
}
NodeIterator niter = node.getNodes();
while (niter.hasNext()) {
NodeImpl n = (NodeImpl) niter.nextNode();
recursiveAdd(events, node, n);
}
}
use of javax.jcr.NodeIterator in project jackrabbit by apache.
the class ACLEditor method setPolicy.
/**
* @see AccessControlEditor#setPolicy(String,AccessControlPolicy)
*/
public void setPolicy(String nodePath, AccessControlPolicy policy) throws AccessControlException, PathNotFoundException, RepositoryException {
checkProtectsNode(nodePath);
checkValidPolicy(nodePath, policy);
ACLTemplate acl = (ACLTemplate) policy;
NodeImpl acNode = getAcNode(nodePath);
if (acNode == null) {
throw new PathNotFoundException("No such node " + nodePath);
}
// write the entries to the node
NodeImpl aclNode;
if (acNode.hasNode(N_POLICY)) {
aclNode = acNode.getNode(N_POLICY);
// remove all existing aces
for (NodeIterator aceNodes = aclNode.getNodes(); aceNodes.hasNext(); ) {
NodeImpl aceNode = (NodeImpl) aceNodes.nextNode();
removeItem(aceNode);
}
} else {
/* doesn't exist yet -> create */
aclNode = addNode(acNode, N_POLICY, NT_REP_ACL);
}
/* add all new entries defined on the template */
AccessControlEntry[] aces = acl.getAccessControlEntries();
for (AccessControlEntry ace1 : aces) {
AccessControlEntryImpl ace = (AccessControlEntryImpl) ace1;
// create the ACE node
Name nodeName = getUniqueNodeName(aclNode, "entry");
Name ntName = (ace.isAllow()) ? NT_REP_GRANT_ACE : NT_REP_DENY_ACE;
NodeImpl aceNode = addNode(aclNode, nodeName, ntName);
ValueFactory vf = session.getValueFactory();
// write the rep:principalName property
setProperty(aceNode, P_PRINCIPAL_NAME, vf.createValue(ace.getPrincipal().getName()));
// ... and the rep:privileges property
Privilege[] privs = ace.getPrivileges();
Value[] vs = new Value[privs.length];
for (int j = 0; j < privs.length; j++) {
vs[j] = vf.createValue(privs[j].getName(), PropertyType.NAME);
}
setProperty(aceNode, P_PRIVILEGES, vs);
// store the restrictions:
Set<Name> restrNames = ace.getRestrictions().keySet();
for (Name restrName : restrNames) {
Value value = ace.getRestriction(restrName);
setProperty(aceNode, restrName, value);
}
}
// mark the parent modified.
markModified((NodeImpl) aclNode.getParent());
}
use of javax.jcr.NodeIterator in project jackrabbit by apache.
the class MembershipCache method traverseAndCollect.
/**
* traverses the groups structure to find the groups of which the given authorizable is member of.
*
* @param authorizableNodeIdentifier Identifier of the authorizable node
* @param pIds output set to update of group node ids that were found via the property memberships
* @param nIds output set to update of group node ids that were found via the node memberships
* @param node the node to traverse
* @throws RepositoryException if an error occurs
*/
private void traverseAndCollect(String authorizableNodeIdentifier, Set<String> pIds, Set<String> nIds, NodeImpl node) throws RepositoryException {
if (node.isNodeType(NT_REP_GROUP)) {
String groupId = node.getIdentifier();
if (node.hasProperty(P_MEMBERS)) {
for (Value value : node.getProperty(P_MEMBERS).getValues()) {
String v = value.getString();
if (v.equals(authorizableNodeIdentifier)) {
pIds.add(groupId);
}
}
}
NodeIterator iter = node.getNodes();
while (iter.hasNext()) {
NodeImpl child = (NodeImpl) iter.nextNode();
if (child.isNodeType(NT_REP_MEMBERS)) {
isMemberOfNodeBaseMembershipGroup(authorizableNodeIdentifier, groupId, nIds, child);
}
}
} else {
NodeIterator iter = node.getNodes();
while (iter.hasNext()) {
NodeImpl child = (NodeImpl) iter.nextNode();
traverseAndCollect(authorizableNodeIdentifier, pIds, nIds, child);
}
}
}
use of javax.jcr.NodeIterator in project jackrabbit by apache.
the class ACLProvider method getEffectivePolicies.
/**
* @see org.apache.jackrabbit.core.security.authorization.AccessControlProvider#getEffectivePolicies(org.apache.jackrabbit.spi.Path,org.apache.jackrabbit.core.security.authorization.CompiledPermissions)
*/
public AccessControlPolicy[] getEffectivePolicies(Path absPath, CompiledPermissions permissions) throws ItemNotFoundException, RepositoryException {
if (absPath == null) {
// TODO: JCR-2774
log.warn("TODO: JCR-2774 - Repository level permissions.");
return new AccessControlPolicy[0];
}
String jcrPath = session.getJCRPath(absPath);
String pName = ISO9075.encode(session.getJCRName(ACLTemplate.P_NODE_PATH));
int ancestorCnt = absPath.getAncestorCount();
// search all ACEs whose rep:nodePath property equals the specified
// absPath or any of it's ancestors
StringBuilder stmt = new StringBuilder("/jcr:root");
stmt.append(acRoot.getPath());
stmt.append("//element(*,");
stmt.append(session.getJCRName(NT_REP_ACE));
stmt.append(")[");
for (int i = 0; i <= ancestorCnt; i++) {
String path = Text.getRelativeParent(jcrPath, i);
if (i > 0) {
stmt.append(" or ");
}
stmt.append("@");
stmt.append(pName);
stmt.append("='");
stmt.append(path.replaceAll("'", "''"));
stmt.append("'");
}
stmt.append("]");
QueryResult result;
try {
QueryManager qm = session.getWorkspace().getQueryManager();
Query q = qm.createQuery(stmt.toString(), Query.XPATH);
result = q.execute();
} catch (RepositoryException e) {
log.error("Unexpected error while searching effective policies. {}", e.getMessage());
throw new UnsupportedOperationException("Retrieve effective policies at absPath '" + jcrPath + "' not supported.", e);
}
/**
* Loop over query results and verify that
* - the corresponding ACE really takes effect on the specified absPath.
* - the corresponding ACL can be read by the editing session.
*/
Set<AccessControlPolicy> acls = new LinkedHashSet<AccessControlPolicy>();
for (NodeIterator it = result.getNodes(); it.hasNext(); ) {
Node aceNode = it.nextNode();
String accessControlledNodePath = Text.getRelativeParent(aceNode.getPath(), 2);
Path acPath = session.getQPath(accessControlledNodePath);
AccessControlPolicy[] policies = editor.getPolicies(accessControlledNodePath);
if (policies.length > 0) {
ACLTemplate acl = (ACLTemplate) policies[0];
for (AccessControlEntry ace : acl.getAccessControlEntries()) {
ACLTemplate.Entry entry = (ACLTemplate.Entry) ace;
if (entry.matches(jcrPath)) {
if (permissions.grants(acPath, Permission.READ_AC)) {
acls.add(new UnmodifiableAccessControlList(acl));
break;
} else {
throw new AccessDeniedException("Access denied at " + accessControlledNodePath);
}
}
}
}
}
return acls.toArray(new AccessControlPolicy[acls.size()]);
}
Aggregations