DbAclTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. <?php
  2. /**
  3. * DbAclTest file.
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package Cake.Test.Case.Controller.Component.Acl
  16. * @since CakePHP(tm) v 2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('ComponentCollection', 'Controller');
  20. App::uses('AclComponent', 'Controller/Component');
  21. App::uses('DbAcl', 'Controller/Component/Acl');
  22. App::uses('AclNode', 'Model');
  23. App::uses('Permission', 'Model');
  24. require_once dirname(dirname(dirname(dirname(__FILE__)))) . DS . 'Model' . DS . 'models.php';
  25. /**
  26. * AclNodeTwoTestBase class
  27. *
  28. * @package Cake.Test.Case.Controller.Component.Acl
  29. */
  30. class AclNodeTwoTestBase extends AclNode {
  31. /**
  32. * useDbConfig property
  33. *
  34. * @var string 'test'
  35. */
  36. public $useDbConfig = 'test';
  37. /**
  38. * cacheSources property
  39. *
  40. * @var bool false
  41. */
  42. public $cacheSources = false;
  43. }
  44. /**
  45. * AroTwoTest class
  46. *
  47. * @package Cake.Test.Case.Controller.Component.Acl
  48. */
  49. class AroTwoTest extends AclNodeTwoTestBase {
  50. /**
  51. * name property
  52. *
  53. * @var string 'AroTwoTest'
  54. */
  55. public $name = 'AroTwoTest';
  56. /**
  57. * useTable property
  58. *
  59. * @var string 'aro_twos'
  60. */
  61. public $useTable = 'aro_twos';
  62. /**
  63. * hasAndBelongsToMany property
  64. *
  65. * @var array
  66. */
  67. public $hasAndBelongsToMany = array('AcoTwoTest' => array('with' => 'PermissionTwoTest'));
  68. }
  69. /**
  70. * AcoTwoTest class
  71. *
  72. * @package Cake.Test.Case.Controller.Component.Acl
  73. */
  74. class AcoTwoTest extends AclNodeTwoTestBase {
  75. /**
  76. * name property
  77. *
  78. * @var string 'AcoTwoTest'
  79. */
  80. public $name = 'AcoTwoTest';
  81. /**
  82. * useTable property
  83. *
  84. * @var string 'aco_twos'
  85. */
  86. public $useTable = 'aco_twos';
  87. /**
  88. * hasAndBelongsToMany property
  89. *
  90. * @var array
  91. */
  92. public $hasAndBelongsToMany = array('AroTwoTest' => array('with' => 'PermissionTwoTest'));
  93. }
  94. /**
  95. * PermissionTwoTest class
  96. *
  97. * @package Cake.Test.Case.Controller.Component.Acl
  98. */
  99. class PermissionTwoTest extends Permission {
  100. /**
  101. * name property
  102. *
  103. * @var string 'PermissionTwoTest'
  104. */
  105. public $name = 'PermissionTwoTest';
  106. /**
  107. * useTable property
  108. *
  109. * @var string 'aros_aco_twos'
  110. */
  111. public $useTable = 'aros_aco_twos';
  112. /**
  113. * cacheQueries property
  114. *
  115. * @var bool false
  116. */
  117. public $cacheQueries = false;
  118. /**
  119. * belongsTo property
  120. *
  121. * @var array
  122. */
  123. public $belongsTo = array('AroTwoTest' => array('foreignKey' => 'aro_id'), 'AcoTwoTest' => array('foreignKey' => 'aco_id'));
  124. /**
  125. * actsAs property
  126. *
  127. * @var mixed null
  128. */
  129. public $actsAs = null;
  130. }
  131. /**
  132. * DbAclTwoTest class
  133. *
  134. * @package Cake.Test.Case.Controller.Component.Acl
  135. */
  136. class DbAclTwoTest extends DbAcl {
  137. /**
  138. * construct method
  139. *
  140. * @return void
  141. */
  142. public function __construct() {
  143. $this->Aro = new AroTwoTest();
  144. $this->Aro->Permission = new PermissionTwoTest();
  145. $this->Aco = new AcoTwoTest();
  146. $this->Aro->Permission = new PermissionTwoTest();
  147. $this->Permission = $this->Aro->Permission;
  148. $this->Permission->Aro = $this->Aro;
  149. $this->Permission->Aco = $this->Aco;
  150. }
  151. }
  152. /**
  153. * Test case for AclComponent using the DbAcl implementation.
  154. *
  155. * @package Cake.Test.Case.Controller.Component.Acl
  156. */
  157. class DbAclTest extends CakeTestCase {
  158. /**
  159. * fixtures property
  160. *
  161. * @var array
  162. */
  163. public $fixtures = array('core.aro_two', 'core.aco_two', 'core.aros_aco_two');
  164. /**
  165. * setUp method
  166. *
  167. * @return void
  168. */
  169. public function setUp() {
  170. parent::setUp();
  171. Configure::write('Acl.classname', 'DbAclTwoTest');
  172. Configure::write('Acl.database', 'test');
  173. $Collection = new ComponentCollection();
  174. $this->Acl = new AclComponent($Collection);
  175. }
  176. /**
  177. * tearDown method
  178. *
  179. * @return void
  180. */
  181. public function tearDown() {
  182. parent::tearDown();
  183. unset($this->Acl);
  184. }
  185. /**
  186. * testAclCreate method
  187. *
  188. * @return void
  189. */
  190. public function testCreate() {
  191. $this->Acl->Aro->create(array('alias' => 'Chotchkey'));
  192. $this->assertTrue((bool)$this->Acl->Aro->save());
  193. $parent = $this->Acl->Aro->id;
  194. $this->Acl->Aro->create(array('parent_id' => $parent, 'alias' => 'Joanna'));
  195. $this->assertTrue((bool)$this->Acl->Aro->save());
  196. $this->Acl->Aro->create(array('parent_id' => $parent, 'alias' => 'Stapler'));
  197. $this->assertTrue((bool)$this->Acl->Aro->save());
  198. $root = $this->Acl->Aco->node('ROOT');
  199. $parent = $root[0]['AcoTwoTest']['id'];
  200. $this->Acl->Aco->create(array('parent_id' => $parent, 'alias' => 'Drinks'));
  201. $this->assertTrue((bool)$this->Acl->Aco->save());
  202. $this->Acl->Aco->create(array('parent_id' => $parent, 'alias' => 'PiecesOfFlair'));
  203. $this->assertTrue((bool)$this->Acl->Aco->save());
  204. }
  205. /**
  206. * testAclCreateWithParent method
  207. *
  208. * @return void
  209. */
  210. public function testCreateWithParent() {
  211. $parent = $this->Acl->Aro->findByAlias('Peter', null, null, -1);
  212. $this->Acl->Aro->create();
  213. $this->Acl->Aro->save(array(
  214. 'alias' => 'Subordinate',
  215. 'model' => 'User',
  216. 'foreign_key' => 7,
  217. 'parent_id' => $parent['AroTwoTest']['id']
  218. ));
  219. $result = $this->Acl->Aro->findByAlias('Subordinate', null, null, -1);
  220. $this->assertEquals(16, $result['AroTwoTest']['lft']);
  221. $this->assertEquals(17, $result['AroTwoTest']['rght']);
  222. }
  223. /**
  224. * testDbAclAllow method
  225. *
  226. * @expectedException PHPUnit_Framework_Error_Warning
  227. * @return void
  228. */
  229. public function testAllow() {
  230. $this->assertFalse($this->Acl->check('Micheal', 'tpsReports', 'read'));
  231. $this->assertTrue($this->Acl->allow('Micheal', 'tpsReports', array('read', 'delete', 'update')));
  232. $this->assertTrue($this->Acl->check('Micheal', 'tpsReports', 'update'));
  233. $this->assertTrue($this->Acl->check('Micheal', 'tpsReports', 'read'));
  234. $this->assertTrue($this->Acl->check('Micheal', 'tpsReports', 'delete'));
  235. $this->assertFalse($this->Acl->check('Micheal', 'tpsReports', 'create'));
  236. $this->assertTrue($this->Acl->allow('Micheal', 'ROOT/tpsReports', 'create'));
  237. $this->assertTrue($this->Acl->check('Micheal', 'tpsReports', 'create'));
  238. $this->assertTrue($this->Acl->check('Micheal', 'tpsReports', 'delete'));
  239. $this->assertTrue($this->Acl->allow('Micheal', 'printers', 'create'));
  240. // Michael no longer has his delete permission for tpsReports!
  241. $this->assertTrue($this->Acl->check('Micheal', 'tpsReports', 'delete'));
  242. $this->assertTrue($this->Acl->check('Micheal', 'printers', 'create'));
  243. $this->assertFalse($this->Acl->check('root/users/Samir', 'ROOT/tpsReports/view'));
  244. $this->assertTrue($this->Acl->allow('root/users/Samir', 'ROOT/tpsReports/view', '*'));
  245. $this->assertTrue($this->Acl->check('Samir', 'view', 'read'));
  246. $this->assertTrue($this->Acl->check('root/users/Samir', 'ROOT/tpsReports/view', 'update'));
  247. $this->assertFalse($this->Acl->check('root/users/Samir', 'ROOT/tpsReports/update','*'));
  248. $this->assertTrue($this->Acl->allow('root/users/Samir', 'ROOT/tpsReports/update', '*'));
  249. $this->assertTrue($this->Acl->check('Samir', 'update', 'read'));
  250. $this->assertTrue($this->Acl->check('root/users/Samir', 'ROOT/tpsReports/update', 'update'));
  251. // Samir should still have his tpsReports/view permissions, but does not
  252. $this->assertTrue($this->Acl->check('root/users/Samir', 'ROOT/tpsReports/view', 'update'));
  253. $this->assertFalse($this->Acl->allow('Lumbergh', 'ROOT/tpsReports/DoesNotExist', 'create'));
  254. }
  255. /**
  256. * testAllowInvalidNode method
  257. *
  258. * @expectedException PHPUnit_Framework_Error_Warning
  259. * @return void
  260. */
  261. public function testAllowInvalidNode() {
  262. $this->Acl->allow('Homer', 'tpsReports', 'create');
  263. }
  264. /**
  265. * testDbAclCheck method
  266. *
  267. * @return void
  268. */
  269. public function testCheck() {
  270. $this->assertTrue($this->Acl->check('Samir', 'print', 'read'));
  271. $this->assertTrue($this->Acl->check('Lumbergh', 'current', 'read'));
  272. $this->assertFalse($this->Acl->check('Milton', 'smash', 'read'));
  273. $this->assertFalse($this->Acl->check('Milton', 'current', 'update'));
  274. $this->assertFalse($this->Acl->check(null, 'printers', 'create'));
  275. $this->assertFalse($this->Acl->check('managers', null, 'read'));
  276. $this->assertTrue($this->Acl->check('Bobs', 'ROOT/tpsReports/view/current', 'read'));
  277. $this->assertFalse($this->Acl->check('Samir', 'ROOT/tpsReports/update', 'read'));
  278. $this->assertFalse($this->Acl->check('root/users/Milton', 'smash', 'delete'));
  279. }
  280. /**
  281. * testCheckInvalidNode method
  282. *
  283. * @expectedException PHPUnit_Framework_Error_Warning
  284. * @return void
  285. */
  286. public function testCheckInvalidNode() {
  287. $this->assertFalse($this->Acl->check('WRONG', 'tpsReports', 'read'));
  288. }
  289. /**
  290. * testCheckInvalidPermission method
  291. *
  292. * @expectedException PHPUnit_Framework_Error_Notice
  293. * @return void
  294. */
  295. public function testCheckInvalidPermission() {
  296. $this->Acl->check('Lumbergh', 'smash', 'foobar');
  297. }
  298. /**
  299. * testCheckMissingPermission method
  300. *
  301. * @expectedException PHPUnit_Framework_Error_Warning
  302. * @return void
  303. */
  304. public function testCheckMissingPermission() {
  305. $this->Acl->check('users', 'NonExistent', 'read');
  306. }
  307. /**
  308. * testDbAclCascadingDeny function
  309. *
  310. * Setup the acl permissions such that Bobs inherits from admin.
  311. * deny Admin delete access to a specific resource, check the permissions are inherited.
  312. *
  313. * @return void
  314. */
  315. public function testAclCascadingDeny() {
  316. $this->Acl->inherit('Bobs', 'ROOT', '*');
  317. $this->assertTrue($this->Acl->check('admin', 'tpsReports', 'delete'));
  318. $this->assertTrue($this->Acl->check('Bobs', 'tpsReports', 'delete'));
  319. $this->Acl->deny('admin', 'tpsReports', 'delete');
  320. $this->assertFalse($this->Acl->check('admin', 'tpsReports', 'delete'));
  321. $this->assertFalse($this->Acl->check('Bobs', 'tpsReports', 'delete'));
  322. }
  323. /**
  324. * testDbAclDeny method
  325. *
  326. * @expectedException PHPUnit_Framework_Error_Warning
  327. * @return void
  328. */
  329. public function testDeny() {
  330. $this->assertTrue($this->Acl->check('Micheal', 'smash', 'delete'));
  331. $this->Acl->deny('Micheal', 'smash', 'delete');
  332. $this->assertFalse($this->Acl->check('Micheal', 'smash', 'delete'));
  333. $this->assertTrue($this->Acl->check('Micheal', 'smash', 'read'));
  334. $this->assertTrue($this->Acl->check('Micheal', 'smash', 'create'));
  335. $this->assertTrue($this->Acl->check('Micheal', 'smash', 'update'));
  336. $this->assertFalse($this->Acl->check('Micheal', 'smash', '*'));
  337. $this->assertTrue($this->Acl->check('Samir', 'refill', '*'));
  338. $this->Acl->deny('Samir', 'refill', '*');
  339. $this->assertFalse($this->Acl->check('Samir', 'refill', 'create'));
  340. $this->assertFalse($this->Acl->check('Samir', 'refill', 'update'));
  341. $this->assertFalse($this->Acl->check('Samir', 'refill', 'read'));
  342. $this->assertFalse($this->Acl->check('Samir', 'refill', 'delete'));
  343. $result = $this->Acl->Aro->Permission->find('all', array('conditions' => array('AroTwoTest.alias' => 'Samir')));
  344. $expected = '-1';
  345. $this->assertEquals($expected, $result[0]['PermissionTwoTest']['_delete']);
  346. $this->assertFalse($this->Acl->deny('Lumbergh', 'ROOT/tpsReports/DoesNotExist', 'create'));
  347. }
  348. /**
  349. * testAclNodeLookup method
  350. *
  351. * @return void
  352. */
  353. public function testAclNodeLookup() {
  354. $result = $this->Acl->Aro->node('root/users/Samir');
  355. $expected = array(
  356. array('AroTwoTest' => array('id' => '7', 'parent_id' => '4', 'model' => 'User', 'foreign_key' => 3, 'alias' => 'Samir')),
  357. array('AroTwoTest' => array('id' => '4', 'parent_id' => '1', 'model' => 'Group', 'foreign_key' => 3, 'alias' => 'users')),
  358. array('AroTwoTest' => array('id' => '1', 'parent_id' => null, 'model' => null, 'foreign_key' => null, 'alias' => 'root'))
  359. );
  360. $this->assertEquals($expected, $result);
  361. $result = $this->Acl->Aco->node('ROOT/tpsReports/view/current');
  362. $expected = array(
  363. array('AcoTwoTest' => array('id' => '4', 'parent_id' => '3', 'model' => null, 'foreign_key' => null, 'alias' => 'current')),
  364. array('AcoTwoTest' => array('id' => '3', 'parent_id' => '2', 'model' => null, 'foreign_key' => null, 'alias' => 'view')),
  365. array('AcoTwoTest' => array('id' => '2', 'parent_id' => '1', 'model' => null, 'foreign_key' => null, 'alias' => 'tpsReports')),
  366. array('AcoTwoTest' => array('id' => '1', 'parent_id' => null, 'model' => null, 'foreign_key' => null, 'alias' => 'ROOT')),
  367. );
  368. $this->assertEquals($expected, $result);
  369. }
  370. /**
  371. * testDbInherit method
  372. *
  373. * @return void
  374. */
  375. public function testInherit() {
  376. //parent doesn't have access inherit should still deny
  377. $this->assertFalse($this->Acl->check('Milton', 'smash', 'delete'));
  378. $this->Acl->inherit('Milton', 'smash', 'delete');
  379. $this->assertFalse($this->Acl->check('Milton', 'smash', 'delete'));
  380. //inherit parent
  381. $this->assertFalse($this->Acl->check('Milton', 'smash', 'read'));
  382. $this->Acl->inherit('Milton', 'smash', 'read');
  383. $this->assertTrue($this->Acl->check('Milton', 'smash', 'read'));
  384. }
  385. /**
  386. * testDbGrant method
  387. *
  388. * @expectedException PHPUnit_Framework_Error_Warning
  389. * @return void
  390. */
  391. public function testGrant() {
  392. $this->assertFalse($this->Acl->check('Samir', 'tpsReports', 'create'));
  393. $this->Acl->allow('Samir', 'tpsReports', 'create');
  394. $this->assertTrue($this->Acl->check('Samir', 'tpsReports', 'create'));
  395. $this->assertFalse($this->Acl->check('Micheal', 'view', 'read'));
  396. $this->Acl->allow('Micheal', 'view', array('read', 'create', 'update'));
  397. $this->assertTrue($this->Acl->check('Micheal', 'view', 'read'));
  398. $this->assertTrue($this->Acl->check('Micheal', 'view', 'create'));
  399. $this->assertTrue($this->Acl->check('Micheal', 'view', 'update'));
  400. $this->assertFalse($this->Acl->check('Micheal', 'view', 'delete'));
  401. $this->assertFalse($this->Acl->allow('Peter', 'ROOT/tpsReports/DoesNotExist', 'create'));
  402. }
  403. /**
  404. * testDbRevoke method
  405. *
  406. * @expectedException PHPUnit_Framework_Error_Warning
  407. * @return void
  408. */
  409. public function testRevoke() {
  410. $this->assertTrue($this->Acl->check('Bobs', 'tpsReports', 'read'));
  411. $this->Acl->deny('Bobs', 'tpsReports', 'read');
  412. $this->assertFalse($this->Acl->check('Bobs', 'tpsReports', 'read'));
  413. $this->assertTrue($this->Acl->check('users', 'printers', 'read'));
  414. $this->Acl->deny('users', 'printers', 'read');
  415. $this->assertFalse($this->Acl->check('users', 'printers', 'read'));
  416. $this->assertFalse($this->Acl->check('Samir', 'printers', 'read'));
  417. $this->assertFalse($this->Acl->check('Peter', 'printers', 'read'));
  418. $this->Acl->deny('Bobs', 'ROOT/printers/DoesNotExist', 'create');
  419. }
  420. /**
  421. * debug function - to help editing/creating test cases for the ACL component
  422. *
  423. * To check the overall ACL status at any time call $this->_debug();
  424. * Generates a list of the current aro and aco structures and a grid dump of the permissions that are defined
  425. * Only designed to work with the db based ACL
  426. *
  427. * @param bool $treesToo
  428. * @return void
  429. */
  430. protected function _debug($printTreesToo = false) {
  431. $this->Acl->Aro->displayField = 'alias';
  432. $this->Acl->Aco->displayField = 'alias';
  433. $aros = $this->Acl->Aro->find('list', array('order' => 'lft'));
  434. $acos = $this->Acl->Aco->find('list', array('order' => 'lft'));
  435. $rights = array('*', 'create', 'read', 'update', 'delete');
  436. $permissions['Aros v Acos >'] = $acos;
  437. foreach ($aros as $aro) {
  438. $row = array();
  439. foreach ($acos as $aco) {
  440. $perms = '';
  441. foreach ($rights as $right) {
  442. if ($this->Acl->check($aro, $aco, $right)) {
  443. if ($right == '*') {
  444. $perms .= '****';
  445. break;
  446. }
  447. $perms .= $right[0];
  448. } elseif ($right != '*') {
  449. $perms .= ' ';
  450. }
  451. }
  452. $row[] = $perms;
  453. }
  454. $permissions[$aro] = $row;
  455. }
  456. foreach ($permissions as $key => $values) {
  457. array_unshift($values, $key);
  458. $values = array_map(array(&$this, '_pad'), $values);
  459. $permissions[$key] = implode (' ', $values);
  460. }
  461. $permissions = array_map(array(&$this, '_pad'), $permissions);
  462. array_unshift($permissions, 'Current Permissions :');
  463. if ($printTreesToo) {
  464. debug(array('aros' => $this->Acl->Aro->generateTreeList(), 'acos' => $this->Acl->Aco->generateTreeList()));
  465. }
  466. debug(implode("\r\n", $permissions));
  467. }
  468. /**
  469. * pad function
  470. * Used by debug to format strings used in the data dump
  471. *
  472. * @param string $string
  473. * @param integer $len
  474. * @return void
  475. */
  476. protected function _pad($string = '', $len = 14) {
  477. return str_pad($string, $len);
  478. }
  479. }