From 11b1034d5540ebc42048d2f576e802e97dd859d3 Mon Sep 17 00:00:00 2001 From: Groups Date: Sat, 29 Apr 2017 23:51:23 +0300 Subject: [PATCH 1/2] added basic ACL --- src/Pho/Framework/Acl.php | 26 ++++++++++++++++ src/Pho/Framework/Actor.php | 10 +++++- src/Pho/Framework/Frame.php | 10 ++++-- src/Pho/Framework/Object.php | 8 +++++ src/Pho/Framework/ParticleTrait.php | 22 ++++++++++---- tests/SimpleTest.php | 47 ++++++++++++++++++++++++----- 6 files changed, 107 insertions(+), 16 deletions(-) create mode 100644 src/Pho/Framework/Acl.php diff --git a/src/Pho/Framework/Acl.php b/src/Pho/Framework/Acl.php new file mode 100644 index 0000000..ea7ce87 --- /dev/null +++ b/src/Pho/Framework/Acl.php @@ -0,0 +1,26 @@ +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() + ]; + } + +} \ No newline at end of file diff --git a/src/Pho/Framework/Actor.php b/src/Pho/Framework/Actor.php index 5cbb959..e8f60e7 100644 --- a/src/Pho/Framework/Actor.php +++ b/src/Pho/Framework/Actor.php @@ -11,6 +11,8 @@ namespace Pho\Framework; +use Pho\Lib\Graph; + /** * The Actor Particle * @@ -23,7 +25,7 @@ * * @author Emre Sokullu */ -class Actor extends \Pho\Lib\Graph\Node implements ParticleInterface { +class Actor extends Graph\Node implements ParticleInterface { use ParticleTrait; @@ -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(); + } + } \ No newline at end of file diff --git a/src/Pho/Framework/Frame.php b/src/Pho/Framework/Frame.php index ba0c489..df395dc 100644 --- a/src/Pho/Framework/Frame.php +++ b/src/Pho/Framework/Frame.php @@ -11,7 +11,7 @@ namespace Pho\Framework; -use Pho\Lib\Graph\SubGraph; +use Pho\Lib\Graph; /** * The Frame Particle @@ -31,7 +31,7 @@ * * @author Emre Sokullu */ -class Frame extends \Pho\Lib\Graph\SubGraph implements ParticleInterface { +class Frame extends Graph\SubGraph implements ParticleInterface { use ParticleTrait; @@ -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(); + } + } \ No newline at end of file diff --git a/src/Pho/Framework/Object.php b/src/Pho/Framework/Object.php index 8be289e..bb05bf7 100644 --- a/src/Pho/Framework/Object.php +++ b/src/Pho/Framework/Object.php @@ -11,6 +11,8 @@ namespace Pho\Framework; +use Pho\Lib\Graph; + /** * The Object Particle * @@ -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(); + } + } \ No newline at end of file diff --git a/src/Pho/Framework/ParticleTrait.php b/src/Pho/Framework/ParticleTrait.php index b56dc26..e040897 100644 --- a/src/Pho/Framework/ParticleTrait.php +++ b/src/Pho/Framework/ParticleTrait.php @@ -11,7 +11,6 @@ namespace Pho\Framework; -use Pho\Lib\Graph; use Pho\Framework\Exceptions\InvalidEdgeHeadTypeException; use Zend\File\ClassFileLocator; @@ -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(); } @@ -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; + } + } \ No newline at end of file diff --git a/tests/SimpleTest.php b/tests/SimpleTest.php index d0482bf..d48bf18 100644 --- a/tests/SimpleTest.php +++ b/tests/SimpleTest.php @@ -34,7 +34,7 @@ public function testActor() { public function testActorEdge() { $actor = new Framework\Actor($this->graph); - $object = new Framework\Object($this->graph); + $object = new Framework\Object($actor, $this->graph); $edge = $actor->writes($object); $this->assertInstanceOf(Framework\ActorOut\Writes::class, $edge); $this->assertInstanceOf(Graph\Predicate::class, $edge->predicate()); @@ -42,14 +42,14 @@ public function testActorEdge() { public function testActorPredicate() { $actor = new Framework\Actor($this->graph); - $object = new Framework\Object($this->graph); + $object = new Framework\Object($actor, $this->graph); $edge = $actor->subscribes($object); $this->assertInstanceOf(Framework\ActorOut\SubscribesPredicate::class, $edge->predicate()); } public function testObjectGetter() { $actor = new Framework\Actor($this->graph); - $object = new Framework\Object($this->graph); + $object = new Framework\Object($actor, $this->graph); $edge = $actor->writes($object); $this->assertInstanceOf(Framework\ActorOut\Writes::class, $object->getWriters()[0]); $this->assertCount(1, $object->getWriters()); @@ -59,7 +59,7 @@ public function testObjectGetter() { public function testFiltering() { $actor = new Framework\Actor($this->graph); - $object = new Framework\Object($this->graph); + $object = new Framework\Object($actor, $this->graph); $edge = $actor->writes($object); $edge = $actor->reads($object); $this->assertCount(1, $actor->getWrites()); @@ -70,7 +70,7 @@ public function testFiltering() { */ public function testEdgeInheritance() { $actor = new Framework\Actor($this->graph); - $object = new Framework\Object($this->graph); + $object = new Framework\Object($actor, $this->graph); $edge = $actor->writes($object); $this->assertCount(1, $actor->getSubscriptions()); $this->assertCount(0, $actor->getReads()); @@ -87,13 +87,46 @@ public function testImpossibleEdge() { public function testEdgeInvoke() { $actor = new Framework\Actor($this->graph); - $object = new Framework\Object($this->graph); + $object = new Framework\Object($actor, $this->graph); $edge = $actor->writes($object); $this->assertInstanceOf(Framework\Object::class, $edge()); $this->assertEquals($object->id(), $edge()->id()); } - + public function testActorToArray() { + $actor = new Framework\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 Framework\Actor($this->graph); + $frame = new Framework\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"]); + } } \ No newline at end of file From 34d80ac55281984fa8acfb86900a4396f68fab16 Mon Sep 17 00:00:00 2001 From: Groups Date: Sun, 30 Apr 2017 00:06:10 +0300 Subject: [PATCH 2/2] relocated tests --- tests/{ => Pho/Framework}/SimpleTest.php | 53 ++++++++++++------------ 1 file changed, 26 insertions(+), 27 deletions(-) rename tests/{ => Pho/Framework}/SimpleTest.php (67%) diff --git a/tests/SimpleTest.php b/tests/Pho/Framework/SimpleTest.php similarity index 67% rename from tests/SimpleTest.php rename to tests/Pho/Framework/SimpleTest.php index d48bf18..e65a914 100644 --- a/tests/SimpleTest.php +++ b/tests/Pho/Framework/SimpleTest.php @@ -9,9 +9,8 @@ * file that was distributed with this source code. */ -// namespace Pho\Framework\Tests; +namespace Pho\Framework; -use Pho\Framework; use Pho\Lib\Graph; class SimpleTest extends \PHPUnit\Framework\TestCase @@ -27,39 +26,39 @@ public function tearDown() { } public function testActor() { - $node = new Framework\Actor($this->graph); + $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 Framework\Actor($this->graph); - $object = new Framework\Object($actor, $this->graph); + $actor = new Actor($this->graph); + $object = new Object($actor, $this->graph); $edge = $actor->writes($object); - $this->assertInstanceOf(Framework\ActorOut\Writes::class, $edge); + $this->assertInstanceOf(ActorOut\Writes::class, $edge); $this->assertInstanceOf(Graph\Predicate::class, $edge->predicate()); } public function testActorPredicate() { - $actor = new Framework\Actor($this->graph); - $object = new Framework\Object($actor, $this->graph); + $actor = new Actor($this->graph); + $object = new Object($actor, $this->graph); $edge = $actor->subscribes($object); - $this->assertInstanceOf(Framework\ActorOut\SubscribesPredicate::class, $edge->predicate()); + $this->assertInstanceOf(ActorOut\SubscribesPredicate::class, $edge->predicate()); } public function testObjectGetter() { - $actor = new Framework\Actor($this->graph); - $object = new Framework\Object($actor, $this->graph); + $actor = new Actor($this->graph); + $object = new Object($actor, $this->graph); $edge = $actor->writes($object); - $this->assertInstanceOf(Framework\ActorOut\Writes::class, $object->getWriters()[0]); + $this->assertInstanceOf(ActorOut\Writes::class, $object->getWriters()[0]); $this->assertCount(1, $object->getWriters()); $this->assertCount(1, $actor->getWrites()); - $this->assertInstanceOf(Framework\ActorOut\Writes::class, $actor->getWrites()[0]); + $this->assertInstanceOf(ActorOut\Writes::class, $actor->getWrites()[0]); } public function testFiltering() { - $actor = new Framework\Actor($this->graph); - $object = new Framework\Object($actor, $this->graph); + $actor = new Actor($this->graph); + $object = new Object($actor, $this->graph); $edge = $actor->writes($object); $edge = $actor->reads($object); $this->assertCount(1, $actor->getWrites()); @@ -69,8 +68,8 @@ public function testFiltering() { * Since write extends subscribes */ public function testEdgeInheritance() { - $actor = new Framework\Actor($this->graph); - $object = new Framework\Object($actor, $this->graph); + $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()); @@ -80,24 +79,24 @@ public function testEdgeInheritance() { * @expectedException Pho\Framework\Exceptions\InvalidEdgeHeadTypeException */ public function testImpossibleEdge() { - $actor1 = new Framework\Actor($this->graph); - $actor2 = new Framework\Actor($this->graph); + $actor1 = new Actor($this->graph); + $actor2 = new Actor($this->graph); $edge = $actor1->writes($actor2); } public function testEdgeInvoke() { - $actor = new Framework\Actor($this->graph); - $object = new Framework\Object($actor, $this->graph); + $actor = new Actor($this->graph); + $object = new Object($actor, $this->graph); $edge = $actor->writes($object); - $this->assertInstanceOf(Framework\Object::class, $edge()); + $this->assertInstanceOf(Object::class, $edge()); $this->assertEquals($object->id(), $edge()->id()); } public function testActorToArray() { - $actor = new Framework\Actor($this->graph); + $actor = new Actor($this->graph); $array = $actor->toArray(); - $faker = Faker\Factory::create(); + $faker = \Faker\Factory::create(); $this->assertArrayHasKey("id", $array); $this->assertArrayHasKey("attributes", $array); $this->assertCount(0, $array["attributes"]); @@ -111,9 +110,9 @@ public function testActorToArray() { } public function testFrameToArray() { - $faker = Faker\Factory::create(); - $actor = new Framework\Actor($this->graph); - $frame = new Framework\Frame($actor, $this->graph); + $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);