Skip to content

Commit

Permalink
Merge pull request #39113 from fsamapoor/refactor_lib_private_collabo…
Browse files Browse the repository at this point in the history
…ration_part1

[1/2] Refactors lib/private/Collaboration
  • Loading branch information
icewind1991 authored Sep 22, 2023
2 parents 6b767e0 + 14ad1e9 commit 7fecdf6
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 195 deletions.
25 changes: 12 additions & 13 deletions lib/private/Collaboration/AutoComplete/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,47 +29,46 @@

class Manager implements IManager {
/** @var string[] */
protected $sorters = [];
protected array $sorters = [];

/** @var ISorter[] */
protected $sorterInstances = [];
/** @var IServerContainer */
private $c;
protected array $sorterInstances = [];

public function __construct(IServerContainer $container) {
$this->c = $container;
public function __construct(
private IServerContainer $container,
) {
}

public function runSorters(array $sorters, array &$sortArray, array $context) {
public function runSorters(array $sorters, array &$sortArray, array $context): void {
$sorterInstances = $this->getSorters();
while ($sorter = array_shift($sorters)) {
if (isset($sorterInstances[$sorter])) {
$sorterInstances[$sorter]->sort($sortArray, $context);
} else {
$this->c->getLogger()->warning('No sorter for ID "{id}", skipping', [
$this->container->getLogger()->warning('No sorter for ID "{id}", skipping', [
'app' => 'core', 'id' => $sorter
]);
}
}
}

public function registerSorter($className) {
public function registerSorter($className): void {
$this->sorters[] = $className;
}

protected function getSorters() {
protected function getSorters(): array {
if (count($this->sorterInstances) === 0) {
foreach ($this->sorters as $sorter) {
/** @var ISorter $instance */
$instance = $this->c->resolve($sorter);
$instance = $this->container->resolve($sorter);
if (!$instance instanceof ISorter) {
$this->c->getLogger()->notice('Skipping sorter which is not an instance of ISorter. Class name: {class}',
$this->container->getLogger()->notice('Skipping sorter which is not an instance of ISorter. Class name: {class}',
['app' => 'core', 'class' => $sorter]);
continue;
}
$sorterId = trim($instance->getId());
if (trim($sorterId) === '') {
$this->c->getLogger()->notice('Skipping sorter with empty ID. Class name: {class}',
$this->container->getLogger()->notice('Skipping sorter with empty ID. Class name: {class}',
['app' => 'core', 'class' => $sorter]);
continue;
}
Expand Down
34 changes: 13 additions & 21 deletions lib/private/Collaboration/Collaborators/GroupPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,26 @@
use OCP\Share\IShare;

class GroupPlugin implements ISearchPlugin {
/** @var bool */
protected $shareeEnumeration;
/** @var bool */
protected $shareWithGroupOnly;
/** @var bool */
protected $shareeEnumerationInGroupOnly;
/** @var bool */
protected $groupSharingDisabled;

/** @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;
protected bool $shareeEnumeration;

protected bool $shareWithGroupOnly;

protected bool $shareeEnumerationInGroupOnly;

protected bool $groupSharingDisabled;

public function __construct(
private IConfig $config,
private IGroupManager $groupManager,
private IUserSession $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';
$this->shareeEnumerationInGroupOnly = $this->shareeEnumeration && $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_group', 'no') === 'yes';
$this->groupSharingDisabled = $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes') === 'no';
}

public function search($search, $limit, $offset, ISearchResult $searchResult) {
public function search($search, $limit, $offset, ISearchResult $searchResult): bool {
if ($this->groupSharingDisabled) {
return false;
}
Expand Down
30 changes: 10 additions & 20 deletions lib/private/Collaboration/Collaborators/LookupPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,21 @@
use Psr\Log\LoggerInterface;

class LookupPlugin implements ISearchPlugin {
/** @var IConfig */
private $config;
/** @var IClientService */
private $clientService;
/** @var string remote part of the current user's cloud id */
private $currentUserRemote;
/** @var ICloudIdManager */
private $cloudIdManager;
/** @var LoggerInterface */
private $logger;
private string $currentUserRemote;

public function __construct(IConfig $config,
IClientService $clientService,
IUserSession $userSession,
ICloudIdManager $cloudIdManager,
LoggerInterface $logger) {
$this->config = $config;
$this->clientService = $clientService;
$this->cloudIdManager = $cloudIdManager;
public function __construct(
private IConfig $config,
private IClientService $clientService,
IUserSession $userSession,
private ICloudIdManager $cloudIdManager,
private LoggerInterface $logger,
) {
$currentUserCloudId = $userSession->getUser()->getCloudId();
$this->currentUserRemote = $cloudIdManager->resolveCloudId($currentUserCloudId)->getRemote();
$this->logger = $logger;
}

public function search($search, $limit, $offset, ISearchResult $searchResult) {
public function search($search, $limit, $offset, ISearchResult $searchResult): bool {
$isGlobalScaleEnabled = $this->config->getSystemValueBool('gs.enabled', false);
$isLookupServerEnabled = $this->config->getAppValue('files_sharing', 'lookupServerEnabled', 'yes') === 'yes';
$hasInternetConnection = $this->config->getSystemValueBool('has_internet_connection', true);
Expand Down Expand Up @@ -103,7 +93,7 @@ public function search($search, $limit, $offset, ISearchResult $searchResult) {
if ($this->currentUserRemote === $remote) {
continue;
}
$name = isset($lookup['name']['value']) ? $lookup['name']['value'] : '';
$name = $lookup['name']['value'] ?? '';
$label = empty($name) ? $lookup['federationId'] : $name . ' (' . $lookup['federationId'] . ')';
$result[] = [
'label' => $label,
Expand Down
65 changes: 21 additions & 44 deletions lib/private/Collaboration/Collaborators/MailPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,50 +41,27 @@
use OCP\Mail\IMailer;

class MailPlugin implements ISearchPlugin {
/* @var bool */
protected $shareWithGroupOnly;
/* @var bool */
protected $shareeEnumeration;
/* @var bool */
protected $shareeEnumerationInGroupOnly;
/* @var bool */
protected $shareeEnumerationPhone;
/* @var bool */
protected $shareeEnumerationFullMatch;
/* @var bool */
protected $shareeEnumerationFullMatchEmail;
protected bool $shareWithGroupOnly;

/** @var IManager */
private $contactsManager;
/** @var ICloudIdManager */
private $cloudIdManager;
/** @var IConfig */
private $config;
protected bool $shareeEnumeration;

/** @var IGroupManager */
private $groupManager;
/** @var KnownUserService */
private $knownUserService;
/** @var IUserSession */
private $userSession;
/** @var IMailer */
private $mailer;
protected bool $shareeEnumerationInGroupOnly;

public function __construct(IManager $contactsManager,
ICloudIdManager $cloudIdManager,
IConfig $config,
IGroupManager $groupManager,
KnownUserService $knownUserService,
IUserSession $userSession,
IMailer $mailer) {
$this->contactsManager = $contactsManager;
$this->cloudIdManager = $cloudIdManager;
$this->config = $config;
$this->groupManager = $groupManager;
$this->knownUserService = $knownUserService;
$this->userSession = $userSession;
$this->mailer = $mailer;
protected bool $shareeEnumerationPhone;

protected bool $shareeEnumerationFullMatch;

protected bool $shareeEnumerationFullMatchEmail;

public function __construct(
private IManager $contactsManager,
private ICloudIdManager $cloudIdManager,
private IConfig $config,
private IGroupManager $groupManager,
private KnownUserService $knownUserService,
private IUserSession $userSession,
private IMailer $mailer,
) {
$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';
$this->shareeEnumerationInGroupOnly = $this->shareeEnumeration && $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_group', 'no') === 'yes';
Expand All @@ -96,7 +73,7 @@ public function __construct(IManager $contactsManager,
/**
* {@inheritdoc}
*/
public function search($search, $limit, $offset, ISearchResult $searchResult) {
public function search($search, $limit, $offset, ISearchResult $searchResult): bool {
if ($this->shareeEnumerationFullMatch && !$this->shareeEnumerationFullMatchEmail) {
return false;
}
Expand All @@ -120,8 +97,8 @@ public function search($search, $limit, $offset, ISearchResult $searchResult) {
[
'limit' => $limit,
'offset' => $offset,
'enumeration' => (bool) $this->shareeEnumeration,
'fullmatch' => (bool) $this->shareeEnumerationFullMatch,
'enumeration' => $this->shareeEnumeration,
'fullmatch' => $this->shareeEnumerationFullMatch,
]
);
$lowerSearch = strtolower($search);
Expand Down Expand Up @@ -286,6 +263,6 @@ public function search($search, $limit, $offset, ISearchResult $searchResult) {

public function isCurrentUser(ICloudId $cloud): bool {
$currentUser = $this->userSession->getUser();
return $currentUser instanceof IUser ? $currentUser->getUID() === $cloud->getUser() : false;
return $currentUser instanceof IUser && $currentUser->getUID() === $cloud->getUser();
}
}
17 changes: 7 additions & 10 deletions lib/private/Collaboration/Collaborators/RemoteGroupPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@
use OCP\Share\IShare;

class RemoteGroupPlugin implements ISearchPlugin {
protected $shareeEnumeration;
private bool $enabled = false;

/** @var ICloudIdManager */
private $cloudIdManager;
/** @var bool */
private $enabled = false;

public function __construct(ICloudFederationProviderManager $cloudFederationProviderManager, ICloudIdManager $cloudIdManager) {
public function __construct(
ICloudFederationProviderManager $cloudFederationProviderManager,
private ICloudIdManager $cloudIdManager,
) {
try {
$fileSharingProvider = $cloudFederationProviderManager->getCloudFederationProvider('file');
$supportedShareTypes = $fileSharingProvider->getSupportedShareTypes();
Expand All @@ -50,10 +48,9 @@ public function __construct(ICloudFederationProviderManager $cloudFederationProv
} catch (\Exception $e) {
// do nothing, just don't enable federated group shares
}
$this->cloudIdManager = $cloudIdManager;
}

public function search($search, $limit, $offset, ISearchResult $searchResult) {
public function search($search, $limit, $offset, ISearchResult $searchResult): bool {
$result = ['wide' => [], 'exact' => []];
$resultType = new SearchResultType('remote_groups');

Expand Down Expand Up @@ -83,7 +80,7 @@ public function search($search, $limit, $offset, ISearchResult $searchResult) {
* @return array [user, remoteURL]
* @throws \InvalidArgumentException
*/
public function splitGroupRemote($address) {
public function splitGroupRemote($address): array {
try {
$cloudId = $this->cloudIdManager->resolveCloudId($address);
return [$cloudId->getUser(), $cloudId->getRemote()];
Expand Down
34 changes: 12 additions & 22 deletions lib/private/Collaboration/Collaborators/RemotePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,22 @@
use OCP\Share\IShare;

class RemotePlugin implements ISearchPlugin {
protected $shareeEnumeration;
protected bool $shareeEnumeration;

/** @var IManager */
private $contactsManager;
/** @var ICloudIdManager */
private $cloudIdManager;
/** @var IConfig */
private $config;
/** @var IUserManager */
private $userManager;
/** @var string */
private $userId = '';
private string $userId;

public function __construct(IManager $contactsManager, ICloudIdManager $cloudIdManager, IConfig $config, IUserManager $userManager, IUserSession $userSession) {
$this->contactsManager = $contactsManager;
$this->cloudIdManager = $cloudIdManager;
$this->config = $config;
$this->userManager = $userManager;
$user = $userSession->getUser();
if ($user !== null) {
$this->userId = $user->getUID();
}
public function __construct(
private IManager $contactsManager,
private ICloudIdManager $cloudIdManager,
private IConfig $config,
private IUserManager $userManager,
IUserSession $userSession,
) {
$this->userId = $userSession->getUser()?->getUID() ?? '';
$this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
}

public function search($search, $limit, $offset, ISearchResult $searchResult) {
public function search($search, $limit, $offset, ISearchResult $searchResult): bool {
$result = ['wide' => [], 'exact' => []];
$resultType = new SearchResultType('remotes');

Expand Down Expand Up @@ -185,7 +175,7 @@ public function search($search, $limit, $offset, ISearchResult $searchResult) {
* @return array [user, remoteURL]
* @throws \InvalidArgumentException
*/
public function splitUserRemote($address) {
public function splitUserRemote(string $address): array {
try {
$cloudId = $this->cloudIdManager->resolveCloudId($address);
return [$cloudId->getUser(), $cloudId->getRemote()];
Expand Down
Loading

0 comments on commit 7fecdf6

Please sign in to comment.