Skip to content

Commit e04484d

Browse files
authored
Merge pull request #305 from nextcloud/nextcloud33
Use lazy loading for config and update to nextcloud 33
2 parents 10b669b + a528ddc commit e04484d

14 files changed

Lines changed: 166 additions & 263 deletions

appinfo/info.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<name>Google integration</name>
55
<summary>Import Google data into Nextcloud</summary>
66
<description><![CDATA[Google integration allows you to automatically migrate your Google calendars, contacts, and files into Nextcloud.]]></description>
7-
<version>4.2.0</version>
7+
<version>4.3.0</version>
88
<licence>agpl</licence>
99
<author>Julien Veyssier</author>
1010
<namespace>Google</namespace>
@@ -16,7 +16,7 @@
1616
<bugs>https://github.com/nextcloud/integration_google/issues</bugs>
1717
<screenshot>https://github.com/nextcloud/integration_google/raw/master/img/screenshot1.jpg</screenshot>
1818
<dependencies>
19-
<nextcloud min-version="32" max-version="32"/>
19+
<nextcloud min-version="32" max-version="33"/>
2020
</dependencies>
2121
<settings>
2222
<admin>OCA\Google\Settings\Admin</admin>

lib/Controller/ConfigController.php

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
use OCP\AppFramework\Http\DataResponse;
2424
use OCP\AppFramework\Http\RedirectResponse;
2525
use OCP\AppFramework\Http\TemplateResponse;
26+
use OCP\AppFramework\Services\IAppConfig;
2627
use OCP\AppFramework\Services\IInitialState;
28+
use OCP\Config\IUserConfig;
2729
use OCP\Constants;
2830
use OCP\Contacts\IManager as IContactManager;
29-
use OCP\IConfig;
3031
use OCP\IL10N;
3132
use OCP\IRequest;
3233
use OCP\IURLGenerator;
@@ -44,7 +45,8 @@ class ConfigController extends Controller {
4445
public function __construct(
4546
string $appName,
4647
IRequest $request,
47-
private IConfig $config,
48+
private IAppConfig $appConfig,
49+
private IUserConfig $userConfig,
4850
private IURLGenerator $urlGenerator,
4951
private IL10N $l,
5052
private IContactManager $contactsManager,
@@ -70,16 +72,16 @@ public function setConfig(array $values): DataResponse {
7072
return new DataResponse([], Http::STATUS_BAD_REQUEST);
7173
}
7274
foreach ($values as $key => $value) {
73-
$this->config->setUserValue($this->userId, Application::APP_ID, $key, $value);
75+
$this->userConfig->setValueString($this->userId, Application::APP_ID, $key, $value, lazy: true);
7476
}
7577
$result = [];
7678

7779
if (isset($values['user_name']) && $values['user_name'] === '') {
78-
$this->config->deleteUserValue($this->userId, Application::APP_ID, 'user_id');
79-
$this->config->deleteUserValue($this->userId, Application::APP_ID, 'user_name');
80-
$this->config->deleteUserValue($this->userId, Application::APP_ID, 'refresh_token');
81-
$this->config->deleteUserValue($this->userId, Application::APP_ID, 'token_expires_at');
82-
$this->config->deleteUserValue($this->userId, Application::APP_ID, 'token');
80+
$this->userConfig->deleteUserConfig($this->userId, Application::APP_ID, 'user_id');
81+
$this->userConfig->deleteUserConfig($this->userId, Application::APP_ID, 'user_name');
82+
$this->userConfig->deleteUserConfig($this->userId, Application::APP_ID, 'refresh_token');
83+
$this->userConfig->deleteUserConfig($this->userId, Application::APP_ID, 'token_expires_at');
84+
$this->userConfig->deleteUserConfig($this->userId, Application::APP_ID, 'token');
8385
$result['user_name'] = '';
8486
} else {
8587
if (isset($values['drive_output_dir'])) {
@@ -106,10 +108,8 @@ public function setAdminConfig(array $values): DataResponse {
106108
if ($key === 'client_secret' && $value === 'dummySecret') {
107109
continue;
108110
}
109-
if (in_array($key, ['client_secret', 'client_id'], true) && $value !== '') {
110-
$value = $this->crypto->encrypt($value);
111-
}
112-
$this->config->setAppValue(Application::APP_ID, $key, $value);
111+
$sensitive = in_array($key, ['client_secret', 'client_id'], true);
112+
$this->appConfig->setAppValueString($key, $value, lazy: true, sensitive: $sensitive);
113113
}
114114
return new DataResponse(1);
115115
}
@@ -172,9 +172,9 @@ public function oauthRedirect(string $code = '', string $state = '', string $sco
172172
);
173173
}
174174

175-
$configState = $this->config->getUserValue($this->userId, Application::APP_ID, 'oauth_state');
176-
$clientID = $this->secretService->getEncryptedAppValue('client_id');
177-
$clientSecret = $this->secretService->getEncryptedAppValue('client_secret');
175+
$configState = $this->userConfig->getValueString($this->userId, Application::APP_ID, 'oauth_state', lazy: true);
176+
$clientID = $this->appConfig->getAppValueString('client_id', lazy: true);
177+
$clientSecret = $this->appConfig->getAppValueString('client_secret', lazy: true);
178178

179179
// Store given scopes in space-separated string
180180
$scopes = explode(' ', $scope);
@@ -186,13 +186,13 @@ public function oauthRedirect(string $code = '', string $state = '', string $sco
186186
'can_access_calendar' => (in_array(self::CALENDAR_SCOPE, $scopes) && in_array(self::CALENDAR_EVENTS_SCOPE, $scopes)) ? 1 : 0,
187187
];
188188

189-
$this->config->setUserValue($this->userId, Application::APP_ID, 'user_scopes', json_encode($scopesArray));
189+
$this->userConfig->setValueString($this->userId, Application::APP_ID, 'user_scopes', json_encode($scopesArray), lazy: true);
190190

191191
// anyway, reset state
192-
$this->config->setUserValue($this->userId, Application::APP_ID, 'oauth_state', '');
192+
$this->userConfig->setValueString($this->userId, Application::APP_ID, 'oauth_state', '', lazy: true);
193193

194194
if ($clientID && $clientSecret && $configState !== '' && $configState === $state) {
195-
$redirect_uri = $this->config->getUserValue($this->userId, Application::APP_ID, 'redirect_uri');
195+
$redirect_uri = $this->userConfig->getValueString($this->userId, Application::APP_ID, 'redirect_uri', lazy: true);
196196
/** @var array{access_token?:string, refresh_token?:string, expires_in?:string, error?:string} $result */
197197
$result = $this->googleApiService->requestOAuthAccessToken([
198198
'client_id' => $clientID,
@@ -207,12 +207,12 @@ public function oauthRedirect(string $code = '', string $state = '', string $sco
207207
if (isset($result['expires_in'])) {
208208
$nowTs = (new DateTime())->getTimestamp();
209209
$expiresAt = $nowTs + (int)$result['expires_in'];
210-
$this->config->setUserValue($this->userId, Application::APP_ID, 'token_expires_at', (string)$expiresAt);
210+
$this->userConfig->setValueInt($this->userId, Application::APP_ID, 'token_expires_at', $expiresAt, lazy: true);
211211
}
212212
$this->secretService->setEncryptedUserValue($this->userId, 'token', $accessToken);
213213
$this->secretService->setEncryptedUserValue($this->userId, 'refresh_token', $refreshToken);
214214
$username = $this->storeUserInfo();
215-
$usePopup = $this->config->getAppValue(Application::APP_ID, 'use_popup', '0') === '1';
215+
$usePopup = $this->appConfig->getAppValueString('use_popup', '0', lazy: true) === '1';
216216
if ($usePopup) {
217217
return new RedirectResponse(
218218
$this->urlGenerator->linkToRoute('integration_google.config.popupSuccessPage', ['username' => $username])
@@ -248,12 +248,12 @@ private function storeUserInfo(): string {
248248
/** @var array{id?:string, name?:string} $info */
249249
$info = $this->googleApiService->request($this->userId, 'oauth2/v1/userinfo', ['alt' => 'json']);
250250
if (isset($info['name'], $info['id'])) {
251-
$this->config->setUserValue($this->userId, Application::APP_ID, 'user_id', $info['id']);
252-
$this->config->setUserValue($this->userId, Application::APP_ID, 'user_name', $info['name']);
251+
$this->userConfig->setValueString($this->userId, Application::APP_ID, 'user_id', $info['id'], lazy: true);
252+
$this->userConfig->setValueString($this->userId, Application::APP_ID, 'user_name', $info['name'], lazy: true);
253253
return $info['name'];
254254
} else {
255-
$this->config->setUserValue($this->userId, Application::APP_ID, 'user_id', '');
256-
$this->config->setUserValue($this->userId, Application::APP_ID, 'user_name', '');
255+
$this->userConfig->setValueString($this->userId, Application::APP_ID, 'user_id', '', lazy: true);
256+
$this->userConfig->setValueString($this->userId, Application::APP_ID, 'user_name', '', lazy: true);
257257
return '';
258258
}
259259
}

lib/Controller/GoogleAPIController.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use OCA\Google\Service\SecretService;
2020
use OCP\AppFramework\Controller;
2121
use OCP\AppFramework\Http\DataResponse;
22-
use OCP\IConfig;
22+
use OCP\Config\IUserConfig;
2323
use OCP\IRequest;
2424

2525
class GoogleAPIController extends Controller {
@@ -29,7 +29,7 @@ class GoogleAPIController extends Controller {
2929
public function __construct(
3030
string $appName,
3131
IRequest $request,
32-
private IConfig $config,
32+
private IUserConfig $userConfig,
3333
private GoogleContactsAPIService $googleContactsAPIService,
3434
private GoogleDriveAPIService $googleDriveAPIService,
3535
private GoogleCalendarAPIService $googleCalendarAPIService,
@@ -50,10 +50,10 @@ public function getImportDriveInformation(): DataResponse {
5050
return new DataResponse([], 400);
5151
}
5252
return new DataResponse([
53-
'importing_drive' => $this->config->getUserValue($this->userId, Application::APP_ID, 'importing_drive') === '1',
54-
'last_drive_import_timestamp' => (int)$this->config->getUserValue($this->userId, Application::APP_ID, 'last_drive_import_timestamp', '0'),
55-
'nb_imported_files' => (int)$this->config->getUserValue($this->userId, Application::APP_ID, 'nb_imported_files', '0'),
56-
'drive_imported_size' => (int)$this->config->getUserValue($this->userId, Application::APP_ID, 'drive_imported_size', '0'),
53+
'importing_drive' => $this->userConfig->getValueString($this->userId, Application::APP_ID, 'importing_drive', lazy: true) === '1',
54+
'last_drive_import_timestamp' => $this->userConfig->getValueInt($this->userId, Application::APP_ID, 'last_drive_import_timestamp', lazy: true),
55+
'nb_imported_files' => $this->userConfig->getValueInt($this->userId, Application::APP_ID, 'nb_imported_files', lazy: true),
56+
'drive_imported_size' => $this->userConfig->getValueInt($this->userId, Application::APP_ID, 'drive_imported_size', lazy: true),
5757
]);
5858
}
5959

lib/Migration/Version03001001Date20241111105515.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
use Closure;
1212
use OCA\Google\AppInfo\Application;
13+
use OCP\AppFramework\Services\IAppConfig;
1314
use OCP\DB\QueryBuilder\IQueryBuilder;
14-
use OCP\IConfig;
1515
use OCP\IDBConnection;
1616
use OCP\Migration\IOutput;
1717
use OCP\Migration\SimpleMigrationStep;
@@ -20,7 +20,7 @@
2020
class Version03001001Date20241111105515 extends SimpleMigrationStep {
2121

2222
public function __construct(
23-
private IConfig $config,
23+
private IAppConfig $appConfig,
2424
private IDBConnection $connection,
2525
private ICrypto $crypto,
2626
) {
@@ -34,11 +34,11 @@ public function __construct(
3434
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
3535
// migrate api credentials in app config
3636
foreach (['client_id', 'client_secret'] as $key) {
37-
$value = $this->config->getAppValue(Application::APP_ID, $key);
37+
$value = $this->appConfig->getAppValueString($key, '');
3838
if ($value === '') {
3939
continue;
4040
}
41-
$this->config->setAppValue(Application::APP_ID, $key, $this->crypto->encrypt($value));
41+
$this->appConfig->setAppValueString($key, $this->crypto->encrypt($value));
4242
}
4343

4444
// user tokens
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
namespace OCA\Google\Migration;
10+
11+
use Closure;
12+
use OCP\AppFramework\Services\IAppConfig;
13+
use OCP\Migration\IOutput;
14+
use OCP\Migration\SimpleMigrationStep;
15+
use OCP\Security\ICrypto;
16+
17+
class Version04003001Date20251022113940 extends SimpleMigrationStep {
18+
19+
public function __construct(
20+
private IAppConfig $appConfig,
21+
private ICrypto $crypto,
22+
) {
23+
}
24+
25+
/**
26+
* @param IOutput $output
27+
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
28+
* @param array $options
29+
*/
30+
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
31+
// migrate api credentials in app config to sensitive instead of manually encrypting and decrypting
32+
foreach (['client_id', 'client_secret'] as $key) {
33+
$value = $this->appConfig->getAppValueString($key, lazy: true);
34+
if ($value === '') {
35+
continue;
36+
}
37+
$value = $this->crypto->decrypt($value);
38+
$this->appConfig->setAppValueString($key, $value, lazy: true, sensitive: true);
39+
}
40+
}
41+
}

lib/Service/GoogleAPIService.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
use GuzzleHttp\Exception\ConnectException;
1919
use GuzzleHttp\Exception\ServerException;
2020
use OCA\Google\AppInfo\Application;
21+
use OCP\AppFramework\Services\IAppConfig;
22+
use OCP\Config\IUserConfig;
2123
use OCP\Http\Client\IClientService;
2224
use OCP\Http\Client\IResponse;
23-
use OCP\IConfig;
2425
use OCP\IL10N;
2526
use OCP\Notification\IManager as INotificationManager;
2627
use Psr\Log\LoggerInterface;
@@ -37,7 +38,8 @@ public function __construct(
3738
string $appName,
3839
private LoggerInterface $logger,
3940
private IL10N $l10n,
40-
private IConfig $config,
41+
private IAppConfig $appConfig,
42+
private IUserConfig $userConfig,
4143
private INotificationManager $notificationManager,
4244
IClientService $clientService,
4345
private SecretService $secretService,
@@ -353,10 +355,9 @@ public function simpleDownload(string $userId, string $url, $resource, array $pa
353355

354356
private function checkTokenExpiration(string $userId): void {
355357
$refreshToken = $this->secretService->getEncryptedUserValue($userId, 'refresh_token');
356-
$expireAt = $this->config->getUserValue($userId, Application::APP_ID, 'token_expires_at');
357-
if ($refreshToken !== '' && $expireAt !== '') {
358+
$expireAt = $this->userConfig->getValueInt($userId, Application::APP_ID, 'token_expires_at', lazy: true);
359+
if ($refreshToken !== '' && $expireAt !== 0) {
358360
$nowTs = (new DateTime())->getTimestamp();
359-
$expireAt = (int)$expireAt;
360361
// if token expires in less than 2 minutes or has already expired
361362
if ($nowTs > $expireAt - 120) {
362363
$this->refreshToken($userId);
@@ -367,8 +368,8 @@ private function checkTokenExpiration(string $userId): void {
367368
public function refreshToken(string $userId): array {
368369
$this->logger->debug('Trying to REFRESH the access token', ['app' => Application::APP_ID]);
369370
$refreshToken = $this->secretService->getEncryptedUserValue($userId, 'refresh_token');
370-
$clientID = $this->secretService->getEncryptedAppValue('client_id');
371-
$clientSecret = $this->secretService->getEncryptedAppValue('client_secret');
371+
$clientID = $this->appConfig->getAppValueString('client_id', lazy: true);
372+
$clientSecret = $this->appConfig->getAppValueString('client_secret', lazy: true);
372373
$result = $this->requestOAuthAccessToken([
373374
'client_id' => $clientID,
374375
'client_secret' => $clientSecret,
@@ -382,7 +383,7 @@ public function refreshToken(string $userId): array {
382383
if (isset($result['expires_in'])) {
383384
$nowTs = (new DateTime())->getTimestamp();
384385
$expiresAt = $nowTs + (int)$result['expires_in'];
385-
$this->config->setUserValue($userId, Application::APP_ID, 'token_expires_at', $expiresAt);
386+
$this->userConfig->setValueInt($userId, Application::APP_ID, 'token_expires_at', $expiresAt, lazy: true);
386387
}
387388
} else {
388389
$responseTxt = json_encode($result);

lib/Service/GoogleCalendarAPIService.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use Generator;
1919
use OCA\DAV\CalDAV\CalDavBackend;
2020
use OCA\Google\AppInfo\Application;
21-
use OCP\IConfig;
21+
use OCP\Config\IUserConfig;
2222
use OCP\IL10N;
2323
use Ortic\ColorConverter\Color;
2424
use Ortic\ColorConverter\Colors\Named;
@@ -45,7 +45,7 @@ public function __construct(
4545
private IL10N $l10n,
4646
private CalDavBackend $caldavBackend,
4747
private GoogleAPIService $googleApiService,
48-
private IConfig $config,
48+
private IUserConfig $userConfig,
4949
) {
5050
$this->utcTimezone = new DateTimeZone('-0000');
5151
}
@@ -330,7 +330,7 @@ public function importCalendar(string $userId, string $calId, string $calName, ?
330330
}
331331

332332
date_default_timezone_set('UTC');
333-
$allEvents = $this->config->getUserValue($userId, Application::APP_ID, 'consider_all_events', '1') === '1';
333+
$allEvents = $this->userConfig->getValueString($userId, Application::APP_ID, 'consider_all_events', '1', lazy: true) === '1';
334334
$eventsGenerator = $this->getCalendarEvents($userId, $calId, $allEvents);
335335

336336
// Normal events

lib/Service/GoogleContactsAPIService.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
use Generator;
1818
use OCA\DAV\CardDAV\CardDavBackend;
1919
use OCA\Google\AppInfo\Application;
20+
use OCP\Config\IUserConfig;
2021
use OCP\Contacts\IManager as IContactManager;
21-
use OCP\IConfig;
2222
use Psr\Log\LoggerInterface;
2323
use Sabre\VObject\Component\VCard;
2424
use Throwable;
@@ -34,7 +34,7 @@ public function __construct(
3434
private IContactManager $contactsManager,
3535
private CardDavBackend $cdBackend,
3636
private GoogleAPIService $googleApiService,
37-
private IConfig $config,
37+
private IUserConfig $userConfig,
3838
) {
3939
}
4040

@@ -85,7 +85,7 @@ public function getContactNumber(string $userId): array {
8585
return $contacts;
8686
}
8787
$result['nbContacts'] = $contacts['totalItems'] ?? 0;
88-
$scopes = $this->config->getUserValue($userId, Application::APP_ID, 'user_scopes', '{}');
88+
$scopes = $this->userConfig->getValueString($userId, Application::APP_ID, 'user_scopes', '{}', lazy: true);
8989
$scopes = json_decode($scopes, true);
9090
if (isset($scopes['can_access_other_contacts']) && $scopes['can_access_other_contacts'] === 1) {
9191
$params = [
@@ -205,7 +205,7 @@ public function importContacts(string $userId, ?string $uri, int $key, ?string $
205205
}
206206
$existingAddressBook = $addressBook;
207207
}
208-
$otherContacts = $this->config->getUserValue($userId, Application::APP_ID, 'consider_other_contacts', '0') === '1';
208+
$otherContacts = $this->userConfig->getValueString($userId, Application::APP_ID, 'consider_other_contacts', '0', lazy: true) === '1';
209209
$groupsById = $this->getContactGroupsById($userId);
210210
$contacts = $this->getContactList($userId, $otherContacts);
211211
$nbAdded = 0;

0 commit comments

Comments
 (0)