/** * Creates a Node object based on given $routeName or current route. * * @param string|null $routeName * * @return Node */ public function createPageFromRoute($routeName = null) { if (!$routeName) { $routeName = $this->pageStack->getRequest()->attributes->get('_route'); if (!$routeName) { throw new \RuntimeException('Could not detect route name'); } } $reflection = new \ReflectionClass($this->router->getGenerator()); $key = 'jarves_routes'; $cache = $this->cacher->getFastCache($key); $validCache = false; $routes = []; if ($cache) { $validCache = $cache['time'] === filemtime($reflection->getFileName()) && isset($cache['routes']) && is_string($cache['routes']); if ($validCache) { $routes = unserialize($cache['routes']); } } if (!$validCache) { $routes = $this->router->getRouteCollection()->all(); $this->cacher->setFastCache($key, ['time' => filemtime($reflection->getFileName()), 'routes' => serialize($routes)]); } if (!isset($routes[$routeName])) { throw new \RuntimeException("Route with name `{$routeName}` does not exist"); } $route = $routes[$routeName]; $url = $this->router->generate($routeName, $this->pageStack->getRequest()->attributes->all()); $page = Node::createPage($route->getOption('title'), parse_url($url)['path'], $route->getOption('theme'), $route->getOption('layout')); if ($route->getOption('meta')) { foreach ((array) $route->getOption('meta') as $key => $value) { $page->meta->set($key, $value); } } return $page; }
/** * @static * * @param Node $pNode * @param array $pBoxedContents */ function installContents(Node $pNode, $pBoxedContents) { if (!is_array($pBoxedContents)) { return; } /** * 0: type74 * 1: title * 2: template * 3: content * */ foreach ($pBoxedContents as $boxId => $contents) { foreach ($contents as $content) { $oContent = new Content(); $oContent->setNodeId($pNode->getId()); $oContent->setBoxId($boxId); $oContent->setType($content[0]); $oContent->setTemplate($content[2]); $oContent->setContent($content[3]); $oContent->save(); } } }
/** * @return string */ public function getLayout(Node $node) { if ($nodeId = (int) $this->pageStack->getRequest()->get('_jarves_editor_node')) { if ($this->editMode->isEditMode($nodeId)) { if ($layout = $this->pageStack->getRequest()->get('_jarves_editor_layout')) { return $layout; } } } return $node->getLayout(); }
protected function exportNode($position, Domain $domain, Node $node, ExportPointer $exportPointer) { $data = $node->toArray(TableMap::TYPE_CAMELNAME); if (!$node->getUrn()) { return; } $path = $node->getUrn(); $exportPointer->pushPath($path); if ($node->getId() === $domain->getStartnodeId()) { $domain->setVirtualColumn('startnodePath', $path); } $nodeData = $this->clearData($data, ['id', 'lft', 'rgt', 'lvl', 'pid', 'urn', 'domainId'], ['type' => 0, 'layout' => 'default', 'visible' => true]); $nodeData['sort'] = $position; $contents = $node->getContents(); $contentsData = []; foreach ($contents as $content) { $contentData = $content->toArray(TableMap::TYPE_CAMELNAME); $jsonDecoded = json_decode($contentData['content'], true); if (JSON_ERROR_NONE === json_last_error()) { $contentData['content'] = $jsonDecoded; } $contentData = $this->clearData($contentData, ['id', 'nodeId'], ['template' => 'JarvesBundle:Default:content.html.twig', 'type' => 'text']); $contentsData[] = $contentData; } $nodeData['contents'] = $contentsData; $exportPointer->addData($nodeData, '.yml'); foreach ($node->getChildren() as $idx => $child) { $this->exportNode($idx, $domain, $child, $exportPointer); } $exportPointer->popPath(); }
public function testNestedSubPermission() { $this->getACL()->setCaching(false); $this->getACL()->removeObjectRules('jarves/node'); $tokenStorage = $this->getTokenStorage(); $token = new UsernamePasswordToken(UserQuery::create()->findOneByUsername('test'), null, "main"); $tokenStorage->setToken($token); $user = $this->getPageStack()->getUser(); $this->assertEquals('test', $user->getUsername()); $domain = DomainQuery::create()->findOne(); $root = NodeQuery::create()->findRoot($domain->getId()); $subNode = new Node(); $subNode->setTitle('TestNode tree'); $subNode->insertAsFirstChildOf($root); $subNode->save(); $subNode2 = new Node(); $subNode2->setTitle('TestNode sub'); $subNode2->insertAsFirstChildOf($subNode); $subNode2->save(); //make access for all $rule = new Acl(); $rule->setAccess(true); $rule->setObject('jarves/node'); $rule->setTargetType(\Jarves\ACL::TARGET_TYPE_USER); $rule->setTargetId($user->getId()); $rule->setMode(\Jarves\ACL::MODE_ALL); $rule->setConstraintType(\Jarves\ACL::CONSTRAINT_ALL); $rule->setPrio(2); $rule->save(); //revoke access for all children of `TestNode tree` $rule2 = new Acl(); $rule2->setAccess(false); $rule2->setObject('jarves/node'); $rule2->setTargetType(\Jarves\ACL::TARGET_TYPE_USER); $rule2->setTargetId($user->getId()); $rule2->setMode(\Jarves\ACL::MODE_ALL); $rule2->setConstraintType(\Jarves\ACL::CONSTRAINT_CONDITION); $rule2->setConstraintCode(json_encode(['title', '=', 'TestNode tree'])); $rule2->setPrio(3); $rule2->setSub(true); $rule2->save(); $this->getCacher()->invalidateCache('core'); $node1RequestListing = ACLRequest::create('jarves/node', $subNode->getId())->onlyListingMode(); $node2RequestListing = ACLRequest::create('jarves/node', $subNode2->getId())->onlyListingMode(); $this->assertFalse($this->getACL()->check($node1RequestListing)); $this->assertFalse($this->getACL()->check($node2RequestListing)); $items = $this->getObjects()->getBranch('jarves/node', $subNode->getId(), null, 1, null, ['permissionCheck' => true]); $this->assertNull($items, 'rule2 revokes the access to all elements'); $item = $this->getObjects()->get('jarves/node', $subNode2->getId(), ['permissionCheck' => true]); $this->assertNull($item); // Deactivate sub $rule2->setSub(false); $rule2->save(); $this->assertFalse($this->getACL()->check($node1RequestListing)); $this->assertTrue($this->getACL()->check($node2RequestListing)); $items = $this->getObjects()->getBranch('jarves/node', $subNode->getId(), null, 1, null, ['permissionCheck' => true]); $this->assertEquals('TestNode sub', $items[0]['title'], 'We got TestNode sub'); $item = $this->getObjects()->get('jarves/node', $subNode2->getId(), ['permissionCheck' => true]); $this->assertEquals('TestNode sub', $item['title'], 'We got TestNode sub'); // Activate access $rule2->setAccess(true); $rule2->save(); $this->assertTrue($this->getACL()->check($node1RequestListing)); $this->assertTrue($this->getACL()->check($node2RequestListing)); $items = $this->getObjects()->getBranch('jarves/node', $subNode->getId(), null, 1, null, ['permissionCheck' => true]); $this->assertEquals('TestNode sub', $items[0]['title'], 'We got TestNode sub'); $subNode->delete(); $subNode2->delete(); $rule->delete(); $rule2->delete(); $this->getACL()->setCaching(true); }
public function registerPluginRoutes(Node $page) { $domain = $this->pageStack->getDomain($page->getDomainId()); $this->stopwatch->start('Register Plugin Routes'); //add all router to current router and fire sub-request $cacheKey = 'core/node/plugins-' . $page->getId(); $plugins = $this->cacher->getDistributedCache($cacheKey); if (null === $plugins) { $plugins = ContentQuery::create()->filterByNodeId($page->getId())->filterByType('plugin')->find(); $this->cacher->setDistributedCache($cacheKey, serialize($plugins)); } else { $plugins = unserialize($plugins); } /** @var $plugins Content[] */ foreach ($plugins as $plugin) { if (!$plugin->getContent()) { continue; } $data = json_decode($plugin->getContent(), true); if (!$data) { $this->logger->alert(sprintf('On page `%s` [%d] is a invalid plugin `%d`.', $page->getTitle(), $page->getId(), $plugin->getId())); continue; } $bundleName = isset($data['module']) ? $data['module'] : @$data['bundle']; $config = $this->jarves->getConfig($bundleName); if (!$config) { $this->logger->alert(sprintf('Bundle `%s` for plugin `%s` on page `%s` [%d] does not not exist.', $bundleName, @$data['plugin'], $page->getTitle(), $page->getId())); continue; } $pluginDefinition = $config->getPlugin(@$data['plugin']); if (!$pluginDefinition) { $this->logger->alert(sprintf('In bundle `%s` the plugin `%s` on page `%s` [%d] does not not exist.', $bundleName, @$data['plugin'], $page->getTitle(), $page->getId())); continue; } if ($pluginRoutes = $pluginDefinition->getRoutes()) { foreach ($pluginRoutes as $idx => $route) { $controller = $pluginDefinition->getController(); $defaults = array('_controller' => $route->getController() ?: $controller, '_jarves_is_plugin' => true, '_content' => $plugin, '_title' => sprintf('%s: %s', $bundleName, $pluginDefinition->getLabel()), 'options' => isset($data['options']) && is_array($data['options']) ? $data['options'] : [], 'jarvesFrontend' => true, 'nodeId' => $page->getId()); if ($route->getDefaults()) { $defaults = array_merge($defaults, $route->getArrayDefaults()); } $url = $this->pageStack->getRouteUrl($page->getId()); $this->routes->add('jarves_frontend_plugin_' . ($route->getId() ?: $plugin->getId()) . '_' . $idx, new SyRoute($url . '/' . $route->getPattern(), $defaults, $route->getArrayRequirements() ?: array(), [], '', [], $route->getMethods() ?: [])); } } } $this->stopwatch->stop('Register Plugin Routes'); }
protected function import(InputInterface $input, OutputInterface $output) { $basePath = 'app/jarves/'; $openedFiles = []; $domains = []; $nodes = []; $rootNodes = []; $ymlParser = new Parser(); Propel::getWriteConnection('default')->beginTransaction(); //import files $fileReferencesPath = $basePath . '/file_references.yml'; if (file_exists($fileReferencesPath)) { $openedFiles[$fileReferencesPath] = filemtime($fileReferencesPath); $fileReferences = $ymlParser->parse(file_get_contents($fileReferencesPath)); $output->writeln(sprintf('Import %d file references ...', count($fileReferences))); FileQuery::create()->deleteAll(); foreach ($fileReferences as $id => $path) { $file = new File(); $file->setId($id); $file->setPath($path); $file->save(); } } $relativePathBase = $basePath . 'website/'; $dir = opendir($relativePathBase); while ($domainName = readdir($dir)) { if ('.' === $domainName || '..' === $domainName || '.yml' === substr($domainName, -4)) { continue; } $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($basePath . 'website/' . $domainName)); $files = iterator_to_array($files); ksort($files); $output->writeln(sprintf('Import domain %s ...', $domainName)); $domain = DomainQuery::create()->findOneByDomain($domainName); if (!$domain) { $domain = new Domain(); } $ymlPath = $relativePathBase . $domainName . '.yml'; $domainFromYml = $ymlParser->parse(file_get_contents($ymlPath)); $openedFiles[$ymlPath] = filemtime($ymlPath); $oldData = $domain->toArray(TableMap::TYPE_CAMELNAME); $domain->fromArray(array_merge($oldData, $domainFromYml), TableMap::TYPE_CAMELNAME); $domain->setStartnodeId(null); if (isset($domainFromYml['startnode'])) { $domain->setVirtualColumn('startnodePath', $domainFromYml['startnode']); } $domain->save(); $domains[$domainName] = $domain; NodeQuery::create()->filterByDomain($domain)->delete(); $rootNode = new Node(); $rootNode->setTitle('root'); $rootNode->makeRoot(); $rootNode->setDomain($domain); $rootNode->save(); $rootNodes[$domainName] = $rootNode; $nodes[''] = $rootNode; $parentNodeQueue = []; /** @var \SplFileInfo $file */ foreach ($files as $file) { if ('.' === $file->getFilename() || '..' === $file->getFilename() || '.yml' !== substr($file->getFilename(), -4)) { continue; } $path = $file->getPath() . '/' . $file->getFilename(); $path = substr($path, strlen($relativePathBase . $domainName) + 1); if (!$path) { continue; } $parentPath = ''; if (false !== strpos($path, '/')) { $parentPath = dirname($path); } $baseName = substr(basename($path), 0, -4); //without .yml if ($baseName) { //its a node $node = isset($nodes[$path]) ? $nodes[$path] : new Node(); $ymlPath = $relativePathBase . $domainName . '/' . $path; $nodeFromYml = $ymlParser->parse(file_get_contents($ymlPath)); $openedFiles[$ymlPath] = filemtime($ymlPath); $node->fromArray($nodeFromYml, TableMap::TYPE_CAMELNAME); $node->setDomain($domain); $urn = $baseName; if (false !== ($dotPos = strpos($baseName, '.'))) { $prefix = substr($baseName, 0, $dotPos); if ($prefix) { $urn = substr($baseName, $dotPos + 1); } } $node->setUrn($urn); $output->writeln(sprintf('Import page %s%s ...', str_repeat(' ', substr_count($path, '/')), $node->getTitle())); $nodes[substr($path, 0, -4)] = $node; $end = isset($parentNodeQueue[$parentPath]) ? count($parentNodeQueue[$parentPath]) : 1; $position = isset($nodeFromYml['sort']) ? $nodeFromYml['sort'] : $end; $parentNodeQueue[$parentPath][$position][] = $node; if (isset($nodeFromYml['contents'])) { foreach ($nodeFromYml['contents'] as $idx => $contentFromYaml) { if (isset($contentFromYaml['content']) && is_array($contentFromYaml['content'])) { $contentFromYaml['content'] = json_encode($contentFromYaml['content']); } $content = new Content(); $content->setSort($idx); $content->fromArray($contentFromYaml, TableMap::TYPE_CAMELNAME); $content->setNode($node); } if ($domain->hasVirtualColumn('startnodePath') && substr($path, 0, -4) === $domain->getVirtualColumn('startnodePath')) { $domain->setStartnode($node); } } } } //save queued nodes foreach ($parentNodeQueue as $parentPath => $nodesQueue) { ksort($nodesQueue); //key is sort /** @var Node $node */ foreach ($nodesQueue as $position => $nodesToInsert) { foreach ($nodesToInsert as $node) { $node->insertAsLastChildOf($nodes[$parentPath]); $node->save(); //saves Content as well. } } } $domain->save(); } Propel::getWriteConnection('default')->commit(); $cacher = $this->getContainer()->get('jarves.cache.cacher'); $cacher->invalidateCache('core'); return $openedFiles; }