[Share 2.0] When deleting a group share delete children

For group shares we can have children. Those are custom shares when a
user has moved or deleted a group share. Those also have to be deleted
if the group share is removed.
This commit is contained in:
Roeland Jago Douma 2016-01-28 20:35:46 +01:00
parent 8b3d7d09d5
commit 1ff4ec1cd3
2 changed files with 67 additions and 65 deletions

View File

@ -288,21 +288,21 @@ class DefaultShareProvider implements IShareProvider {
* Delete a share
*
* @param \OCP\Share\IShare $share
* @throws BackendError
*/
public function delete(\OCP\Share\IShare $share) {
// Fetch share to make sure it exists
$share = $this->getShareById($share->getId());
$qb = $this->dbConn->getQueryBuilder();
$qb->delete('share')
->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())));
try {
$qb->execute();
} catch (\Exception $e) {
throw new BackendError();
/*
* If the share is a group share delete all possible
* user defined groups shares.
*/
if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
$qb->orWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())));
}
$qb->execute();
}
/**

View File

@ -330,19 +330,14 @@ class DefaultShareProviderTest extends \Test\TestCase {
$share->method('getId')->willReturn($id);
$provider = $this->getMockBuilder('OC\Share20\DefaultShareProvider')
->setConstructorArgs([
$this->dbConn,
$this->userManager,
$this->groupManager,
$this->rootFolder,
]
)
->setMethods(['getShareById'])
->getMock();
$provider
->expects($this->once())
->method('getShareById')
->willReturn($share);
->setConstructorArgs([
$this->dbConn,
$this->userManager,
$this->groupManager,
$this->rootFolder,
])
->setMethods(['getShareById'])
->getMock();
$provider->delete($share);
@ -357,53 +352,60 @@ class DefaultShareProviderTest extends \Test\TestCase {
$this->assertEmpty($result);
}
/**
* @expectedException \OC\Share20\Exception\BackendError
*/
public function testDeleteFails() {
public function testDeleteGroupShareWithUserGroupShares() {
$qb = $this->dbConn->getQueryBuilder();
$qb->insert('share')
->values([
'share_type' => $qb->expr()->literal(\OCP\Share::SHARE_TYPE_GROUP),
'share_with' => $qb->expr()->literal('sharedWith'),
'uid_owner' => $qb->expr()->literal('sharedBy'),
'item_type' => $qb->expr()->literal('file'),
'file_source' => $qb->expr()->literal(42),
'file_target' => $qb->expr()->literal('myTarget'),
'permissions' => $qb->expr()->literal(13),
]);
$this->assertEquals(1, $qb->execute());
$id = $qb->getLastInsertId();
$qb = $this->dbConn->getQueryBuilder();
$qb->insert('share')
->values([
'share_type' => $qb->expr()->literal(2),
'share_with' => $qb->expr()->literal('sharedWithUser'),
'uid_owner' => $qb->expr()->literal('sharedBy'),
'item_type' => $qb->expr()->literal('file'),
'file_source' => $qb->expr()->literal(42),
'file_target' => $qb->expr()->literal('myTarget'),
'permissions' => $qb->expr()->literal(13),
'parent' => $qb->expr()->literal($id),
]);
$this->assertEquals(1, $qb->execute());
$share = $this->getMock('OCP\Share\IShare');
$share
->method('getId')
->willReturn(42);
$expr = $this->getMock('OCP\DB\QueryBuilder\IExpressionBuilder');
$qb = $this->getMock('OCP\DB\QueryBuilder\IQueryBuilder');
$qb->expects($this->once())
->method('delete')
->will($this->returnSelf());
$qb->expects($this->once())
->method('expr')
->willReturn($expr);
$qb->expects($this->once())
->method('where')
->will($this->returnSelf());
$qb->expects($this->once())
->method('execute')
->will($this->throwException(new \Exception));
$db = $this->getMock('OCP\IDBConnection');
$db->expects($this->once())
->method('getQueryBuilder')
->with()
->willReturn($qb);
$share->method('getId')->willReturn($id);
$share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_GROUP);
$provider = $this->getMockBuilder('OC\Share20\DefaultShareProvider')
->setConstructorArgs([
$db,
$this->userManager,
$this->groupManager,
$this->rootFolder,
]
)
->setMethods(['getShareById'])
->getMock();
$provider
->expects($this->once())
->method('getShareById')
->with(42)
->willReturn($share);
->setConstructorArgs([
$this->dbConn,
$this->userManager,
$this->groupManager,
$this->rootFolder,
])
->setMethods(['getShareById'])
->getMock();
$provider->delete($share);
$qb = $this->dbConn->getQueryBuilder();
$qb->select('*')
->from('share');
$cursor = $qb->execute();
$result = $cursor->fetchAll();
$cursor->closeCursor();
$this->assertEmpty($result);
}
public function testGetChildren() {