Skip to content

Commit 215fd9f

Browse files
authored
Merge pull request #3 from binsky08/fix/834-private
General backend type safety improvements and php code modernization
2 parents 39d7434 + 58265f3 commit 215fd9f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+651
-631
lines changed

.editorconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
2+
# SPDX-License-Identifier: AGPL-3.0-or-later
3+
4+
[*]
5+
charset = utf-8
6+
indent_style = tab
7+
end_of_line = lf
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[{package.json,webpack.config.js}]
12+
indent_style = space
13+
indent_size = 2

appinfo/routes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
['name' => 'Icon#getLocalIconList', 'url' => '/api/v2/icon/list', 'verb' => 'GET'],
8989

9090
//
91-
['name' => 'Vault#preflighted_cors', 'url' => '/api/v2/{path}', 'verb' => 'OPTIONS', 'requirements' => array('path' => '.+')],
91+
['name' => 'Vault#preflighted_cors', 'url' => '/api/v2/{path}', 'verb' => 'OPTIONS', 'requirements' => ['path' => '.+']],
9292
//Internal API
9393
['name' => 'Internal#remind', 'url' => '/api/internal/notifications/remind/{credential_id}', 'verb' => 'POST'],
9494
['name' => 'Internal#read', 'url' => '/api/internal/notifications/read/{credential_id}', 'verb' => 'DELETE'],

composer.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,12 @@
66
"email": "brantje@gmail.com"
77
}
88
],
9-
"require": {}
9+
"license": "AGPL-3.0-or-later",
10+
"scripts": {
11+
"rector:check": "rector --dry-run --clear-cache",
12+
"rector:fix": "rector"
13+
},
14+
"require-dev": {
15+
"rector/rector": "^2.1"
16+
}
1017
}

lib/Activity.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -227,18 +227,15 @@ public function getSpecialParameterList($app, $text) {
227227
* @param string $type
228228
* @return string|false
229229
*/
230-
public function getTypeIcon($type) {
231-
switch ($type) {
232-
case self::TYPE_ITEM_ACTION:
233-
case self::TYPE_ITEM_EXPIRED:
234-
return 'icon-password';
235-
case self::TYPE_ITEM_SHARED:
236-
return 'icon-share';
237-
case self::TYPE_ITEM_RENAMED:
238-
return 'icon-rename';
239-
}
240-
return false;
241-
}
230+
public function getTypeIcon($type)
231+
{
232+
return match ($type) {
233+
self::TYPE_ITEM_ACTION, self::TYPE_ITEM_EXPIRED => 'icon-password',
234+
self::TYPE_ITEM_SHARED => 'icon-share',
235+
self::TYPE_ITEM_RENAMED => 'icon-rename',
236+
default => false,
237+
};
238+
}
242239

243240
/**
244241
* The extension can define the parameter grouping by returning the index as integer.

lib/AppInfo/Application.php

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

8585
$context->registerSearchProvider(Provider::class);
8686

87-
$context->registerService(View::class, function () {
88-
return new View('');
89-
}, false);
87+
$context->registerService(View::class, fn() => new View(''), false);
9088

91-
$context->registerService('isCLI', function () {
92-
return \OC::$CLI;
93-
});
89+
$context->registerService('isCLI', fn() => \OC::$CLI);
9490

9591
$context->registerMiddleware(ShareMiddleware::class);
9692
$context->registerMiddleware(APIMiddleware::class);
@@ -121,20 +117,16 @@ public function register(IRegistrationContext $context): void {
121117
});
122118

123119

124-
$context->registerService('CronService', function (ContainerInterface $c) {
125-
return new CronService(
120+
$context->registerService('CronService', fn(ContainerInterface $c) => new CronService(
126121
$c->get(CredentialService::class),
127122
$c->get(LoggerInterface::class),
128123
$c->get(Utils::class),
129124
$c->get(NotificationService::class),
130125
$c->get(ActivityService::class),
131126
$c->get(IDBConnection::class)
132-
);
133-
});
127+
));
134128

135-
$context->registerService('Logger', function (ContainerInterface $c) {
136-
return $c->get(ServerContainer::class)->getLogger();
137-
});
129+
$context->registerService('Logger', fn(ContainerInterface $c) => $c->get(ServerContainer::class)->getLogger());
138130
}
139131

140132
public function boot(IBootContext $context): void {

lib/BackgroundJob/ExpireCredentials.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class ExpireCredentials extends TimedJob {
4545
public function __construct(
4646
ITimeFactory $timeFactory,
4747
protected IConfig $config,
48-
private CronService $cronService,
48+
private readonly CronService $cronService,
4949
) {
5050
parent::__construct($timeFactory);
5151

lib/Controller/AdminController.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,28 @@
2222
use OCA\PassmanNext\Utility\Utils;
2323
use OCP\AppFramework\ApiController;
2424
use OCP\AppFramework\Http\JSONResponse;
25-
use OCP\IConfig;
2625
use OCP\IRequest;
2726
use OCP\IUserManager;
2827

2928

3029
class AdminController extends ApiController {
31-
private $userId;
32-
3330
public function __construct(
3431
$AppName,
3532
IRequest $request,
36-
$UserId,
37-
private VaultService $vaultService,
38-
private CredentialService $credentialService,
39-
private FileService $fileService,
40-
private CredentialRevisionService $revisionService,
41-
private DeleteVaultRequestService $deleteVaultRequestService,
42-
private IConfig $config,
43-
private IUserManager $userManager,
33+
private $userId,
34+
private readonly VaultService $vaultService,
35+
private readonly CredentialService $credentialService,
36+
private readonly FileService $fileService,
37+
private readonly CredentialRevisionService $revisionService,
38+
private readonly DeleteVaultRequestService $deleteVaultRequestService,
39+
private readonly IUserManager $userManager,
4440
) {
4541
parent::__construct(
4642
$AppName,
4743
$request,
4844
'GET, POST, DELETE, PUT, PATCH, OPTIONS',
4945
'Authorization, Content-Type, Accept',
5046
86400);
51-
$this->userId = $UserId;
5247

5348
}
5449

@@ -121,7 +116,7 @@ public function acceptRequestDeletion($vault_guid, $requested_by){
121116
$req = $this->deleteVaultRequestService->getDeleteRequestForVault($vault_guid);
122117
try{
123118
$vault = $this->vaultService->getByGuid($vault_guid, $requested_by);
124-
} catch (\Exception $e){
119+
} catch (\Exception){
125120
//Ignore
126121
}
127122

@@ -175,7 +170,7 @@ public function deleteRequestDeletion($vault_guid) {
175170
$result = false;
176171
try {
177172
$delete_request = $this->deleteVaultRequestService->getDeleteRequestForVault($vault_guid);
178-
} catch (\Exception $exception){
173+
} catch (\Exception){
179174
// Ignore it
180175
}
181176

lib/Controller/CredentialController.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,22 @@
2828

2929

3030
class CredentialController extends ApiController {
31-
private $userId;
32-
3331
public function __construct(
3432
$AppName,
3533
IRequest $request,
36-
$userId,
37-
private CredentialService $credentialService,
38-
private ActivityService $activityService,
39-
private CredentialRevisionService $credentialRevisionService,
40-
private ShareService $sharingService,
41-
private SettingsService $settings,
34+
private $userId,
35+
private readonly CredentialService $credentialService,
36+
private readonly ActivityService $activityService,
37+
private readonly CredentialRevisionService $credentialRevisionService,
38+
private readonly ShareService $sharingService,
39+
private readonly SettingsService $settings,
4240
) {
4341
parent::__construct(
4442
$AppName,
4543
$request,
4644
'GET, POST, DELETE, PUT, PATCH, OPTIONS',
4745
'Authorization, Content-Type, Accept',
4846
86400);
49-
$this->userId = $userId;
5047
}
5148

5249

@@ -146,8 +143,8 @@ public function updateCredential($changed, $created,
146143

147144

148145
if (!hash_equals($storedCredential->getUserId(), $this->userId)) {
149-
$acl = $this->sharingService->getCredentialAclForUser($this->userId, $storedCredential->getGuid());
150-
if ($acl->hasPermission(SharingACL::WRITE)) {
146+
$sharingAcl = $this->sharingService->getACL($this->userId, $storedCredential->getGuid());
147+
if ($sharingAcl->hasPermission(SharingACL::WRITE)) {
151148
$credential['shared_key'] = $storedCredential->getSharedKey();
152149
} else {
153150
return new DataResponse(['msg' => 'Not authorized'], Http::STATUS_UNAUTHORIZED);
@@ -156,7 +153,7 @@ public function updateCredential($changed, $created,
156153
return new DataResponse(['msg' => 'Not authorized'], Http::STATUS_UNAUTHORIZED);
157154
}
158155

159-
if (!$acl->hasPermission(SharingACL::FILES)) {
156+
if (!$sharingAcl->hasPermission(SharingACL::FILES)) {
160157
// what ever the client transmitted, if it has no files permission, the previous files content will be preserved
161158
$credential['files'] = $storedCredential->getFiles();
162159
}
@@ -199,7 +196,7 @@ public function updateCredential($changed, $created,
199196

200197
try {
201198
$acl_list = $this->sharingService->getCredentialAclList($storedCredential->getGuid());
202-
} catch (\Exception $exception) {
199+
} catch (\Exception) {
203200
// Just check if we have an acl list
204201
}
205202
if (!empty($acl_list)) {
@@ -264,7 +261,7 @@ public function updateCredential($changed, $created,
264261
public function deleteCredential($credential_guid) {
265262
try {
266263
$credential = $this->credentialService->getCredentialByGUID($credential_guid, $this->userId);
267-
} catch (\Exception $e) {
264+
} catch (\Exception) {
268265
return new NotFoundJSONResponse();
269266
}
270267
if ($credential instanceof Credential) {
@@ -285,7 +282,7 @@ public function deleteCredential($credential_guid) {
285282
public function getRevision($credential_guid) {
286283
try {
287284
$credential = $this->credentialService->getCredentialByGUID($credential_guid);
288-
} catch (\Exception $ex) {
285+
} catch (\Exception) {
289286
return new NotFoundJSONResponse();
290287
}
291288
// If the request was made by the owner of the credential
@@ -320,7 +317,7 @@ public function updateRevision($revision_id, $credential_data) {
320317
$revision = null;
321318
try {
322319
$revision = $this->credentialRevisionService->getRevision($revision_id);
323-
} catch (\Exception $exception) {
320+
} catch (\Exception) {
324321
return new JSONResponse([]);
325322
}
326323

lib/Controller/FileController.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,19 @@
1818
use Psr\Log\LoggerInterface;
1919

2020
class FileController extends ApiController {
21-
private $userId;
22-
2321
public function __construct(
2422
$AppName,
2523
IRequest $request,
26-
$UserId,
27-
private FileService $fileService,
28-
private LoggerInterface $logger,
24+
private $userId,
25+
private readonly FileService $fileService,
26+
private readonly LoggerInterface $logger,
2927
) {
3028
parent::__construct(
3129
$AppName,
3230
$request,
3331
'GET, POST, DELETE, PUT, PATCH, OPTIONS',
3432
'Authorization, Content-Type, Accept',
3533
86400);
36-
$this->userId = $UserId;
3734
}
3835

3936

@@ -72,9 +69,9 @@ public function deleteFile($file_id) {
7269
* @NoAdminRequired
7370
* @NoCSRFRequired
7471
*/
75-
public function deleteFiles($file_ids) {
72+
public function deleteFiles(string $file_ids) {
7673
$failed_file_ids = [];
77-
if ($file_ids != null && !empty($file_ids)) {
74+
if (!empty($file_ids)) {
7875
$decoded_file_ids = json_decode($file_ids);
7976
foreach ($decoded_file_ids as $file_id) {
8077
try {
@@ -97,8 +94,8 @@ public function deleteFiles($file_ids) {
9794
public function updateFile($file_id, $file_data, $filename) {
9895
try {
9996
$file = $this->fileService->getFile($file_id, $this->userId);
100-
} catch (\Exception $doesNotExistException) {
101-
97+
} catch (\Exception) {
98+
// do just nothing when having problems getting the requested file
10299
}
103100
if ($file) {
104101
if ($file_data) {

lib/Controller/IconController.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,22 @@
2424
use OCP\IURLGenerator;
2525

2626
class IconController extends ApiController {
27-
private $userId;
2827
const ICON_CACHE_OFFSET = 2592000; // 3600 * 24 * 30
2928

3029
public function __construct(
3130
$AppName,
3231
IRequest $request,
33-
$UserId,
34-
private CredentialService $credentialService,
35-
private AppManager $am,
36-
private IURLGenerator $urlGenerator,
32+
private $userId,
33+
private readonly CredentialService $credentialService,
34+
private readonly AppManager $am,
35+
private readonly IURLGenerator $urlGenerator,
3736
) {
3837
parent::__construct(
3938
$AppName,
4039
$request,
4140
'GET, POST, DELETE, PUT, PATCH, OPTIONS',
4241
'Authorization, Content-Type, Accept',
4342
86400);
44-
$this->userId = $UserId;
45-
4643
}
4744

4845
/**
@@ -78,7 +75,7 @@ public function getIcon($base64Url, $credentialId) {
7875
try {
7976
$credential = $this->credentialService->getCredentialById($credentialId, $this->userId);
8077
$credential = $credential->jsonSerialize();
81-
} catch (DoesNotExistException $e) {
78+
} catch (DoesNotExistException) {
8279
// Credential is not found, continue
8380
$credential = false;
8481
}
@@ -97,22 +94,22 @@ public function getIcon($base64Url, $credentialId) {
9794
$data = $icon->icoData;
9895
$type = $icon->icoType;
9996
}
100-
} catch (\InvalidArgumentException $e) {
97+
} catch (\InvalidArgumentException) {
10198
//no need to do stuff in catch
10299
//if IconService fails the predefined $data and $type are used
103100
}
104101

105102
if (isset($credential) && $credential['user_id'] == $this->userId) {
106103
$iconData = [
107-
'type' => ($type) ? $type : 'x-icon',
104+
'type' => $type ?: 'x-icon',
108105
'content' => base64_encode($data)
109106
];
110107
$credential['icon'] = json_encode($iconData);
111108
try {
112109
if ($credential) {
113110
$this->credentialService->updateCredential($credential);
114111
}
115-
} catch (DriverException $exception) {
112+
} catch (DriverException) {
116113
/**
117114
* @FIXME Syntax error or access violation: 1118 Row size too large
118115
* This happens when favicons are quite big.
@@ -146,7 +143,7 @@ public function getLocalIconList() {
146143
$icons = [];
147144
foreach ($result as $icon) {
148145
$iconPath = $icon;
149-
$path = explode('passman/', $iconPath);
146+
$path = explode('passman/', (string) $iconPath);
150147
$pack = explode('/', $path[1])[2];
151148
$mime = mime_content_type($iconPath);
152149
if ($mime !== 'directory') {

0 commit comments

Comments
 (0)