EncryptionProfile.php in Encrypt 8.3
File
src/Entity/EncryptionProfile.php
View source
<?php
namespace Drupal\encrypt\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Component\Utility\Crypt;
use Drupal\encrypt\EncryptionProfileInterface;
use Drupal\encrypt\Exception\EncryptException;
use Drupal\encrypt\EncryptionMethodInterface;
use Drupal\encrypt\EncryptionMethodPluginCollection;
use Drupal\key\Entity\Key;
class EncryptionProfile extends ConfigEntityBase implements EncryptionProfileInterface, EntityWithPluginCollectionInterface {
use StringTranslationTrait;
protected $id;
protected $label;
protected $encryption_method;
protected $pluginCollection;
protected $encryption_method_configuration = [];
protected $encryption_key;
protected $encryption_key_entity;
public function getPluginCollections() {
return [
'encryption_method_configuration' => $this
->getPluginCollection(),
];
}
protected function getPluginCollection() {
if (!$this->pluginCollection && $this->encryption_method) {
$this->pluginCollection = new EncryptionMethodPluginCollection(\Drupal::service('plugin.manager.encrypt.encryption_methods'), $this->encryption_method, $this->encryption_method_configuration);
}
return $this->pluginCollection;
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
$errors = $this
->validate();
if (!empty($errors)) {
throw new EncryptException(implode(';', $errors));
}
}
public function getEncryptionMethod() {
if ($this
->getEncryptionMethodId()) {
return $this
->getPluginCollection()
->get($this
->getEncryptionMethodId());
}
}
public function getEncryptionMethodId() {
return $this->encryption_method;
}
public function setEncryptionMethod(EncryptionMethodInterface $encryption_method) {
$this->encryption_method = $encryption_method
->getPluginId();
$this
->getPluginCollection()
->addInstanceID($encryption_method
->getPluginId());
}
public function getEncryptionKey() {
if (!isset($this->encryption_key_entity) || $this->encryption_key_entity
->id() != $this
->getEncryptionKeyId()) {
$this->encryption_key_entity = $this
->getKeyRepository()
->getKey($this
->getEncryptionKeyId());
}
return $this->encryption_key_entity;
}
public function getEncryptionKeyId() {
return $this->encryption_key;
}
public function setEncryptionKey(Key $key) {
$this->encryption_key_entity = $key;
$this->encryption_key = $key
->id();
}
public function validate($text = NULL) {
$errors = [];
if (!$this
->getEncryptionMethodId()) {
$errors[] = $this
->t('No encryption method selected.');
}
if (!$this
->getEncryptionKeyId()) {
$errors[] = $this
->t('No encryption key selected.');
}
if ($this
->getEncryptionMethodId() && $this
->getEncryptionKeyId()) {
$encryption_method = $this
->getEncryptionMethod();
if (!$encryption_method) {
$errors[] = $this
->t('The encryption method linked to this encryption profile does not exist.');
}
$selected_key = $this
->getEncryptionKey();
if (!$selected_key) {
$errors[] = $this
->t('The key linked to this encryption profile does not exist.');
}
if (empty($errors)) {
$allowed_key_types = $encryption_method
->getPluginDefinition()['key_type'];
if (!empty($allowed_key_types)) {
$selected_key_type = $selected_key
->getKeyType();
if (!in_array($selected_key_type
->getPluginId(), $allowed_key_types)) {
$errors[] = $this
->t('The selected key cannot be used with the selected encryption method.');
}
}
$encryption_method = $this
->getEncryptionMethod();
if (!$text) {
$text = Crypt::randomBytesBase64();
}
$dependency_errors = $encryption_method
->checkDependencies($text, $selected_key
->getKeyValue());
$errors = array_merge($errors, $dependency_errors);
}
}
return $errors;
}
public function calculateDependencies() {
parent::calculateDependencies();
$this
->addDependency('config', $this
->getEncryptionKey()
->getConfigDependencyName());
return $this;
}
protected function getKeyRepository() {
return \Drupal::service('key.repository');
}
}