| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395 |
- <?php
- /**
- * TinyAuthorizeTest file
- *
- */
- App::uses('TinyAuthorize', 'Tools.Controller/Component/Auth');
- App::uses('MyCakeTestCase', 'Tools.TestSuite');
- App::uses('Controller', 'Controller');
- App::uses('ComponentCollection', 'Controller');
- App::uses('CakeRequest', 'Network');
- /**
- * Test case for DirectAuthentication
- *
- */
- class TinyAuthorizeTest extends MyCakeTestCase {
- public $fixtures = array('core.user', 'core.auth_user', 'plugin.tools.role');
- public $Collection;
- public $request;
- /**
- * Setup
- *
- * @return void
- */
- public function setUp() {
- parent::setUp();
- $this->Collection = new ComponentCollection();
- $this->request = new CakeRequest(null, false);
- $aclData = <<<INI
- [Users]
- ; add = public
- edit = user
- admin_index = admin
- [Comments]
- ; index is public
- add,edit,delete = user
- * = admin
- [Tags]
- add = *
- very_long_action_name_action = user
- public_action = public
- INI;
- file_put_contents(TMP . 'acl.ini', $aclData);
- $this->assertTrue(file_exists(TMP . 'acl.ini'));
- Configure::write('Role', array('user' => 1, 'moderator' => 2, 'admin' => 3, 'public' => -1));
- }
- public function tearDown() {
- unlink(TMP . 'acl.ini');
- parent::tearDown();
- }
- /**
- * Test applying settings in the constructor
- *
- * @return void
- */
- public function testConstructor() {
- $object = new TestTinyAuthorize($this->Collection, array(
- 'aclModel' => 'AuthRole',
- 'aclKey' => 'auth_role_id',
- 'autoClearCache' => true,
- ));
- $this->assertEquals('AuthRole', $object->settings['aclModel']);
- $this->assertEquals('auth_role_id', $object->settings['aclKey']);
- }
- /**
- * @return void
- */
- public function testGetAcl() {
- $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
- $res = $object->getAcl();
- $expected = array(
- 'users' => array(
- 'edit' => array(1),
- 'admin_index' => array(3)
- ),
- 'comments' => array(
- 'add' => array(1),
- 'edit' => array(1),
- 'delete' => array(1),
- '*' => array(3),
- ),
- 'tags' => array(
- 'add' => array(1, 2, 3, -1),
- 'very_long_action_name_action' => array(1),
- 'public_action' => array(-1)
- ),
- );
- $this->debug($res);
- $this->assertEquals($expected, $res);
- }
- /**
- * @return void
- */
- public function testBasicUserMethodDisallowed() {
- $this->request->params['controller'] = 'users';
- $this->request->params['action'] = 'edit';
- $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
- $this->assertEquals('Role', $object->settings['aclModel']);
- $this->assertEquals('role_id', $object->settings['aclKey']);
- $user = array(
- 'role_id' => 4,
- );
- $res = $object->authorize($user, $this->request);
- $this->assertFalse($res);
- $user = array(
- 'role_id' => 3,
- );
- $res = $object->authorize($user, $this->request);
- $this->assertFalse($res);
- }
- /**
- * @return void
- */
- public function testBasicUserMethodAllowed() {
- $this->request->params['controller'] = 'users';
- $this->request->params['action'] = 'edit';
- $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
- // single role_id field in users table
- $user = array(
- 'role_id' => 1,
- );
- $res = $object->authorize($user, $this->request);
- $this->assertTrue($res);
- $this->request->params['action'] = 'admin_index';
- $user = array(
- 'role_id' => 3,
- );
- $res = $object->authorize($user, $this->request);
- $this->assertTrue($res);
- }
- /**
- * @return void
- */
- public function testBasicUserMethodAllowedWithLongActionNames() {
- $this->request->params['controller'] = 'tags';
- $this->request->params['action'] = 'very_long_action_name_action';
- $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
- // single role_id field in users table
- $user = array(
- 'role_id' => 1,
- );
- $res = $object->authorize($user, $this->request);
- $this->assertTrue($res);
- $user = array(
- 'role_id' => 3,
- );
- $res = $object->authorize($user, $this->request);
- $this->assertFalse($res);
- }
- /**
- * @return void
- */
- public function testBasicUserMethodAllowedMultiRole() {
- $this->request->params['controller'] = 'users';
- $this->request->params['action'] = 'admin_index';
- $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
- // flat list of roles
- $user = array(
- 'Role' => array(1, 3),
- );
- $res = $object->authorize($user, $this->request);
- $this->assertTrue($res);
- // verbose role defition using the new 2.x contain param for Auth
- $user = array(
- 'Role' => array(array('id' => 1, 'RoleUser' => array()), array('id' => 3, 'RoleUser' => array())),
- );
- $res = $object->authorize($user, $this->request);
- $this->assertTrue($res);
- }
- /**
- * @return void
- */
- public function testBasicUserMethodAllowedWildcard() {
- $this->request->params['controller'] = 'tags';
- $this->request->params['action'] = 'public_action';
- $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
- $user = array(
- 'role_id' => 6,
- );
- $res = $object->authorize($user, $this->request);
- $this->assertTrue($res);
- }
- /**
- * @return void
- */
- public function testUserMethodsAllowed() {
- $this->request->params['controller'] = 'users';
- $this->request->params['action'] = 'some_action';
- $object = new TestTinyAuthorize($this->Collection, array('allowUser' => true, 'autoClearCache' => true));
- $user = array(
- 'role_id' => 1,
- );
- $res = $object->authorize($user, $this->request);
- $this->assertTrue($res);
- $this->request->params['controller'] = 'users';
- $this->request->params['action'] = 'admin_index';
- $object = new TestTinyAuthorize($this->Collection, array('allowUser' => true, 'autoClearCache' => true));
- $user = array(
- 'role_id' => 1,
- );
- $res = $object->authorize($user, $this->request);
- $this->assertFalse($res);
- $user = array(
- 'role_id' => 3,
- );
- $res = $object->authorize($user, $this->request);
- $this->assertTrue($res);
- }
- /**
- * @return void
- */
- public function testAdminMethodsAllowed() {
- $this->request->params['controller'] = 'users';
- $this->request->params['action'] = 'some_action';
- $config = array('allowAdmin' => true, 'adminRole' => 3, 'autoClearCache' => true);
- $object = new TestTinyAuthorize($this->Collection, $config);
- $user = array(
- 'role_id' => 1,
- );
- $res = $object->authorize($user, $this->request);
- $this->assertFalse($res);
- $this->request->params['controller'] = 'users';
- $this->request->params['action'] = 'admin_index';
- $object = new TestTinyAuthorize($this->Collection, $config);
- $user = array(
- 'role_id' => 1,
- );
- $res = $object->authorize($user, $this->request);
- $this->assertFalse($res);
- $user = array(
- 'role_id' => 3,
- );
- $res = $object->authorize($user, $this->request);
- $this->assertTrue($res);
- }
- /**
- * Should only be used in combination with Auth->allow() to mark those as public in the acl.ini, as well.
- * Not necessary and certainly not recommended as acl.ini only.
- *
- * @return void
- */
- public function testBasicUserMethodAllowedPublically() {
- $this->request->params['controller'] = 'tags';
- $this->request->params['action'] = 'add';
- $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
- $user = array(
- 'role_id' => 2,
- );
- $res = $object->authorize($user, $this->request);
- $this->assertTrue($res);
- $this->request->params['controller'] = 'comments';
- $this->request->params['action'] = 'foo';
- $user = array(
- 'role_id' => 3,
- );
- $res = $object->authorize($user, $this->request);
- $this->assertTrue($res);
- }
- /**
- * TinyAuthorizeTest::testWithRoleTable()
- *
- * @return void
- */
- public function testWithRoleTable() {
- $User = ClassRegistry::init('User');
- $User->bindModel(array('belongsTo' => array('Role')), false);
- // We want the session to be used.
- Configure::delete('Role');
- $this->request->params['controller'] = 'users';
- $this->request->params['action'] = 'edit';
- $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
- // User role is 4 here, though. Also contains left joined Role date here just to check that it works, too.
- $user = array(
- 'Role' => array(
- 'id' => '4',
- 'alias' => 'user',
- ),
- 'role_id' => 4,
- );
- $res = $object->authorize($user, $this->request);
- $this->assertTrue($res);
- Configure::delete('Role');
- $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
- $user = array(
- 'role_id' => 6,
- );
- $res = $object->authorize($user, $this->request);
- $this->assertFalse($res);
- $this->assertTrue((bool)(Configure::read('Role')));
- // Multi-role test - failure
- Configure::delete('Role');
- $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
- $user = array(
- 'Role' => array(
- array('id' => 7, 'alias' => 'user'),
- array('id' => 8, 'alias' => 'partner'),
- )
- );
- $res = $object->authorize($user, $this->request);
- $this->assertFalse($res);
- $this->assertTrue((bool)(Configure::read('Role')));
- Configure::delete('Role');
- $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
- // Multi-role test
- $user = array(
- 'Role' => array(
- array('id' => 4, 'alias' => 'user'),
- array('id' => 6, 'alias' => 'partner'),
- )
- );
- $res = $object->authorize($user, $this->request);
- $this->assertTrue($res);
- }
- }
- class TestTinyAuthorize extends TinyAuthorize {
- public function matchArray() {
- return $this->_matchArray;
- }
- public function getAcl() {
- return $this->_getAcl();
- }
- protected function _getAcl($path = TMP) {
- return parent::_getAcl($path);
- }
- }
|