Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extention of OCS to list the people/groups we can share with #13587

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions apps/files_sharing/api/local.php
Original file line number Diff line number Diff line change
Expand Up @@ -603,4 +603,78 @@ private static function getShareFromId($shareID) {

}

public static function shareWith() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the result set can be really hugh - which requires pagination - please add parameters like $limit and $offset

furthermore clients want to implement type ahead input fields which requires a $search parameter

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes that makes sense! It is added now. However now we still construct the entire array first on the server. So I will improve that later.

Also I need to look up how to test this.

$limit = 25;
$offset = 0;
$search = '';
if (array_key_exists('limit', $_GET)) {
$limit = intval($_GET['limit']);
if ($limit <= 0) {
$limit = null;
}
}
if (array_key_exists('offset', $_GET)) {
$offset = intval($_GET['offset']);
if ($offset < 0) {
$offset = 0;
}
}
if (array_key_exists('search', $_GET)) {
$search = (string)$_GET['search'];
}


$shareWithinGroupOnly = \OC\Share\Share::shareWithGroupMembersOnly();
$shareWith = array();

if ($shareWithinGroupOnly) {
$_user = \OC::$server->getUserSession()->getUser();
if ($_user == null) {
return new \OC_OCS_Result(null, 404, 'could not obtain list of users');
}
$groups = \OC::$server->getGroupManager()->getuserGroupIds($_user);;
$users = array();
foreach ($groups as $group) {
$users += \OC::$server->getGroupManager()->displayNamesInGroup($group, $search);
}
// Now filter group list
if (!empty($search)) {
$groups = array_filter($groups, function($group) use ($search) {
return stristr($group, $search) !== FALSE;
});
}
} else {
$groups = \OC_Group::getGroups($search);
$users = array();
$_users = \OC::$server->getUserManager()->searchDisplayName($search);
foreach ($_users as $user) {
$users[$user->getUID()] = $user->getDisplayName();
}
}
//Remove calling user from user array
$users = array_diff($users, array(\OC_User::getUser()));

foreach ($users as $uid => $displayName) {
$shareWith[] = array(
'displayName' => $displayName,
'shareType' => \OCP\Share::SHARE_TYPE_USER,
'shareWith' => $uid
);
}

foreach ($groups as $group) {
$shareWith[] = array(
'displayName' => $group,
'shareType' => \OCP\Share::SHARE_TYPE_GROUP,
'shareWith' => $group
);
}

$sorter = new \OC\Share\SearchResultSorter($search, 'displayName', new \OC\Log());
usort($shareWith, array($sorter, 'sort'));

$shareWith = array_slice($shareWith, $offset, $limit);

return new \OC_OCS_Result($shareWith);
}
}
5 changes: 5 additions & 0 deletions apps/files_sharing/appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,8 @@ function() {
'/apps/files_sharing/api/v1/shares/{id}',
array('\OCA\Files_Sharing\API\Local', 'deleteShare'),
'files_sharing');

\OC_API::register('get',
'/apps/files_sharing/api/v1/sharewith',
array('\OCA\Files_Sharing\API\Local', 'shareWith'),
'files_sharing');
51 changes: 51 additions & 0 deletions apps/files_sharing/tests/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -1333,4 +1333,55 @@ public function testDefaultExpireDate() {
\OC::$server->getAppConfig()->setValue('core', 'shareapi_enforce_expire_date', 'no');

}

/**
* @medium
*/
public function testShareWith() {
//TODO: Mock config etc
$appConfig = \OC::$server->getConfig();

//Helper functions
$filter_user = function($item) {
return $item['shareType'] == \OCP\Share::SHARE_TYPE_USER;
};
$filter_group = function($item) {
return $item['shareType'] == \OCP\Share::SHARE_TYPE_GROUP;
};
$getShareWith = function($item) {
return $item['shareWith'];
};

//Test if the test users and groups are in the list
//Except of course for test user 1 since we cannot share with ourself
$appConfig->setAppValue('core', 'shareapi_only_share_with_group_members', 'no');
$result = \OCA\Files_Sharing\API\Local::shareWith();
$this->assertTrue($result->succeeded());

$data = $result->getData();
$users = array_map($getShareWith, array_filter($data, $filter_user));
$groups = array_map($getShareWith, array_filter($data, $filter_group));
$this->assertFalse(in_array(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1, $users));
$this->assertTrue(in_array(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, $users));
$this->assertTrue(in_array(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3, $users));
$this->assertTrue(in_array(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_GROUP1, $groups));
$this->assertTrue(in_array("group", $groups));

//Test if we get the correct result if we only allow sharing within our group
//TODO: extend setup so we can also check for users
$appConfig->setAppValue('core', 'shareapi_only_share_with_group_members', 'yes');
$result = \OCA\Files_Sharing\API\Local::shareWith();
$this->assertTrue($result->succeeded());

$data = $result->getData();
$users = array_map($getShareWith, array_filter($data, $filter_user));
$groups = array_map($getShareWith, array_filter($data, $filter_group));
$this->assertFalse(in_array(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1, $users));
$this->assertTrue(in_array(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, $users));
$this->assertTrue(in_array(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3, $users));
$this->assertFalse(in_array(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_GROUP1, $groups));
$this->assertTrue(in_array("group", $groups));

$appConfig->setAppValue('core', 'shareapi_only_share_with_group_members', 'no');
}
}
4 changes: 4 additions & 0 deletions lib/private/share/searchresultsorter.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public function sort($a, $b) {
}
$nameA = mb_strtolower($a[$this->key], $this->encoding);
$nameB = mb_strtolower($b[$this->key], $this->encoding);

if (empty($this->search)) {
return(strcmp($nameA, $nameB));
}
$i = mb_strpos($nameA, $this->search, 0, $this->encoding);
$j = mb_strpos($nameB, $this->search, 0, $this->encoding);

Expand Down