Skip to content

Commit

Permalink
Merge pull request #3 from phonetworks/with_acl
Browse files Browse the repository at this point in the history
Introduction of ACL
  • Loading branch information
esokullu authored Apr 29, 2017
2 parents 27029b1 + 34d80ac commit 1d438c5
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 108 deletions.
26 changes: 26 additions & 0 deletions src/Pho/Framework/Acl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Pho\Framework;

use Pho\Lib\Graph;

class Acl {

protected $creator;
protected $context;

public function __construct(Actor $creator, Graph\GraphInterface $context) {
$this->creator = $creator;
$this->context = $context;
}

public function toArray(): array
{
//eval(\Psy\sh());
return [
"creator" => (string) $this->creator->id(),
"context" => ($this->context instanceof Graph\Graph) ? Graph\Graph::class : (string) $this->context->id()
];
}

}
10 changes: 9 additions & 1 deletion src/Pho/Framework/Actor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Pho\Framework;

use Pho\Lib\Graph;

/**
* The Actor Particle
*
Expand All @@ -23,7 +25,7 @@
*
* @author Emre Sokullu <[email protected]>
*/
class Actor extends \Pho\Lib\Graph\Node implements ParticleInterface {
class Actor extends Graph\Node implements ParticleInterface {

use ParticleTrait;

Expand All @@ -36,4 +38,10 @@ class Actor extends \Pho\Lib\Graph\Node implements ParticleInterface {
*/
const EDGES_IN = [ActorOut\Reads::class, ActorOut\Subscribes::class, ObjectOut\Transmits::class];

public function __construct(Graph\GraphInterface $context) {
parent::__construct($context);
$this->acl = new Acl($this, $context);
$this->setupEdges();
}

}
10 changes: 8 additions & 2 deletions src/Pho/Framework/Frame.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Pho\Framework;

use Pho\Lib\Graph\SubGraph;
use Pho\Lib\Graph;

/**
* The Frame Particle
Expand All @@ -31,7 +31,7 @@
*
* @author Emre Sokullu <[email protected]>
*/
class Frame extends \Pho\Lib\Graph\SubGraph implements ParticleInterface {
class Frame extends Graph\SubGraph implements ParticleInterface {

use ParticleTrait;

Expand All @@ -44,4 +44,10 @@ class Frame extends \Pho\Lib\Graph\SubGraph implements ParticleInterface {
*/
const EDGES_IN = [ActorOut\Reads::class, ActorOut\Subscribes::class, ActorOut\Writes::class, ObjectOut\Transmits::class];

public function __construct(Actor $creator, Graph\GraphInterface $context) {
parent::__construct($context);
$this->acl = new Acl($creator, $context);
$this->setupEdges();
}

}
8 changes: 8 additions & 0 deletions src/Pho/Framework/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Pho\Framework;

use Pho\Lib\Graph;

/**
* The Object Particle
*
Expand All @@ -34,4 +36,10 @@ class Object extends \Pho\Lib\Graph\Node implements ParticleInterface {
*/
const EDGES_IN = [ActorOut\Reads::class, ActorOut\Subscribes::class, ActorOut\Writes::class, ObjectOut\Transmits::class];

public function __construct(Actor $creator, Graph\GraphInterface $context) {
parent::__construct($context);
$this->acl = new Acl($creator, $context);
$this->setupEdges();
}

}
22 changes: 16 additions & 6 deletions src/Pho/Framework/ParticleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Pho\Framework;

use Pho\Lib\Graph;
use Pho\Framework\Exceptions\InvalidEdgeHeadTypeException;
use Zend\File\ClassFileLocator;

Expand Down Expand Up @@ -112,12 +111,16 @@ trait ParticleTrait {
protected $edge_out_getter_classes = [];

/**
* Constructor.
*
* @param Pho\Lib\Graph\GraphInterface $graph The graph that this particle belongs to.
* Access Control List object
*
* @var Acl
*/
protected $acl;

/**
* Trait constructor.
*/
public function __construct(Graph\GraphInterface $graph) {
parent::__construct($graph);
protected function setupEdges() {
$this->_setupEdgesIn();
$this->_setupEdgesOut();
}
Expand Down Expand Up @@ -260,4 +263,11 @@ protected function _callGetter(string $name, array $args): array
}


public function toArray(): array
{
$array = parent::toArray();
$array["acl"] = $this->acl->toArray();
return $array;
}

}
131 changes: 131 additions & 0 deletions tests/Pho/Framework/SimpleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

/*
* This file is part of the Pho package.
*
* (c) Emre Sokullu <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Pho\Framework;

use Pho\Lib\Graph;

class SimpleTest extends \PHPUnit\Framework\TestCase
{
private $graph;

public function setUp() {
$this->graph = new Graph\Graph();
}

public function tearDown() {
unset($this->graph);
}

public function testActor() {
$node = new Actor($this->graph);
$node_expected_to_be_identical = $this->graph->get($node->id());
$this->assertEquals($node->id(), $node_expected_to_be_identical->id());
}

public function testActorEdge() {
$actor = new Actor($this->graph);
$object = new Object($actor, $this->graph);
$edge = $actor->writes($object);
$this->assertInstanceOf(ActorOut\Writes::class, $edge);
$this->assertInstanceOf(Graph\Predicate::class, $edge->predicate());
}

public function testActorPredicate() {
$actor = new Actor($this->graph);
$object = new Object($actor, $this->graph);
$edge = $actor->subscribes($object);
$this->assertInstanceOf(ActorOut\SubscribesPredicate::class, $edge->predicate());
}

public function testObjectGetter() {
$actor = new Actor($this->graph);
$object = new Object($actor, $this->graph);
$edge = $actor->writes($object);
$this->assertInstanceOf(ActorOut\Writes::class, $object->getWriters()[0]);
$this->assertCount(1, $object->getWriters());
$this->assertCount(1, $actor->getWrites());
$this->assertInstanceOf(ActorOut\Writes::class, $actor->getWrites()[0]);
}

public function testFiltering() {
$actor = new Actor($this->graph);
$object = new Object($actor, $this->graph);
$edge = $actor->writes($object);
$edge = $actor->reads($object);
$this->assertCount(1, $actor->getWrites());
}

/**
* Since write extends subscribes
*/
public function testEdgeInheritance() {
$actor = new Actor($this->graph);
$object = new Object($actor, $this->graph);
$edge = $actor->writes($object);
$this->assertCount(1, $actor->getSubscriptions());
$this->assertCount(0, $actor->getReads());
}

/**
* @expectedException Pho\Framework\Exceptions\InvalidEdgeHeadTypeException
*/
public function testImpossibleEdge() {
$actor1 = new Actor($this->graph);
$actor2 = new Actor($this->graph);
$edge = $actor1->writes($actor2);
}

public function testEdgeInvoke() {
$actor = new Actor($this->graph);
$object = new Object($actor, $this->graph);
$edge = $actor->writes($object);
$this->assertInstanceOf(Object::class, $edge());
$this->assertEquals($object->id(), $edge()->id());
}


public function testActorToArray() {
$actor = new Actor($this->graph);
$array = $actor->toArray();
$faker = \Faker\Factory::create();
$this->assertArrayHasKey("id", $array);
$this->assertArrayHasKey("attributes", $array);
$this->assertCount(0, $array["attributes"]);
$actor->attributes()->username = $faker->username;
$this->assertCount(1, $actor->toArray()["attributes"]);
$this->assertArrayHasKey("edge_list", $array);
$this->assertArrayHasKey("acl", $array);
$this->assertCount(2, $array["acl"]);
$this->assertArrayHasKey("context", $array["acl"]);
$this->assertArrayHasKey("creator", $array["acl"]);
}

public function testFrameToArray() {
$faker = \Faker\Factory::create();
$actor = new Actor($this->graph);
$frame = new Frame($actor, $this->graph);
$edge = $actor->writes($frame);
$array = $frame->toArray();
$this->assertArrayHasKey("id", $array);
$this->assertArrayHasKey("attributes", $array);
$this->assertCount(0, $array["attributes"]);
$actor->attributes()->username = $faker->username;
$this->assertCount(1, $actor->toArray()["attributes"]);
$this->assertArrayHasKey("edge_list", $array);
$this->assertArrayHasKey("acl", $array);
$this->assertCount(2, $array["acl"]);
$this->assertArrayHasKey("context", $array["acl"]);
$this->assertArrayHasKey("creator", $array["acl"]);
$this->assertEquals($actor->id(), $array["acl"]["creator"]);
}

}
99 changes: 0 additions & 99 deletions tests/SimpleTest.php

This file was deleted.

0 comments on commit 1d438c5

Please sign in to comment.