hryvinskyi / magento2-privacy-consent-api
Contracts of the privacy consent suite: consents and their versions, the consent journal, forms, withdrawal actions.
Package info
github.com/hryvinskyi/magento2-privacy-consent-api
Type:magento2-module
pkg:composer/hryvinskyi/magento2-privacy-consent-api
Requires
- php: ~8.1.0||~8.2.0||~8.3.0||~8.4.0||~8.5.0
- magento/framework: *
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
The published contracts of the privacy consent suite for Magento 2: consents and their versions, the forms that show them, the consent journal and withdrawal actions. It holds interfaces, two closed sets and one exception. Every type is marked @api. The implementations are in hryvinskyi/magento2-privacy-consent, and a package that adds a form, a withdrawal action or an import depends on this package only.
What it does for a store
Nothing on its own. A store installs it because another package of the suite requires it. A developer reads it to learn what the suite offers and what a package of their own may call or implement.
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.
- Nothing else: the only requirement is
magento/framework.
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-api
bin/magento module:enable Hryvinskyi_PrivacyConsentApi
bin/magento setup:upgrade
bin/magento setup:di:compile # production mode only
bin/magento cache:flush
Verify: bin/magento module:status Hryvinskyi_PrivacyConsentApi lists the module as enabled.
An interface without an implementation cannot be resolved. Install hryvinskyi/magento2-privacy-consent for the implementations.
Configuration
None. The contracts are not configured. The settings the engine reads are described in the core package.
Limits
- The package registers no preference. A class that asks for one of these interfaces gets the core's implementation only while the core is installed.
- The constants of the contracts carry no type, because the package supports PHP 8.1.
- An
Api/Datatype is read-only and is built through the matching factory interface.PostedConsentInterfaceis the exception: it has setters, so the web API can fill it from the checkout request.
Contracts
All names are under Hryvinskyi\PrivacyConsentApi\Api.
Closed sets and constants
| Type | What it holds |
|---|---|
Data\ConsentAction |
The journal's action, stored as given or withdrawn. |
Data\IpMode |
How a record keeps the client address, stored as anonymised, full or none. |
Data\ConsentInterface::CACHE_TAG |
hpc_consent, the prefix of the full-page cache tags of the pages that show consents. A page showing the checkboxes of a form carries hpc_consent_<form code>, such as hpc_consent_contact. |
Data\ConsentInterface::CODE_PATTERN |
The spelling of a consent code: /^[a-z][a-z0-9_]{0,63}$/D. |
ConsentWithdrawalInterface::FORM_CODE_ACCOUNT, FORM_CODE_UNSUBSCRIBE |
The two form codes the journal holds for a withdrawal: account (the consents page) and unsubscribe (a newsletter unsubscribe anywhere). They are paths, not forms a consent can be assigned to. |
RecordAnonymisationInterface::FIELD_EMAIL, FIELD_IP |
The two journal fields an anonymisation can replace: email and ip. |
Consents and versions
| Interface | What it is for |
|---|---|
ConsentConfigInterface |
isEnabled(storeId) and getIpMode(storeId). A stored value that is no mode reads as anonymised. |
Data\ConsentInterface |
A consent as saved: code, title, checkbox text, CMS page id, required, active, sort order, store views, form codes and withdrawal action codes. |
Data\ConsentDraftInterface, ConsentDraftFactoryInterface |
The unchecked input the repository saves; the id is null for a new consent. |
ConsentRepositoryInterface |
getById, getByCode, getList, save (checks the draft and writes the next version when what a person sees changed) and deleteById (refused while the journal names the consent). Both clean the cache tags of the forms the consent showed in and shows in. |
Data\ConsentVersionInterface, ConsentVersionRepositoryInterface |
A version never changes: number, SHA-256 hash, text, CMS page identifier, hash of the page's readable content, required flag, creation time in UTC. The repository reads only. |
ShownConsentsInterface, Data\ShownConsentInterface |
The consents a form of a store view shows, each with its current version. |
Data\ConsentSearchResultsInterface |
The result of ConsentRepositoryInterface::getList. |
Forms and recording
| Interface | What it is for |
|---|---|
FormProviderInterface, FormListInterface |
A form's code and label, and the list of registered forms. |
Data\ConsentSubjectInterface, ConsentSubjectFactoryInterface |
The person a record is about: forCustomer(id, email), forGuest(email) or anonymous(). The e-mail is trimmed and lower-cased. |
Data\PostedConsentInterface, PostedConsentFactoryInterface |
One ticked box: a consent code and the id of the version it showed. A version id below 1 reads as none. The checkout sends it as the privacy_consents extension attribute. |
Data\ConsentSubmissionInterface, ConsentSubmissionFactoryInterface |
What a form hands over after its own action succeeded: form code, store view, subject, ticked boxes, reference, raw client address. |
ConsentRecorderInterface |
recordGiven(submission) writes one given row for each ticked consent the form showed. |
RequiredConsentCheckInterface, Exception\MissingConsentException |
missing(...) and assertAccepted(...). The exception's message is the one translatable sentence Please accept: %1. with the titles joined by , . |
The journal
| Interface | What it is for |
|---|---|
Data\ConsentRecordInterface, ConsentRecordRepositoryInterface, Data\ConsentRecordSearchResultsInterface |
Read-only access to a journal record, by id or by search criteria. |
ConsentRecordDeletionInterface |
deleteById(recordId). An unknown id throws NoSuchEntityException. It opens no transaction. |
Data\RecordScopeInterface, RecordScopeFactoryInterface |
One subject's records: a customer id, or an e-mail within store views (an e-mail scope matches only records that name no customer). |
ConsentRecordFinderInterface, Data\ConsentRecordDetailInterface |
The ids, or the records with consent title and version number, of a scope. |
ConsentRecordAnonymiserInterface, RecordAnonymisationInterface |
Replaces only the fields the caller's port includes, never the customer id, and sets the anonymisation time. A record that already holds its replacements is not counted. It opens no transaction. |
Data\ImportedRecordInterface, ImportedRecordFactoryInterface, ConsentRecordImportInterface |
The neutral import of an external journal: skips a source and reference already present, applies the store view's address mode, and fails the batch on any other error. |
Withdrawal
| Interface | What it is for |
|---|---|
WithdrawalActionInterface, WithdrawalActionListInterface |
Something the store does when a consent is withdrawn, and the list of registered actions. |
ConsentWithdrawalInterface |
withdraw(customerId, consentCode, storeId, clientAddress) for the consents page, and recordWithdrawnByAction(...) for a withdrawal that happened elsewhere (it writes the journal row and runs no action). A customer's latest record is read from the records naming the customer only. Two withdrawals of one consent at once are journaled once. |
CustomerConsentsInterface, Data\CustomerConsentStateInterface |
A customer's standing for each consent shown in a store view: the latest record, the version number, and whether it can be withdrawn. |
Registrations
None. The package ships no di.xml, route, table, ACL resource, event or command. The admin permissions and their ids (Hryvinskyi\PrivacyConsent\Api\AclResourcesInterface) belong to the core package, which declares them in its acl.xml.
Extension points
The contracts name the extension points; the core package describes how to register one:
- a form:
FormProviderInterface, registered in theprovidersargument of the form list; - a withdrawal action:
WithdrawalActionInterface, registered in theactionsargument of the action list; - an import of an external journal:
ConsentRecordImportInterface.
Tests
Run from the Magento root.
- Unit:
vendor/bin/phpunit -c app/code/Hryvinskyi/PrivacyConsentApi/phpunit.xml.dist. It pins the stored values of the two closed sets and the refusal sentence. - Static analysis: PHPStan 2.x at level 10 with the package's
phpstan.neon.dist(phpstan analyse -c app/code/Hryvinskyi/PrivacyConsentApi/phpstan.neon.dist --level=10), andvendor/bin/phpcs --standard=app/code/Hryvinskyi/PrivacyConsentApi/phpcs.xml.dist app/code/Hryvinskyi/PrivacyConsentApi.
Translations
The refusal sentence Please accept: %1. 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.