J3.x

Integrate Extensions with the Privacy Component: Difference between revisions

From Joomla! Documentation

Created page with "The information below is provided to extension developers to help create integrations with the Privacy Tool Suite. == Privacy Related Extension..."
 
No edit summary
Line 27: Line 27:
$policy['published'] = true;
$policy['published'] = true;
$policy['editLink']  = ''; // The link to the item's edit page, processed through JRoute, i.e. JRoute::_('index.php?option=com_content&task=article.edit&id=1');
$policy['editLink']  = ''; // The link to the item's edit page, processed through JRoute, i.e. JRoute::_('index.php?option=com_content&task=article.edit&id=1');
}
</source>
== Add Data to Export Requests ==
As a convenience, there are several helper methods in the <tt>PrivacyPlugin</tt> class and it is recommended that privacy plugins extend this class to inherit those helpers (this is similar to the <tt>FinderIndexerAdapter</tt> class for Smart Search plugins as an example).
<source lang="php">
JLoader::register('PrivacyPlugin', JPATH_ADMINISTRATOR . '/components/com_privacy/helpers/plugin.php');
class PlgPrivacyContent extends PrivacyPlugin {}
</source>
To add data to an export request, a plugin in the privacy or system plugin groups must subscribe to the <tt>onPrivacyExportRequest</tt> event.
The event receives two arguments:
* <tt>$request</tt> - A <tt>PrivacyTableRequest</tt> object containing the information request record from the database
* <tt>$user</tt> - If there is an account for the email address of the information request, a <tt>Joomla\CMS\User\User</tt> object is given containing the user account data
The event must return an array of <tt>PrivacyExportDomain</tt> objects containing the data to be exported for a given domain.  If the plugin has no data, it must return an empty array.
A <tt>PrivacyExportDomain</tt> data object typically represents the data found from a table in the database and is made up of three elements:
* Domain name - A name to identify the domain
* Domain description - A short description of the data contained in the domain
* Domain items - An array of <tt>PrivacyExportItem</tt> data objects containing all items within the domain
A <tt>PrivacyExportItem</tt> data object represents a single item within the data domain and is made up of two elements:
* Item ID - The primary identifier for this item within the domain, typically this will be the database record's primary key
* Item fields - An array of <tt>PrivacyExportField</tt> data objects containing each field for the item
The <tt>PrivacyExportField</tt> data object represents a single field within an item and is made up of two elements:
* Field name - The name of the field
* Field value - The field's value
The general workflow for the export process is this:
* Validate the plugin should actually process data
* Query the data from the database
* Create a domain for the results (the <tt>createDomain</tt> method inherited from the <tt>PrivacyPlugin</tt> class can help with this)
* Add items for each row
** The <tt>createItemFromArray</tt> method inherited from the <tt>PrivacyPlugin</tt> class can be used to create a <tt>PrivacyExportItem</tt> object from an array (this should be used in conjunction with the database's <tt>loadAssocList</tt> method
** The <tt>createItemForTable</tt> method inherited from the <tt>PrivacyPlugin</tt> class can be used to create a <tt>PrivacyExportItem</tt> object from a <tt>Joomla\CMS\Table\Table</tt> object
* Return the domain
Below is an example for exporting articles created by a user, including custom field data.
<source lang="php">
use Joomla\CMS\User\User;
JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
JLoader::register('PrivacyPlugin', JPATH_ADMINISTRATOR . '/components/com_privacy/helpers/plugin.php');
class PlgPrivacyContent extends PrivacyPlugin
{
/**
* @var  JDatabaseDriver
*/
protected $db;
/**
* @var  array
*/
protected $contents = array();
public function onPrivacyExportRequest(PrivacyTableRequest $request, User $user = null)
{
// This plugin only processes data for registered user accounts
if (!$user)
{
return array();
}
$domains  = array();
$domains[] = $this->createContentDomain($user);
// Create domains for each article's custom fields
foreach ($this->contents as $content)
{
$domains[] = $this->createContentCustomFieldsDomain($content);
}
return $domains;
}
private function createContentDomain(JUser $user)
{
$domain = $this->createDomain('user content', 'Joomla! user content data');
$query = $this->db->getQuery(true)
->select('*')
->from($this->db->quoteName('#__content'))
->where($this->db->quoteName('created_by') . ' = ' . (int) $user->id)
->order($this->db->quoteName('ordering') . ' ASC');
$items = $this->db->setQuery($query)->loadAssocList();
// Add each article to the domain
foreach ($items as $item)
{
$domain->addItem($this->createItemFromArray($item));
// Store the article for use in the custom fields processing
$this->contents[] = (object) $item;
}
return $domain;
}
private function createContentCustomFieldsDomain($content)
{
$domain = $this->createDomain('content custom fields', 'Joomla! content custom fields data');
// Get item's fields, also preparing their value property for manual display
$fields = FieldsHelper::getFields('com_content.article', $content);
foreach ($fields as $field)
{
$fieldValue = is_array($field->value) ? implode(', ', $field->value) : $field->value;
$data = array(
'content_id'  => $content->id,
'field_name'  => $field->name,
'field_title' => $field->title,
'field_value' => $fieldValue,
);
$domain->addItem($this->createItemFromArray($data));
}
return $domain;
}
}
}
</source>
</source>

Revision as of 02:34, 29 August 2018

The information below is provided to extension developers to help create integrations with the Privacy Tool Suite.

Privacy Related Extension Capabilities

The new privacy component has a Capabilities section which allows extensions to report privacy related capabilities. The aim for this section is to help users to understand what an extension may do related to personal data on users and help site owners plan accordingly. For details on this section, including how a plugin should integrate with it, please see the Report Extension Capabilities in Privacy Component page.

Check for a Published Privacy Policy

The privacy component's health check notifies users if there is a privacy policy published on the website. This check is performed by the onPrivacyCheckPrivacyPolicyPublished event which can be subscribed to by plugins in the privacy, system, and user plugin groups. The event receives an associative array by reference with two keys as its argument:

  • "published" - A boolean value indicating that there is a published policy
  • "editLink" - The URL to edit the policy item, this is displayed if there is a privacy policy published

As a best practice, it is suggested that plugins first check if the "published" flag is already set to true and not make further changes to the data array if so. Note that the System - Privacy Consent core plugin will process this event.

public function onPrivacyCheckPrivacyPolicyPublished(&$policy)
{
	// If another plugin has already indicated a policy is published, we won't change anything here
	if ($policy['published'])
	{
		return;
	}

	// Do stuff to find the privacy policy data

	$policy['published'] = true;
	$policy['editLink']  = ''; // The link to the item's edit page, processed through JRoute, i.e. JRoute::_('index.php?option=com_content&task=article.edit&id=1');
}

Add Data to Export Requests

As a convenience, there are several helper methods in the PrivacyPlugin class and it is recommended that privacy plugins extend this class to inherit those helpers (this is similar to the FinderIndexerAdapter class for Smart Search plugins as an example).

JLoader::register('PrivacyPlugin', JPATH_ADMINISTRATOR . '/components/com_privacy/helpers/plugin.php');

class PlgPrivacyContent extends PrivacyPlugin {}

To add data to an export request, a plugin in the privacy or system plugin groups must subscribe to the onPrivacyExportRequest event.

The event receives two arguments:

  • $request - A PrivacyTableRequest object containing the information request record from the database
  • $user - If there is an account for the email address of the information request, a Joomla\CMS\User\User object is given containing the user account data

The event must return an array of PrivacyExportDomain objects containing the data to be exported for a given domain. If the plugin has no data, it must return an empty array.

A PrivacyExportDomain data object typically represents the data found from a table in the database and is made up of three elements:

  • Domain name - A name to identify the domain
  • Domain description - A short description of the data contained in the domain
  • Domain items - An array of PrivacyExportItem data objects containing all items within the domain

A PrivacyExportItem data object represents a single item within the data domain and is made up of two elements:

  • Item ID - The primary identifier for this item within the domain, typically this will be the database record's primary key
  • Item fields - An array of PrivacyExportField data objects containing each field for the item

The PrivacyExportField data object represents a single field within an item and is made up of two elements:

  • Field name - The name of the field
  • Field value - The field's value

The general workflow for the export process is this:

  • Validate the plugin should actually process data
  • Query the data from the database
  • Create a domain for the results (the createDomain method inherited from the PrivacyPlugin class can help with this)
  • Add items for each row
    • The createItemFromArray method inherited from the PrivacyPlugin class can be used to create a PrivacyExportItem object from an array (this should be used in conjunction with the database's loadAssocList method
    • The createItemForTable method inherited from the PrivacyPlugin class can be used to create a PrivacyExportItem object from a Joomla\CMS\Table\Table object
  • Return the domain

Below is an example for exporting articles created by a user, including custom field data.

use Joomla\CMS\User\User;

JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
JLoader::register('PrivacyPlugin', JPATH_ADMINISTRATOR . '/components/com_privacy/helpers/plugin.php');

class PlgPrivacyContent extends PrivacyPlugin
{
	/**
	 * @var  JDatabaseDriver
	 */
	protected $db;

	/**
	 * @var  array
	 */
	protected $contents = array();

	public function onPrivacyExportRequest(PrivacyTableRequest $request, User $user = null)
	{
		// This plugin only processes data for registered user accounts
		if (!$user)
		{
			return array();
		}

		$domains   = array();
		$domains[] = $this->createContentDomain($user);

		// Create domains for each article's custom fields
		foreach ($this->contents as $content)
		{
			$domains[] = $this->createContentCustomFieldsDomain($content);
		}

		return $domains;
	}

	private function createContentDomain(JUser $user)
	{
		$domain = $this->createDomain('user content', 'Joomla! user content data');

		$query = $this->db->getQuery(true)
			->select('*')
			->from($this->db->quoteName('#__content'))
			->where($this->db->quoteName('created_by') . ' = ' . (int) $user->id)
			->order($this->db->quoteName('ordering') . ' ASC');

		$items = $this->db->setQuery($query)->loadAssocList();

		// Add each article to the domain
		foreach ($items as $item)
		{
			$domain->addItem($this->createItemFromArray($item));

			// Store the article for use in the custom fields processing
			$this->contents[] = (object) $item;
		}

		return $domain;
	}

	private function createContentCustomFieldsDomain($content)
	{
		$domain = $this->createDomain('content custom fields', 'Joomla! content custom fields data');

		// Get item's fields, also preparing their value property for manual display
		$fields = FieldsHelper::getFields('com_content.article', $content);

		foreach ($fields as $field)
		{
			$fieldValue = is_array($field->value) ? implode(', ', $field->value) : $field->value;

			$data = array(
				'content_id'  => $content->id,
				'field_name'  => $field->name,
				'field_title' => $field->title,
				'field_value' => $fieldValue,
			);

			$domain->addItem($this->createItemFromArray($data));
		}

		return $domain;
	}
}