GithubHelp home page GithubHelp logo

thebnl / silverstripe-cookie-consent Goto Github PK

View Code? Open in Web Editor NEW
16.0 3.0 6.0 2.78 MB

GDPR compliant cookie popup and consent checker

License: BSD 3-Clause "New" or "Revised" License

PHP 80.91% JavaScript 5.61% Scheme 8.22% SCSS 5.25%
gdpr gdpr-cookie gdpr-consent cookie cookiecutter silverstripe-cookie-consent silverstripe

silverstripe-cookie-consent's Introduction

Silverstripe Cookie Consent

GDPR compliant cookie bar and consent checker

Installation

Install the module trough composer:

composer require bramdeleeuw/cookieconsent

Include the popup template in your base Page.ss

<% include CookieConsent %>

Configuration

You can configure the cookies and cookie groups trough the yml config. You need to configure by provider, for providers the dots are converted to underscores e.g. ads.marketingcompany.com becomes ads_marketingcompany_com.

By configuring cookies trough yml you can check for consent in your code and make the necessary changes e.g. require the analytics or other cookies or skip placing them.

The texts for the configured cookies are editable trough the Site Config, here other cookies can also be added by CMS users. For example if a site user decides to embed a Youtube video he or she can specify the cookies that are placed by Youtube. I reccomend the following three groups to be created, these have default content, of course you are free to configure groups as you see fit.

Broarm\CookieConsent\CookieConsent:
  cookies:
    Necessary:
      local:
        - CookieConsent
        - ShowPopUp
    Marketing:
      ads_marketingcompany_com:
        - _track
    Analytics:
      local:
        - _ga
        - _gid

This module comes with some default content for cookies we've encountered before. If you want to set default content for these cookies yourself that is possible trough the lang files. If you have cookie descriptions that are not in this module, contributions to the lang files are much appreciated! Translations are managed trough Transifex.

The files are structured as such:

en:
  CookieConsent_{provider}:
    {cookie}_Purpose: 'Cookie description'
    {cookie}_Expiry: 'Cookie expire time'
  # for cookies from your own domain:
  CookieConsent_local:
    PHPSESSID_Purpose: 'Session'
    PHPSESSID_Expiry: 'Session'
  # for cookies from an external domain:
  CookieConsent_ads_marketingcompany_com:
    _track_Purpose: 'Cookie description'
    _track_Expiry: 'Cookie expire time'

Then you can check for consent in your code by calling

if (CookieConsent::check('Analytics')) {
    // include google analytics
}

You can also configure the requirement of the default js and css. Make sure you combine at least the javascript in you bundle if you chose not to require by default!

Broarm\CookieConsent\CookieConsent:
  include_javascript: true
  include_css: true
  create_default_pages: true

Enable XHR mode

When you use static publishing, you'll want to enable XHR mode. XHR mode accepts the cookies trough an xhr request and shows/hides the consent popup with the help of some javascript.

In your yml config set xhr_mode to true

Broarm\CookieConsent\CookieConsent:
  xhr_mode: true

In your javascript, you can make use of the utility class. This handles the xhr request and visibility of the popup:

import CookieConsent from '../vendor/bramdeleeuw/cookieconsent/javascript/src/cookieconsent';
const consent = new CookieConsent();
consent.enableXHRMode();

Include assets in your bundle

If you want to include the scss or js in your own bundle you can do that by:

// Import the CookieConsent utility 
import CookieConsent from '../vendor/bramdeleeuw/cookieconsent/javascript/src/cookieconsent';

const consent = new CookieConsent();

// This tool let's you check for cookie consent in your js files before you apply any cookies
if (consent.check('Marketing')) {
  // add marketing cookie
}

// If you use Google Tag Manager this tool can also push the consent into the dataLayer object
consent.pushToDataLayer();

For the scss you can just import the scss file

@import "cookieconsent/scss/cookieconsent";

Make sure the relative paths to the files match your use case.

Default Pages

This module also sets up 3 default pages on running dev/build. If you want to prevent that behaviour you should disable the create_default_pages config setting. The pages created are a CookiePolicyPage, PrivacyPolicyPage and TermsAndConditionsPage and are filled with bare bones content for each of the page types. Of course it is your or your CMS users responsibility to alter these texts to make them fitting to your use case!

Maintainers

Bram de Leeuw

silverstripe-cookie-consent's People

Contributors

dependabot[bot] avatar gordonbanderson avatar gregor-agnes avatar josephlewisnz avatar rvxd avatar thebnl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

silverstripe-cookie-consent's Issues

Saving cookie prefs fail when more then 3 cookie categories

Hi
We use the module in several projects - when using more than 3 cookie categories (f.e. necessary, analytics, social media, video) the "saving" action at the cookie settings page (CookiePolicyPage) is behaving strange.
After choosing 3 of 4 categories and submitting, some categories stay active, others get unchecked again and do not get saved.
I just did a fresh install with SS4.8, it´s the same.
Any ideas? Thanks!

Template Layout Override

Tried to override the page layout and found that I had to put the override into

templates/Broarm/CookieConsent/CookiePolicyPage.ss templates/Broarm/CookieConsent/CookieConsentCheckboxField_holder.ss

Instead of templates/Layout and templates/Includes

Can't Override Package Config

Trying to disable css and javascript through a app/_config/cookies.yml file with the following content:

---
Name: cookies
---
Broarm\CookieConsent\CookieConsent:
  include_javascript: false
  include_css: false
  create_default_pages: false
  cookies:
    Necessary:
      local:
        - PHPSESSID
        - CookieConsent
    Analytics:
      local:
        - _ga
        - _gid

However, both this config and the one that comes with the package seem to get merged together, with the package config taking priority. This gives me the following:

Array
(
    [cookies] => Array
        (
            [Necessary] => Array
                (
                    [local] => Array
                        (
                            [0] => PHPSESSID
                            [1] => CookieConsent
                            [2] => PHPSESSID
                            [3] => CookieConsent
                        )

                )

            [Analytics] => Array
                (
                    [local] => Array
                        (
                            [0] => _ga
                            [1] => _gid
                        )

                )

        )

    [include_javascript] => 1
    [include_css] => 1
    [create_default_pages] => 1
    [extensions] => Array
        (
        )

    [unextendable_classes] => Array
        (
            [0] => SilverStripe\View\ViewableData
            [1] => SilverStripe\Control\RequestHandler
        )

)

As you can see the include_javascript, include_css and create_default_pages are being ignored. I also had to change the XD\CookieConsent\CookieConsent: in the documentation to Broarm\CookieConsent\CookieConsent:.

SS4 Support

Hi. Trying to install this into my project, fairly new to SS4, but everything installed via composer so far has gone into my vendor directory. However when I install this it goes into the root directory. Is this correct behaviour or have I missed something?

Attempt to edit group causes uncaught exception

Go to settings > cookie consent > cookies > necessary and click to edit triggers the following. Possible related to https://github.com/tractorcow/silverstripe-fluent/issues/318 and that the silverstripe-australia/gridfieldextensions is abandoned in favour of symbiote/silverstripe-gridfieldextensions

[Emergency] Uncaught Exception: Inline adding requires the editable columns component
GET /admin/settings/EditForm/field/Cookies/item/1/edit
Line 90 in /var/www/html/gridfieldextensions/src/GridFieldAddNewInlineButton.php

Source
81     public function getHTMLFragments($grid)
82     {
83         if ($grid->getList() && !singleton($grid->getModelClass())->canCreate()) {
84             return array();
85         }
86 
87         $fragment = $this->getFragment();
88 
89         if (!$editable = $grid->getConfig()->getComponentByType('GridFieldEditableColumns')) {
90             throw new Exception('Inline adding requires the editable columns component');
91         }
92 
93         Requirements::javascript(THIRDPARTY_DIR . '/javascript-templates/tmpl.js');
94         GridFieldExtensions::include_requirements();
95 
96         $data = new ArrayData(array(
Trace
Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton->getHTMLFragments(SilverStripe\Forms\GridField\GridField) 
GridField.php:341
SilverStripe\Forms\GridField\GridField->FieldHolder() 
call_user_func_array(Array, Array) 
ViewableData.php:481
SilverStripe\View\ViewableData->obj(FieldHolder, , 1) 
ViewableData.php:543
SilverStripe\View\ViewableData->XML_val(FieldHolder, , 1) 
SSViewer_Scope.php:323
SilverStripe\View\SSViewer_Scope->__call(XML_val, Array) 
SSViewer_DataPresenter.php:309
SilverStripe\View\SSViewer_DataPresenter->__call(XML_val, Array) 
.cachevendor.silverstripe.admin.themes.cms-forms.templates.SilverStripe.Forms.CMSTabSet.ss:52
include(/var/www/html/silverstripe-cache/www-data/.cachevendor.silverstripe.admin.themes.cms-forms.templates.SilverStripe.Forms.CMSTabSet.ss) 
SSViewer.php:602
SilverStripe\View\SSViewer->includeGeneratedTemplate(/var/www/html/silverstripe-cache/www-data/.cachevendor.silverstripe.admin.themes.cms-forms.templates.SilverStripe.Forms.CMSTabSet.ss, SilverStripe\Forms\TabSet, , Array, ) 
SSViewer.php:674
SilverStripe\View\SSViewer->process(SilverStripe\Forms\TabSet, ) 
ViewableData.php:397
SilverStripe\View\ViewableData->renderWith(SilverStripe\View\SSViewer) 
TabSet.php:137
SilverStripe\Forms\TabSet->FieldHolder() 
call_user_func_array(Array, Array) 
ViewableData.php:481
SilverStripe\View\ViewableData->obj(FieldHolder, , 1) 
ViewableData.php:543
SilverStripe\View\ViewableData->XML_val(FieldHolder, , 1) 
SSViewer_Scope.php:323
SilverStripe\View\SSViewer_Scope->__call(XML_val, Array) 
SSViewer_DataPresenter.php:309
SilverStripe\View\SSViewer_DataPresenter->__call(XML_val, Array) 
.cachevendor.silverstripe.admin.templates.SilverStripe.Admin.Includes.LeftAndMain_EditForm.ss:161
include(/var/www/html/silverstripe-cache/www-data/.cachevendor.silverstripe.admin.templates.SilverStripe.Admin.Includes.LeftAndMain_EditForm.ss) 
SSViewer.php:602
SilverStripe\View\SSViewer->includeGeneratedTemplate(/var/www/html/silverstripe-cache/www-data/.cachevendor.silverstripe.admin.templates.SilverStripe.Admin.Includes.LeftAndMain_EditForm.ss, SilverStripe\Forms\Form, , Array, ) 
SSViewer.php:674
SilverStripe\View\SSViewer->process(SilverStripe\Forms\Form, ) 
ViewableData.php:397
SilverStripe\View\ViewableData->renderWith(SilverStripe\View\SSViewer) 
Form.php:1567
SilverStripe\Forms\Form->forTemplate() 
ViewableData.php:545
SilverStripe\View\ViewableData->XML_val(ItemEditForm, , 1) 
SSViewer_Scope.php:323
SilverStripe\View\SSViewer_Scope->__call(XML_val, Array) 
SSViewer_DataPresenter.php:309
SilverStripe\View\SSViewer_DataPresenter->__call(XML_val, Array) 
.cachevendor.silverstripe.framework.templates.SilverStripe.Forms.GridField.GridFieldDetailForm_ItemRequest.ss:2
include(/var/www/html/silverstripe-cache/www-data/.cachevendor.silverstripe.framework.templates.SilverStripe.Forms.GridField.GridFieldDetailForm_ItemRequest.ss) 
SSViewer.php:602
SilverStripe\View\SSViewer->includeGeneratedTemplate(/var/www/html/silverstripe-cache/www-data/.cachevendor.silverstripe.framework.templates.SilverStripe.Forms.GridField.GridFieldDetailForm_ItemRequest.ss, SilverStripe\View\ViewableData_Customised, , Array, ) 
SSViewer.php:674
SilverStripe\View\SSViewer->process(SilverStripe\View\ViewableData_Customised, ) 
ViewableData.php:397
SilverStripe\View\ViewableData->renderWith(SilverStripe\View\SSViewer) 
GridFieldDetailForm_ItemRequest.php:142
SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest->edit(SilverStripe\Control\HTTPRequest) 
RequestHandler.php:319
SilverStripe\Control\RequestHandler->handleAction(SilverStripe\Control\HTTPRequest, edit) 
RequestHandler.php:201
SilverStripe\Control\RequestHandler->handleRequest(SilverStripe\Control\HTTPRequest) 
GridFieldDetailForm.php:118
SilverStripe\Forms\GridField\GridFieldDetailForm->handleItem(SilverStripe\Forms\GridField\GridField, SilverStripe\Control\HTTPRequest) 
GridField.php:1031
SilverStripe\Forms\GridField\GridField->handleRequest(SilverStripe\Control\HTTPRequest) 
RequestHandler.php:225
SilverStripe\Control\RequestHandler->handleRequest(SilverStripe\Control\HTTPRequest) 
RequestHandler.php:225
SilverStripe\Control\RequestHandler->handleRequest(SilverStripe\Control\HTTPRequest) 
Controller.php:212
SilverStripe\Control\Controller->handleRequest(SilverStripe\Control\HTTPRequest) 
LeftAndMain.php:752
SilverStripe\Admin\LeftAndMain->handleRequest(SilverStripe\Control\HTTPRequest) 
AdminRootController.php:123
SilverStripe\Admin\AdminRootController->handleRequest(SilverStripe\Control\HTTPRequest) 
Director.php:361
SilverStripe\Control\Director->SilverStripe\Control\{closure}(SilverStripe\Control\HTTPRequest) 
VersionedHTTPMiddleware.php:40
SilverStripe\Versioned\VersionedHTTPMiddleware->process(SilverStripe\Control\HTTPRequest, Closure) 
HTTPMiddlewareAware.php:62
SilverStripe\Control\Director->SilverStripe\Control\Middleware\{closure}(SilverStripe\Control\HTTPRequest) 
BasicAuthMiddleware.php:68
SilverStripe\Security\BasicAuthMiddleware->process(SilverStripe\Control\HTTPRequest, Closure) 
HTTPMiddlewareAware.php:62
SilverStripe\Control\Director->SilverStripe\Control\Middleware\{closure}(SilverStripe\Control\HTTPRequest) 
AuthenticationMiddleware.php:61
SilverStripe\Security\AuthenticationMiddleware->process(SilverStripe\Control\HTTPRequest, Closure) 
HTTPMiddlewareAware.php:62
SilverStripe\Control\Director->SilverStripe\Control\Middleware\{closure}(SilverStripe\Control\HTTPRequest) 
CanonicalURLMiddleware.php:155
SilverStripe\Control\Middleware\CanonicalURLMiddleware->process(SilverStripe\Control\HTTPRequest, Closure) 
HTTPMiddlewareAware.php:62
SilverStripe\Control\Director->SilverStripe\Control\Middleware\{closure}(SilverStripe\Control\HTTPRequest) 
FlushMiddleware.php:26
SilverStripe\Control\Middleware\FlushMiddleware->process(SilverStripe\Control\HTTPRequest, Closure) 
HTTPMiddlewareAware.php:62
SilverStripe\Control\Director->SilverStripe\Control\Middleware\{closure}(SilverStripe\Control\HTTPRequest) 
RequestProcessor.php:66
SilverStripe\Control\RequestProcessor->process(SilverStripe\Control\HTTPRequest, Closure) 
HTTPMiddlewareAware.php:62
SilverStripe\Control\Director->SilverStripe\Control\Middleware\{closure}(SilverStripe\Control\HTTPRequest) 
SessionMiddleware.php:20
SilverStripe\Control\Middleware\SessionMiddleware->process(SilverStripe\Control\HTTPRequest, Closure) 
HTTPMiddlewareAware.php:62
SilverStripe\Control\Director->SilverStripe\Control\Middleware\{closure}(SilverStripe\Control\HTTPRequest) 
AllowedHostsMiddleware.php:60
SilverStripe\Control\Middleware\AllowedHostsMiddleware->process(SilverStripe\Control\HTTPRequest, Closure) 
HTTPMiddlewareAware.php:62
SilverStripe\Control\Director->SilverStripe\Control\Middleware\{closure}(SilverStripe\Control\HTTPRequest) 
TrustedProxyMiddleware.php:176
SilverStripe\Control\Middleware\TrustedProxyMiddleware->process(SilverStripe\Control\HTTPRequest, Closure) 
HTTPMiddlewareAware.php:62
SilverStripe\Control\Director->SilverStripe\Control\Middleware\{closure}(SilverStripe\Control\HTTPRequest) 
HTTPMiddlewareAware.php:65
SilverStripe\Control\Director->callMiddleware(SilverStripe\Control\HTTPRequest, Closure) 
Director.php:370
SilverStripe\Control\Director->handleRequest(SilverStripe\Control\HTTPRequest) 
HTTPApplication.php:48
SilverStripe\Control\HTTPApplication->SilverStripe\Control\{closure}(SilverStripe\Control\HTTPRequest) 
call_user_func(Closure, SilverStripe\Control\HTTPRequest) 
HTTPApplication.php:66
SilverStripe\Control\HTTPApplication->SilverStripe\Control\{closure}(SilverStripe\Control\HTTPRequest) 
call_user_func(Closure, SilverStripe\Control\HTTPRequest) 
ErrorControlChainMiddleware.php:56
SilverStripe\Core\Startup\ErrorControlChainMiddleware->SilverStripe\Core\Startup\{closure}(SilverStripe\Core\Startup\ErrorControlChain) 
call_user_func(Closure, SilverStripe\Core\Startup\ErrorControlChain) 
ErrorControlChain.php:236
SilverStripe\Core\Startup\ErrorControlChain->step() 
ErrorControlChain.php:226
SilverStripe\Core\Startup\ErrorControlChain->execute() 
ErrorControlChainMiddleware.php:69
SilverStripe\Core\Startup\ErrorControlChainMiddleware->process(SilverStripe\Control\HTTPRequest, Closure) 
HTTPMiddlewareAware.php:62
SilverStripe\Control\HTTPApplication->SilverStripe\Control\Middleware\{closure}(SilverStripe\Control\HTTPRequest) 
HTTPMiddlewareAware.php:65
SilverStripe\Control\HTTPApplication->callMiddleware(SilverStripe\Control\HTTPRequest, Closure) 
HTTPApplication.php:67
SilverStripe\Control\HTTPApplication->execute(SilverStripe\Control\HTTPRequest, Closure, ) 
HTTPApplication.php:49
SilverStripe\Control\HTTPApplication->handle(SilverStripe\Control\HTTPRequest) 
index.php:26

Module causes a failing dev/build

Hi,

I recently tried to update a 4.5.1 project to a newer version (4.7.3@stable) but the module caused a building failure.
I noticed a warming about an array to string conversion inside the CookieGroup file on lines 147 - 149:

$cookie = CookieDescription::create(array( 'ConfigName' => $cookieName, 'Title' => $cookieName, **'Provider' => $providerLabel, 'Purpose' => _t("CookieConsent_{$providerName}.{$cookieName}_Purpose", "$cookieName"), 'Expiry' => _t("CookieConsent_{$providerName}.{$cookieName}_Expiry", 'Session')** 150 ));

The same message also occurs on this line:
DB::alteration_message(sprintf('Cookie "%s" created and added to group "%s"', $cookieName, $groupName), 'created');

Deprecation error on PHP 8.1

Hi,

I just got the following error in bramdeleeuw/cookieconsent/src/CookieConsent.php when running on PHP 8.1:

[Deprecated] explode(): Passing null to parameter #2 ($string) of type string is deprecated
GET /server-error/
Line 123 in /usr/src/myapp/vendor/bramdeleeuw/cookieconsent/src/CookieConsent.php

It seems Cookie::get(CookieConsent::COOKIE_NAME) return null which is no longer allowed.

Configure domain & path variables for the cookie

Hi,

For a specific use case I am required to configure the domain & path for the cookie::set() function. I couldn't find any option to configure this using the yml and in the sourcecode I only found that null is being passed to these parameters.

Elemental support

I tried this plugin with Elemental, it seems to not work perfectly. The scripts and styles get loaded, and the overlay appears, but then it stops.

Also, I am using UserForms to provide a GDPR request form for cancellation and query for information on the privacy page. This means the privacy page will need to be a UserformPage.

The plugin seems to be very well done otherwise, my compliments. Will try it again in a minimum use case to try to figure out what went wrong.

However, what I didn't like very much is that the javascript uses vue-foundation, would have liked better for it to use jQuery.

Tried on SS3.6.

isRequired configurable in CMS?

Hello,
really nice module, good job!
What do you think about making isRequired CookieGroup configurable in CMS. I need 2 separate groups, but both necessary...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.