alessandrolandim/cookie-consent-bundle

Symfony 插件,用于实现 Cookie Consent 以符合 AVG/GDPR。

安装: 5

依赖者: 0

建议者: 0

安全性: 0

星级: 0

关注者: 0

分支: 58

类型:symfony-bundle

0.10.0 2022-02-02 20:38 UTC

README

Scrutinizer Code Quality Code Coverage Build Status

Symfony 的 Cookie Consent 插件

Symfony 插件,用于将 Cookie Consent 添加到您的网站,以符合 AVG/GDPR 对 cookie 的规定。

安装

步骤 1:使用 composer 下载

在 Symfony 应用程序中运行此命令以安装并集成 Cookie Consent 插件到您的应用程序中

composer require connectholland/cookie-consent-bundle

步骤 2:启用插件

当不使用 symfony flex 时,请手动在内核中启用插件

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new ConnectHolland\CookieConsentBundle\CHCookieConsentBundle(),
        // ...
    );
}

步骤 3:启用路由

当不使用 symfony flex 时,请手动启用插件的路由

# app/config/routing.yml
ch_cookie_consent:
    resource: "@CHCookieConsentBundle/Resources/config/routing.yaml"

步骤 4:配置以满足您的需求

使用以下可能的设置配置您的 Cookie Consent

ch_cookie_consent:
    theme: 'light' # light, dark
    categories: # Below are the default supported categories
        - 'analytics'
        - 'tracking'
        - 'marketing'
        - 'social_media'
    use_logger: true # Logs user actions to database
    position: 'top' # top, bottom
    simplified: false # When set to true the user can only deny or accept all cookies at once
    http_only: true # Sets HttpOnly on cookies
    form_action: $routeName # When set, xhr-Requests will only be sent to this route. Take care of having the route available.
    csrf_protection: true # The cookie consent form is csrf protected or not
    default_option: true # When set true the option will Yes will be default

用法

Twig 实现

通过 render_esi 在任何您喜欢的位置加载 cookie consent(以防止缓存)

{{ render_esi(path('ch_cookie_consent.show')) }}
{{ render_esi(path('ch_cookie_consent.show_if_cookie_consent_not_set')) }}

如果您想使用特定的区域设置加载 cookie consent,可以传递区域设置作为参数

{{ render_esi(path('ch_cookie_consent.show', { 'locale' : 'en' })) }}
{{ render_esi(path('ch_cookie_consent.show_if_cookie_consent_not_set', { 'locale' : app.request.locale })) }}

Cookie

当用户提交表单时,偏好设置将保存为 cookie。cookie 的有效期为 1 年。以下 cookie 将被保存

  • Cookie_Consent:提交日期
  • Cookie_Consent_Key:生成的标识符,用于识别用户提交的 Cookie Consent
  • Cookie_Category_[CATEGORY]:用户选择的值(truefalse

日志记录

AVG/GDPR 要求网站管理员能够解释用户提供的所有 cookie 偏好。为此,我们将所有 cookie 偏好记录到数据库中。IP 地址将被匿名化。此选项可以在配置中禁用。

Database logging

主题

Dark Theme Light Theme

Twig 扩展

以下 Twig 扩展函数可用

chcookieconsent_isCategoryAllowedByUser 检查用户是否已授予特定 cookie 类别的权限

{% if chcookieconsent_isCategoryAllowedByUser('analytics') == true %}
    ...
{% endif %}

chcookieconsent_isCookieConsentSavedByUser 检查用户是否已保存任何 cookie 偏好

{% if chcookieconsent_isCookieConsentSavedByUser() == true %}
    ...
{% endif %}

自定义

类别

您可以通过更改配置并确保有这些类别的翻译来添加或删除任何类别。

翻译

所有文本都可以通过覆盖 CHCookieConsentBundle 翻译文件来更改。

样式

CHCookieConsentBundle 附带默认样式。一个 sass 文件在 Resources/assets/css/cookie_consent.scss 中可用,一个构建后的 css 文件在 Resources/public/css/cookie_consent.css 中可用。可以通过设置 sass 文件中可用的变量轻松调整颜色。

要安装这些资产,请运行

bin/console assets:install

并在模板中包含样式

{% include "@CHCookieConsent/cookie_consent_styling.html.twig" %}

JavaScript

通过加载 Resources/public/js/cookie_consent.js,将通过 AJAX 提交 cookie consent,并将 cookie consent 显示在网站顶部,同时将其他网站内容向下推。

事件

当表单按钮被点击时,将创建 cookie-consent-form-submit-successful 事件。使用以下代码来监听事件并添加您自定义的功能。

document.addEventListener('cookie-consent-form-submit-successful', function (e) {
    // ... your functionality
    // ... e.detail is available to see which button is clicked.
}, false);

模板主题

您可以通过将模板放在您的项目中来覆盖模板(对于 Symfony 5 项目除外)

# app/Resources/CHCookieConsentBundle/views/cookie_consent.html.twig
{% extends '@!CHCookieConsent/cookie_consent.html.twig' %}

{% block title %}
    Your custom title
{% endblock %}

Symfony 5 项目的模板覆盖

您可以通过将 templaces 放置在您项目中如下来覆盖模板。请注意,放置模板的位置非常重要:“app/templates/bundles/CHCookieConsentBundle/”。

# app/templates/bundles/CHCookieConsentBundle/cookie_consent.html.twig
{% extends '@!CHCookieConsent/cookie_consent.html.twig' %}

{% block intro %}
    Your custom intro
{% endblock %}