From d322494ef687f226b955f3c1b9212f4c48e62571 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 16 Feb 2024 14:56:09 +0100 Subject: [PATCH] Bug 36084: svc - config/systempreferences Signed-off-by: Jonathan Druart --- .../prog/en/modules/admin/didyoumean.tt | 19 ++++--- .../intranet-tmpl/prog/js/fetch/api-client.js | 2 + .../js/fetch/system-preferences-api-client.js | 50 +++++++++++++++++++ .../prog/js/pages/preferences.js | 43 ++++++---------- svc/config/systempreferences | 12 ----- 5 files changed, 76 insertions(+), 50 deletions(-) create mode 100644 koha-tmpl/intranet-tmpl/prog/js/fetch/system-preferences-api-client.js diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/didyoumean.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/didyoumean.tt index 506ff216bb..204948c6cc 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/didyoumean.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/didyoumean.tt @@ -117,16 +117,15 @@ function yesimeant() { var OPACdidyoumean = serialize_plugins('opac'); - - const csrf_token = "[% Koha.GenerateCSRF | $raw %]"; - let data = "pref_OPACdidyoumean=%s&csrf_token=%s".format(encodeURIComponent(OPACdidyoumean), csrf_token); - - $.ajax({ - data: data, - type: 'POST', - url: '/cgi-bin/koha/svc/config/systempreferences/', - success: function () { alert(_("Successfully saved configuration")); }, - }); + const client = APIClient.syspref; + client.sysprefs.update('OPACdidyoumean', OPACdidyoumean).then( + success => { + alert(_("Successfully saved configuration")); + }, + error => { + console.warn("Something wrong happened: %s".format(error)); + } + ); return false; } diff --git a/koha-tmpl/intranet-tmpl/prog/js/fetch/api-client.js b/koha-tmpl/intranet-tmpl/prog/js/fetch/api-client.js index 0f9b151067..b9b739b72a 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/fetch/api-client.js +++ b/koha-tmpl/intranet-tmpl/prog/js/fetch/api-client.js @@ -1,7 +1,9 @@ import ArticleRequestAPIClient from "./article-request-api-client.js"; import AVAPIClient from "./authorised-value-api-client.js"; +import SysprefAPIClient from "./system-preferences-api-client.js"; export const APIClient = { article_request: new ArticleRequestAPIClient(), authorised_value: new AVAPIClient(), + syspref: new SysprefAPIClient(), }; diff --git a/koha-tmpl/intranet-tmpl/prog/js/fetch/system-preferences-api-client.js b/koha-tmpl/intranet-tmpl/prog/js/fetch/system-preferences-api-client.js new file mode 100644 index 0000000000..1b8634951b --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/js/fetch/system-preferences-api-client.js @@ -0,0 +1,50 @@ +import HttpClient from "./http-client.js"; + +export class SysprefAPIClient extends HttpClient { + constructor() { + super({ + baseURL: "/cgi-bin/koha/svc/config/systempreferences", + }); + } + + get sysprefs() { + return { + get: variable => + this.get({ + endpoint: "/?pref=" + variable, + }), + update: (variable, value) => + this.post({ + endpoint: "", + body: "pref_%s=%s".format( + encodeURIComponent(variable), + encodeURIComponent(value) + ), + headers: { + "Content-Type": + "application/x-www-form-urlencoded;charset=utf-8", + }, + }), + update_all: sysprefs => + this.post({ + endpoint: "", + body: Object.keys(sysprefs) + .map(variable => + sysprefs[variable].length + ? sysprefs[variable].map(value => + "%s=%s".format(variable, value) + ) + : "%s=".format(variable) + ) + .flat(Infinity) + .join("&"), + headers: { + "Content-Type": + "application/x-www-form-urlencoded;charset=utf-8", + }, + }), + }; + } +} + +export default SysprefAPIClient; diff --git a/koha-tmpl/intranet-tmpl/prog/js/pages/preferences.js b/koha-tmpl/intranet-tmpl/prog/js/pages/preferences.js index c0837f730c..2092dd8a68 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/pages/preferences.js +++ b/koha-tmpl/intranet-tmpl/prog/js/pages/preferences.js @@ -8,41 +8,28 @@ KOHA.Preferences = { return; } - modified_prefs = $( form ).find( '.modified' ); - // $.serialize removes empty value, we need to keep them. - // If a multiple select has all its entries unselected - var unserialized = new Array(); - $(modified_prefs).each(function(){ - if ( $(this).attr('multiple') && $(this).val().length == 0 ) { - unserialized.push($(this)); - } - }); - data = modified_prefs.serialize(); - $(unserialized).each(function(){ - data += '&' + $(this).attr('name') + '='; - }); - if ( !data ) { + let sysprefs = $(form).find('.modified').toArray().reduce((map, e) => ({ ...map, [$(e).attr('name')]: [$(e).val()].flat()}), {}); + if ( !Object.keys(sysprefs).length ) { humanMsg.displayAlert( __("Nothing to save") ); return; } - let csrf_token_el = $( form ).find('input[name="csrf_token"]'); - if (csrf_token_el.length > 0){ - let csrf_token = csrf_token_el.val(); - if (csrf_token){ - data += '&' + 'csrf_token=' + csrf_token; - } - } KOHA.AJAX.MarkRunning($(form).find('.save-all'), __("Saving...") ); - KOHA.AJAX.Submit( { - data: data, - url: '/cgi-bin/koha/svc/config/systempreferences/', - success: function ( data ) { KOHA.Preferences.Success( form ) }, - complete: function () { KOHA.AJAX.MarkDone( $( form ).find( '.save-all' ) ) } - } ); + const client = APIClient.syspref; + client.sysprefs.update_all(sysprefs).then( + success => { + KOHA.Preferences.Success( form ); + }, + error => { + console.warn("Something wrong happened: %s".format(error)); + } + ).then(() => { + KOHA.AJAX.MarkDone( $( form ).find( '.save-all' ) ); + }); + }, Success: function ( form ) { var msg = ""; - modified_prefs.each(function(){ + $(form).find('.modified').each(function(){ var modified_pref = $(this).attr("id"); modified_pref = modified_pref.replace("pref_",""); msg += "" + __("Saved preference %s").format(modified_pref) + "\n"; diff --git a/svc/config/systempreferences b/svc/config/systempreferences index fd09218b6b..f3f1494a90 100755 --- a/svc/config/systempreferences +++ b/svc/config/systempreferences @@ -64,12 +64,6 @@ Used to set a single system preference. sub set_preference { my ( $preference ) = @_; - die "wrong_csrf_token\n" unless Koha::Token->new->check_csrf( - { - session_id => scalar $query->cookie('CGISESSID'), - token => scalar $query->param('csrf_token'), - } - ); my $value = join( ',', $query->param( 'value' ) ); C4::Context->set_preference( $preference, $value ); @@ -129,12 +123,6 @@ pref_virtualshelves=0 =cut sub set_preferences { - die "wrong_csrf_token\n" unless Koha::Token->new->check_csrf( - { - session_id => scalar $query->cookie('CGISESSID'), - token => scalar $query->param('csrf_token'), - } - ); foreach my $param ( $query->param() ) { my ( $pref ) = ( $param =~ /pref_(.*)/ ); -- 2.39.5