/** * Find ResourceRights for each descendant of a resource for a role. * * @param \Claroline\CoreBundle\Entity\Resource\ResourceNode $resource * @param \Claroline\CoreBundle\Entity\Role $role * * @return array */ public function findRecursiveByResourceAndRole(ResourceNode $resource, Role $role) { $dql = "\n SELECT rights, role, resource\n FROM Claroline\\CoreBundle\\Entity\\Resource\\ResourceRights rights\n JOIN rights.resourceNode resource\n JOIN rights.role role\n WHERE resource.path LIKE :path AND role.name = :roleName\n "; $query = $this->_em->createQuery($dql); $query->setParameter('path', $resource->getPath() . '%'); $query->setParameter('roleName', $role->getName()); return $query->getResult(); }
/** * Returns the ancestors of a resource, including the resource itself. * * @param ResourceNode $resource * * @return array[array] An array of resources represented as arrays */ public function findAncestors(ResourceNode $resource) { // No need to access DB to get ancestors as they are given by the materialized path. $regex = '/-(\\d+)' . ResourceNode::PATH_SEPARATOR . '/'; $parts = preg_split($regex, $resource->getPath(), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); $ancestors = array(); $currentPath = ''; for ($i = 0, $count = count($parts); $i < $count; $i += 2) { $ancestor = array(); $currentPath = $currentPath . $parts[$i] . '-' . $parts[$i + 1] . '`'; $ancestor['path'] = $currentPath; $ancestor['name'] = $parts[$i]; $ancestor['id'] = (int) $parts[$i + 1]; $ancestors[] = $ancestor; } return $ancestors; }
/** * @EXT\Route( * "/filter/{nodeId}", * name="claro_resource_filter", * options={"expose"=true} * ) * @EXT\ParamConverter( * "node", * class="ClarolineCoreBundle:Resource\ResourceNode", * options={"id" = "nodeId", "strictId" = true} * ) * * Returns a json representation of a resource search result. * * @param ResourceNode $node The id of the node from which the search was started * * @throws \Exception * @return Response */ public function filterAction(ResourceNode $node = null) { $criteria = $this->resourceManager->buildSearchArray($this->request->query->all()); $criteria['roots'] = $node ? array($node->getPath()) : array(); $path = $node ? $this->resourceManager->getAncestors($node) : array(); $userRoles = $this->roleManager->getStringRolesFromToken($this->tokenStorage->getToken()); //by criteria recursive => infinite loop $resources = $this->resourceManager->getByCriteria($criteria, $userRoles, true); return new JsonResponse(array('id' => $node ? $node->getId() : '0', 'nodes' => $resources, 'path' => $path)); }