Skip to content

Commit 7e3af67

Browse files
committed
refactor(core): Use constructor properties in Sharing
Signed-off-by: provokateurin <kate@provokateurin.de>
1 parent 0bf8efd commit 7e3af67

17 files changed

Lines changed: 60 additions & 93 deletions

core/AppInfo/Application.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,13 @@ public function register(IRegistrationContext $context): void {
118118

119119
$registry = Server::get(ISharingRegistry::class);
120120

121-
$registry->registerRecipientType(new EmailShareRecipientType());
121+
$registry->registerRecipientType(Server::get(EmailShareRecipientType::class));
122122
$registry->registerRecipientType(Server::get(GroupShareRecipientType::class));
123123
$registry->registerRecipientType(Server::get(TeamShareRecipientType::class));
124-
$registry->registerRecipientType(new TokenShareRecipientType());
124+
$registry->registerRecipientType(Server::get(TokenShareRecipientType::class));
125125
$registry->registerRecipientType(Server::get(UserShareRecipientType::class));
126126

127-
$registry->registerPropertyType(new ExpirationDateSharePropertyType());
127+
$registry->registerPropertyType(Server::get(ExpirationDateSharePropertyType::class));
128128
$registry->markPropertyTypeCompatibleWithRecipientType(ExpirationDateSharePropertyType::class, EmailShareRecipientType::class);
129129
$registry->markPropertyTypeCompatibleWithRecipientType(ExpirationDateSharePropertyType::class, GroupShareRecipientType::class);
130130
$registry->markPropertyTypeCompatibleWithRecipientType(ExpirationDateSharePropertyType::class, TeamShareRecipientType::class);
@@ -141,14 +141,14 @@ public function register(IRegistrationContext $context): void {
141141
$registry->markPropertyTypeCompatibleWithRecipientType(NoteSharePropertyType::class, TokenShareRecipientType::class);
142142
$registry->markPropertyTypeCompatibleWithRecipientType(NoteSharePropertyType::class, UserShareRecipientType::class);
143143

144-
$registry->registerPropertyType(new PasswordSharePropertyType());
144+
$registry->registerPropertyType(Server::get(PasswordSharePropertyType::class));
145145
$registry->markPropertyTypeCompatibleWithRecipientType(PasswordSharePropertyType::class, EmailShareRecipientType::class);
146146
$registry->markPropertyTypeCompatibleWithRecipientType(PasswordSharePropertyType::class, TokenShareRecipientType::class);
147147

148148
$registry->registerPermissionPreset(new ViewSharePermissionPreset());
149149
$registry->registerPermissionPreset(new EditSharePermissionPreset());
150150

151-
$registry->registerPermissionType(null, new ReshareSharePermissionType());
151+
$registry->registerPermissionType(null, Server::get(ReshareSharePermissionType::class));
152152
// Cannot use the APP_ID from files_sharing Application and EXCLUDE_RESHARE_FROM_EDIT from files_sharing ConfigLexicon, because the classes are not registered yet.
153153
if (!Server::get(IAppConfig::class)->getValueBool('files_sharing', 'shareapi_exclude_reshare_from_edit')) {
154154
$registry->markPermissionTypeCompatibleWithPermissionPreset(ReshareSharePermissionType::class, EditSharePermissionPreset::class);

core/Sharing/Permission/ReshareSharePermissionType.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@
1313
use OCP\Constants;
1414
use OCP\IAppConfig;
1515
use OCP\L10N\IFactory;
16-
use OCP\Server;
1716
use OCP\Sharing\Permission\ISharePermissionType;
1817

19-
final class ReshareSharePermissionType implements ISharePermissionType {
20-
private ?IAppConfig $appConfig = null;
21-
22-
private function getAppConfig(): IAppConfig {
23-
return $this->appConfig ??= Server::get(IAppConfig::class);
18+
final readonly class ReshareSharePermissionType implements ISharePermissionType {
19+
public function __construct(
20+
private IAppConfig $appConfig,
21+
) {
2422
}
2523

2624
#[\Override]
@@ -40,6 +38,6 @@ public function getPriority(): int {
4038

4139
#[\Override]
4240
public function isEnabledByDefault(): bool {
43-
return ($this->getAppConfig()->getValueInt(Application::APP_ID, 'shareapi_default_permissions') & Constants::PERMISSION_SHARE) === Constants::PERMISSION_SHARE;
41+
return ($this->appConfig->getValueInt(Application::APP_ID, 'shareapi_default_permissions') & Constants::PERMISSION_SHARE) === Constants::PERMISSION_SHARE;
4442
}
4543
}

core/Sharing/Property/ExpirationDateSharePropertyType.php

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use DateTimeInterface;
1515
use OC\Core\AppInfo\Application;
1616
use OCP\L10N\IFactory;
17-
use OCP\Server;
1817
use OCP\Share\IManager;
1918
use OCP\Sharing\Property\ADateSharePropertyType;
2019
use OCP\Sharing\Property\ISharePropertyTypeFilter;
@@ -26,13 +25,9 @@
2625
final class ExpirationDateSharePropertyType extends ADateSharePropertyType implements ISharePropertyTypeFilter {
2726
private readonly DateTimeImmutable $now;
2827

29-
private ?IManager $legacyManager = null;
30-
31-
private function getLegacyManager(): IManager {
32-
return $this->legacyManager ??= Server::get(IManager::class);
33-
}
34-
35-
public function __construct() {
28+
public function __construct(
29+
private readonly IManager $legacyManager,
30+
) {
3631
$this->now = new DateTimeImmutable();
3732
}
3833

@@ -58,15 +53,15 @@ public function isAdvanced(): bool {
5853

5954
#[\Override]
6055
public function isRequired(): bool {
61-
if ($this->getLegacyManager()->shareApiLinkDefaultExpireDateEnforced()) {
56+
if ($this->legacyManager->shareApiLinkDefaultExpireDateEnforced()) {
6257
return true;
6358
}
6459

65-
if ($this->getLegacyManager()->shareApiRemoteDefaultExpireDateEnforced()) {
60+
if ($this->legacyManager->shareApiRemoteDefaultExpireDateEnforced()) {
6661
return true;
6762
}
6863

69-
return $this->getLegacyManager()->shareApiInternalDefaultExpireDateEnforced();
64+
return $this->legacyManager->shareApiInternalDefaultExpireDateEnforced();
7065
}
7166

7267
#[\Override]
@@ -93,16 +88,16 @@ public function getMaxDate(): ?DateTimeImmutable {
9388
private function getMaxExpirationDate(): ?DateTimeImmutable {
9489
// We do not have any distinction between link/remote/internal, so we just apply the lowest expiration days count to be safe.
9590
$days = INF;
96-
if ($this->getLegacyManager()->shareApiLinkDefaultExpireDate()) {
97-
$days = min($days, $this->getLegacyManager()->shareApiLinkDefaultExpireDays());
91+
if ($this->legacyManager->shareApiLinkDefaultExpireDate()) {
92+
$days = min($days, $this->legacyManager->shareApiLinkDefaultExpireDays());
9893
}
9994

100-
if ($this->getLegacyManager()->shareApiRemoteDefaultExpireDate()) {
101-
$days = min($days, $this->getLegacyManager()->shareApiRemoteDefaultExpireDays());
95+
if ($this->legacyManager->shareApiRemoteDefaultExpireDate()) {
96+
$days = min($days, $this->legacyManager->shareApiRemoteDefaultExpireDays());
10297
}
10398

104-
if ($this->getLegacyManager()->shareApiInternalDefaultExpireDate()) {
105-
$days = min($days, $this->getLegacyManager()->shareApiInternalDefaultExpireDays());
99+
if ($this->legacyManager->shareApiInternalDefaultExpireDate()) {
100+
$days = min($days, $this->legacyManager->shareApiInternalDefaultExpireDays());
106101
}
107102

108103
if ($days !== INF) {

core/Sharing/Property/PasswordSharePropertyType.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,18 @@
1212
use OC\Core\AppInfo\Application;
1313
use OCP\L10N\IFactory;
1414
use OCP\Security\IHasher;
15-
use OCP\Server;
1615
use OCP\Share\IManager;
1716
use OCP\Sharing\Property\APasswordSharePropertyType;
1817
use OCP\Sharing\Property\ISharePropertyTypeFilter;
1918
use OCP\Sharing\Share;
2019
use OCP\Sharing\ShareAccessContext;
2120

2221
final class PasswordSharePropertyType extends APasswordSharePropertyType implements ISharePropertyTypeFilter {
23-
private ?IManager $legacyManager = null;
2422

25-
private ?IHasher $hasher = null;
26-
27-
private function getLegacyManager(): IManager {
28-
return $this->legacyManager ??= Server::get(IManager::class);
29-
}
30-
31-
private function getHasher(): IHasher {
32-
return $this->hasher ??= Server::get(IHasher::class);
23+
public function __construct(
24+
private readonly IManager $legacyManager,
25+
private readonly IHasher $hasher,
26+
) {
3327
}
3428

3529
#[\Override]
@@ -55,7 +49,7 @@ public function isAdvanced(): bool {
5549
#[\Override]
5650
public function isRequired(): bool {
5751
// TODO: Enable group memberships check based on the owner.
58-
return $this->getLegacyManager()->shareApiLinkEnforcePassword(false);
52+
return $this->legacyManager->shareApiLinkEnforcePassword(false);
5953
}
6054

6155
#[\Override]
@@ -72,7 +66,7 @@ public function isFiltered(ShareAccessContext $accessContext, Share $share): boo
7266

7367
if (($property = $share->properties[self::class] ?? null) !== null && $property->value !== null) {
7468
// TODO: Check if the hash has to be updated and save it.
75-
return !$this->getHasher()->verify($argument, $property->value);
69+
return !$this->hasher->verify($argument, $property->value);
7670
}
7771

7872
return false;

core/Sharing/Recipient/EmailShareRecipientType.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use OCP\IUser;
1616
use OCP\L10N\IFactory;
1717
use OCP\Mail\IEmailValidator;
18-
use OCP\Server;
1918
use OCP\Share\IShare;
2019
use OCP\Sharing\Icon\ShareIconSVG;
2120
use OCP\Sharing\Icon\ShareIconURL;
@@ -24,10 +23,9 @@
2423
// TODO: Add logic to send emails when share state is updated to active
2524

2625
final class EmailShareRecipientType extends AShareRecipientTypeSearchCollaborator {
27-
private ?IEmailValidator $emailValidator = null;
28-
29-
private function getEmailValidator(): IEmailValidator {
30-
return $this->emailValidator ??= Server::get(IEmailValidator::class);
26+
public function __construct(
27+
private readonly IEmailValidator $emailValidator,
28+
) {
3129
}
3230

3331
#[\Override]
@@ -37,7 +35,7 @@ public function getDisplayName(IFactory $l10nFactory): string {
3735

3836
#[\Override]
3937
public function validateRecipient(string $recipient): bool {
40-
return $this->getEmailValidator()->isValid($recipient);
38+
return $this->emailValidator->isValid($recipient);
4139
}
4240

4341
#[\Override]

core/Sharing/Recipient/GroupShareRecipientType.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use OCP\Interaction\Receivers\GroupReceiver;
2222
use OCP\IUser;
2323
use OCP\L10N\IFactory;
24-
use OCP\Server;
2524
use OCP\Share\IShare;
2625
use OCP\Sharing\Icon\ShareIconSVG;
2726
use OCP\Sharing\Icon\ShareIconURL;
@@ -34,28 +33,23 @@
3433
* @template-implements IEventListener<GroupDeletedEvent>
3534
*/
3635
final class GroupShareRecipientType extends AShareRecipientTypeSearchCollaborator implements IEventListener {
37-
private ?IGroupManager $groupManager = null;
38-
3936
public function __construct(
4037
IEventDispatcher $eventDispatcher,
4138
private readonly IDBConnection $dbConnection,
39+
private readonly IGroupManager $groupManager,
4240
private readonly ISharingManager $manager,
4341
) {
4442
$eventDispatcher->addServiceListener(GroupDeletedEvent::class, self::class);
4543
}
4644

47-
private function getGroupManager(): IGroupManager {
48-
return $this->groupManager ??= Server::get(IGroupManager::class);
49-
}
50-
5145
#[\Override]
5246
public function getDisplayName(IFactory $l10nFactory): string {
5347
return $l10nFactory->get(Application::APP_ID)->t('Group');
5448
}
5549

5650
#[\Override]
5751
public function validateRecipient(string $recipient): bool {
58-
return $this->getGroupManager()->groupExists($recipient);
52+
return $this->groupManager->groupExists($recipient);
5953
}
6054

6155
#[\Override]
@@ -64,12 +58,12 @@ public function getRecipients(?IUser $currentUser, mixed $arguments): array {
6458
return [];
6559
}
6660

67-
return $this->getGroupManager()->getUserGroupIds($currentUser);
61+
return $this->groupManager->getUserGroupIds($currentUser);
6862
}
6963

7064
#[\Override]
7165
public function getRecipientDisplayName(string $recipient): ?string {
72-
$displayName = $this->getGroupManager()->getDisplayName($recipient);
66+
$displayName = $this->groupManager->getDisplayName($recipient);
7367
if ($displayName === '') {
7468
return null;
7569
}

core/Sharing/Recipient/TeamShareRecipientType.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use OCP\Interaction\Receivers\CircleReceiver;
2121
use OCP\IUser;
2222
use OCP\L10N\IFactory;
23-
use OCP\Server;
2423
use OCP\Share\IShare;
2524
use OCP\Sharing\Icon\ShareIconSVG;
2625
use OCP\Sharing\Icon\ShareIconURL;
@@ -35,28 +34,23 @@
3534
* @template-implements IEventListener<DestroyingCircleEvent>
3635
*/
3736
final class TeamShareRecipientType extends AShareRecipientTypeSearchCollaborator implements IEventListener {
38-
private ?ITeamManager $teamManager = null;
39-
4037
public function __construct(
4138
IEventDispatcher $eventDispatcher,
4239
private readonly IDBConnection $dbConnection,
40+
private readonly ITeamManager $teamManager,
4341
private readonly ISharingManager $manager,
4442
) {
4543
$eventDispatcher->addServiceListener(DestroyingCircleEvent::class, self::class);
4644
}
4745

48-
private function getTeamManager(): ITeamManager {
49-
return $this->teamManager ??= Server::get(ITeamManager::class);
50-
}
51-
5246
#[\Override]
5347
public function getDisplayName(IFactory $l10nFactory): string {
5448
return $l10nFactory->get(Application::APP_ID)->t('Team');
5549
}
5650

5751
#[\Override]
5852
public function validateRecipient(string $recipient): bool {
59-
return $this->getTeamManager()->getTeam($recipient) instanceof Team;
53+
return $this->teamManager->getTeam($recipient) instanceof Team;
6054
}
6155

6256
#[\Override]
@@ -65,12 +59,12 @@ public function getRecipients(?IUser $currentUser, mixed $arguments): array {
6559
return [];
6660
}
6761

68-
return array_map(static fn (Team $team): string => $team->getId(), $this->getTeamManager()->getTeamsForUser($currentUser->getUID()));
62+
return array_map(static fn (Team $team): string => $team->getId(), $this->teamManager->getTeamsForUser($currentUser->getUID()));
6963
}
7064

7165
#[\Override]
7266
public function getRecipientDisplayName(string $recipient): ?string {
73-
return $this->getTeamManager()->getTeam($recipient)?->getDisplayName();
67+
return $this->teamManager->getTeam($recipient)?->getDisplayName();
7468
}
7569

7670
#[\Override]

core/Sharing/Recipient/TokenShareRecipientType.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,17 @@
1414
use OCP\Interaction\Receivers\LinkReceiver;
1515
use OCP\IUser;
1616
use OCP\L10N\IFactory;
17-
use OCP\Server;
1817
use OCP\Share\IManager;
1918
use OCP\Sharing\Icon\ShareIconSVG;
2019
use OCP\Sharing\Icon\ShareIconURL;
2120
use OCP\Sharing\Recipient\IShareRecipientType;
2221
use OCP\Sharing\Recipient\IShareRecipientTypePublicSecret;
2322

2423
// TODO: Rename to Link?
25-
final class TokenShareRecipientType implements IShareRecipientType, IShareRecipientTypePublicSecret {
26-
private ?IManager $legacyManager = null;
27-
28-
private function getLegacyManager(): IManager {
29-
return $this->legacyManager ??= Server::get(IManager::class);
24+
final readonly class TokenShareRecipientType implements IShareRecipientType, IShareRecipientTypePublicSecret {
25+
public function __construct(
26+
private IManager $legacyManager,
27+
) {
3028
}
3129

3230
#[\Override]
@@ -66,6 +64,6 @@ public function isSecretPublic(string $recipient): bool {
6664

6765
#[\Override]
6866
public function isSecretUpdatable(string $recipient): bool {
69-
return $this->getLegacyManager()->allowCustomTokens();
67+
return $this->legacyManager->allowCustomTokens();
7068
}
7169
}

0 commit comments

Comments
 (0)