Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit 96070bf

Browse files
committed
ENH: refs #0436. Add a test for policies on folder/item creation
1 parent e9fb5a0 commit 96070bf

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

core/controllers/FolderController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public function removeitemAction()
267267
/** create a folder (dialog,ajax only)*/
268268
public function createfolderAction()
269269
{
270-
$this->_helper->layout->disableLayout();
270+
$this->disableLayout();
271271
$folder_id = $this->_getParam('folderId');
272272
$folder = $this->Folder->load($folder_id);
273273
$header = "";

core/tests/controllers/ShareControllerTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,71 @@ public function testApplyPoliciesRecursive()
234234
$this->assertTrue($this->Folder->policyCheck($subfolder, $user2, MIDAS_POLICY_READ));
235235
$this->assertTrue($this->Item->policyCheck($item, $user2, MIDAS_POLICY_READ));
236236
}
237+
238+
/** Test that creating a folder or item gives admin access to the creator */
239+
public function testPoliciesOnCreation()
240+
{
241+
$usersFile = $this->loadData('User', 'policies');
242+
$user1 = $this->User->load($usersFile[0]->getKey());
243+
$user2 = $this->User->load($usersFile[1]->getKey());
244+
245+
// user 2 should not have read privileges yet
246+
$folder = $this->Folder->load(1007);
247+
$this->assertFalse($this->Folder->policyCheck($folder, $user2, MIDAS_POLICY_READ));
248+
249+
// now create a new privilege entry for user2
250+
$this->resetAll();
251+
$this->request->setMethod('POST');
252+
$url = '/share/dialog?type=folder&element=1007&createPolicy&newPolicyType=user';
253+
$url .= '&newPolicyId='.$user2->getKey();
254+
$this->dispatchUrI($url, $user1);
255+
256+
// user 2 should now have read privileges, but not any higher
257+
$folder = $this->Folder->load(1007);
258+
$this->assertTrue($this->Folder->policyCheck($folder, $user2, MIDAS_POLICY_READ));
259+
$this->assertFalse($this->Folder->policyCheck($folder, $user2, MIDAS_POLICY_WRITE));
260+
261+
// now change permissions for user 2 to add edit privileges
262+
$this->resetAll();
263+
$this->request->setMethod('POST');
264+
$url = '/share/dialog?type=folder&element=1007&changePolicy&changeType=user';
265+
$url .= '&changeId='.$user2->getKey().'&changeVal='.MIDAS_POLICY_WRITE;
266+
$this->dispatchUrI($url, $user1);
267+
268+
// user 2 should now have write privileges, but not any higher
269+
$folder = $this->Folder->load(1007);
270+
$this->assertTrue($this->Folder->policyCheck($folder, $user2, MIDAS_POLICY_WRITE));
271+
$this->assertFalse($this->Folder->policyCheck($folder, $user2, MIDAS_POLICY_ADMIN));
272+
273+
// Create a folder inside the parent where we have write access
274+
$this->resetAll();
275+
$this->request->setMethod('POST');
276+
$this->dispatchUrI('/folder/createfolder?folderId=1007&createFolder&name=HelloWorld', $user2);
277+
$resp = json_decode($this->getBody());
278+
$this->assertTrue($resp[0] != false);
279+
$this->assertNotEmpty($resp[2]);
280+
$this->assertNotEmpty($resp[3]);
281+
$this->assertEquals($resp[2]->folder_id, '1007');
282+
$this->assertEquals($resp[3]->parent_id, '1007');
283+
284+
// The user should have admin access to the child, but not the parent
285+
$parentFolder = $this->Folder->load($resp[2]->folder_id);
286+
$childFolder = $this->Folder->load($resp[3]->folder_id);
287+
$this->assertTrue($this->Folder->policyCheck($childFolder, $user2, MIDAS_POLICY_ADMIN));
288+
$this->assertFalse($this->Folder->policyCheck($parentFolder, $user2, MIDAS_POLICY_ADMIN));
289+
290+
// Create an item inside the parent where we have write access
291+
$this->resetAll();
292+
$this->params = array();
293+
$this->params['parent'] = '1007';
294+
$this->params['license'] = 0;
295+
$this->params['testpath'] = BASE_PATH.'/tests/testfiles/search.png'; //testing mode param
296+
$this->dispatchUrI('/upload/saveuploaded', $user2);
297+
$search = $this->Item->getItemsFromSearch('search.png', $user2);
298+
$this->assertNotEmpty($search, 'Unable to find uploaded item');
299+
300+
// The user should have admin access to the item
301+
$item = $this->Item->load($search[0]->item_id);
302+
$this->assertTrue($this->Item->policyCheck($item, $user2, MIDAS_POLICY_ADMIN));
303+
}
237304
}

0 commit comments

Comments
 (0)