TinyAuthorizeTest.php 9.6 KB

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