Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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