From: <lph...@us...> - 2009-07-27 20:02:37
|
Revision: 20362 http://tikiwiki.svn.sourceforge.net/tikiwiki/?rev=20362&view=rev Author: lphuberdeau Date: 2009-07-27 20:02:19 +0000 (Mon, 27 Jul 2009) Log Message: ----------- [NEW] Component to handle category security for object category modification Added Paths: ----------- branches/experimental/perms-take2/lib/core/lib/Category/ branches/experimental/perms-take2/lib/core/lib/Category/Manipulator.php branches/experimental/perms-take2/lib/core/test/Category/ branches/experimental/perms-take2/lib/core/test/Category/ManipulatorTest.php Added: branches/experimental/perms-take2/lib/core/lib/Category/Manipulator.php =================================================================== --- branches/experimental/perms-take2/lib/core/lib/Category/Manipulator.php (rev 0) +++ branches/experimental/perms-take2/lib/core/lib/Category/Manipulator.php 2009-07-27 20:02:19 UTC (rev 20362) @@ -0,0 +1,87 @@ +<?php + +class Category_Manipulator +{ + private $objectType; + private $objectId; + + private $current = array(); + private $managed = array(); + private $new = array(); + + private $prepared = false; + + function __construct( $objectType, $objectId ) { + $this->objectType = $objectType; + $this->objectId = $objectId; + } + + function setCurrentCategories( array $categories ) { + $this->current = $categories; + } + + function setManagedCategories( array $categories ) { + $this->managed = $categories; + } + + function setNewCategories( array $categories ) { + $this->new = $categories; + } + + function getAddedCategories() { + if( ! $this->canModifyObject() ) { + return array(); + } + + $this->prepare(); + + $attempt = array_diff( $this->new, $this->current ); + return $this->filter( $attempt, 'add_object' ); + } + + function getRemovedCategories() { + if( ! $this->canModifyObject() ) { + return array(); + } + + $this->prepare(); + + $attempt = array_diff( $this->current, $this->new ); + return $this->filter( $attempt, 'remove_object' ); + } + + private function filter( $categories, $permission ) { + $out = array(); + foreach( $categories as $categ ) { + $perms = Perms::get( array( 'type' => 'category', 'object' => $categ ) ); + + if( $perms->$permission ) { + $out[] = $categ; + } + } + + return $out; + } + + private function canModifyObject() { + $objectperms = Perms::get( array( 'type' => $this->objectType, 'object' => $this->objectId ) ); + + return $objectperms->modify_object_categories; + } + + private function prepare() { + if( $this->prepared ) { + return; + } + + $categories = $this->managed; + Perms::bulk( array( 'type' => 'category' ), 'object', $categories ); + + $this->current = array_intersect( $this->current, $this->managed ); + $this->new = array_intersect( $this->new, $this->new ); + + $this->prepared = true; + } +} + +?> Added: branches/experimental/perms-take2/lib/core/test/Category/ManipulatorTest.php =================================================================== --- branches/experimental/perms-take2/lib/core/test/Category/ManipulatorTest.php (rev 0) +++ branches/experimental/perms-take2/lib/core/test/Category/ManipulatorTest.php 2009-07-27 20:02:19 UTC (rev 20362) @@ -0,0 +1,100 @@ +<?php + +class Category_ManipulatorTest extends TikiTestCase +{ + function testSimpleManipulation() { + $perms = new Perms; + $perms->setResolverFactories( array( + new Perms_ResolverFactory_StaticFactory( 'root', new Perms_Resolver_Default( true ) ), + ) ); + Perms::set( $perms ); + + $manip = new Category_Manipulator( 'wiki page', 'Hello World' ); + $manip->setCurrentCategories( array( 1, 2, 3, 7 ) ); + $manip->setManagedCategories( range( 1, 10 ) ); + + $manip->setNewCategories( array( 1, 2, 4 ) ); + + $this->assertEquals( array( 4 ), $manip->getAddedCategories() ); + $this->assertEquals( array( 3, 7 ), $manip->getRemovedCategories() ); + } + + function testLimitationOnRange() { + $perms = new Perms; + $perms->setResolverFactories( array( + new Perms_ResolverFactory_StaticFactory( 'root', new Perms_Resolver_Default( true ) ), + ) ); + Perms::set( $perms ); + + $manip = new Category_Manipulator( 'wiki page', 'Hello World' ); + $manip->setCurrentCategories( array( 1, 2, 3, 7 ) ); + $manip->setManagedCategories( range( 1, 5 ) ); + + $manip->setNewCategories( array( 1, 2, 4 ) ); + + $this->assertEquals( array( 4 ), $manip->getAddedCategories() ); + $this->assertEquals( array( 3 ), $manip->getRemovedCategories() ); + } + + function testNotAllowedToModifyObject() { + $perms = new Perms; + $perms->setResolverFactories( array( + new Perms_ResolverFactory_TestFactory( array( 'type', 'object' ), array( + 'wiki page:Hello World' => new Perms_Resolver_Default( false ), + ) ), + new Perms_ResolverFactory_StaticFactory( 'root', new Perms_Resolver_Default( true ) ), + ) ); + Perms::set( $perms ); + + $manip = new Category_Manipulator( 'wiki page', 'Hello World' ); + $manip->setCurrentCategories( array( 1, 2, 3, 7 ) ); + $manip->setManagedCategories( range( 1, 5 ) ); + + $manip->setNewCategories( array( 1, 2, 4 ) ); + + $this->assertEquals( array(), $manip->getAddedCategories() ); + $this->assertEquals( array(), $manip->getRemovedCategories() ); + } + + function testCannotAddAny() { + $perms = new Perms; + $perms->setResolverFactories( array( + new Perms_ResolverFactory_TestFactory( array( 'type', 'object' ), array( + 'category:4' => new Perms_Resolver_Default( false ), + ) ), + new Perms_ResolverFactory_StaticFactory( 'root', new Perms_Resolver_Default( true ) ), + ) ); + Perms::set( $perms ); + + $manip = new Category_Manipulator( 'wiki page', 'Hello World' ); + $manip->setCurrentCategories( array( 1, 2, 3, 7 ) ); + $manip->setManagedCategories( range( 1, 5 ) ); + + $manip->setNewCategories( array( 1, 2, 4 ) ); + + $this->assertEquals( array(), $manip->getAddedCategories() ); + $this->assertEquals( array( 3 ), $manip->getRemovedCategories() ); + } + + function testCannotRemoveAny() { + $perms = new Perms; + $perms->setResolverFactories( array( + new Perms_ResolverFactory_TestFactory( array( 'type', 'object' ), array( + 'category:3' => new Perms_Resolver_Default( false ), + ) ), + new Perms_ResolverFactory_StaticFactory( 'root', new Perms_Resolver_Default( true ) ), + ) ); + Perms::set( $perms ); + + $manip = new Category_Manipulator( 'wiki page', 'Hello World' ); + $manip->setCurrentCategories( array( 1, 2, 3, 7 ) ); + $manip->setManagedCategories( range( 1, 5 ) ); + + $manip->setNewCategories( array( 1, 2, 4 ) ); + + $this->assertEquals( array( 4 ), $manip->getAddedCategories() ); + $this->assertEquals( array(), $manip->getRemovedCategories() ); + } +} + +?> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |