Skip to content

Commit e3919da

Browse files
committed
fix(external_storage): fix settings save
Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
1 parent 4efd119 commit e3919da

File tree

2 files changed

+46
-23
lines changed

2 files changed

+46
-23
lines changed

apps/files_external/lib/Controller/AjaxController.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99
use OCA\Files_External\Lib\Auth\Password\GlobalAuth;
1010
use OCA\Files_External\Lib\Auth\PublicKey\RSA;
1111
use OCP\AppFramework\Controller;
12+
use OCP\AppFramework\Http;
1213
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
1314
use OCP\AppFramework\Http\Attribute\PasswordConfirmationRequired;
1415
use OCP\AppFramework\Http\JSONResponse;
1516
use OCP\IGroupManager;
17+
use OCP\IL10N;
1618
use OCP\IRequest;
1719
use OCP\IUserSession;
20+
use Rubix\ML\Helpers\JSON;
1821

1922
class AjaxController extends Controller {
2023
/**
@@ -32,6 +35,7 @@ public function __construct(
3235
private GlobalAuth $globalAuth,
3336
private IUserSession $userSession,
3437
private IGroupManager $groupManager,
38+
private IL10N $l10n,
3539
) {
3640
parent::__construct($appName, $request);
3741
}
@@ -56,13 +60,13 @@ private function generateSshKeys($keyLength) {
5660
#[NoAdminRequired]
5761
public function getSshKeys($keyLength = 1024) {
5862
$key = $this->generateSshKeys($keyLength);
59-
return new JSONResponse(
60-
['data' => [
63+
return new JSONResponse([
64+
'data' => [
6165
'private_key' => $key['privatekey'],
6266
'public_key' => $key['publickey']
6367
],
64-
'status' => 'success'
65-
]);
68+
'status' => 'success'
69+
]);
6670
}
6771

6872
/**
@@ -73,10 +77,13 @@ public function getSshKeys($keyLength = 1024) {
7377
*/
7478
#[NoAdminRequired]
7579
#[PasswordConfirmationRequired(strict: true)]
76-
public function saveGlobalCredentials($uid, $user, $password) {
80+
public function saveGlobalCredentials($uid, $user, $password): JSONResponse {
7781
$currentUser = $this->userSession->getUser();
7882
if ($currentUser === null) {
79-
return false;
83+
return new JSONResponse([
84+
'status' => 'error',
85+
'message' => $this->l10n->t('You are not logged in'),
86+
], Http::STATUS_UNAUTHORIZED);
8087
}
8188

8289
// Non-admins can only edit their own credentials
@@ -87,9 +94,14 @@ public function saveGlobalCredentials($uid, $user, $password) {
8794

8895
if ($allowedToEdit) {
8996
$this->globalAuth->saveAuth($uid, $user, $password);
90-
return true;
97+
return new JSONResponse([
98+
'status' => 'success'
99+
]);
91100
}
92101

93-
return false;
102+
return new JSONResponse([
103+
'status' => 'success',
104+
'message' => $this->l10n->t('Permission denied'),
105+
], Http::STATUS_FORBIDDEN);
94106
}
95107
}

apps/files_external/src/settings.js

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
* SPDX-License-Identifier: AGPL-3.0-or-later
55
*/
66

7-
import axios from '@nextcloud/axios'
8-
import { t } from '@nextcloud/l10n'
97
import { addPasswordConfirmationInterceptors, PwdConfirmationMode } from '@nextcloud/password-confirmation'
8+
import { generateUrl } from '@nextcloud/router'
9+
import { showError } from '@nextcloud/dialogs'
10+
import { t } from '@nextcloud/l10n'
11+
import axios, { isAxiosError } from '@nextcloud/axios'
1012

1113
import jQuery from 'jquery'
1214

@@ -1522,21 +1524,30 @@ window.addEventListener('DOMContentLoaded', function() {
15221524
const uid = $form.find('[name=uid]').val()
15231525
const user = $form.find('[name=username]').val()
15241526
const password = $form.find('[name=password]').val()
1525-
await axios.request({
1526-
method: 'POST',
1527-
data: JSON.stringify({
1528-
uid,
1529-
user,
1530-
password,
1531-
}),
1532-
url: OC.generateUrl('apps/files_external/globalcredentials'),
1533-
confirmPassword: PwdConfirmationMode.Strict,
1534-
})
15351527

1536-
$submit.val(t('files_external', 'Saved'))
1537-
setTimeout(function() {
1528+
try {
1529+
await axios.request({
1530+
method: 'POST',
1531+
data: {
1532+
uid,
1533+
user,
1534+
password,
1535+
},
1536+
url: generateUrl('apps/files_external/globalcredentials'),
1537+
confirmPassword: PwdConfirmationMode.Strict,
1538+
})
1539+
1540+
$submit.val(t('files_external', 'Saved'))
1541+
setTimeout(function() {
1542+
$submit.val(t('files_external', 'Save'))
1543+
}, 2500)
1544+
} catch (error) {
15381545
$submit.val(t('files_external', 'Save'))
1539-
}, 2500)
1546+
if (isAxiosError(error)) {
1547+
const message = error.response?.data?.message || t('files_external', 'Failed to save global credentials')
1548+
showError(t('files_external', 'Failed to save global credentials: {message}', { message }))
1549+
}
1550+
}
15401551

15411552
return false
15421553
})

0 commit comments

Comments
 (0)