From e963741fe7a1b1496d08b1064c277c0f52d82bc6 Mon Sep 17 00:00:00 2001 From: Andrew Isherwood Date: Fri, 5 Feb 2021 14:39:32 +0000 Subject: [PATCH] Bug 27378: Delete cookies when consent removed We have provided the ability for a user to remove previously granted consent, but we were not removing the cookie associated with that consent. This was a complete oversight on my part. I've introduced an additional bit of metadata for each "consent" defined in the ConsentJS syspref, this allows the admin defining a "consent" to specify a string (which is treated as a regex) that allows us to identify a cookie name associated with that consent. So, for example, if Google Analytics was being added, an identifying string might be '_ga'. This allows us to subsequently remove a cookie that a previously granted consent has set. This commit also adds some missing 'html' filters in koha-tmpl/intranet-tmpl/prog/en/includes/intranet-bottom.inc and koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-bottom.inc Signed-ff-by: Barry Cannon --- .../prog/en/includes/intranet-bottom.inc | 2 +- .../intranet-tmpl/prog/js/cookieconsent.js | 16 +++++++ .../prog/js/pages/preferences.js | 46 +++++++++++++++++-- .../bootstrap/en/includes/opac-bottom.inc | 2 +- .../opac-tmpl/bootstrap/js/cookieconsent.js | 15 ++++++ 5 files changed, 76 insertions(+), 5 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/intranet-bottom.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/intranet-bottom.inc index 5a94f7d53a..9bcca6d5b4 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/intranet-bottom.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/intranet-bottom.inc @@ -136,7 +136,7 @@ [% IF Koha.Preference( 'CookieConsent' ) && Koha.Preference( 'ConsentJS' ).length > 0 %] [% consents = JSConsents.all() %] [% FOREACH consent IN consents %] - + [% END %] [% END %] [% IF Koha.Preference( 'CookieConsent' ) %] diff --git a/koha-tmpl/intranet-tmpl/prog/js/cookieconsent.js b/koha-tmpl/intranet-tmpl/prog/js/cookieconsent.js index c718c73e39..ff00a738d7 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/cookieconsent.js +++ b/koha-tmpl/intranet-tmpl/prog/js/cookieconsent.js @@ -102,6 +102,21 @@ const code = atob($(this).data('consent-code')); const func = Function(code); func(); + } else { + // This code doesn't have consent to run, we may need to remove + // any cookies it has previously set + const matchPattern = $(this).data('consent-match-pattern'); + if (matchPattern.length > 0) { + const regex = new RegExp(matchPattern); + const allCookies = document.cookie.split('; '); + allCookies.forEach(function (cookie) { + const name = cookie.split('=')[0]; + if (regex.test(name)) { + document.cookie = name + '=; expires=Thu, 01 Jan 1970 00: 00: 01 GMT'; + } + }); + + } } }); } @@ -125,6 +140,7 @@ e.preventDefault(); localStorage.setItem('cookieConsent', []); hideContainer(); + runConsentedCode(); }); // "Accept selected" handler diff --git a/koha-tmpl/intranet-tmpl/prog/js/pages/preferences.js b/koha-tmpl/intranet-tmpl/prog/js/pages/preferences.js index e5cc0b02a2..420ebb0c88 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/pages/preferences.js +++ b/koha-tmpl/intranet-tmpl/prog/js/pages/preferences.js @@ -342,7 +342,38 @@ $( document ).ready( function () { const id = 'ConsentJS_' + idx; const code = item.code && item.code.length > 0 ? atob(item.code) : ''; const itemId = item.id && item.id.length > 0 ? item.id : ''; - return '
' + __('Required') + '
' + __('Required') + '
' + checkBox('opacConsent_' + id, 'opacConsent', item.opacConsent) + '
' + checkBox('staffConsent_' + id, 'staffConsent', item.staffConsent) + '
Delete
'; + return '
' + + '
' + + '
' + + ' ' + + ' ' + __('Required') + '' + + '
' + + '
' + + ' ' + + ' ' + __('Required') + '' + + '
' + + '
' + + ' ' + + checkBox('opacConsent_' + id, 'opacConsent', item.opacConsent) + + '
' + + '
' + + ' ' + + checkBox('staffConsent_' + id, 'staffConsent', item.staffConsent) + + '
' + + '
' + + ' ' + + ' ' + __('Required') + '' + + '
' + + '
' + + '
' + + ' ' + + '
' + + ' ' + __('Click to expand') + '' + + ' ' + + '
' + + '
' + + ' Delete' + + '
'; } // Return the markup for all consentJS items concatenated @@ -362,6 +393,7 @@ $( document ).ready( function () { return { name: '', description: '', + matchPattern: '', code: '', consentInOpac: false, consentInStaff: false @@ -439,18 +471,25 @@ $( document ).ready( function () { '_' + Math.random().toString(36).substr(2, 9); const name = $(this).find('.metaName').val(); const desc = $(this).find('.metaDescription').val(); + const matchPattern = $(this).find('.metaMatchPattern').val(); const opacConsent = $(this).find('.opacConsent').is(':checked') const staffConsent = $(this).find('.staffConsent').is(':checked'); const code = $(this).find('.preference-code').val(); - // If the name, description and code are empty, then they've + // If the name, description, match pattern code are empty, then they've // added a new entry, but not filled it in, we can skip it - if (name.length === 0 && desc.length === 0 && code.length === 0) { + if ( + name.length === 0 && + desc.length === 0 && + matchPattern.length === 0 && + code.length === 0 + ) { return; } // They've filled in at least some info if ( (name.length === 0) || (desc.length === 0) || + (matchPattern.length === 0) || (code.length === 0) ) { invalid.push(this); @@ -459,6 +498,7 @@ $( document ).ready( function () { id: id, name: name, description: desc, + matchPattern: matchPattern, opacConsent: opacConsent, staffConsent: staffConsent, code: btoa(code) diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-bottom.inc b/koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-bottom.inc index 7ce587e7e5..35f6c2ff2e 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-bottom.inc +++ b/koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-bottom.inc @@ -124,7 +124,7 @@ [% IF Koha.Preference( 'CookieConsent' ) && Koha.Preference( 'ConsentJS' ).length > 0 %] [% consents = JSConsents.all() %] [% FOREACH consent IN consents %] - + [% END %] [% END %] diff --git a/koha-tmpl/opac-tmpl/bootstrap/js/cookieconsent.js b/koha-tmpl/opac-tmpl/bootstrap/js/cookieconsent.js index 8f7a8c0252..b0e67ed6a7 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/js/cookieconsent.js +++ b/koha-tmpl/opac-tmpl/bootstrap/js/cookieconsent.js @@ -116,6 +116,20 @@ const code = atob($(this).data('consent-code')); const func = Function(code); func(); + } else { + // This code doesn't have consent to run, we may need to remove + // any cookies it has previously set + const matchPattern = $(this).data('consent-match-pattern'); + if (matchPattern.length > 0) { + const regex = new RegExp(matchPattern); + const allCookies = document.cookie.split('; '); + allCookies.forEach(function (cookie) { + const name = cookie.split('=')[0]; + if (regex.test(name)) { + document.cookie = name + '=; expires=Thu, 01 Jan 1970 00: 00: 01 GMT'; + } + }); + } } }); } @@ -140,6 +154,7 @@ e.preventDefault(); localStorage.setItem('cookieConsent', []); hideContainer(); + runConsentedCode(); showYourCookies(); }); -- 2.20.1