Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: Use inline password confirmation in external storage settings
Signed-off-by: Louis Chemineau <[email protected]>
  • Loading branch information
artonge committed Feb 11, 2025
commit 8a8a92b88f9d3c0452abf1bc5455265d49c0765a
2 changes: 1 addition & 1 deletion apps/files_external/lib/Controller/AjaxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function getSshKeys($keyLength = 1024) {
* @return bool
*/
#[NoAdminRequired]
#[PasswordConfirmationRequired]
#[PasswordConfirmationRequired(strict: true)]
public function saveGlobalCredentials($uid, $user, $password) {
$currentUser = $this->userSession->getUser();
if ($currentUser === null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function __construct(
*
* @return DataResponse
*/
#[PasswordConfirmationRequired]
#[PasswordConfirmationRequired(strict: true)]
public function create(
$mountPoint,
$backend,
Expand Down Expand Up @@ -157,7 +157,7 @@ public function create(
*
* @return DataResponse
*/
#[PasswordConfirmationRequired]
#[PasswordConfirmationRequired(strict: true)]
public function update(
$id,
$mountPoint,
Expand Down
2 changes: 1 addition & 1 deletion apps/files_external/lib/Controller/StoragesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public function show(int $id, $testOnly = true) {
*
* @return DataResponse
*/
#[PasswordConfirmationRequired]
#[PasswordConfirmationRequired(strict: true)]
public function destroy(int $id) {
try {
$this->service->removeStorage($id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function show($id, $testOnly = true) {
* @return DataResponse
*/
#[NoAdminRequired]
#[PasswordConfirmationRequired]
#[PasswordConfirmationRequired(strict: true)]
public function update(
$id,
$backendOptions,
Expand Down
6 changes: 3 additions & 3 deletions apps/files_external/lib/Controller/UserStoragesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function show(int $id, $testOnly = true) {
* @return DataResponse
*/
#[NoAdminRequired]
#[PasswordConfirmationRequired]
#[PasswordConfirmationRequired(strict: true)]
public function create(
$mountPoint,
$backend,
Expand Down Expand Up @@ -180,7 +180,7 @@ public function create(
* @return DataResponse
*/
#[NoAdminRequired]
#[PasswordConfirmationRequired]
#[PasswordConfirmationRequired(strict: true)]
public function update(
$id,
$mountPoint,
Expand Down Expand Up @@ -232,7 +232,7 @@ public function update(
* {@inheritdoc}
*/
#[NoAdminRequired]
#[PasswordConfirmationRequired]
#[PasswordConfirmationRequired(strict: true)]
public function destroy(int $id) {
return parent::destroy($id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
*
*/

(function(){
import axios from '@nextcloud/axios'
import { t } from '@nextcloud/l10n'
import { addPasswordConfirmationInterceptors, PwdConfirmationMode } from '@nextcloud/password-confirmation'

addPasswordConfirmationInterceptors(axios)

/**
* Returns the selection of applicable users in the given configuration row
Expand Down Expand Up @@ -278,7 +282,7 @@ StorageConfig.prototype = {
url = OC.generateUrl(this._url + '/{id}', {id: this.id});
}

window.OC.PasswordConfirmation.requirePasswordConfirmation(() => this._save(method, url, options), options.error);
this._save(method, url, options);
},

/**
Expand All @@ -287,22 +291,20 @@ StorageConfig.prototype = {
* @param {string} url
* @param {{success: Function, error: Function}} options
*/
_save: function(method, url, options) {
self = this;

$.ajax({
type: method,
url: url,
contentType: 'application/json',
data: JSON.stringify(this.getData()),
success: function(result) {
self.id = result.id;
if (_.isFunction(options.success)) {
options.success(result);
}
},
error: options.error
});
_save: async function(method, url, options) {
try {
const response = await axios.request({
confirmPassword: PwdConfirmationMode.Strict,
method: method,
url: url,
data: this.getData(),
})
const result = response.data
this.id = result.id
options.success(result)
} catch (error) {
options.error(error)
}
},

/**
Expand Down Expand Up @@ -355,7 +357,7 @@ StorageConfig.prototype = {
* @param {Function} [options.success] success callback
* @param {Function} [options.error] error callback
*/
destroy: function(options) {
destroy: async function(options) {
if (!_.isNumber(this.id)) {
// the storage hasn't even been created => success
if (_.isFunction(options.success)) {
Expand All @@ -364,20 +366,16 @@ StorageConfig.prototype = {
return;
}

window.OC.PasswordConfirmation.requirePasswordConfirmation(() => this._destroy(options), options.error)
},

/**
* Private implementation of the DELETE method called after password confirmation
* @param {{ success: Function, error: Function }} options
*/
_destroy: function(options) {
$.ajax({
type: 'DELETE',
url: OC.generateUrl(this._url + '/{id}', {id: this.id}),
success: options.success,
error: options.error
});
try {
await axios.request({
method: 'DELETE',
url: OC.generateUrl(this._url + '/{id}', {id: this.id}),
confirmPassword: PwdConfirmationMode.Strict,
})
options.success()
} catch (e) {
options.error(e)
}
},

/**
Expand Down Expand Up @@ -1490,38 +1488,32 @@ window.addEventListener('DOMContentLoaded', function() {
$allowUserMounting.trigger('change');

}
});
})

$('#global_credentials').on('submit', async function (event) {
event.preventDefault();
var $form = $(this);
var $submit = $form.find('[type=submit]');
$submit.val(t('files_external', 'Saving …'));

function _submitCredentials(success) {
var uid = $form.find('[name=uid]').val();
var user = $form.find('[name=username]').val();
var password = $form.find('[name=password]').val();
$.ajax({
type: 'POST',
contentType: 'application/json',
await axios.request({
method: 'POST',
data: JSON.stringify({
uid,
user,
password,
}),
url: OC.generateUrl('apps/files_external/globalcredentials'),
dataType: 'json',
success,
});
}

$('#global_credentials').on('submit', function() {
var $form = $(this);
var $submit = $form.find('[type=submit]');
$submit.val(t('files_external', 'Saving …'));
url: OC.generateUrl('apps/files_external/globalcredentials'),
confirmPassword: PwdConfirmationMode.Strict,
})

window.OC.PasswordConfirmation
.requirePasswordConfirmation(() => _submitCredentials(function() {
$submit.val(t('files_external', 'Saved'));
setTimeout(function(){
$submit.val(t('files_external', 'Save'));
}, 2500);
}));
$submit.val(t('files_external', 'Saved'));
setTimeout(function(){
$submit.val(t('files_external', 'Save'));
}, 2500);

return false;
});
Expand Down Expand Up @@ -1551,5 +1543,3 @@ OCA.Files_External.Settings = OCA.Files_External.Settings || {};
OCA.Files_External.Settings.GlobalStorageConfig = GlobalStorageConfig;
OCA.Files_External.Settings.UserStorageConfig = UserStorageConfig;
OCA.Files_External.Settings.MountConfigListView = MountConfigListView;

})();
6 changes: 2 additions & 4 deletions apps/files_external/templates/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
$l->t("Once every direct access");
$l->t('Read only');

script('files_external', [
'settings',
'templates'
]);
\OCP\Util::addScript('files_external', 'settings');
\OCP\Util::addScript('files_external', 'templates');
style('files_external', 'settings');

// load custom JS
Expand Down
4 changes: 4 additions & 0 deletions lib/private/AppFramework/DependencyInjection/DIContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
use OC\Log\PsrLoggerAdapter;
use OC\ServerContainer;
use OC\Settings\AuthorizedGroupMapper;
use OC\User\Manager as UserManager;
use OCA\WorkflowEngine\Manager;
use OCP\AppFramework\Http\IOutput;
use OCP\AppFramework\IAppContainer;
Expand Down Expand Up @@ -278,6 +279,9 @@ public function __construct(string $appName, array $urlParams = [], ?ServerConta
$c->get(IUserSession::class),
$c->get(ITimeFactory::class),
$c->get(\OC\Authentication\Token\IProvider::class),
$c->get(LoggerInterface::class),
$c->get(IRequest::class),
$c->get(UserManager::class),
)
);
$dispatcher->registerMiddleware(
Expand Down
Loading