mirror of
https://github.com/brmlab/brmbiolab_sklad.git
synced 2025-06-10 22:24:12 +02:00
Initial commit
This commit is contained in:
commit
3b93da31de
1004 changed files with 265840 additions and 0 deletions
543
lib/Cake/Test/Case/Controller/Component/Acl/DbAclTest.php
Normal file
543
lib/Cake/Test/Case/Controller/Component/Acl/DbAclTest.php
Normal file
|
@ -0,0 +1,543 @@
|
|||
<?php
|
||||
/**
|
||||
* DbAclTest file.
|
||||
*
|
||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* For full copyright and license information, please see the LICENSE.txt
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @package Cake.Test.Case.Controller.Component.Acl
|
||||
* @since CakePHP(tm) v 2.0
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
App::uses('ComponentCollection', 'Controller');
|
||||
App::uses('AclComponent', 'Controller/Component');
|
||||
App::uses('DbAcl', 'Controller/Component/Acl');
|
||||
App::uses('AclNode', 'Model');
|
||||
App::uses('Permission', 'Model');
|
||||
require_once dirname(dirname(dirname(dirname(__FILE__)))) . DS . 'Model' . DS . 'models.php';
|
||||
|
||||
/**
|
||||
* AclNodeTwoTestBase class
|
||||
*
|
||||
* @package Cake.Test.Case.Controller.Component.Acl
|
||||
*/
|
||||
class AclNodeTwoTestBase extends AclNode {
|
||||
|
||||
/**
|
||||
* useDbConfig property
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $useDbConfig = 'test';
|
||||
|
||||
/**
|
||||
* cacheSources property
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $cacheSources = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* AroTwoTest class
|
||||
*
|
||||
* @package Cake.Test.Case.Controller.Component.Acl
|
||||
*/
|
||||
class AroTwoTest extends AclNodeTwoTestBase {
|
||||
|
||||
/**
|
||||
* name property
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $name = 'AroTwoTest';
|
||||
|
||||
/**
|
||||
* useTable property
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $useTable = 'aro_twos';
|
||||
|
||||
/**
|
||||
* hasAndBelongsToMany property
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $hasAndBelongsToMany = array('AcoTwoTest' => array('with' => 'PermissionTwoTest'));
|
||||
}
|
||||
|
||||
/**
|
||||
* AcoTwoTest class
|
||||
*
|
||||
* @package Cake.Test.Case.Controller.Component.Acl
|
||||
*/
|
||||
class AcoTwoTest extends AclNodeTwoTestBase {
|
||||
|
||||
/**
|
||||
* name property
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $name = 'AcoTwoTest';
|
||||
|
||||
/**
|
||||
* useTable property
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $useTable = 'aco_twos';
|
||||
|
||||
/**
|
||||
* hasAndBelongsToMany property
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $hasAndBelongsToMany = array('AroTwoTest' => array('with' => 'PermissionTwoTest'));
|
||||
}
|
||||
|
||||
/**
|
||||
* PermissionTwoTest class
|
||||
*
|
||||
* @package Cake.Test.Case.Controller.Component.Acl
|
||||
*/
|
||||
class PermissionTwoTest extends Permission {
|
||||
|
||||
/**
|
||||
* name property
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $name = 'PermissionTwoTest';
|
||||
|
||||
/**
|
||||
* useTable property
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $useTable = 'aros_aco_twos';
|
||||
|
||||
/**
|
||||
* cacheQueries property
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $cacheQueries = false;
|
||||
|
||||
/**
|
||||
* belongsTo property
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $belongsTo = array('AroTwoTest' => array('foreignKey' => 'aro_id'), 'AcoTwoTest' => array('foreignKey' => 'aco_id'));
|
||||
|
||||
/**
|
||||
* actsAs property
|
||||
*
|
||||
* @var mixed
|
||||
*/
|
||||
public $actsAs = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* DbAclTwoTest class
|
||||
*
|
||||
* @package Cake.Test.Case.Controller.Component.Acl
|
||||
*/
|
||||
class DbAclTwoTest extends DbAcl {
|
||||
|
||||
/**
|
||||
* construct method
|
||||
*
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->Aro = new AroTwoTest();
|
||||
$this->Aro->Permission = new PermissionTwoTest();
|
||||
$this->Aco = new AcoTwoTest();
|
||||
$this->Aro->Permission = new PermissionTwoTest();
|
||||
|
||||
$this->Permission = $this->Aro->Permission;
|
||||
$this->Permission->Aro = $this->Aro;
|
||||
$this->Permission->Aco = $this->Aco;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test case for AclComponent using the DbAcl implementation.
|
||||
*
|
||||
* @package Cake.Test.Case.Controller.Component.Acl
|
||||
*/
|
||||
class DbAclTest extends CakeTestCase {
|
||||
|
||||
/**
|
||||
* fixtures property
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $fixtures = array('core.aro_two', 'core.aco_two', 'core.aros_aco_two');
|
||||
|
||||
/**
|
||||
* setUp method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
Configure::write('Acl.classname', 'DbAclTwoTest');
|
||||
Configure::write('Acl.database', 'test');
|
||||
$Collection = new ComponentCollection();
|
||||
$this->Acl = new AclComponent($Collection);
|
||||
}
|
||||
|
||||
/**
|
||||
* tearDown method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function tearDown() {
|
||||
parent::tearDown();
|
||||
unset($this->Acl);
|
||||
}
|
||||
|
||||
/**
|
||||
* testAclCreate method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCreate() {
|
||||
$this->Acl->Aro->create(array('alias' => 'Chotchkey'));
|
||||
$this->assertTrue((bool)$this->Acl->Aro->save());
|
||||
|
||||
$parent = $this->Acl->Aro->id;
|
||||
|
||||
$this->Acl->Aro->create(array('parent_id' => $parent, 'alias' => 'Joanna'));
|
||||
$this->assertTrue((bool)$this->Acl->Aro->save());
|
||||
|
||||
$this->Acl->Aro->create(array('parent_id' => $parent, 'alias' => 'Stapler'));
|
||||
$this->assertTrue((bool)$this->Acl->Aro->save());
|
||||
|
||||
$root = $this->Acl->Aco->node('ROOT');
|
||||
$parent = $root[0]['AcoTwoTest']['id'];
|
||||
|
||||
$this->Acl->Aco->create(array('parent_id' => $parent, 'alias' => 'Drinks'));
|
||||
$this->assertTrue((bool)$this->Acl->Aco->save());
|
||||
|
||||
$this->Acl->Aco->create(array('parent_id' => $parent, 'alias' => 'PiecesOfFlair'));
|
||||
$this->assertTrue((bool)$this->Acl->Aco->save());
|
||||
}
|
||||
|
||||
/**
|
||||
* testAclCreateWithParent method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCreateWithParent() {
|
||||
$parent = $this->Acl->Aro->findByAlias('Peter', null, null, -1);
|
||||
$this->Acl->Aro->create();
|
||||
$this->Acl->Aro->save(array(
|
||||
'alias' => 'Subordinate',
|
||||
'model' => 'User',
|
||||
'foreign_key' => 7,
|
||||
'parent_id' => $parent['AroTwoTest']['id']
|
||||
));
|
||||
$result = $this->Acl->Aro->findByAlias('Subordinate', null, null, -1);
|
||||
$this->assertEquals(16, $result['AroTwoTest']['lft']);
|
||||
$this->assertEquals(17, $result['AroTwoTest']['rght']);
|
||||
}
|
||||
|
||||
/**
|
||||
* testDbAclAllow method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAllow() {
|
||||
$this->assertFalse($this->Acl->check('Micheal', 'tpsReports', 'read'));
|
||||
$this->assertTrue($this->Acl->allow('Micheal', 'tpsReports', array('read', 'delete', 'update')));
|
||||
$this->assertTrue($this->Acl->check('Micheal', 'tpsReports', 'update'));
|
||||
$this->assertTrue($this->Acl->check('Micheal', 'tpsReports', 'read'));
|
||||
$this->assertTrue($this->Acl->check('Micheal', 'tpsReports', 'delete'));
|
||||
|
||||
$this->assertFalse($this->Acl->check('Micheal', 'tpsReports', 'create'));
|
||||
$this->assertTrue($this->Acl->allow('Micheal', 'ROOT/tpsReports', 'create'));
|
||||
$this->assertTrue($this->Acl->check('Micheal', 'tpsReports', 'create'));
|
||||
$this->assertTrue($this->Acl->check('Micheal', 'tpsReports', 'delete'));
|
||||
$this->assertTrue($this->Acl->allow('Micheal', 'printers', 'create'));
|
||||
// Michael no longer has his delete permission for tpsReports!
|
||||
$this->assertTrue($this->Acl->check('Micheal', 'tpsReports', 'delete'));
|
||||
$this->assertTrue($this->Acl->check('Micheal', 'printers', 'create'));
|
||||
|
||||
$this->assertFalse($this->Acl->check('root/users/Samir', 'ROOT/tpsReports/view'));
|
||||
$this->assertTrue($this->Acl->allow('root/users/Samir', 'ROOT/tpsReports/view', '*'));
|
||||
$this->assertTrue($this->Acl->check('Samir', 'view', 'read'));
|
||||
$this->assertTrue($this->Acl->check('root/users/Samir', 'ROOT/tpsReports/view', 'update'));
|
||||
|
||||
$this->assertFalse($this->Acl->check('root/users/Samir', 'ROOT/tpsReports/update', '*'));
|
||||
$this->assertTrue($this->Acl->allow('root/users/Samir', 'ROOT/tpsReports/update', '*'));
|
||||
$this->assertTrue($this->Acl->check('Samir', 'update', 'read'));
|
||||
$this->assertTrue($this->Acl->check('root/users/Samir', 'ROOT/tpsReports/update', 'update'));
|
||||
// Samir should still have his tpsReports/view permissions, but does not
|
||||
$this->assertTrue($this->Acl->check('root/users/Samir', 'ROOT/tpsReports/view', 'update'));
|
||||
|
||||
$this->assertFalse($this->Acl->allow('Lumbergh', 'ROOT/tpsReports/DoesNotExist', 'create'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that allow() with an invalid permission name triggers an error.
|
||||
*
|
||||
* @expectedException CakeException
|
||||
* @return void
|
||||
*/
|
||||
public function testAllowInvalidPermission() {
|
||||
$this->assertFalse($this->Acl->allow('Micheal', 'tpsReports', 'derp'));
|
||||
}
|
||||
|
||||
/**
|
||||
* testAllowInvalidNode method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAllowInvalidNode() {
|
||||
$this->assertFalse($this->Acl->allow('Homer', 'tpsReports', 'create'));
|
||||
}
|
||||
|
||||
/**
|
||||
* testDbAclCheck method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCheck() {
|
||||
$this->assertTrue($this->Acl->check('Samir', 'print', 'read'));
|
||||
$this->assertTrue($this->Acl->check('Lumbergh', 'current', 'read'));
|
||||
$this->assertFalse($this->Acl->check('Milton', 'smash', 'read'));
|
||||
$this->assertFalse($this->Acl->check('Milton', 'current', 'update'));
|
||||
|
||||
$this->assertFalse($this->Acl->check(null, 'printers', 'create'));
|
||||
$this->assertFalse($this->Acl->check('managers', null, 'read'));
|
||||
|
||||
$this->assertTrue($this->Acl->check('Bobs', 'ROOT/tpsReports/view/current', 'read'));
|
||||
$this->assertFalse($this->Acl->check('Samir', 'ROOT/tpsReports/update', 'read'));
|
||||
|
||||
$this->assertFalse($this->Acl->check('root/users/Milton', 'smash', 'delete'));
|
||||
}
|
||||
|
||||
/**
|
||||
* testCheckInvalidNode method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCheckInvalidNode() {
|
||||
$this->assertFalse($this->Acl->check('WRONG', 'tpsReports', 'read'));
|
||||
}
|
||||
|
||||
/**
|
||||
* testCheckInvalidPermission method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCheckInvalidPermission() {
|
||||
$this->assertFalse($this->Acl->check('Lumbergh', 'smash', 'foobar'));
|
||||
}
|
||||
|
||||
/**
|
||||
* testCheckMissingPermission method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCheckMissingPermission() {
|
||||
$this->assertFalse($this->Acl->check('users', 'NonExistent', 'read'));
|
||||
}
|
||||
|
||||
/**
|
||||
* testDbAclCascadingDeny function
|
||||
*
|
||||
* Setup the acl permissions such that Bobs inherits from admin.
|
||||
* deny Admin delete access to a specific resource, check the permissions are inherited.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAclCascadingDeny() {
|
||||
$this->Acl->inherit('Bobs', 'ROOT', '*');
|
||||
$this->assertTrue($this->Acl->check('admin', 'tpsReports', 'delete'));
|
||||
$this->assertTrue($this->Acl->check('Bobs', 'tpsReports', 'delete'));
|
||||
$this->Acl->deny('admin', 'tpsReports', 'delete');
|
||||
$this->assertFalse($this->Acl->check('admin', 'tpsReports', 'delete'));
|
||||
$this->assertFalse($this->Acl->check('Bobs', 'tpsReports', 'delete'));
|
||||
}
|
||||
|
||||
/**
|
||||
* testDbAclDeny method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testDeny() {
|
||||
$this->assertTrue($this->Acl->check('Micheal', 'smash', 'delete'));
|
||||
$this->Acl->deny('Micheal', 'smash', 'delete');
|
||||
$this->assertFalse($this->Acl->check('Micheal', 'smash', 'delete'));
|
||||
$this->assertTrue($this->Acl->check('Micheal', 'smash', 'read'));
|
||||
$this->assertTrue($this->Acl->check('Micheal', 'smash', 'create'));
|
||||
$this->assertTrue($this->Acl->check('Micheal', 'smash', 'update'));
|
||||
$this->assertFalse($this->Acl->check('Micheal', 'smash', '*'));
|
||||
|
||||
$this->assertTrue($this->Acl->check('Samir', 'refill', '*'));
|
||||
$this->Acl->deny('Samir', 'refill', '*');
|
||||
$this->assertFalse($this->Acl->check('Samir', 'refill', 'create'));
|
||||
$this->assertFalse($this->Acl->check('Samir', 'refill', 'update'));
|
||||
$this->assertFalse($this->Acl->check('Samir', 'refill', 'read'));
|
||||
$this->assertFalse($this->Acl->check('Samir', 'refill', 'delete'));
|
||||
|
||||
$result = $this->Acl->Aro->Permission->find('all', array('conditions' => array('AroTwoTest.alias' => 'Samir')));
|
||||
$expected = '-1';
|
||||
$this->assertEquals($expected, $result[0]['PermissionTwoTest']['_delete']);
|
||||
|
||||
$this->assertFalse($this->Acl->deny('Lumbergh', 'ROOT/tpsReports/DoesNotExist', 'create'));
|
||||
}
|
||||
|
||||
/**
|
||||
* testAclNodeLookup method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAclNodeLookup() {
|
||||
$result = $this->Acl->Aro->node('root/users/Samir');
|
||||
$expected = array(
|
||||
array('AroTwoTest' => array('id' => '7', 'parent_id' => '4', 'model' => 'User', 'foreign_key' => 3, 'alias' => 'Samir')),
|
||||
array('AroTwoTest' => array('id' => '4', 'parent_id' => '1', 'model' => 'Group', 'foreign_key' => 3, 'alias' => 'users')),
|
||||
array('AroTwoTest' => array('id' => '1', 'parent_id' => null, 'model' => null, 'foreign_key' => null, 'alias' => 'root'))
|
||||
);
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
$result = $this->Acl->Aco->node('ROOT/tpsReports/view/current');
|
||||
$expected = array(
|
||||
array('AcoTwoTest' => array('id' => '4', 'parent_id' => '3', 'model' => null, 'foreign_key' => null, 'alias' => 'current')),
|
||||
array('AcoTwoTest' => array('id' => '3', 'parent_id' => '2', 'model' => null, 'foreign_key' => null, 'alias' => 'view')),
|
||||
array('AcoTwoTest' => array('id' => '2', 'parent_id' => '1', 'model' => null, 'foreign_key' => null, 'alias' => 'tpsReports')),
|
||||
array('AcoTwoTest' => array('id' => '1', 'parent_id' => null, 'model' => null, 'foreign_key' => null, 'alias' => 'ROOT')),
|
||||
);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* testDbInherit method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testInherit() {
|
||||
//parent doesn't have access inherit should still deny
|
||||
$this->assertFalse($this->Acl->check('Milton', 'smash', 'delete'));
|
||||
$this->Acl->inherit('Milton', 'smash', 'delete');
|
||||
$this->assertFalse($this->Acl->check('Milton', 'smash', 'delete'));
|
||||
|
||||
//inherit parent
|
||||
$this->assertFalse($this->Acl->check('Milton', 'smash', 'read'));
|
||||
$this->Acl->inherit('Milton', 'smash', 'read');
|
||||
$this->assertTrue($this->Acl->check('Milton', 'smash', 'read'));
|
||||
}
|
||||
|
||||
/**
|
||||
* testDbGrant method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGrant() {
|
||||
$this->assertFalse($this->Acl->check('Samir', 'tpsReports', 'create'));
|
||||
$this->Acl->allow('Samir', 'tpsReports', 'create');
|
||||
$this->assertTrue($this->Acl->check('Samir', 'tpsReports', 'create'));
|
||||
|
||||
$this->assertFalse($this->Acl->check('Micheal', 'view', 'read'));
|
||||
$this->Acl->allow('Micheal', 'view', array('read', 'create', 'update'));
|
||||
$this->assertTrue($this->Acl->check('Micheal', 'view', 'read'));
|
||||
$this->assertTrue($this->Acl->check('Micheal', 'view', 'create'));
|
||||
$this->assertTrue($this->Acl->check('Micheal', 'view', 'update'));
|
||||
$this->assertFalse($this->Acl->check('Micheal', 'view', 'delete'));
|
||||
|
||||
$this->assertFalse($this->Acl->allow('Peter', 'ROOT/tpsReports/DoesNotExist', 'create'));
|
||||
}
|
||||
|
||||
/**
|
||||
* testDbRevoke method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testRevoke() {
|
||||
$this->assertTrue($this->Acl->check('Bobs', 'tpsReports', 'read'));
|
||||
$this->Acl->deny('Bobs', 'tpsReports', 'read');
|
||||
$this->assertFalse($this->Acl->check('Bobs', 'tpsReports', 'read'));
|
||||
|
||||
$this->assertTrue($this->Acl->check('users', 'printers', 'read'));
|
||||
$this->Acl->deny('users', 'printers', 'read');
|
||||
$this->assertFalse($this->Acl->check('users', 'printers', 'read'));
|
||||
$this->assertFalse($this->Acl->check('Samir', 'printers', 'read'));
|
||||
$this->assertFalse($this->Acl->check('Peter', 'printers', 'read'));
|
||||
|
||||
$this->Acl->deny('Bobs', 'ROOT/printers/DoesNotExist', 'create');
|
||||
}
|
||||
|
||||
/**
|
||||
* debug function - to help editing/creating test cases for the ACL component
|
||||
*
|
||||
* To check the overall ACL status at any time call $this->_debug();
|
||||
* Generates a list of the current aro and aco structures and a grid dump of the permissions that are defined
|
||||
* Only designed to work with the db based ACL
|
||||
*
|
||||
* @param bool $treesToo
|
||||
* @return void
|
||||
*/
|
||||
protected function _debug($printTreesToo = false) {
|
||||
$this->Acl->Aro->displayField = 'alias';
|
||||
$this->Acl->Aco->displayField = 'alias';
|
||||
$aros = $this->Acl->Aro->find('list', array('order' => 'lft'));
|
||||
$acos = $this->Acl->Aco->find('list', array('order' => 'lft'));
|
||||
$rights = array('*', 'create', 'read', 'update', 'delete');
|
||||
$permissions['Aros v Acos >'] = $acos;
|
||||
foreach ($aros as $aro) {
|
||||
$row = array();
|
||||
foreach ($acos as $aco) {
|
||||
$perms = '';
|
||||
foreach ($rights as $right) {
|
||||
if ($this->Acl->check($aro, $aco, $right)) {
|
||||
if ($right === '*') {
|
||||
$perms .= '****';
|
||||
break;
|
||||
}
|
||||
$perms .= $right[0];
|
||||
} elseif ($right !== '*') {
|
||||
$perms .= ' ';
|
||||
}
|
||||
}
|
||||
$row[] = $perms;
|
||||
}
|
||||
$permissions[$aro] = $row;
|
||||
}
|
||||
foreach ($permissions as $key => $values) {
|
||||
array_unshift($values, $key);
|
||||
$values = array_map(array(&$this, '_pad'), $values);
|
||||
$permissions[$key] = implode(' ', $values);
|
||||
}
|
||||
$permissions = array_map(array(&$this, '_pad'), $permissions);
|
||||
array_unshift($permissions, 'Current Permissions :');
|
||||
if ($printTreesToo) {
|
||||
debug(array('aros' => $this->Acl->Aro->generateTreeList(), 'acos' => $this->Acl->Aco->generateTreeList()));
|
||||
}
|
||||
debug(implode("\r\n", $permissions));
|
||||
}
|
||||
|
||||
/**
|
||||
* pad function
|
||||
* Used by debug to format strings used in the data dump
|
||||
*
|
||||
* @param string $string
|
||||
* @param int $len
|
||||
* @return void
|
||||
*/
|
||||
protected function _pad($string = '', $len = 14) {
|
||||
return str_pad($string, $len);
|
||||
}
|
||||
}
|
68
lib/Cake/Test/Case/Controller/Component/Acl/IniAclTest.php
Normal file
68
lib/Cake/Test/Case/Controller/Component/Acl/IniAclTest.php
Normal file
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
/**
|
||||
* IniAclTest file.
|
||||
*
|
||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* For full copyright and license information, please see the LICENSE.txt
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @package Cake.Test.Case.Controller.Component.Acl
|
||||
* @since CakePHP(tm) v 2.0
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
App::uses('IniAcl', 'Controller/Component/Acl');
|
||||
|
||||
/**
|
||||
* Test case for the IniAcl implementation
|
||||
*
|
||||
* @package Cake.Test.Case.Controller.Component.Acl
|
||||
*/
|
||||
class IniAclTest extends CakeTestCase {
|
||||
|
||||
/**
|
||||
* testIniCheck method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCheck() {
|
||||
$iniFile = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS . 'acl.ini.php';
|
||||
|
||||
$Ini = new IniAcl();
|
||||
$Ini->config = $Ini->readConfigFile($iniFile);
|
||||
|
||||
$this->assertFalse($Ini->check('admin', 'ads'));
|
||||
$this->assertTrue($Ini->check('admin', 'posts'));
|
||||
|
||||
$this->assertTrue($Ini->check('jenny', 'posts'));
|
||||
$this->assertTrue($Ini->check('jenny', 'ads'));
|
||||
|
||||
$this->assertTrue($Ini->check('paul', 'posts'));
|
||||
$this->assertFalse($Ini->check('paul', 'ads'));
|
||||
|
||||
$this->assertFalse($Ini->check('nobody', 'comments'));
|
||||
}
|
||||
|
||||
/**
|
||||
* check should accept a user array.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCheckArray() {
|
||||
$iniFile = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS . 'acl.ini.php';
|
||||
|
||||
$Ini = new IniAcl();
|
||||
$Ini->config = $Ini->readConfigFile($iniFile);
|
||||
$Ini->userPath = 'User.username';
|
||||
|
||||
$user = array(
|
||||
'User' => array('username' => 'admin')
|
||||
);
|
||||
$this->assertTrue($Ini->check($user, 'posts'));
|
||||
}
|
||||
}
|
374
lib/Cake/Test/Case/Controller/Component/Acl/PhpAclTest.php
Normal file
374
lib/Cake/Test/Case/Controller/Component/Acl/PhpAclTest.php
Normal file
|
@ -0,0 +1,374 @@
|
|||
<?php
|
||||
/**
|
||||
* PhpAclTest file.
|
||||
*
|
||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* For full copyright and license information, please see the LICENSE.txt
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @package Cake.Test.Case.Controller.Component.Acl
|
||||
* @since CakePHP(tm) v 2.1
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
App::uses('AclComponent', 'Controller/Component');
|
||||
App::uses('PhpAcl', 'Controller/Component/Acl');
|
||||
class_exists('AclComponent');
|
||||
|
||||
/**
|
||||
* Test case for the PhpAcl implementation
|
||||
*
|
||||
* @package Cake.Test.Case.Controller.Component.Acl
|
||||
*/
|
||||
class PhpAclTest extends CakeTestCase {
|
||||
|
||||
/**
|
||||
* Setup
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
Configure::write('Acl.classname', 'PhpAcl');
|
||||
$Collection = new ComponentCollection();
|
||||
$this->PhpAcl = new PhpAcl();
|
||||
$this->Acl = new AclComponent($Collection, array(
|
||||
'adapter' => array(
|
||||
'config' => CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS . 'acl.php',
|
||||
),
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test role inheritance
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testRoleInheritance() {
|
||||
$roles = $this->Acl->Aro->roles('User/peter');
|
||||
$this->assertEquals(array('Role/accounting'), $roles[0]);
|
||||
$this->assertEquals(array('User/peter'), $roles[1]);
|
||||
|
||||
$roles = $this->Acl->Aro->roles('hardy');
|
||||
$this->assertEquals(array('Role/database_manager', 'Role/data_acquirer'), $roles[0]);
|
||||
$this->assertEquals(array('Role/accounting', 'Role/data_analyst'), $roles[1]);
|
||||
$this->assertEquals(array('Role/accounting_manager', 'Role/reports'), $roles[2]);
|
||||
$this->assertEquals(array('User/hardy'), $roles[3]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test adding a role
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAddRole() {
|
||||
$this->assertEquals(array(array(PhpAro::DEFAULT_ROLE)), $this->Acl->Aro->roles('foobar'));
|
||||
$this->Acl->Aro->addRole(array('User/foobar' => 'Role/accounting'));
|
||||
$this->assertEquals(array(array('Role/accounting'), array('User/foobar')), $this->Acl->Aro->roles('foobar'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test resolving ARO
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAroResolve() {
|
||||
$this->Acl->Aro->map = array(
|
||||
'User' => 'FooModel/nickname',
|
||||
'Role' => 'FooModel/role',
|
||||
);
|
||||
|
||||
$this->assertEquals('Role/default', $this->Acl->Aro->resolve('Foo.bar'));
|
||||
$this->assertEquals('User/hardy', $this->Acl->Aro->resolve('FooModel/hardy'));
|
||||
$this->assertEquals('User/hardy', $this->Acl->Aro->resolve('hardy'));
|
||||
$this->assertEquals('User/hardy', $this->Acl->Aro->resolve(array('FooModel' => array('nickname' => 'hardy'))));
|
||||
$this->assertEquals('Role/admin', $this->Acl->Aro->resolve(array('FooModel' => array('role' => 'admin'))));
|
||||
$this->assertEquals('Role/admin', $this->Acl->Aro->resolve('Role/admin'));
|
||||
|
||||
$this->assertEquals('Role/admin', $this->Acl->Aro->resolve('admin'));
|
||||
$this->assertEquals('Role/admin', $this->Acl->Aro->resolve('FooModel/admin'));
|
||||
$this->assertEquals('Role/accounting', $this->Acl->Aro->resolve('accounting'));
|
||||
|
||||
$this->assertEquals(PhpAro::DEFAULT_ROLE, $this->Acl->Aro->resolve('bla'));
|
||||
$this->assertEquals(PhpAro::DEFAULT_ROLE, $this->Acl->Aro->resolve(array('FooModel' => array('role' => 'hardy'))));
|
||||
}
|
||||
|
||||
/**
|
||||
* test correct resolution of defined aliases
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAroAliases() {
|
||||
$this->Acl->Aro->map = array(
|
||||
'User' => 'User/username',
|
||||
'Role' => 'User/group_id',
|
||||
);
|
||||
|
||||
$this->Acl->Aro->aliases = array(
|
||||
'Role/1' => 'Role/admin',
|
||||
'Role/24' => 'Role/accounting',
|
||||
);
|
||||
|
||||
$user = array(
|
||||
'User' => array(
|
||||
'username' => 'unknown_user',
|
||||
'group_id' => '1',
|
||||
),
|
||||
);
|
||||
// group/1
|
||||
$this->assertEquals('Role/admin', $this->Acl->Aro->resolve($user));
|
||||
// group/24
|
||||
$this->assertEquals('Role/accounting', $this->Acl->Aro->resolve('Role/24'));
|
||||
$this->assertEquals('Role/accounting', $this->Acl->Aro->resolve('24'));
|
||||
|
||||
// check department
|
||||
$user = array(
|
||||
'User' => array(
|
||||
'username' => 'foo',
|
||||
'group_id' => '25',
|
||||
),
|
||||
);
|
||||
|
||||
$this->Acl->Aro->addRole(array('Role/IT' => null));
|
||||
$this->Acl->Aro->addAlias(array('Role/25' => 'Role/IT'));
|
||||
$this->Acl->allow('Role/IT', '/rules/debugging/*');
|
||||
|
||||
$this->assertEquals(array(array('Role/IT')), $this->Acl->Aro->roles($user));
|
||||
$this->assertTrue($this->Acl->check($user, '/rules/debugging/stats/pageload'));
|
||||
$this->assertTrue($this->Acl->check($user, '/rules/debugging/sql/queries'));
|
||||
// Role/default is allowed users dashboard, but not Role/IT
|
||||
$this->assertFalse($this->Acl->check($user, '/controllers/users/dashboard'));
|
||||
|
||||
$this->assertFalse($this->Acl->check($user, '/controllers/invoices/send'));
|
||||
// wee add an more specific entry for user foo to also inherit from Role/accounting
|
||||
$this->Acl->Aro->addRole(array('User/foo' => 'Role/IT, Role/accounting'));
|
||||
$this->assertTrue($this->Acl->check($user, '/controllers/invoices/send'));
|
||||
}
|
||||
|
||||
/**
|
||||
* test check method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCheck() {
|
||||
$this->assertTrue($this->Acl->check('jan', '/controllers/users/Dashboard'));
|
||||
$this->assertTrue($this->Acl->check('some_unknown_role', '/controllers/users/Dashboard'));
|
||||
$this->assertTrue($this->Acl->check('Role/admin', 'foo/bar'));
|
||||
$this->assertTrue($this->Acl->check('role/admin', '/foo/bar'));
|
||||
$this->assertTrue($this->Acl->check('jan', 'foo/bar'));
|
||||
$this->assertTrue($this->Acl->check('user/jan', 'foo/bar'));
|
||||
$this->assertTrue($this->Acl->check('Role/admin', 'controllers/bar'));
|
||||
$this->assertTrue($this->Acl->check(array('User' => array('username' => 'jan')), '/controllers/bar/bll'));
|
||||
$this->assertTrue($this->Acl->check('Role/database_manager', 'controllers/db/create'));
|
||||
$this->assertTrue($this->Acl->check('User/db_manager_2', 'controllers/db/create'));
|
||||
$this->assertFalse($this->Acl->check('db_manager_2', '/controllers/users/Dashboard'));
|
||||
|
||||
// inheritance: hardy -> reports -> data_analyst -> database_manager
|
||||
$this->assertTrue($this->Acl->check('User/hardy', 'controllers/db/create'));
|
||||
$this->assertFalse($this->Acl->check('User/jeff', 'controllers/db/create'));
|
||||
|
||||
$this->assertTrue($this->Acl->check('Role/database_manager', 'controllers/db/select'));
|
||||
$this->assertTrue($this->Acl->check('User/db_manager_2', 'controllers/db/select'));
|
||||
$this->assertFalse($this->Acl->check('User/jeff', 'controllers/db/select'));
|
||||
|
||||
$this->assertTrue($this->Acl->check('Role/database_manager', 'controllers/db/drop'));
|
||||
$this->assertTrue($this->Acl->check('User/db_manager_1', 'controllers/db/drop'));
|
||||
$this->assertFalse($this->Acl->check('db_manager_2', 'controllers/db/drop'));
|
||||
|
||||
$this->assertTrue($this->Acl->check('db_manager_2', 'controllers/invoices/edit'));
|
||||
$this->assertFalse($this->Acl->check('database_manager', 'controllers/invoices/edit'));
|
||||
$this->assertFalse($this->Acl->check('db_manager_1', 'controllers/invoices/edit'));
|
||||
|
||||
// Role/manager is allowed /controllers/*/*_manager
|
||||
$this->assertTrue($this->Acl->check('stan', 'controllers/invoices/manager_edit'));
|
||||
$this->assertTrue($this->Acl->check('Role/manager', 'controllers/baz/manager_foo'));
|
||||
$this->assertFalse($this->Acl->check('User/stan', 'custom/foo/manager_edit'));
|
||||
$this->assertFalse($this->Acl->check('stan', 'bar/baz/manager_foo'));
|
||||
$this->assertFalse($this->Acl->check('Role/accounting', 'bar/baz/manager_foo'));
|
||||
$this->assertFalse($this->Acl->check('accounting', 'controllers/baz/manager_foo'));
|
||||
|
||||
$this->assertTrue($this->Acl->check('User/stan', 'controllers/articles/edit'));
|
||||
$this->assertTrue($this->Acl->check('stan', 'controllers/articles/add'));
|
||||
$this->assertTrue($this->Acl->check('stan', 'controllers/articles/publish'));
|
||||
$this->assertFalse($this->Acl->check('User/stan', 'controllers/articles/delete'));
|
||||
$this->assertFalse($this->Acl->check('accounting', 'controllers/articles/edit'));
|
||||
$this->assertFalse($this->Acl->check('accounting', 'controllers/articles/add'));
|
||||
$this->assertFalse($this->Acl->check('role/accounting', 'controllers/articles/publish'));
|
||||
}
|
||||
|
||||
/**
|
||||
* lhs of defined rules are case insensitive
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCheckIsCaseInsensitive() {
|
||||
$this->assertTrue($this->Acl->check('hardy', 'controllers/forms/new'));
|
||||
$this->assertTrue($this->Acl->check('Role/data_acquirer', 'controllers/forms/new'));
|
||||
$this->assertTrue($this->Acl->check('hardy', 'controllers/FORMS/NEW'));
|
||||
$this->assertTrue($this->Acl->check('Role/data_acquirer', 'controllers/FORMS/NEW'));
|
||||
}
|
||||
|
||||
/**
|
||||
* allow should work in-memory
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAllow() {
|
||||
$this->assertFalse($this->Acl->check('jeff', 'foo/bar'));
|
||||
|
||||
$this->Acl->allow('jeff', 'foo/bar');
|
||||
|
||||
$this->assertTrue($this->Acl->check('jeff', 'foo/bar'));
|
||||
$this->assertFalse($this->Acl->check('peter', 'foo/bar'));
|
||||
$this->assertFalse($this->Acl->check('hardy', 'foo/bar'));
|
||||
|
||||
$this->Acl->allow('Role/accounting', 'foo/bar');
|
||||
|
||||
$this->assertTrue($this->Acl->check('peter', 'foo/bar'));
|
||||
$this->assertTrue($this->Acl->check('hardy', 'foo/bar'));
|
||||
|
||||
$this->assertFalse($this->Acl->check('Role/reports', 'foo/bar'));
|
||||
}
|
||||
|
||||
/**
|
||||
* deny should work in-memory
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testDeny() {
|
||||
$this->assertTrue($this->Acl->check('stan', 'controllers/baz/manager_foo'));
|
||||
|
||||
$this->Acl->deny('stan', 'controllers/baz/manager_foo');
|
||||
|
||||
$this->assertFalse($this->Acl->check('stan', 'controllers/baz/manager_foo'));
|
||||
$this->assertTrue($this->Acl->check('Role/manager', 'controllers/baz/manager_foo'));
|
||||
$this->assertTrue($this->Acl->check('stan', 'controllers/baz/manager_bar'));
|
||||
$this->assertTrue($this->Acl->check('stan', 'controllers/baz/manager_foooooo'));
|
||||
}
|
||||
|
||||
/**
|
||||
* test that a deny rule wins over an equally specific allow rule
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testDenyRuleIsStrongerThanAllowRule() {
|
||||
$this->assertFalse($this->Acl->check('peter', 'baz/bam'));
|
||||
$this->Acl->allow('peter', 'baz/bam');
|
||||
$this->assertTrue($this->Acl->check('peter', 'baz/bam'));
|
||||
$this->Acl->deny('peter', 'baz/bam');
|
||||
$this->assertFalse($this->Acl->check('peter', 'baz/bam'));
|
||||
|
||||
$this->assertTrue($this->Acl->check('stan', 'controllers/reports/foo'));
|
||||
// stan is denied as he's sales and sales is denied /controllers/*/delete
|
||||
$this->assertFalse($this->Acl->check('stan', 'controllers/reports/delete'));
|
||||
$this->Acl->allow('stan', 'controllers/reports/delete');
|
||||
$this->assertFalse($this->Acl->check('Role/sales', 'controllers/reports/delete'));
|
||||
$this->assertTrue($this->Acl->check('stan', 'controllers/reports/delete'));
|
||||
$this->Acl->deny('stan', 'controllers/reports/delete');
|
||||
$this->assertFalse($this->Acl->check('stan', 'controllers/reports/delete'));
|
||||
|
||||
// there is already an equally specific deny rule that will win
|
||||
$this->Acl->allow('stan', 'controllers/reports/delete');
|
||||
$this->assertFalse($this->Acl->check('stan', 'controllers/reports/delete'));
|
||||
}
|
||||
|
||||
/**
|
||||
* test that an invalid configuration throws exception
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testInvalidConfigWithAroMissing() {
|
||||
$this->setExpectedException(
|
||||
'AclException',
|
||||
'"roles" section not found in configuration'
|
||||
);
|
||||
$config = array('aco' => array('allow' => array('foo' => '')));
|
||||
$this->PhpAcl->build($config);
|
||||
}
|
||||
|
||||
public function testInvalidConfigWithAcosMissing() {
|
||||
$this->setExpectedException(
|
||||
'AclException',
|
||||
'Neither "allow" nor "deny" rules were provided in configuration.'
|
||||
);
|
||||
|
||||
$config = array(
|
||||
'roles' => array('Role/foo' => null),
|
||||
);
|
||||
|
||||
$this->PhpAcl->build($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* test resolving of ACOs
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAcoResolve() {
|
||||
$this->assertEquals(array('foo', 'bar'), $this->Acl->Aco->resolve('foo/bar'));
|
||||
$this->assertEquals(array('foo', 'bar'), $this->Acl->Aco->resolve('foo/bar'));
|
||||
$this->assertEquals(array('foo', 'bar', 'baz'), $this->Acl->Aco->resolve('foo/bar/baz'));
|
||||
$this->assertEquals(array('foo', '*-bar', '?-baz'), $this->Acl->Aco->resolve('foo/*-bar/?-baz'));
|
||||
|
||||
$this->assertEquals(array('foo', 'bar', '[a-f0-9]{24}', '*_bla', 'bla'), $this->Acl->Aco->resolve('foo/bar/[a-f0-9]{24}/*_bla/bla'));
|
||||
|
||||
// multiple slashes will be squashed to a single, trimmed and then exploded
|
||||
$this->assertEquals(array('foo', 'bar'), $this->Acl->Aco->resolve('foo//bar'));
|
||||
$this->assertEquals(array('foo', 'bar'), $this->Acl->Aco->resolve('//foo///bar/'));
|
||||
$this->assertEquals(array('foo', 'bar'), $this->Acl->Aco->resolve('/foo//bar//'));
|
||||
$this->assertEquals(array('foo', 'bar'), $this->Acl->Aco->resolve('/foo // bar'));
|
||||
$this->assertEquals(array(), $this->Acl->Aco->resolve('/////'));
|
||||
}
|
||||
|
||||
/**
|
||||
* test that declaring cyclic dependencies should give an error when building the tree
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAroDeclarationContainsCycles() {
|
||||
$config = array(
|
||||
'roles' => array(
|
||||
'Role/a' => null,
|
||||
'Role/b' => 'User/b',
|
||||
'User/a' => 'Role/a, Role/b',
|
||||
'User/b' => 'User/a',
|
||||
|
||||
),
|
||||
'rules' => array(
|
||||
'allow' => array(
|
||||
'*' => 'Role/a',
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$this->expectError('PHPUnit_Framework_Error', 'cycle detected' /* ... */);
|
||||
$this->PhpAcl->build($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* test that with policy allow, only denies count
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testPolicy() {
|
||||
// allow by default
|
||||
$this->Acl->settings['adapter']['policy'] = PhpAcl::ALLOW;
|
||||
$this->Acl->adapter($this->PhpAcl);
|
||||
|
||||
$this->assertTrue($this->Acl->check('Role/sales', 'foo'));
|
||||
$this->assertTrue($this->Acl->check('Role/sales', 'controllers/bla/create'));
|
||||
$this->assertTrue($this->Acl->check('Role/default', 'foo'));
|
||||
// undefined user, undefined aco
|
||||
$this->assertTrue($this->Acl->check('foobar', 'foo/bar'));
|
||||
|
||||
// deny rule: Role.sales -> controllers.*.delete
|
||||
$this->assertFalse($this->Acl->check('Role/sales', 'controllers/bar/delete'));
|
||||
$this->assertFalse($this->Acl->check('Role/sales', 'controllers/bar', 'delete'));
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue