Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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