Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
16591 anikendra 1
<?php
2
/**
3
 * PhpAclTest 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.1
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('AclComponent', 'Controller/Component');
20
App::uses('PhpAcl', 'Controller/Component/Acl');
21
class_exists('AclComponent');
22
 
23
/**
24
 * Test case for the PhpAcl implementation
25
 *
26
 * @package       Cake.Test.Case.Controller.Component.Acl
27
 */
28
class PhpAclTest extends CakeTestCase {
29
 
30
/**
31
 * Setup
32
 *
33
 * @return void
34
 */
35
	public function setUp() {
36
		parent::setUp();
37
		Configure::write('Acl.classname', 'PhpAcl');
38
		$Collection = new ComponentCollection();
39
		$this->PhpAcl = new PhpAcl();
40
		$this->Acl = new AclComponent($Collection, array(
41
			'adapter' => array(
42
				'config' => CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS . 'acl.php',
43
			),
44
		));
45
	}
46
 
47
/**
48
 * Test role inheritance
49
 *
50
 * @return void
51
 */
52
	public function testRoleInheritance() {
53
		$roles = $this->Acl->Aro->roles('User/peter');
54
		$this->assertEquals(array('Role/accounting'), $roles[0]);
55
		$this->assertEquals(array('User/peter'), $roles[1]);
56
 
57
		$roles = $this->Acl->Aro->roles('hardy');
58
		$this->assertEquals(array('Role/database_manager', 'Role/data_acquirer'), $roles[0]);
59
		$this->assertEquals(array('Role/accounting', 'Role/data_analyst'), $roles[1]);
60
		$this->assertEquals(array('Role/accounting_manager', 'Role/reports'), $roles[2]);
61
		$this->assertEquals(array('User/hardy'), $roles[3]);
62
	}
63
 
64
/**
65
 * Test adding a role
66
 *
67
 * @return void
68
 */
69
	public function testAddRole() {
70
		$this->assertEquals(array(array(PhpAro::DEFAULT_ROLE)), $this->Acl->Aro->roles('foobar'));
71
		$this->Acl->Aro->addRole(array('User/foobar' => 'Role/accounting'));
72
		$this->assertEquals(array(array('Role/accounting'), array('User/foobar')), $this->Acl->Aro->roles('foobar'));
73
	}
74
 
75
/**
76
 * Test resolving ARO
77
 *
78
 * @return void
79
 */
80
	public function testAroResolve() {
81
		$this->Acl->Aro->map = array(
82
			'User' => 'FooModel/nickname',
83
			'Role' => 'FooModel/role',
84
		);
85
 
86
		$this->assertEquals('Role/default', $this->Acl->Aro->resolve('Foo.bar'));
87
		$this->assertEquals('User/hardy', $this->Acl->Aro->resolve('FooModel/hardy'));
88
		$this->assertEquals('User/hardy', $this->Acl->Aro->resolve('hardy'));
89
		$this->assertEquals('User/hardy', $this->Acl->Aro->resolve(array('FooModel' => array('nickname' => 'hardy'))));
90
		$this->assertEquals('Role/admin', $this->Acl->Aro->resolve(array('FooModel' => array('role' => 'admin'))));
91
		$this->assertEquals('Role/admin', $this->Acl->Aro->resolve('Role/admin'));
92
 
93
		$this->assertEquals('Role/admin', $this->Acl->Aro->resolve('admin'));
94
		$this->assertEquals('Role/admin', $this->Acl->Aro->resolve('FooModel/admin'));
95
		$this->assertEquals('Role/accounting', $this->Acl->Aro->resolve('accounting'));
96
 
97
		$this->assertEquals(PhpAro::DEFAULT_ROLE, $this->Acl->Aro->resolve('bla'));
98
		$this->assertEquals(PhpAro::DEFAULT_ROLE, $this->Acl->Aro->resolve(array('FooModel' => array('role' => 'hardy'))));
99
	}
100
 
101
/**
102
 * test correct resolution of defined aliases
103
 *
104
 * @return void
105
 */
106
	public function testAroAliases() {
107
		$this->Acl->Aro->map = array(
108
			'User' => 'User/username',
109
			'Role' => 'User/group_id',
110
		);
111
 
112
		$this->Acl->Aro->aliases = array(
113
			'Role/1' => 'Role/admin',
114
			'Role/24' => 'Role/accounting',
115
		);
116
 
117
		$user = array(
118
			'User' => array(
119
				'username' => 'unknown_user',
120
				'group_id' => '1',
121
			),
122
		);
123
		// group/1
124
		$this->assertEquals('Role/admin', $this->Acl->Aro->resolve($user));
125
		// group/24
126
		$this->assertEquals('Role/accounting', $this->Acl->Aro->resolve('Role/24'));
127
		$this->assertEquals('Role/accounting', $this->Acl->Aro->resolve('24'));
128
 
129
		// check department
130
		$user = array(
131
			'User' => array(
132
				'username' => 'foo',
133
				'group_id' => '25',
134
			),
135
		);
136
 
137
		$this->Acl->Aro->addRole(array('Role/IT' => null));
138
		$this->Acl->Aro->addAlias(array('Role/25' => 'Role/IT'));
139
		$this->Acl->allow('Role/IT', '/rules/debugging/*');
140
 
141
		$this->assertEquals(array(array('Role/IT')), $this->Acl->Aro->roles($user));
142
		$this->assertTrue($this->Acl->check($user, '/rules/debugging/stats/pageload'));
143
		$this->assertTrue($this->Acl->check($user, '/rules/debugging/sql/queries'));
144
		// Role/default is allowed users dashboard, but not Role/IT
145
		$this->assertFalse($this->Acl->check($user, '/controllers/users/dashboard'));
146
 
147
		$this->assertFalse($this->Acl->check($user, '/controllers/invoices/send'));
148
		// wee add an more specific entry for user foo to also inherit from Role/accounting
149
		$this->Acl->Aro->addRole(array('User/foo' => 'Role/IT, Role/accounting'));
150
		$this->assertTrue($this->Acl->check($user, '/controllers/invoices/send'));
151
	}
152
 
153
/**
154
 * test check method
155
 *
156
 * @return void
157
 */
158
	public function testCheck() {
159
		$this->assertTrue($this->Acl->check('jan', '/controllers/users/Dashboard'));
160
		$this->assertTrue($this->Acl->check('some_unknown_role', '/controllers/users/Dashboard'));
161
		$this->assertTrue($this->Acl->check('Role/admin', 'foo/bar'));
162
		$this->assertTrue($this->Acl->check('role/admin', '/foo/bar'));
163
		$this->assertTrue($this->Acl->check('jan', 'foo/bar'));
164
		$this->assertTrue($this->Acl->check('user/jan', 'foo/bar'));
165
		$this->assertTrue($this->Acl->check('Role/admin', 'controllers/bar'));
166
		$this->assertTrue($this->Acl->check(array('User' => array('username' => 'jan')), '/controllers/bar/bll'));
167
		$this->assertTrue($this->Acl->check('Role/database_manager', 'controllers/db/create'));
168
		$this->assertTrue($this->Acl->check('User/db_manager_2', 'controllers/db/create'));
169
		$this->assertFalse($this->Acl->check('db_manager_2', '/controllers/users/Dashboard'));
170
 
171
		// inheritance: hardy -> reports -> data_analyst -> database_manager
172
		$this->assertTrue($this->Acl->check('User/hardy', 'controllers/db/create'));
173
		$this->assertFalse($this->Acl->check('User/jeff', 'controllers/db/create'));
174
 
175
		$this->assertTrue($this->Acl->check('Role/database_manager', 'controllers/db/select'));
176
		$this->assertTrue($this->Acl->check('User/db_manager_2', 'controllers/db/select'));
177
		$this->assertFalse($this->Acl->check('User/jeff', 'controllers/db/select'));
178
 
179
		$this->assertTrue($this->Acl->check('Role/database_manager', 'controllers/db/drop'));
180
		$this->assertTrue($this->Acl->check('User/db_manager_1', 'controllers/db/drop'));
181
		$this->assertFalse($this->Acl->check('db_manager_2', 'controllers/db/drop'));
182
 
183
		$this->assertTrue($this->Acl->check('db_manager_2', 'controllers/invoices/edit'));
184
		$this->assertFalse($this->Acl->check('database_manager', 'controllers/invoices/edit'));
185
		$this->assertFalse($this->Acl->check('db_manager_1', 'controllers/invoices/edit'));
186
 
187
		// Role/manager is allowed /controllers/*/*_manager
188
		$this->assertTrue($this->Acl->check('stan', 'controllers/invoices/manager_edit'));
189
		$this->assertTrue($this->Acl->check('Role/manager', 'controllers/baz/manager_foo'));
190
		$this->assertFalse($this->Acl->check('User/stan', 'custom/foo/manager_edit'));
191
		$this->assertFalse($this->Acl->check('stan', 'bar/baz/manager_foo'));
192
		$this->assertFalse($this->Acl->check('Role/accounting', 'bar/baz/manager_foo'));
193
		$this->assertFalse($this->Acl->check('accounting', 'controllers/baz/manager_foo'));
194
 
195
		$this->assertTrue($this->Acl->check('User/stan', 'controllers/articles/edit'));
196
		$this->assertTrue($this->Acl->check('stan', 'controllers/articles/add'));
197
		$this->assertTrue($this->Acl->check('stan', 'controllers/articles/publish'));
198
		$this->assertFalse($this->Acl->check('User/stan', 'controllers/articles/delete'));
199
		$this->assertFalse($this->Acl->check('accounting', 'controllers/articles/edit'));
200
		$this->assertFalse($this->Acl->check('accounting', 'controllers/articles/add'));
201
		$this->assertFalse($this->Acl->check('role/accounting', 'controllers/articles/publish'));
202
	}
203
 
204
/**
205
 * lhs of defined rules are case insensitive
206
 *
207
 * @return void
208
 */
209
	public function testCheckIsCaseInsensitive() {
210
		$this->assertTrue($this->Acl->check('hardy', 'controllers/forms/new'));
211
		$this->assertTrue($this->Acl->check('Role/data_acquirer', 'controllers/forms/new'));
212
		$this->assertTrue($this->Acl->check('hardy', 'controllers/FORMS/NEW'));
213
		$this->assertTrue($this->Acl->check('Role/data_acquirer', 'controllers/FORMS/NEW'));
214
	}
215
 
216
/**
217
 * allow should work in-memory
218
 *
219
 * @return void
220
 */
221
	public function testAllow() {
222
		$this->assertFalse($this->Acl->check('jeff', 'foo/bar'));
223
 
224
		$this->Acl->allow('jeff', 'foo/bar');
225
 
226
		$this->assertTrue($this->Acl->check('jeff', 'foo/bar'));
227
		$this->assertFalse($this->Acl->check('peter', 'foo/bar'));
228
		$this->assertFalse($this->Acl->check('hardy', 'foo/bar'));
229
 
230
		$this->Acl->allow('Role/accounting', 'foo/bar');
231
 
232
		$this->assertTrue($this->Acl->check('peter', 'foo/bar'));
233
		$this->assertTrue($this->Acl->check('hardy', 'foo/bar'));
234
 
235
		$this->assertFalse($this->Acl->check('Role/reports', 'foo/bar'));
236
	}
237
 
238
/**
239
 * deny should work in-memory
240
 *
241
 * @return void
242
 */
243
	public function testDeny() {
244
		$this->assertTrue($this->Acl->check('stan', 'controllers/baz/manager_foo'));
245
 
246
		$this->Acl->deny('stan', 'controllers/baz/manager_foo');
247
 
248
		$this->assertFalse($this->Acl->check('stan', 'controllers/baz/manager_foo'));
249
		$this->assertTrue($this->Acl->check('Role/manager', 'controllers/baz/manager_foo'));
250
		$this->assertTrue($this->Acl->check('stan', 'controllers/baz/manager_bar'));
251
		$this->assertTrue($this->Acl->check('stan', 'controllers/baz/manager_foooooo'));
252
	}
253
 
254
/**
255
 * test that a deny rule wins over an equally specific allow rule
256
 *
257
 * @return void
258
 */
259
	public function testDenyRuleIsStrongerThanAllowRule() {
260
		$this->assertFalse($this->Acl->check('peter', 'baz/bam'));
261
		$this->Acl->allow('peter', 'baz/bam');
262
		$this->assertTrue($this->Acl->check('peter', 'baz/bam'));
263
		$this->Acl->deny('peter', 'baz/bam');
264
		$this->assertFalse($this->Acl->check('peter', 'baz/bam'));
265
 
266
		$this->assertTrue($this->Acl->check('stan', 'controllers/reports/foo'));
267
		// stan is denied as he's sales and sales is denied /controllers/*/delete
268
		$this->assertFalse($this->Acl->check('stan', 'controllers/reports/delete'));
269
		$this->Acl->allow('stan', 'controllers/reports/delete');
270
		$this->assertFalse($this->Acl->check('Role/sales', 'controllers/reports/delete'));
271
		$this->assertTrue($this->Acl->check('stan', 'controllers/reports/delete'));
272
		$this->Acl->deny('stan', 'controllers/reports/delete');
273
		$this->assertFalse($this->Acl->check('stan', 'controllers/reports/delete'));
274
 
275
		// there is already an equally specific deny rule that will win
276
		$this->Acl->allow('stan', 'controllers/reports/delete');
277
		$this->assertFalse($this->Acl->check('stan', 'controllers/reports/delete'));
278
	}
279
 
280
/**
281
 * test that an invalid configuration throws exception
282
 *
283
 * @return void
284
 */
285
	public function testInvalidConfigWithAroMissing() {
286
		$this->setExpectedException(
287
			'AclException',
288
			'"roles" section not found in configuration'
289
		);
290
		$config = array('aco' => array('allow' => array('foo' => '')));
291
		$this->PhpAcl->build($config);
292
	}
293
 
294
	public function testInvalidConfigWithAcosMissing() {
295
		$this->setExpectedException(
296
			'AclException',
297
			'Neither "allow" nor "deny" rules were provided in configuration.'
298
		);
299
 
300
		$config = array(
301
			'roles' => array('Role/foo' => null),
302
		);
303
 
304
		$this->PhpAcl->build($config);
305
	}
306
 
307
/**
308
 * test resolving of ACOs
309
 *
310
 * @return void
311
 */
312
	public function testAcoResolve() {
313
		$this->assertEquals(array('foo', 'bar'), $this->Acl->Aco->resolve('foo/bar'));
314
		$this->assertEquals(array('foo', 'bar'), $this->Acl->Aco->resolve('foo/bar'));
315
		$this->assertEquals(array('foo', 'bar', 'baz'), $this->Acl->Aco->resolve('foo/bar/baz'));
316
		$this->assertEquals(array('foo', '*-bar', '?-baz'), $this->Acl->Aco->resolve('foo/*-bar/?-baz'));
317
 
318
		$this->assertEquals(array('foo', 'bar', '[a-f0-9]{24}', '*_bla', 'bla'), $this->Acl->Aco->resolve('foo/bar/[a-f0-9]{24}/*_bla/bla'));
319
 
320
		// multiple slashes will be squashed to a single, trimmed and then exploded
321
		$this->assertEquals(array('foo', 'bar'), $this->Acl->Aco->resolve('foo//bar'));
322
		$this->assertEquals(array('foo', 'bar'), $this->Acl->Aco->resolve('//foo///bar/'));
323
		$this->assertEquals(array('foo', 'bar'), $this->Acl->Aco->resolve('/foo//bar//'));
324
		$this->assertEquals(array('foo', 'bar'), $this->Acl->Aco->resolve('/foo // bar'));
325
		$this->assertEquals(array(), $this->Acl->Aco->resolve('/////'));
326
	}
327
 
328
/**
329
 * test that declaring cyclic dependencies should give an error when building the tree
330
 *
331
 * @return void
332
 */
333
	public function testAroDeclarationContainsCycles() {
334
		$config = array(
335
			'roles' => array(
336
				'Role/a' => null,
337
				'Role/b' => 'User/b',
338
				'User/a' => 'Role/a, Role/b',
339
				'User/b' => 'User/a',
340
 
341
			),
342
			'rules' => array(
343
				'allow' => array(
344
					'*' => 'Role/a',
345
				),
346
			),
347
		);
348
 
349
		$this->expectError('PHPUnit_Framework_Error', 'cycle detected' /* ... */);
350
		$this->PhpAcl->build($config);
351
	}
352
 
353
/**
354
 * test that with policy allow, only denies count
355
 *
356
 * @return void
357
 */
358
	public function testPolicy() {
359
		// allow by default
360
		$this->Acl->settings['adapter']['policy'] = PhpAcl::ALLOW;
361
		$this->Acl->adapter($this->PhpAcl);
362
 
363
		$this->assertTrue($this->Acl->check('Role/sales', 'foo'));
364
		$this->assertTrue($this->Acl->check('Role/sales', 'controllers/bla/create'));
365
		$this->assertTrue($this->Acl->check('Role/default', 'foo'));
366
		// undefined user, undefined aco
367
		$this->assertTrue($this->Acl->check('foobar', 'foo/bar'));
368
 
369
		// deny rule: Role.sales -> controllers.*.delete
370
		$this->assertFalse($this->Acl->check('Role/sales', 'controllers/bar/delete'));
371
		$this->assertFalse($this->Acl->check('Role/sales', 'controllers/bar', 'delete'));
372
	}
373
 
374
}