Browse Source

Take a list of share IDs instead of the user and group names

tags/v8.2beta1
Joas Schilling 8 years ago
parent
commit
0227cfff08

+ 55
- 13
apps/files_sharing/api/sharees.php View File

@@ -20,8 +20,10 @@
*/
namespace OCA\Files_Sharing\API;

use Doctrine\DBAL\Connection;
use OC\Share\SearchResultSorter;
use OCP\Contacts\IManager;
use OCP\IDBConnection;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\ILogger;
@@ -54,6 +56,9 @@ class Sharees {
/** @var ILogger */
private $logger;

/** @var IDBConnection */
private $connection;

/**
* @param IGroupManager $groupManager
* @param IUserManager $userManager
@@ -62,6 +67,7 @@ class Sharees {
* @param IUserSession $userSession
* @param IURLGenerator $urlGenerator
* @param ILogger $logger
* @param IDBConnection $connection
*/
public function __construct(IGroupManager $groupManager,
IUserManager $userManager,
@@ -69,7 +75,8 @@ class Sharees {
IConfig $config,
IUserSession $userSession,
IURLGenerator $urlGenerator,
ILogger $logger) {
ILogger $logger,
IDBConnection $connection) {
$this->groupManager = $groupManager;
$this->userManager = $userManager;
$this->contactsManager = $contactsManager;
@@ -77,6 +84,7 @@ class Sharees {
$this->userSession = $userSession;
$this->urlGenerator = $urlGenerator;
$this->logger = $logger;
$this->connection = $connection;
}

/**
@@ -194,7 +202,7 @@ class Sharees {
public function search() {
$search = isset($_GET['search']) ? (string) $_GET['search'] : '';
$itemType = isset($_GET['itemType']) ? (string) $_GET['itemType'] : null;
$existingShares = isset($_GET['existingShares']) ? (array) $_GET['existingShares'] : [];
$shareIds = isset($_GET['existingShares']) ? (array) $_GET['existingShares'] : [];
$page = !empty($_GET['page']) ? max(1, (int) $_GET['page']) : 1;
$perPage = !empty($_GET['limit']) ? max(1, (int) $_GET['limit']) : 200;

@@ -219,7 +227,7 @@ class Sharees {

$shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';

return $this->searchSharees($search, $itemType, $existingShares, $shareTypes, $page, $perPage, $shareWithGroupOnly);
return $this->searchSharees($search, $itemType, $shareIds, $shareTypes, $page, $perPage, $shareWithGroupOnly);
}

/**
@@ -242,25 +250,27 @@ class Sharees {
*
* @param string $search
* @param string $itemType
* @param array $existingShares
* @param array $shareIds
* @param array $shareTypes
* @param int $page
* @param int $perPage
* @param bool $shareWithGroupOnly
* @return \OC_OCS_Result
*/
protected function searchSharees($search, $itemType, array $existingShares, array $shareTypes, $page, $perPage, $shareWithGroupOnly) {
protected function searchSharees($search, $itemType, array $shareIds, array $shareTypes, $page, $perPage, $shareWithGroupOnly) {

$sharedUsers = $sharedGroups = [];
if (!empty($existingShares)) {
if (!empty($existingShares[Share::SHARE_TYPE_USER]) &&
is_array($existingShares[Share::SHARE_TYPE_USER])) {
$sharedUsers = $existingShares[Share::SHARE_TYPE_USER];
$existingSharees = $this->getShareesForShareIds($shareIds);

if (!empty($existingSharees)) {
if (!empty($existingSharees[Share::SHARE_TYPE_USER]) &&
is_array($existingSharees[Share::SHARE_TYPE_USER])) {
$sharedUsers = $existingSharees[Share::SHARE_TYPE_USER];
}

if (!empty($existingShares[Share::SHARE_TYPE_GROUP]) &&
is_array($existingShares[Share::SHARE_TYPE_GROUP])) {
$sharedGroups = $existingShares[Share::SHARE_TYPE_GROUP];
if (!empty($existingSharees[Share::SHARE_TYPE_GROUP]) &&
is_array($existingSharees[Share::SHARE_TYPE_GROUP])) {
$sharedGroups = $existingSharees[Share::SHARE_TYPE_GROUP];
}
}

@@ -305,7 +315,7 @@ class Sharees {
$links = $this->getPaginationLinks($page, $total, [
'search' => $search,
'itemType' => $itemType,
'existingShares' => $existingShares,
'existingShares' => $shareIds,
'shareType' => $shareTypes,
'limit' => $perPage,
]);
@@ -332,6 +342,38 @@ class Sharees {
return $sharees;
}

/**
* Get a list of existing share_with's for the given share IDs (if the current user owns them)
*
* @param array $shareIds
* @return array
*/
protected function getShareesForShareIds($shareIds) {
if (empty($shareIds)) {
return [];
}

$queryBuilder = $this->connection->getQueryBuilder();
$exprBuilder = $queryBuilder->expr();

$queryBuilder->select(['share_type', 'share_with'])
->from('share')
->where($exprBuilder->in('id', $queryBuilder->createParameter('shareIds')))
->andWhere($exprBuilder->eq('uid_owner', $queryBuilder->createParameter('user')))
->andWhere($exprBuilder->isNull('parent'))
->setParameter('shareIds', $shareIds, Connection::PARAM_INT_ARRAY)
->setParameter('user', $this->userSession->getUser()->getUID());
$query = $queryBuilder->execute();

$sharees = [];
while ($row = $query->fetch()) {
$sharees[$row['share_type']][] = $row['share_with'];
}
$query->closeCursor();

return $sharees;
}

/**
* Generates a bunch of pagination links for the current page
*

+ 2
- 1
apps/files_sharing/appinfo/routes.php View File

@@ -108,7 +108,8 @@ $sharees = new \OCA\Files_Sharing\API\Sharees(\OC::$server->getGroupManager(),
\OC::$server->getConfig(),
\OC::$server->getUserSession(),
\OC::$server->getURLGenerator(),
\OC::$server->getLogger());
\OC::$server->getLogger(),
\OC::$server->getDatabaseConnection());

API::register('get',
'/apps/files_sharing/api/v1/sharees',

+ 61
- 53
apps/files_sharing/tests/api/sharees.php View File

@@ -66,7 +66,8 @@ class ShareesTest extends TestCase {
$this->getMockBuilder('OCP\IConfig')->disableOriginalConstructor()->getMock(),
$this->session,
$this->getMockBuilder('OCP\IURLGenerator')->disableOriginalConstructor()->getMock(),
$this->getMockBuilder('OCP\ILogger')->disableOriginalConstructor()->getMock()
$this->getMockBuilder('OCP\ILogger')->disableOriginalConstructor()->getMock(),
\OC::$server->getDatabaseConnection()
);
}

@@ -349,106 +350,106 @@ class ShareesTest extends TestCase {
$allTypes = [\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_REMOTE];

return [
[[], '', true, '', null, [], $allTypes, 1, 200, false],
[[], '', true, [], '', null, $allTypes, 1, 200, false],

// Test itemType
[[
'search' => '',
], '', true, '', null, [], $allTypes, 1, 200, false],
], '', true, [], '', null, $allTypes, 1, 200, false],
[[
'search' => 'foobar',
], '', true, 'foobar', null, [], $allTypes, 1, 200, false],
], '', true, [], 'foobar', null, $allTypes, 1, 200, false],
[[
'search' => 0,
], '', true, '0', null, [], $allTypes, 1, 200, false],
], '', true, [], '0', null, $allTypes, 1, 200, false],

// Test itemType
[[
'itemType' => '',
], '', true, '', '', [], $allTypes, 1, 200, false],
], '', true, [], '', '', $allTypes, 1, 200, false],
[[
'itemType' => 'folder',
], '', true, '', 'folder', [], $allTypes, 1, 200, false],
], '', true, [], '', 'folder', $allTypes, 1, 200, false],
[[
'itemType' => 0,
], '', true, '', '0', [], $allTypes, 1, 200, false],
], '', true, [], '', '0', $allTypes, 1, 200, false],

// Test existingShares
[[
'existingShares' => [],
], '', true, '', null, [], $allTypes, 1, 200, false],
], '', true, [], '', null, $allTypes, 1, 200, false],
[[
'existingShares' => [0 => ['test'], 1 => ['foobar']],
], '', true, '', null, [0 => ['test'], 1 => ['foobar']], $allTypes, 1, 200, false],
'existingShares' => [12, 42],
], '', true, [12, 42], '', null, $allTypes, 1, 200, false],

// Test shareType
[[
], '', true, '', null, [], $allTypes, 1, 200, false],
], '', true, [], '', null, $allTypes, 1, 200, false],
[[
'shareType' => 0,
], '', true, '', null, [], [0], 1, 200, false],
], '', true, [], '', null, [0], 1, 200, false],
[[
'shareType' => '0',
], '', true, '', null, [], [0], 1, 200, false],
], '', true, [], '', null, [0], 1, 200, false],
[[
'shareType' => 1,
], '', true, '', null, [], [1], 1, 200, false],
], '', true, [], '', null, [1], 1, 200, false],
[[
'shareType' => 12,
], '', true, '', null, [], [], 1, 200, false],
], '', true, [], '', null, [], 1, 200, false],
[[
'shareType' => 'foobar',
], '', true, '', null, [], $allTypes, 1, 200, false],
], '', true, [], '', null, $allTypes, 1, 200, false],
[[
'shareType' => [0, 1, 2],
], '', true, '', null, [], [0, 1], 1, 200, false],
], '', true, [], '', null, [0, 1], 1, 200, false],
[[
'shareType' => [0, 1],
], '', true, '', null, [], [0, 1], 1, 200, false],
], '', true, [], '', null, [0, 1], 1, 200, false],
[[
'shareType' => $allTypes,
], '', true, '', null, [], $allTypes, 1, 200, false],
], '', true, [], '', null, $allTypes, 1, 200, false],
[[
'shareType' => $allTypes,
], '', false, '', null, [], [0, 1], 1, 200, false],
], '', false, [], '', null, [0, 1], 1, 200, false],

// Test pagination
[[
'page' => 0,
], '', true, '', null, [], $allTypes, 1, 200, false],
], '', true, [], '', null, $allTypes, 1, 200, false],
[[
'page' => '0',
], '', true, '', null, [], $allTypes, 1, 200, false],
], '', true, [], '', null, $allTypes, 1, 200, false],
[[
'page' => -1,
], '', true, '', null, [], $allTypes, 1, 200, false],
], '', true, [], '', null, $allTypes, 1, 200, false],
[[
'page' => 1,
], '', true, '', null, [], $allTypes, 1, 200, false],
], '', true, [], '', null, $allTypes, 1, 200, false],
[[
'page' => 10,
], '', true, '', null, [], $allTypes, 10, 200, false],
], '', true, [], '', null, $allTypes, 10, 200, false],

// Test limit
[[
'limit' => 0,
], '', true, '', null, [], $allTypes, 1, 200, false],
], '', true, [], '', null, $allTypes, 1, 200, false],
[[
'limit' => '0',
], '', true, '', null, [], $allTypes, 1, 200, false],
], '', true, [], '', null, $allTypes, 1, 200, false],
[[
'limit' => -1,
], '', true, '', null, [], $allTypes, 1, 1, false],
], '', true, [], '', null, $allTypes, 1, 1, false],
[[
'limit' => 1,
], '', true, '', null, [], $allTypes, 1, 1, false],
], '', true, [], '', null, $allTypes, 1, 1, false],
[[
'limit' => 10,
], '', true, '', null, [], $allTypes, 1, 10, false],
], '', true, [], '', null, $allTypes, 1, 10, false],

// Test $shareWithGroupOnly setting
[[], 'no', true, '', null, [], $allTypes, 1, 200, false],
[[], 'yes', true, '', null, [], $allTypes, 1, 200, true],
[[], 'no', true, [], '', null, $allTypes, 1, 200, false],
[[], 'yes', true, [], '', null, $allTypes, 1, 200, true],

];
}
@@ -459,15 +460,15 @@ class ShareesTest extends TestCase {
* @param array $getData
* @param string $apiSetting
* @param bool $remoteSharingEnabled
* @param array $shareIds
* @param string $search
* @param string $itemType
* @param array $existingShares
* @param array $shareTypes
* @param int $page
* @param int $perPage
* @param bool $shareWithGroupOnly
*/
public function testSearch($getData, $apiSetting, $remoteSharingEnabled, $search, $itemType, $existingShares, $shareTypes, $page, $perPage, $shareWithGroupOnly) {
public function testSearch($getData, $apiSetting, $remoteSharingEnabled, $shareIds, $search, $itemType, $shareTypes, $page, $perPage, $shareWithGroupOnly) {
$oldGet = $_GET;
$_GET = $getData;

@@ -487,21 +488,22 @@ class ShareesTest extends TestCase {
$config,
$this->session,
$this->getMockBuilder('OCP\IURLGenerator')->disableOriginalConstructor()->getMock(),
$this->getMockBuilder('OCP\ILogger')->disableOriginalConstructor()->getMock()
$this->getMockBuilder('OCP\ILogger')->disableOriginalConstructor()->getMock(),
\OC::$server->getDatabaseConnection()
])
->setMethods(array('searchSharees', 'isRemoteSharingAllowed'))
->getMock();
$sharees->expects($this->once())
->method('searchSharees')
->with($search, $itemType, $existingShares, $shareTypes, $page, $perPage, $shareWithGroupOnly)
->with($search, $itemType, $shareIds, $shareTypes, $page, $perPage, $shareWithGroupOnly)
->willReturnCallback(function
($isearch, $iitemType, $iexistingShares, $ishareTypes, $ipage, $iperPage, $ishareWithGroupOnly)
use ($search, $itemType, $existingShares, $shareTypes, $page, $perPage, $shareWithGroupOnly) {
($isearch, $iitemType, $ishareIds, $ishareTypes, $ipage, $iperPage, $ishareWithGroupOnly)
use ($search, $itemType, $shareIds, $shareTypes, $page, $perPage, $shareWithGroupOnly) {

// We are doing strict comparisons here, so we can differ 0/'' and null on shareType/itemType
$this->assertSame($search, $isearch);
$this->assertSame($itemType, $iitemType);
$this->assertSame($existingShares, $iexistingShares);
$this->assertSame($shareIds, $ishareIds);
$this->assertSame($shareTypes, $ishareTypes);
$this->assertSame($page, $ipage);
$this->assertSame($perPage, $iperPage);
@@ -540,11 +542,11 @@ class ShareesTest extends TestCase {

public function dataSearchSharees() {
return [
['test', 'folder', [], [\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_REMOTE], 1, 2, false, [], [], [], [], 0, false],
['test', 'folder', [0 => ['test1'], 1 => ['test2 group']], [\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_REMOTE], 1, 2, false, [], [], [], [], 0, false],
['test', 'folder', [], [], [\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_REMOTE], 1, 2, false, [], [], [], [], 0, false],
['test', 'folder', [1, 2], [0 => ['test1'], 1 => ['test2 group']], [\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_REMOTE], 1, 2, false, [], [], [], [], 0, false],
// First page with 2 of 3 results
[
'test', 'folder', [], [\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_REMOTE], 1, 2, false, [
'test', 'folder', [], [], [\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_REMOTE], 1, 2, false, [
['label' => 'test One', 'value' => ['shareType' => \OCP\Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
], [
['label' => 'testgroup1', 'value' => ['shareType' => \OCP\Share::SHARE_TYPE_GROUP, 'shareWith' => 'testgroup1']],
@@ -557,7 +559,7 @@ class ShareesTest extends TestCase {
],
// Second page with the 3rd result
[
'test', 'folder', [], [\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_REMOTE], 2, 2, false, [
'test', 'folder', [], [], [\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_REMOTE], 2, 2, false, [
['label' => 'test One', 'value' => ['shareType' => \OCP\Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
], [
['label' => 'testgroup1', 'value' => ['shareType' => \OCP\Share::SHARE_TYPE_GROUP, 'shareWith' => 'testgroup1']],
@@ -569,7 +571,7 @@ class ShareesTest extends TestCase {
],
// No groups requested
[
'test', 'folder', [], [\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_REMOTE], 1, 2, false, [
'test', 'folder', [], [], [\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_REMOTE], 1, 2, false, [
['label' => 'test One', 'value' => ['shareType' => \OCP\Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
], null, [
['label' => 'testz@remote', 'value' => ['shareType' => \OCP\Share::SHARE_TYPE_REMOTE, 'shareWith' => 'testz@remote']],
@@ -580,7 +582,7 @@ class ShareesTest extends TestCase {
],
// Ingnoring already shared user
[
'test', 'folder', [\OCP\Share::SHARE_TYPE_USER => ['test1']], [\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_REMOTE], 1, 2, false, [
'test', 'folder', [1], [\OCP\Share::SHARE_TYPE_USER => ['test1']], [\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_REMOTE], 1, 2, false, [
['label' => 'test One', 'value' => ['shareType' => \OCP\Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
], [
['label' => 'testgroup1', 'value' => ['shareType' => \OCP\Share::SHARE_TYPE_GROUP, 'shareWith' => 'testgroup1']],
@@ -593,7 +595,7 @@ class ShareesTest extends TestCase {
],
// Share type restricted to user - Only one user
[
'test', 'folder', [], [\OCP\Share::SHARE_TYPE_USER], 1, 2, false, [
'test', 'folder', [], [], [\OCP\Share::SHARE_TYPE_USER], 1, 2, false, [
['label' => 'test One', 'value' => ['shareType' => \OCP\Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
], null, null, [
['label' => 'test One', 'value' => ['shareType' => \OCP\Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
@@ -601,7 +603,7 @@ class ShareesTest extends TestCase {
],
// Share type restricted to user - Multipage result
[
'test', 'folder', [], [\OCP\Share::SHARE_TYPE_USER], 1, 2, false, [
'test', 'folder', [], [], [\OCP\Share::SHARE_TYPE_USER], 1, 2, false, [
['label' => 'test 1', 'value' => ['shareType' => \OCP\Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
['label' => 'test 2', 'value' => ['shareType' => \OCP\Share::SHARE_TYPE_USER, 'shareWith' => 'test2']],
['label' => 'test 3', 'value' => ['shareType' => \OCP\Share::SHARE_TYPE_USER, 'shareWith' => 'test3']],
@@ -612,7 +614,7 @@ class ShareesTest extends TestCase {
],
// Share type restricted to user - Only user already shared
[
'test', 'folder', [\OCP\Share::SHARE_TYPE_USER => ['test1']], [\OCP\Share::SHARE_TYPE_USER], 1, 2, false, [
'test', 'folder', [1], [\OCP\Share::SHARE_TYPE_USER => ['test1']], [\OCP\Share::SHARE_TYPE_USER], 1, 2, false, [
['label' => 'test One', 'value' => ['shareType' => \OCP\Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
], null, null, [], 0, false,
],
@@ -624,6 +626,7 @@ class ShareesTest extends TestCase {
*
* @param string $searchTerm
* @param string $itemType
* @param array $shareIds
* @param array $existingShares
* @param array $shareTypes
* @param int $page
@@ -631,7 +634,7 @@ class ShareesTest extends TestCase {
* @param bool $shareWithGroupOnly
* @param array $expected
*/
public function testSearchSharees($searchTerm, $itemType, array $existingShares, array $shareTypes, $page, $perPage, $shareWithGroupOnly,
public function testSearchSharees($searchTerm, $itemType, array $shareIds, array $existingShares, array $shareTypes, $page, $perPage, $shareWithGroupOnly,
$mockedUserResult, $mockedGroupsResult, $mockedRemotesResult, $expected, $totalItems, $nextLink) {
/** @var \PHPUnit_Framework_MockObject_MockObject|\OCA\Files_Sharing\API\Sharees $sharees */
$sharees = $this->getMockBuilder('\OCA\Files_Sharing\API\Sharees')
@@ -642,10 +645,15 @@ class ShareesTest extends TestCase {
$this->getMockBuilder('OCP\IConfig')->disableOriginalConstructor()->getMock(),
$this->session,
$this->getMockBuilder('OCP\IURLGenerator')->disableOriginalConstructor()->getMock(),
$this->getMockBuilder('OCP\ILogger')->disableOriginalConstructor()->getMock()
$this->getMockBuilder('OCP\ILogger')->disableOriginalConstructor()->getMock(),
\OC::$server->getDatabaseConnection()
])
->setMethods(array('getUsers', 'getGroups', 'getRemote'))
->setMethods(array('getShareesForShareIds', 'getUsers', 'getGroups', 'getRemote'))
->getMock();
$sharees->expects($this->once())
->method('getShareesForShareIds')
->with($shareIds)
->willReturn($existingShares);
$sharees->expects(($mockedUserResult === null) ? $this->never() : $this->once())
->method('getUsers')
->with($searchTerm, $shareWithGroupOnly)
@@ -660,7 +668,7 @@ class ShareesTest extends TestCase {
->willReturn($mockedRemotesResult);

/** @var \OC_OCS_Result $ocs */
$ocs = $this->invokePrivate($sharees, 'searchSharees', [$searchTerm, $itemType, $existingShares, $shareTypes, $page, $perPage, $shareWithGroupOnly]);
$ocs = $this->invokePrivate($sharees, 'searchSharees', [$searchTerm, $itemType, $shareIds, $shareTypes, $page, $perPage, $shareWithGroupOnly]);
$this->assertInstanceOf('\OC_OCS_Result', $ocs);

$this->assertEquals($expected, $ocs->getData());

Loading…
Cancel
Save