TinyAuthorizeTest.php 9.7 KB

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