hryvinskyi / magento2-privacy-consent
Privacy consent engine for Magento 2: versioned consents per store view, the required-consent rule, and a journal of consents given and withdrawn as evidence.
Package info
github.com/hryvinskyi/magento2-privacy-consent
Type:magento2-module
pkg:composer/hryvinskyi/magento2-privacy-consent
Requires
- php: ~8.1.0||~8.2.0||~8.3.0||~8.4.0||~8.5.0
- hryvinskyi/magento2-client-address: ^1.0
- hryvinskyi/magento2-clock: ^1.0
- hryvinskyi/magento2-privacy-consent-api: ^1.0
- magento/framework: *
- magento/module-backend: *
- magento/module-cms: *
- magento/module-config: *
- magento/module-customer: *
- magento/module-store: *
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
The engine of the privacy consent suite: consents that a store shows as checkboxes in its forms, immutable versions of what each consent said, the rule that a required consent must be ticked, and a journal that keeps every consent given or withdrawn as evidence. It has no screen of its own and shows nothing to a shopper. The admin screens are in hryvinskyi/magento2-privacy-consent-admin-ui, the checkboxes in hryvinskyi/magento2-privacy-consent-frontend.
What it does for a store
- Consents. A consent is one statement a person can agree to, such as the privacy policy or marketing e-mails. It has a code, a title, the checkbox text, an optional link to a CMS page, a required flag, an active flag, a sort order, the store views it shows in, the forms it shows in, and the withdrawal actions it triggers. It is shown in a form of a store view only while it is active and assigned to both.
- The title is what customers read in the message that asks them to accept the consent and on their consents page, in every store view the consent shows in. A store with store views in several languages keeps one consent per language.
- What a save refuses. A consent is refused, with a message the admin can act on, when its title is blank or longer than 255 characters or its checkbox text is blank. An active consent is also refused when it has no store view or no form, or when its CMS page is not assigned to every store view it shows in. A page assigned to All Store Views passes. An inactive consent shows nowhere, so none of these three is checked for it: a consent whose store views were all deleted, or whose page lost a store view, can still be deactivated. The checkbox opens the page by its identifier in the visitor's store view, and the version hashes the page's content, so a page missing from a store view would open another page, or none, there.
- What a CMS page save refuses. A page save that would take away a store view an active consent linking to the page shows in is refused before anything is written, with the message
The page is linked to the consent "%1", which shows in store views the page would leave: %2., naming the consent's title and those store views. The page keeps its store views. Taking away a store view that no active linked consent shows in is saved. The check sits on the page resource's save, so the admin editor, the admin's mass actions, the repository and a page model saved directly are all held to it. - The checkbox text is plain text with at most one
{{link}}…{{/link}}span. The span becomes a link to the consent's CMS page; any other markup is printed as text. A text with more than one span, or with a{{outside it, is refused when the consent is saved. A span with no CMS page is shown as its plain words. - Versions. A version records what a person saw and agreed to. It holds the checkbox text, the identifier of the CMS page, the SHA-256 of the page's title, content heading and content, and the required flag. Saving a consent writes the next version (1, 2, …) only when that snapshot changed. A change of the title, sort order, active flag, store views, forms or withdrawal actions writes none, with one exception: the first save after the linked CMS page was deleted writes a version without a page, even when that save is a deactivation (see Limits). Saving the linked CMS page with a changed title, content heading or content writes the next version of every consent that links to it. A page saved with any other change (meta data, layout, active flag) writes none.
- A version can be checked by an auditor. The version's hash is the SHA-256 of the snapshot's canonical JSON: the keys
cms_page_content_hash,cms_page_identifier,requiredandtextin that order, no whitespace, slashes and non-ASCII characters written as they are. The page's content hash is the SHA-256 of{"content":…,"content_heading":…,"title":…}built the same way from the stored page (an empty field counts as""). - The required-consent rule. A form's submission without a required consent that the form shows in the store view is refused before the form's action runs. The refusal is one translatable sentence,
Please accept: %1., with the titles of the missing consents. A ticked code that the form does not show satisfies nothing. - The journal. One record for each ticked consent and each withdrawal. A record is written only for a box that was ticked and only after the form's own action succeeded (the account exists, the message was sent, the subscription was saved, the review was saved, the order was placed). An unticked optional box writes nothing. Each form submission writes its rows in one insert.
- The version a record names. Each ticked box posts the id of the version it showed. The record names that version when it belongs to the ticked consent, and the consent's current version when it does not (an unknown id, another consent's version, or none).
- The client address. A record keeps the address as the store view's mode allows (see Configuration). Changing the mode does not change records already written.
- Withdrawal. A customer can withdraw an optional consent whose latest record for them is
given. A customer's latest record is the latest of the records naming the customer, and only those. A record their e-mail address wrote as a guest is never theirs, whenever and wherever it was written: an account proves its password, not that it controls the address, and anyone can type an address into a form. A guest's latest record is the latest of the records of the address that name no customer. Two withdrawals of one consent at the same time are journaled once: the second waits until the first commits, then finds the consent withdrawn. Thewithdrawnrecord is written before any withdrawal action runs, so an action never runs for a withdrawal that was refused, and a failing action is logged and does not undo the record. A required consent, an inactive consent, a consent not shown in the store view and a consent never given cannot be withdrawn. A withdrawal that happened elsewhere (a newsletter unsubscribe) is journaled withrecordWithdrawnByAction, which runs no action. With the frontend package installed, every newsletter unsubscribe (the account page, the e-mail link, the admin, the API) is journaled this way as a withdrawal, with the form codeunsubscribe, of each consent that is shown in the subscriber's store view, carries thenewsletter_unsubscribewithdrawal action and was last given by that subscriber. A subscriber that belongs to a customer account is journaled for the account and for its address: a subscription made as a guest and linked to the account later gets itswithdrawnrecord for the address, naming no customer. - A consent the journal names cannot be deleted. The refusal says "The consent "%1" is recorded in the consent journal and cannot be deleted. Deactivate it instead." The database enforces it too: a record's consent and version keys restrict deletion. A consent that no record names can be deleted, and its versions go with it.
- The consent pages' cache. A page showing the checkboxes of a form carries the tag
hpc_consent_<form code>, such ashpc_consent_contact. Saving, deactivating or deleting a consent drops the cached pages of the forms it showed in before the save and of those it shows in after it, so a form the consent was taken off is refreshed too. A CMS page save that wrote a version drops the cached pages of the forms of the consents it versioned, after its transaction commits, so a request in between cannot cache the old wording again. The cached pages of other forms stay. - A customer's standing.
CustomerConsentsInterfacelists the consents shown in a store view with the customer's latest record (by the rule under Withdrawal), the version number and whether the customer may withdraw. - Anonymise and delete by subject. The journal never updates a record except to anonymise it. The anonymiser replaces only the fields the caller includes (
email,ip) with the caller's values, never the customer id, and setsanonymised_at. A repeated run changes nothing. Deleting a record is by id. - A log. Withdrawal action failures go to
var/log/hryvinskyi/privacy_consent.log. The log holds action codes, exception classes and codes, and never an e-mail address or a client address.
Requirements
- Magento Open Source or Adobe Commerce 2.4.x. The suite is built and tested on 2.4.9.
- PHP 8.1 to 8.5.
hryvinskyi/magento2-privacy-consent-api^1.0,hryvinskyi/magento2-clock^1.0 andhryvinskyi/magento2-client-address^1.0.- The platform's Store, Customer, CMS, Backend and Config modules.
Installation
Run from the Magento root. hryvinskyi/magento2-privacy-consent-pack installs this package with the rest of the suite.
composer require hryvinskyi/magento2-privacy-consent
bin/magento module:enable Hryvinskyi_PrivacyConsentApi Hryvinskyi_Clock Hryvinskyi_ClientAddress Hryvinskyi_PrivacyConsent
bin/magento setup:upgrade
bin/magento setup:di:compile # production mode only
bin/magento cache:flush
setup:upgrade creates the six tables listed under Registrations. Verify: bin/magento config:show hryvinskyi_privacy_consent/journal/ip_mode prints anonymised.
Without hryvinskyi/magento2-privacy-consent-admin-ui a store has no screen to create a consent, and without hryvinskyi/magento2-privacy-consent-frontend no form shows one.
Configuration
Both settings are read at store view scope. The admin screen for them is in hryvinskyi/magento2-privacy-consent-admin-ui; the defaults are declared here.
| Path | Default | Stored values | Effect |
|---|---|---|---|
hryvinskyi_privacy_consent/general/enabled |
1 |
0, 1 |
When 0, the store view shows no consent checkbox, asks for no consent and journals nothing. |
hryvinskyi_privacy_consent/journal/ip_mode |
anonymised |
anonymised, full, none |
What a record keeps of the client address. A stored value that is none of the three reads as anonymised. |
What each address mode stores:
| Mode | Client address 203.0.113.77 |
Client address 2001:db8:1:2:3::4 |
|---|---|---|
anonymised |
203.0.113.0 (the first 24 bits) |
2001:db8:1:: (the first 48 bits) |
full |
203.0.113.77 |
2001:db8:1:2:3::4 |
none |
nothing | nothing |
A text that is not exactly one address is kept by no mode. An IPv4 address written in IPv6 form (::ffff:203.0.113.77) is anonymised as IPv4. A record with no store view (an imported record whose store view is gone) is anonymised.
To set a mode for one store view:
bin/magento config:set --scope=stores --scope-code=<store_view_code> hryvinskyi_privacy_consent/journal/ip_mode full bin/magento cache:flush config
Replace <store_view_code> with the store view's code. Keeping a full address needs a legal basis of its own.
Admin permissions
The package declares the resources the admin screens check, and publishes their ids as the constants of Hryvinskyi\PrivacyConsent\Api\AclResourcesInterface. Give a role the ones it needs under System > Permissions > User Roles > Role Resources.
| Resource id | Title | Where |
|---|---|---|
Hryvinskyi_PrivacyConsent::privacy_consent |
Privacy Consents | Customers > Privacy Consents |
Hryvinskyi_PrivacyConsent::consents |
View consents | the consents listing and form |
Hryvinskyi_PrivacyConsent::consents_manage |
Manage consents | create, save, delete and disable a consent |
Hryvinskyi_PrivacyConsent::journal |
View the consent journal | the journal and the customer's Consents tab |
Hryvinskyi_PrivacyConsent::journal_export |
Export the consent journal | the journal's export |
Hryvinskyi_PrivacyConsent::journal_delete |
Delete journal records | deleting one record |
Hryvinskyi_PrivacyConsent::config |
Privacy Consents | Stores > Settings > Configuration |
The journal
One row of hryvinskyi_privacy_consent_record is one fact. Rows are never updated except by anonymisation, and deleted only by id.
| Column | What it proves |
|---|---|
record_id |
The row. Delete by id uses it. |
action |
given or withdrawn. |
consent_id |
The consent. Null for an imported row. The database refuses to delete a consent a row names. |
consent_code |
The consent's code when the row was written. An imported row holds its form code here. |
version_id |
The version the person was shown (see What it does for a store). A withdrawn row names the version of the latest given row, the agreement taken back. Null for an imported row. The database refuses to delete a version a row names. |
form_code |
The form that wrote the row: registration, contact, newsletter, newsletter_account, review or checkout. A withdrawal holds account (the consents page) or unsubscribe (a newsletter unsubscribe anywhere). |
store_id |
The store view. Null once the store view was deleted. |
created_at |
When the row was written, in UTC. |
customer_id |
The customer. Null for a guest, for an anonymous row (a guest's review), and once the customer was deleted: the row stays and keeps its e-mail address. |
email |
The subject's e-mail address, trimmed and lower-cased. Null for an anonymous row and after anonymisation. |
reference |
What was submitted: the order's increment id, or the review's id. |
ip |
The client address as the store view's mode kept it. |
import_source, import_ref |
The external journal a row was imported from and its key there. The pair is unique, so an import never adds a row twice. |
is_unverified |
1 when the external journal claims a consent but recorded no proof that a box was shown. Only an import sets it. |
anonymised_at |
When the e-mail address and client address were replaced, in UTC. |
An imported row names no consent, so it does not count as the latest record of a consent: it cannot be withdrawn, and it does not appear on a customer's consents page.
Limits
- The package records a consent only for a form that calls the recorder. The forms it covers are in the frontend package's README.
- Deleting a CMS page unlinks it from its consents (the foreign key sets the link to null). It writes no version: a consent's current version still names the deleted page's identifier and content hash until the consent is saved next, which writes a version without a page. That next save may be a deactivation: deactivating such a consent writes a version, because the snapshot no longer holds the page.
- A page that was loaded before an admin saved the consent and submitted after it records the version it showed, which is the earlier version of the same consent. That is the intended evidence.
- A CMS page save that wrote a version drops the consent pages' cache entries once the page save commits. A reverse proxy must honour the tags
hpc_consent_<form code>in its invalidation, as Magento's built-in full-page cache does. - A withdrawal locks the journal records it reads until its transaction commits, so a second withdrawal of the same consent by the same subject waits for it. When the withdrawal runs inside a caller's transaction (a newsletter subscription save), the locks are held until that transaction commits.
- The code of a consent can be changed by the repository, but the admin form makes it read-only once the consent is saved; keep it stable, because the journal stores it.
- The journal holds an e-mail address and a client address, which are personal data. A data-subject-rights module exports, anonymises and deletes them with the shopper's other data through the finder, anonymiser and deletion contracts below.
- Uninstalling the package (
module:disable,composer remove) leaves the six tables and their rows in place. Dropping them is a deliberate step of the operator, and it destroys the evidence.
Registrations
| Where | What |
|---|---|
| Tables | hryvinskyi_privacy_consent, hryvinskyi_privacy_consent_store, hryvinskyi_privacy_consent_form, hryvinskyi_privacy_consent_withdrawal_action, hryvinskyi_privacy_consent_version, hryvinskyi_privacy_consent_record. |
| Event | cms_page_save_after: Observer\VersionConsentsOnPageSave versions the consents that link to the page. |
| Plugin | before on Magento\Cms\Model\ResourceModel\Page::save: Plugin\Cms\KeepLinkedConsentStores refuses a page save that takes a store view away from an active linked consent. |
| Configuration defaults | config.xml: the two paths above. |
| ACL | acl.xml: the resources above. |
| Preferences | Every interface of the api package that has an implementation here, and the data types it builds through generated factories. |
Pool Hryvinskyi\PrivacyConsent\Model\Form\FormPool |
Argument providers: form code → FormProviderInterface. Empty here. |
Pool Hryvinskyi\PrivacyConsent\Model\Withdrawal\WithdrawalActionPool |
Argument actions: action code → WithdrawalActionInterface. Empty here. |
| Log | Virtual types Hryvinskyi\PrivacyConsent\Model\Log\Handler and …\Logger, file var/log/hryvinskyi/privacy_consent.log. |
The package registers no route, cron job, command, layout or template.
Extension points
A package adds to the engine through two pools. Each contribution goes in the contributing package's global etc/di.xml, never in an area file: an area file that names an array argument the global file already declares replaces it instead of merging with it.
Add a form
The form pool is what the admin offers under Forms and what the recorder accepts. Implement Hryvinskyi\PrivacyConsentApi\Api\FormProviderInterface (a code and a label the admin shows), then register it in your package's etc/di.xml:
<type name="Hryvinskyi\PrivacyConsent\Model\Form\FormPool"> <arguments> <argument name="providers" xsi:type="array"> <item name="quote_request" xsi:type="object">Vendor\Module\Model\Form\QuoteRequestForm</item> </argument> </arguments> </type>
The pool is keyed by each provider's own code. An item that is not a form provider, or two items with one code, fail when the pool is built. A form on the storefront needs more than this entry; the frontend package's README lists every piece.
Add a withdrawal action
Implement Hryvinskyi\PrivacyConsentApi\Api\WithdrawalActionInterface (a code, a label the admin shows, and execute(customerId, storeId, consent)), then register it:
<type name="Hryvinskyi\PrivacyConsent\Model\Withdrawal\WithdrawalActionPool"> <arguments> <argument name="actions" xsi:type="array"> <item name="close_account" xsi:type="object">Vendor\Module\Model\Withdrawal\CloseAccount</item> </argument> </arguments> </type>
The admin lists the action under When Withdrawn of each consent. An action code stored with a consent whose action is no longer registered is skipped when the consent is withdrawn.
Other contracts
ConsentRecorderInterface::recordGivenwrites thegivenrows of a submission your own form collected.ConsentRecordImportInterface::importimports the rows of an external journal, for a package that moves another module's journal into this one. It skips a source and reference already present, so it can be run again.ConsentRecordFinderInterface,ConsentRecordAnonymiserInterfaceandConsentRecordDeletionInterfacefind, anonymise and delete the records of one subject, for a data-subject-rights module.
Tests
Run from the Magento root.
- Unit:
vendor/bin/phpunit -c app/code/Hryvinskyi/PrivacyConsent/phpunit.xml.dist. - Integration: the platform's harness in
dev/tests/integration, against its own database, one run at a time.cd dev/tests/integration && php -d memory_limit=4G ../../../vendor/bin/phpunit -c phpunit.xml ../../../app/code/Hryvinskyi/PrivacyConsent/Test/Integration. A change todb_schema.xmlneeds the harness's database rebuilt. - Static analysis: PHPStan 2.x at level 10 with the package's
phpstan.neon.dist(phpstan analyse -c app/code/Hryvinskyi/PrivacyConsent/phpstan.neon.dist --level=10), andvendor/bin/phpcs --standard=app/code/Hryvinskyi/PrivacyConsent/phpcs.xml.dist app/code/Hryvinskyi/PrivacyConsent.
Translations
The wording ships in en_US and 25 more locales: the official languages of the European Union, Norwegian Bokmål and Ukrainian. The translations are machine-made; have them read by a native speaker before a store relies on them, and override a phrase in a theme or a language pack.