Ejemplo n.º 1
0
 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);
 }
Ejemplo n.º 2
0
 /**
  * @static
  *
  * @param Node $pNode
  * @param array $pChildren
  */
 function installNodes(Node $pNode, $pChildren)
 {
     /*
      * 0: type
      * 1: Title
      * 2: layout
      * 3: url
      * 4: link target
      * 5: contents
      * 6: children
      * 7: visible
      */
     foreach ($pChildren as $Node) {
         $oNode = new Node();
         $oNode->setDomainId($pNode->getDomainId());
         $oNode->setType($Node[0]);
         $oNode->setTitle($Node[1]);
         $oNode->setLayout($Node[2]);
         $oNode->setUrn($Node[3]);
         $oNode->insertAsLastChildOf($pNode);
         if ($Node[4]) {
             $oNode->setLink($Node[4]);
         }
         if (isset($Node[7])) {
             $oNode->setVisible($Node[7]);
         } else {
             $oNode->setVisible(1);
         }
         $oNode->save();
         if (isset($Node[5])) {
             $this->installContents($oNode, $Node[5]);
         }
         if (isset($Node[6])) {
             $this->installNodes($oNode, $Node[6]);
         }
     }
 }
Ejemplo n.º 3
0
 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;
 }