Skip to content

Commit

Permalink
Content Security Policy (CSP) compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
jelicanin committed Jan 9, 2019
1 parent 5faa6db commit 2d130d8
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 16 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Simple & configurable SilverStripe plugin to notify users about cookie policies.

## Maintainer Contacts
* Milan Jelicanin [at] Fractas.com
* Petar Simic [at] Fractas.com


## Requirements
Expand Down
8 changes: 8 additions & 0 deletions _config/config.yml
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@ PageController:
SilverStripe\SiteConfig\SiteConfig:
extensions:
- Fractas\CookiePolicy\CookiePolicySiteConfig
---
After:
- '#rootroutes'
- '#coreroutes'
---
SilverStripe\Control\Director:
rules:
fetchcookiepolicy: Fractas\CookiePolicy\CookiePolicyController
5 changes: 4 additions & 1 deletion client/dist/javascript/jquery.cookie.policy.js
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,13 @@
});
// detect cookie
$(this).ready(function() {
var cookie = $.cookie('cookie_policy'); // => "true";
var cookie = $.cookie('cookie_policy');
if (!cookie) {
$('#cookie_container').show();
}
});
}
$.getJSON($('base')[0].href + 'fetchcookiepolicy', function(data) {
$('body').cookieNotify({btnText: data.CookiePolicyButtonTitle, text: data.CookiePolicyDescription, position: data.CookiePolicyPosition});
});
}(jQuery));
2 changes: 1 addition & 1 deletion client/dist/javascript/jquery.cookie.policy.min.js
100755 → 100644

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 11 additions & 8 deletions src/CookiePolicy.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class CookiePolicy extends Extension
private static $include_cookie_policy_notification = true;
private static $current_site_config = null;
private static $load_jquery = false;
private static $load_jquery_defer = false;
private static $load_script_defer = true;

public function onBeforeInit()
{
Expand All @@ -24,17 +26,18 @@ public function onBeforeInit()
public function onAfterInit()
{
if (self::cookie_policy_notification_enabled()) {
$cookiepolicyjssnippet = ArrayData::create([
'CookiePolicyButtonTitle' => self::$current_site_config->CookiePolicyButtonTitle,
'CookiePolicyDescription' => self::$current_site_config->obj('CookiePolicyDescription'),
'CookiePolicyPosition' => self::$current_site_config->CookiePolicyPosition,
]);

if (Config::inst()->get(static::class, 'load_jquery')) {
Requirements::javascript('silverstripe/admin:thirdparty/jquery/jquery.js');
}
Requirements::javascript('fractas/cookiepolicy:client/dist/javascript/jquery.cookie.policy.min.js');
Requirements::customScript($cookiepolicyjssnippet->renderWith('CookiePolicyJSSnippet'));
if (Config::inst()->get(static::class, 'load_jquery_defer')) {
Requirements::javascript('silverstripe/admin:thirdparty/jquery/jquery.js', ['defer' => true]);
}

if (Config::inst()->get(static::class, 'load_script_defer')) {
Requirements::javascript('fractas/cookiepolicy:client/dist/javascript/jquery.cookie.policy.min.js', ['defer' => true]);
} else {
Requirements::javascript('fractas/cookiepolicy:client/dist/javascript/jquery.cookie.policy.min.js');
}
}
}

Expand Down
60 changes: 60 additions & 0 deletions src/CookiePolicyController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Fractas\CookiePolicy;

use SilverStripe\Core\Config\Config;
use SilverStripe\SiteConfig\SiteConfig;
use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest;

class CookiePolicyController extends Controller
{
private static $current_site_config = null;

private static $allowed_actions = [
'index',
];

private static $url_handlers = [
'fetchcookiepolicy' => 'index',
];

public function index(HTTPRequest $request)
{
$this->getResponse()->setBody(json_encode([
'CookiePolicyButtonTitle' => $this->owner->getCookiePolicyButtonTitle(),
'CookiePolicyDescription' => $this->owner->getCookiePolicyDescription(),
'CookiePolicyPosition' => $this->owner->getCookiePolicyPosition(),
]));

$this->getResponse()->addHeader("Content-type", "application/json");

return $this->getResponse();
}

public function doInit()
{
$siteConfig = SiteConfig::current_site_config();
self::set_current_site_config($siteConfig);
}

public function getCookiePolicyButtonTitle()
{
return self::$current_site_config->CookiePolicyButtonTitle;
}

public function getCookiePolicyDescription()
{
return self::$current_site_config->obj('CookiePolicyDescription')->RAW();
}

public function getCookiePolicyPosition()
{
return self::$current_site_config->CookiePolicyPosition;
}

public static function set_current_site_config($input)
{
self::$current_site_config = $input;
}
}
5 changes: 0 additions & 5 deletions templates/CookiePolicyJSSnippet.ss

This file was deleted.

0 comments on commit 2d130d8

Please sign in to comment.