TinyAuthorizeTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?php
  2. /**
  3. * TinyAuthorizeTest file
  4. *
  5. */
  6. App::uses('TinyAuthorize', 'Tools.Controller/Component/Auth');
  7. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  8. App::uses('Controller', 'Controller');
  9. App::uses('ComponentCollection', 'Controller');
  10. App::uses('CakeRequest', 'Network');
  11. /**
  12. * Test case for DirectAuthentication
  13. *
  14. */
  15. class TinyAuthorizeTest extends MyCakeTestCase {
  16. public $fixtures = array('core.user', 'core.auth_user', 'plugin.tools.role');
  17. public $Collection;
  18. public $request;
  19. /**
  20. * Setup
  21. *
  22. * @return void
  23. */
  24. public function setUp() {
  25. parent::setUp();
  26. $this->Collection = new ComponentCollection();
  27. $this->request = new CakeRequest(null, false);
  28. $aclData = <<<INI
  29. [Users]
  30. ; add = public
  31. edit = user
  32. admin_index = admin
  33. [Comments]
  34. ; index is public
  35. add,edit,delete = user
  36. * = admin
  37. [Tags]
  38. add = *
  39. very_long_action_name_action = user
  40. public_action = public
  41. INI;
  42. file_put_contents(TMP . 'acl.ini', $aclData);
  43. $this->assertTrue(file_exists(TMP . 'acl.ini'));
  44. Configure::write('Role', array('user' => 1, 'moderator' => 2, 'admin' => 3, 'public' => -1));
  45. }
  46. public function tearDown() {
  47. unlink(TMP . 'acl.ini');
  48. parent::tearDown();
  49. }
  50. /**
  51. * Test applying settings in the constructor
  52. *
  53. * @return void
  54. */
  55. public function testConstructor() {
  56. $object = new TestTinyAuthorize($this->Collection, array(
  57. 'aclModel' => 'AuthRole',
  58. 'aclKey' => 'auth_role_id',
  59. 'autoClearCache' => true,
  60. ));
  61. $this->assertEquals('AuthRole', $object->settings['aclModel']);
  62. $this->assertEquals('auth_role_id', $object->settings['aclKey']);
  63. }
  64. /**
  65. * @return void
  66. */
  67. public function testGetAcl() {
  68. $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
  69. $res = $object->getAcl();
  70. $expected = array(
  71. 'users' => array(
  72. 'edit' => array(1),
  73. 'admin_index' => array(3)
  74. ),
  75. 'comments' => array(
  76. 'add' => array(1),
  77. 'edit' => array(1),
  78. 'delete' => array(1),
  79. '*' => array(3),
  80. ),
  81. 'tags' => array(
  82. 'add' => array(1, 2, 3, -1),
  83. 'very_long_action_name_action' => array(1),
  84. 'public_action' => array(-1)
  85. ),
  86. );
  87. $this->debug($res);
  88. $this->assertEquals($expected, $res);
  89. }
  90. /**
  91. * @return void
  92. */
  93. public function testBasicUserMethodDisallowed() {
  94. $this->request->params['controller'] = 'users';
  95. $this->request->params['action'] = 'edit';
  96. $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
  97. $this->assertEquals('Role', $object->settings['aclModel']);
  98. $this->assertEquals('role_id', $object->settings['aclKey']);
  99. $user = array(
  100. 'role_id' => 4,
  101. );
  102. $res = $object->authorize($user, $this->request);
  103. $this->assertFalse($res);
  104. $user = array(
  105. 'role_id' => 3,
  106. );
  107. $res = $object->authorize($user, $this->request);
  108. $this->assertFalse($res);
  109. }
  110. /**
  111. * @return void
  112. */
  113. public function testBasicUserMethodAllowed() {
  114. $this->request->params['controller'] = 'users';
  115. $this->request->params['action'] = 'edit';
  116. $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
  117. // single role_id field in users table
  118. $user = array(
  119. 'role_id' => 1,
  120. );
  121. $res = $object->authorize($user, $this->request);
  122. $this->assertTrue($res);
  123. $this->request->params['action'] = 'admin_index';
  124. $user = array(
  125. 'role_id' => 3,
  126. );
  127. $res = $object->authorize($user, $this->request);
  128. $this->assertTrue($res);
  129. }
  130. /**
  131. * @return void
  132. */
  133. public function testBasicUserMethodAllowedWithLongActionNames() {
  134. $this->request->params['controller'] = 'tags';
  135. $this->request->params['action'] = 'very_long_action_name_action';
  136. $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
  137. // single role_id field in users table
  138. $user = array(
  139. 'role_id' => 1,
  140. );
  141. $res = $object->authorize($user, $this->request);
  142. $this->assertTrue($res);
  143. $user = array(
  144. 'role_id' => 3,
  145. );
  146. $res = $object->authorize($user, $this->request);
  147. $this->assertFalse($res);
  148. }
  149. /**
  150. * @return void
  151. */
  152. public function testBasicUserMethodAllowedMultiRole() {
  153. $this->request->params['controller'] = 'users';
  154. $this->request->params['action'] = 'admin_index';
  155. $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
  156. // flat list of roles
  157. $user = array(
  158. 'Role' => array(1, 3),
  159. );
  160. $res = $object->authorize($user, $this->request);
  161. $this->assertTrue($res);
  162. // verbose role defition using the new 2.x contain param for Auth
  163. $user = array(
  164. 'Role' => array(array('id' => 1, 'RoleUser' => array()), array('id' => 3, 'RoleUser' => array())),
  165. );
  166. $res = $object->authorize($user, $this->request);
  167. $this->assertTrue($res);
  168. }
  169. /**
  170. * @return void
  171. */
  172. public function testBasicUserMethodAllowedWildcard() {
  173. $this->request->params['controller'] = 'tags';
  174. $this->request->params['action'] = 'public_action';
  175. $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
  176. $user = array(
  177. 'role_id' => 6,
  178. );
  179. $res = $object->authorize($user, $this->request);
  180. $this->assertTrue($res);
  181. }
  182. /**
  183. * @return void
  184. */
  185. public function testUserMethodsAllowed() {
  186. $this->request->params['controller'] = 'users';
  187. $this->request->params['action'] = 'some_action';
  188. $object = new TestTinyAuthorize($this->Collection, array('allowUser' => true, 'autoClearCache' => true));
  189. $user = array(
  190. 'role_id' => 1,
  191. );
  192. $res = $object->authorize($user, $this->request);
  193. $this->assertTrue($res);
  194. $this->request->params['controller'] = 'users';
  195. $this->request->params['action'] = 'admin_index';
  196. $object = new TestTinyAuthorize($this->Collection, array('allowUser' => true, 'autoClearCache' => true));
  197. $user = array(
  198. 'role_id' => 1,
  199. );
  200. $res = $object->authorize($user, $this->request);
  201. $this->assertFalse($res);
  202. $user = array(
  203. 'role_id' => 3,
  204. );
  205. $res = $object->authorize($user, $this->request);
  206. $this->assertTrue($res);
  207. }
  208. /**
  209. * @return void
  210. */
  211. public function testAdminMethodsAllowed() {
  212. $this->request->params['controller'] = 'users';
  213. $this->request->params['action'] = 'some_action';
  214. $config = array('allowAdmin' => true, 'adminRole' => 3, 'autoClearCache' => true);
  215. $object = new TestTinyAuthorize($this->Collection, $config);
  216. $user = array(
  217. 'role_id' => 1,
  218. );
  219. $res = $object->authorize($user, $this->request);
  220. $this->assertFalse($res);
  221. $this->request->params['controller'] = 'users';
  222. $this->request->params['action'] = 'admin_index';
  223. $object = new TestTinyAuthorize($this->Collection, $config);
  224. $user = array(
  225. 'role_id' => 1,
  226. );
  227. $res = $object->authorize($user, $this->request);
  228. $this->assertFalse($res);
  229. $user = array(
  230. 'role_id' => 3,
  231. );
  232. $res = $object->authorize($user, $this->request);
  233. $this->assertTrue($res);
  234. }
  235. /**
  236. * Should only be used in combination with Auth->allow() to mark those as public in the acl.ini, as well.
  237. * Not necessary and certainly not recommended as acl.ini only.
  238. *
  239. * @return void
  240. */
  241. public function testBasicUserMethodAllowedPublically() {
  242. $this->request->params['controller'] = 'tags';
  243. $this->request->params['action'] = 'add';
  244. $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
  245. $user = array(
  246. 'role_id' => 2,
  247. );
  248. $res = $object->authorize($user, $this->request);
  249. $this->assertTrue($res);
  250. $this->request->params['controller'] = 'comments';
  251. $this->request->params['action'] = 'foo';
  252. $user = array(
  253. 'role_id' => 3,
  254. );
  255. $res = $object->authorize($user, $this->request);
  256. $this->assertTrue($res);
  257. }
  258. /**
  259. * TinyAuthorizeTest::testWithRoleTable()
  260. *
  261. * @return void
  262. */
  263. public function testWithRoleTable() {
  264. $User = ClassRegistry::init('User');
  265. $User->bindModel(array('belongsTo' => array('Role')), false);
  266. // We want the session to be used.
  267. Configure::delete('Role');
  268. $this->request->params['controller'] = 'users';
  269. $this->request->params['action'] = 'edit';
  270. $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
  271. // User role is 4 here, though. Also contains left joined Role date here just to check that it works, too.
  272. $user = array(
  273. 'Role' => array(
  274. 'id' => '4',
  275. 'alias' => 'user',
  276. ),
  277. 'role_id' => 4,
  278. );
  279. $res = $object->authorize($user, $this->request);
  280. $this->assertTrue($res);
  281. Configure::delete('Role');
  282. $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
  283. $user = array(
  284. 'role_id' => 6,
  285. );
  286. $res = $object->authorize($user, $this->request);
  287. $this->assertFalse($res);
  288. $this->assertTrue((bool)(Configure::read('Role')));
  289. // Multi-role test - failure
  290. Configure::delete('Role');
  291. $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
  292. $user = array(
  293. 'Role' => array(
  294. array('id' => 7, 'alias' => 'user'),
  295. array('id' => 8, 'alias' => 'partner'),
  296. )
  297. );
  298. $res = $object->authorize($user, $this->request);
  299. $this->assertFalse($res);
  300. $this->assertTrue((bool)(Configure::read('Role')));
  301. Configure::delete('Role');
  302. $object = new TestTinyAuthorize($this->Collection, array('autoClearCache' => true));
  303. // Multi-role test
  304. $user = array(
  305. 'Role' => array(
  306. array('id' => 4, 'alias' => 'user'),
  307. array('id' => 6, 'alias' => 'partner'),
  308. )
  309. );
  310. $res = $object->authorize($user, $this->request);
  311. $this->assertTrue($res);
  312. }
  313. }
  314. class TestTinyAuthorize extends TinyAuthorize {
  315. public function matchArray() {
  316. return $this->_matchArray;
  317. }
  318. public function getAcl() {
  319. return $this->_getAcl();
  320. }
  321. protected function _getAcl($path = TMP) {
  322. return parent::_getAcl($path);
  323. }
  324. }