TinyAuthorizeTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 = ['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', ['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, [
  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, ['autoClearCache' => true]);
  69. $res = $object->getAcl();
  70. $expected = [
  71. 'users' => [
  72. 'edit' => [1],
  73. 'admin_index' => [3]
  74. ],
  75. 'comments' => [
  76. 'add' => [1],
  77. 'edit' => [1],
  78. 'delete' => [1],
  79. '*' => [3],
  80. ],
  81. 'tags' => [
  82. 'add' => [1, 2, 3, -1],
  83. 'very_long_action_name_action' => [1],
  84. 'public_action' => [-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, ['autoClearCache' => true]);
  97. $this->assertEquals('Role', $object->settings['aclModel']);
  98. $this->assertEquals('role_id', $object->settings['aclKey']);
  99. $user = [
  100. 'role_id' => 4,
  101. ];
  102. $res = $object->authorize($user, $this->request);
  103. $this->assertFalse($res);
  104. $user = [
  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, ['autoClearCache' => true]);
  117. // single role_id field in users table
  118. $user = [
  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 = [
  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, ['autoClearCache' => true]);
  137. // single role_id field in users table
  138. $user = [
  139. 'role_id' => 1,
  140. ];
  141. $res = $object->authorize($user, $this->request);
  142. $this->assertTrue($res);
  143. $user = [
  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, ['autoClearCache' => true]);
  156. // flat list of roles
  157. $user = [
  158. 'Role' => [1, 3],
  159. ];
  160. $res = $object->authorize($user, $this->request);
  161. $this->assertTrue($res);
  162. // verbose role definition using the new 2.x contain param for Auth
  163. $user = [
  164. 'Role' => [
  165. ['id' => 1, 'RoleUser' => []],
  166. ['id' => 3, 'RoleUser' => []]
  167. ],
  168. ];
  169. $res = $object->authorize($user, $this->request);
  170. $this->assertTrue($res);
  171. }
  172. /**
  173. * @return void
  174. */
  175. public function testBasicUserMethodAllowedWildcard() {
  176. $this->request->params['controller'] = 'tags';
  177. $this->request->params['action'] = 'public_action';
  178. $object = new TestTinyAuthorize($this->Collection, ['autoClearCache' => true]);
  179. $user = [
  180. 'role_id' => 6,
  181. ];
  182. $res = $object->authorize($user, $this->request);
  183. $this->assertTrue($res);
  184. }
  185. /**
  186. * @return void
  187. */
  188. public function testUserMethodsAllowed() {
  189. $this->request->params['controller'] = 'users';
  190. $this->request->params['action'] = 'some_action';
  191. $object = new TestTinyAuthorize($this->Collection, ['allowUser' => true, 'autoClearCache' => true]);
  192. $user = [
  193. 'role_id' => 1,
  194. ];
  195. $res = $object->authorize($user, $this->request);
  196. $this->assertTrue($res);
  197. $this->request->params['controller'] = 'users';
  198. $this->request->params['action'] = 'admin_index';
  199. $object = new TestTinyAuthorize($this->Collection, ['allowUser' => true, 'autoClearCache' => true]);
  200. $user = [
  201. 'role_id' => 1,
  202. ];
  203. $res = $object->authorize($user, $this->request);
  204. $this->assertFalse($res);
  205. $user = [
  206. 'role_id' => 3,
  207. ];
  208. $res = $object->authorize($user, $this->request);
  209. $this->assertTrue($res);
  210. }
  211. /**
  212. * @return void
  213. */
  214. public function testAdminMethodsAllowed() {
  215. $this->request->params['controller'] = 'users';
  216. $this->request->params['action'] = 'some_action';
  217. $config = ['allowAdmin' => true, 'adminRole' => 3, 'autoClearCache' => true];
  218. $object = new TestTinyAuthorize($this->Collection, $config);
  219. $user = [
  220. 'role_id' => 1,
  221. ];
  222. $res = $object->authorize($user, $this->request);
  223. $this->assertFalse($res);
  224. $this->request->params['controller'] = 'users';
  225. $this->request->params['action'] = 'admin_index';
  226. $object = new TestTinyAuthorize($this->Collection, $config);
  227. $user = [
  228. 'role_id' => 1,
  229. ];
  230. $res = $object->authorize($user, $this->request);
  231. $this->assertFalse($res);
  232. $user = [
  233. 'role_id' => 3,
  234. ];
  235. $res = $object->authorize($user, $this->request);
  236. $this->assertTrue($res);
  237. }
  238. /**
  239. * Should only be used in combination with Auth->allow() to mark those as public in the acl.ini, as well.
  240. * Not necessary and certainly not recommended as acl.ini only.
  241. *
  242. * @return void
  243. */
  244. public function testBasicUserMethodAllowedPublically() {
  245. $this->request->params['controller'] = 'tags';
  246. $this->request->params['action'] = 'add';
  247. $object = new TestTinyAuthorize($this->Collection, ['autoClearCache' => true]);
  248. $user = [
  249. 'role_id' => 2,
  250. ];
  251. $res = $object->authorize($user, $this->request);
  252. $this->assertTrue($res);
  253. $this->request->params['controller'] = 'comments';
  254. $this->request->params['action'] = 'foo';
  255. $user = [
  256. 'role_id' => 3,
  257. ];
  258. $res = $object->authorize($user, $this->request);
  259. $this->assertTrue($res);
  260. }
  261. /**
  262. * TinyAuthorizeTest::testWithRoleTable()
  263. *
  264. * @return void
  265. */
  266. public function testWithRoleTable() {
  267. $User = ClassRegistry::init('User');
  268. $User->bindModel(['belongsTo' => ['Role']], false);
  269. // We want the session to be used.
  270. Configure::delete('Role');
  271. $this->request->params['controller'] = 'users';
  272. $this->request->params['action'] = 'edit';
  273. $object = new TestTinyAuthorize($this->Collection, ['autoClearCache' => true]);
  274. // User role is 4 here, though. Also contains left joined Role date here just to check that it works, too.
  275. $user = [
  276. 'Role' => [
  277. 'id' => '4',
  278. 'alias' => 'user',
  279. ],
  280. 'role_id' => 4,
  281. ];
  282. $res = $object->authorize($user, $this->request);
  283. $this->assertTrue($res);
  284. Configure::delete('Role');
  285. $object = new TestTinyAuthorize($this->Collection, ['autoClearCache' => true]);
  286. $user = [
  287. 'role_id' => 6,
  288. ];
  289. $res = $object->authorize($user, $this->request);
  290. $this->assertFalse($res);
  291. $this->assertTrue((bool)(Configure::read('Role')));
  292. // Multi-role test - failure
  293. Configure::delete('Role');
  294. $object = new TestTinyAuthorize($this->Collection, ['autoClearCache' => true]);
  295. $user = [
  296. 'Role' => [
  297. ['id' => 7, 'alias' => 'user'],
  298. ['id' => 8, 'alias' => 'partner'],
  299. ]
  300. ];
  301. $res = $object->authorize($user, $this->request);
  302. $this->assertFalse($res);
  303. $this->assertTrue((bool)(Configure::read('Role')));
  304. Configure::delete('Role');
  305. $object = new TestTinyAuthorize($this->Collection, ['autoClearCache' => true]);
  306. // Multi-role test
  307. $user = [
  308. 'Role' => [
  309. ['id' => 4, 'alias' => 'user'],
  310. ['id' => 6, 'alias' => 'partner'],
  311. ]
  312. ];
  313. $res = $object->authorize($user, $this->request);
  314. $this->assertTrue($res);
  315. }
  316. /**
  317. * Tests superadmin role, allowed to all actions
  318. *
  319. * @return void
  320. */
  321. public function testSuperadminRole() {
  322. $object = new TestTinyAuthorize($this->Collection, [
  323. 'autoClearCache' => true,
  324. 'superadminRole' => 9
  325. ]);
  326. $res = $object->getAcl();
  327. $user = [
  328. 'role_id' => 9,
  329. ];
  330. foreach ($object->getAcl() as $controller => $actions) {
  331. foreach ($actions as $action => $allowed) {
  332. $this->request->params['controller'] = $controller;
  333. $this->request->params['action'] = $action;
  334. $res = $object->authorize($user, $this->request);
  335. $this->assertTrue($res);
  336. }
  337. }
  338. }
  339. }
  340. class TestTinyAuthorize extends TinyAuthorize {
  341. public function matchArray() {
  342. return $this->_matchArray;
  343. }
  344. public function getAcl() {
  345. return $this->_getAcl();
  346. }
  347. protected function _getAcl($path = TMP) {
  348. return parent::_getAcl($path);
  349. }
  350. }