huppys/cookie-consent-bundle

Symfony 扩展包,用于向访客请求对使用 cookie 的同意,以符合 AVG/GDPR。

安装: 21

依赖: 0

建议者: 0

安全: 0

星级: 4

关注者: 0

分支: 16

类型:symfony-bundle

dev-main 2024-06-20 15:33 UTC

This package is auto-updated.

Last update: 2024-09-20 16:01:33 UTC


README

一旦开始收集或处理个人信息(简称 PII),您就有义务允许访客决定收集哪些信息。此 cookie 同意横幅包可以帮助您帮助您的用户。

Symfony 扩展包,用于将 cookie 同意对话框集成到您的网站,并根据 AVG/GDPR 处理 cookie。

安装

步骤 1:使用 composer 下载

在 Symfony 应用程序中运行以下命令以安装并集成 Cookie Consent 扩展包到您的应用程序中

composer require huppys/cookie-consent-bundle

步骤 2:启用扩展包

当不使用 Symfony Flex 时,手动启用扩展包

在 AppKernel.php 中向 registerBundles() 方法添加以下行

<?php
// app/AppKernel.php

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

或在 config/bundles.php 中向数组添加以下行

<?php

return [
    // ...
    huppys\CookieConsentBundle\CookieConsentBundle::class => ['all' => true],
    // ...
];

步骤 3:启用路由

当不使用 Symfony Flex 时,通过向您的 config/routing.yml 添加以下行手动启用扩展包的路由

cookie_consent:
  resource: "@CookieConsentBundle/config/routing.yaml"

步骤 4:根据需要配置

默认情况下,已启用最安全的选项。您可以在 config/packages/cookie_consent.yaml 中更改配置。

cookie_consent:
  cookie_settings:
    name_prefix: '' # string, any string you like to prefix the cookie names with
    cookies:
      consent_cookie:
        expires: 'P180D' # available values: PHP formatted date string, 'P180D' (180 days), 'P1Y' (1 year) etc.
        domain: null # optional: string or null, domain name, e.g. 'example.com'; null means 'use the current domain'
        secure: true # boolean, true by deafult, enable or disable transport only over https
        http_only: true # boolean, refer to mdn docs for more info
        same_site: 'lax' # available values: 'strict', 'lax', 'none'; if value is 'none' the 'secure' flag will be set to true by default
      consent_key_cookie:
        expires: 'P180D'
        domain: null
        secure: true
        http_only: true
        same_site: 'lax'
      consent_categories_cookie:
        expires: 'P180D'
        domain: null
        secure: true
        http_only: true
        same_site: 'lax'
  consent_categories: # Below are the default supported categories
    - 'analytics'
    - 'tracking'
    - 'marketing'
    - 'social_media'
  persist_consent: true # boolean; logs user actions to database
  position: 'dialog' # available values: 'bottom', 'dialog'
  form_action: $routeName # When set, xhr-Requests will only be sent to this route. Take care of having the route available.
  csrf_protection: true # boolean; enable or disable csrf protection for the form

用法

Twig 实现

通过 render_esi(为防止缓存)在任何您喜欢的地方加载 cookie 同意

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

如果您想以特定区域设置加载 cookie 同意,可以将区域设置作为参数传递

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

您必须安装资产,如用于异步表单提交的 JavaScript 和默认样式。要安装这些资产,请运行

bin/console assets:install

Cookie

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

  • consent:提交日期
  • consent-key:作为用户提交的 Cookie 同意的标识符生成的键
  • consent-category-[CATEGORY]:用户的选中值(《true》或《false》)

如果用户拒绝使用 cookie,则仅保存名为 consent 的 cookie,其值为当前日期。

日志记录

AVG/GDPR 要求所有用户的 cookie 偏好都可以由网站管理员解释。为此,我们将所有 cookie 偏好记录到数据库中。IP 地址将被匿名化。您可以通过将 persist_consent 设置为 false 来禁用记录给定的同意。

Database logging

Twig 扩展

以下 Twig 扩展函数可用

cookieconsent_isCategoryAllowedByUser 检查用户是否为特定 cookie 类别提供了其权限。

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

cookieconsent_isCookieConsentOptionSetByUser 检查用户是否保存了任何 cookie 偏好。当用户选择拒绝所有 cookie 时,此值将默认为 true

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

自定义

类别

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

翻译

所有文本都可以通过覆盖 CookieConsentBundle 翻译文件来更改,这些文件位于 Symfony 翻译中。请查看 translations 中的任何 yaml 文件以了解结构。

自定义:内容

此同意层中的大多数块都是可定制的。块包括:

  • header
    • title
    • intro
    • read_more
  • pre_form
  • consent_form ???
    • consent_form_start
    • required_cookies_category
    • consent_form_rest
  • post_form
  • scripts

创建文件:templates/bundles/CookieConsentBundle/cookie_consent.html.twig 并插入以下代码

# app/templates/bundles/CookieConsentBundle/cookie_consent.html.twig
{% extends '@!CookieConsent/cookie_consent.html.twig' %} {# this extends the base layout#}

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

{% block required_cookies_category %}
    {# let's hide this block #}
{% endblock required_cookies_category %}

自定义:主题

cookie 同意表单应该是可主题化的,因此也是如此。

使用 symfony/form 扩展包

要使用 symfony/form 绑定的不同表单主题,请参考Symfony 表单渲染文档。请记住加载属于主题的脚本和样式。

定义自己的表单主题

此捆绑包包含默认主题。要使用此特定主题,请在您的 config/packages/twig.yaml 文件中添加以下行

twig:
  form_themes:
    - '@CookieConsent/form/cookie_consent_form_theme.html.twig'

要使用您自己的主题,在模板文件夹下创建一个 twig 文件,并在 config/packages/twig.yaml 文件中的 twig->form_themes 下引用此 twig 文件,如下所示

twig:
  default_path: '%kernel.project_dir%/templates' # set template base path like this if it fits your project
  form_themes:
    - 'cookie_consent/consent_form_theme.html.twig'

这应该包括对 choice_rowsubmit_row 等块的覆盖

{% block choice_row %}
    <div class="lala">
        {% for child in form %}
            {% if not child.rendered %}
                {{- form_widget(child) -}}
                {{- form_label(child, null, {translation_domain: choice_translation_domain}) -}}
            {% endif %}
        {% endfor %}
    </div>
    <div>
        {{- form_help(form) -}}
    </div>
{% endblock %}

{% block submit_row %}
    {{ block('button_widget') }}
{% endblock %}

由于 Twig 允许加载多个主题,请确保您的主题是最后一个覆盖同名块的。

样式

CookieConsentBundle 包含默认样式。在 assets/css/cookie_consent.scss 中可找到 sass 文件,在 public/css/cookie_consent.css 中可找到构建后的 css 文件。可以通过设置 sass 文件中可用的变量轻松调整颜色。

要将默认样式应用于您的网站,请在 twig 模板中包含以下行

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

JavaScript:事件

当表单按钮被点击时,会触发 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);

故障排除

提交表单后出现错误 500

如果您的后端在提交表单后返回带有 HTTP 状态码 500 的响应,请确保您为配置选项 form_action 提供了路由。