Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6328 from nextcloud/split-sharees-api-logic
Splits off the logic from sharees endpoint thus making it available from within Nc/via PHP.
- Loading branch information
Showing
24 changed files
with
3,286 additions
and
2,123 deletions.
There are no files selected for viewing
614 changes: 36 additions & 578 deletions
614
apps/files_sharing/lib/Controller/ShareesAPIController.php
Large diffs are not rendered by default.
Oops, something went wrong.
1,597 changes: 52 additions & 1,545 deletions
1,597
apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
lib/private/Collaboration/Collaborators/GroupPlugin.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
/** | ||
* @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de> | ||
* | ||
* @author Arthur Schiwon <blizzz@arthur-schiwon.de> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OC\Collaboration\Collaborators; | ||
|
||
use OCP\Collaboration\Collaborators\ISearchPlugin; | ||
use OCP\Collaboration\Collaborators\ISearchResult; | ||
use OCP\Collaboration\Collaborators\SearchResultType; | ||
use OCP\IConfig; | ||
use OCP\IGroup; | ||
use OCP\IGroupManager; | ||
use OCP\IUserSession; | ||
use OCP\Share; | ||
|
||
class GroupPlugin implements ISearchPlugin { | ||
protected $shareeEnumeration; | ||
protected $shareWithGroupOnly; | ||
|
||
/** @var IGroupManager */ | ||
private $groupManager; | ||
/** @var IConfig */ | ||
private $config; | ||
/** @var IUserSession */ | ||
private $userSession; | ||
|
||
public function __construct(IConfig $config, IGroupManager $groupManager, IUserSession $userSession) { | ||
$this->groupManager = $groupManager; | ||
$this->config = $config; | ||
$this->userSession = $userSession; | ||
|
||
$this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes'; | ||
$this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes'; | ||
} | ||
|
||
public function search($search, $limit, $offset, ISearchResult $searchResult) { | ||
$hasMoreResults = false; | ||
$result = ['wide' => [], 'exact' => []]; | ||
|
||
$groups = $this->groupManager->search($search, $limit, $offset); | ||
$groupIds = array_map(function (IGroup $group) { return $group->getGID(); }, $groups); | ||
|
||
if (!$this->shareeEnumeration || sizeof($groups) < $limit) { | ||
$hasMoreResults = true; | ||
} | ||
|
||
$userGroups = []; | ||
if (!empty($groups) && $this->shareWithGroupOnly) { | ||
// Intersect all the groups that match with the groups this user is a member of | ||
$userGroups = $this->groupManager->getUserGroups($this->userSession->getUser()); | ||
$userGroups = array_map(function (IGroup $group) { return $group->getGID(); }, $userGroups); | ||
$groupIds = array_intersect($groupIds, $userGroups); | ||
} | ||
|
||
$lowerSearch = strtolower($search); | ||
foreach ($groups as $group) { | ||
// FIXME: use a more efficient approach | ||
$gid = $group->getGID(); | ||
if (!in_array($gid, $groupIds)) { | ||
continue; | ||
} | ||
if (strtolower($gid) === $lowerSearch || strtolower($group->getDisplayName()) === $lowerSearch) { | ||
$result['exact'][] = [ | ||
'label' => $group->getDisplayName(), | ||
'value' => [ | ||
'shareType' => Share::SHARE_TYPE_GROUP, | ||
'shareWith' => $gid, | ||
], | ||
]; | ||
} else { | ||
$result['wide'][] = [ | ||
'label' => $group->getDisplayName(), | ||
'value' => [ | ||
'shareType' => Share::SHARE_TYPE_GROUP, | ||
'shareWith' => $gid, | ||
], | ||
]; | ||
} | ||
} | ||
|
||
if ($offset === 0 && empty($result['exact'])) { | ||
// On page one we try if the search result has a direct hit on the | ||
// user id and if so, we add that to the exact match list | ||
$group = $this->groupManager->get($search); | ||
if ($group instanceof IGroup && (!$this->shareWithGroupOnly || in_array($group->getGID(), $userGroups))) { | ||
array_push($result['exact'], [ | ||
'label' => $group->getDisplayName(), | ||
'value' => [ | ||
'shareType' => Share::SHARE_TYPE_GROUP, | ||
'shareWith' => $group->getGID(), | ||
], | ||
]); | ||
} | ||
} | ||
|
||
if (!$this->shareeEnumeration) { | ||
$result['wide'] = []; | ||
} | ||
|
||
$type = new SearchResultType('groups'); | ||
$searchResult->addResultSet($type, $result['wide'], $result['exact']); | ||
|
||
return $hasMoreResults; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
/** | ||
* @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de> | ||
* | ||
* @author Arthur Schiwon <blizzz@arthur-schiwon.de> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OC\Collaboration\Collaborators; | ||
|
||
|
||
use OCP\Collaboration\Collaborators\ISearchPlugin; | ||
use OCP\Collaboration\Collaborators\ISearchResult; | ||
use OCP\Collaboration\Collaborators\SearchResultType; | ||
use OCP\Http\Client\IClientService; | ||
use OCP\IConfig; | ||
use OCP\Share; | ||
|
||
class LookupPlugin implements ISearchPlugin { | ||
|
||
/** @var IConfig */ | ||
private $config; | ||
/** @var IClientService */ | ||
private $clientService; | ||
|
||
public function __construct(IConfig $config, IClientService $clientService) { | ||
$this->config = $config; | ||
$this->clientService = $clientService; | ||
} | ||
|
||
public function search($search, $limit, $offset, ISearchResult $searchResult) { | ||
if ($this->config->getAppValue('files_sharing', 'lookupServerEnabled', 'no') !== 'yes') { | ||
return false; | ||
} | ||
|
||
$lookupServerUrl = $this->config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com'); | ||
$lookupServerUrl = rtrim($lookupServerUrl, '/'); | ||
$result = []; | ||
|
||
try { | ||
$client = $this->clientService->newClient(); | ||
$response = $client->get( | ||
$lookupServerUrl . '/users?search=' . urlencode($search), | ||
[ | ||
'timeout' => 10, | ||
'connect_timeout' => 3, | ||
] | ||
); | ||
|
||
$body = json_decode($response->getBody(), true); | ||
|
||
foreach ($body as $lookup) { | ||
$result[] = [ | ||
'label' => $lookup['federationId'], | ||
'value' => [ | ||
'shareType' => Share::SHARE_TYPE_REMOTE, | ||
'shareWith' => $lookup['federationId'], | ||
], | ||
'extra' => $lookup, | ||
]; | ||
} | ||
} catch (\Exception $e) { | ||
} | ||
|
||
$type = new SearchResultType('lookup'); | ||
$searchResult->addResultSet($type, $result, []); | ||
|
||
return false; | ||
} | ||
} |
Oops, something went wrong.