Skip to content
Snippets Groups Projects

Issue #3334840: Drupal\Core\Entity\EntityStorageException: SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'mail'

Closed Issue #3334840: Drupal\Core\Entity\EntityStorageException: SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'mail'
Closed Patrick Kenny requested to merge issue/dbee-3334840:fix-install-hook into 3.x
1 file
+ 56
41
Compare changes
  • Side-by-side
  • Inline
+ 56
41
@@ -16,51 +16,38 @@ use Drupal\encrypt\Entity\EncryptionProfile;
* installed or enabled.
*/
function dbee_install($is_syncing) {
// Call dbee module on an ultimate stage to improve compatibility with custom
// modules : dbee decrypt functions will be call early thanks to the
// dbee_module_implements_alter() function. Email address will be available
// for custom modules. It allows to fix custom modules to.
// Call dbee module at a later stage to improve compatibility with custom
// modules: dbee decrypt functions will be called early thanks to the
// dbee_module_implements_alter() function. Email addresses will be accessible
// to custom modules. This makes it possible to fix custom modules, too.
module_set_weight('dbee', 10);
// Add configuration data for the dbee module.
// Check if a key already exists.
if (_dbee_create_encryption_key(DBEE_DEFAULT_KEY_NAME, DBEE_DEFAULT_KEY_BYTES, DBEE_DEFAULT_KEY_FILENAME, $is_syncing) && _dbee_create_encryt_profile(DBEE_ENCRYPT_NAME, DBEE_DEFAULT_KEY_NAME, DBEE_DEFAULT_ENCRYPTION_METHOD, $is_syncing)) {
// Dbee Key and encryption profile exist or have just been created.
// Edit mail and init storage length.
// Parameters.
$user_table = 'users_field_data';
$mail_index_name = 'user_field__mail';
// Drop mail index.
/** @var \Drupal\Core\Database\Connection $connection */
$connection = \Drupal::service('database');
if ($connection->schema()->indexExists($user_table, $mail_index_name)) {
$connection->schema()->dropIndex($user_table, $mail_index_name);
}
$spec = [
'type' => 'text',
'length' => 600,
];
if ($connection->databaseType() === 'pgsql') {
unset($spec['length']);
}
// Increase mail and init storage length.
foreach (['mail', 'init'] as $field) {
$connection->schema()->changeField($user_table, $field, $field, $spec, []);
}
// Don't recreate the index.
// If successful, encrypt all existing email addresses.
require_once \Drupal::service('extension.list.module')->getPath('dbee') . '/dbee.users.inc';
dbee_update_crypt_all('encrypt');
if (_dbee_create_encryption_key(DBEE_DEFAULT_KEY_NAME, DBEE_DEFAULT_KEY_BYTES, DBEE_DEFAULT_KEY_FILENAME, $is_syncing)) {
// Dbee key exists or has just been created.
_dbee_lengthen_email_fields();
// Enable the dbee extra_field_only on the user view page.
if (!$is_syncing && ($display = \Drupal::entityTypeManager()
->getStorage('entity_view_display')
->load('user.user.default'))) {
if ($components = $display->getComponents()) {
if (empty($components['dbee'])) {
$options = ['weight' => 15];
$display->setComponent('dbee', $options);
$display->save();
// If the module is installed by config sync,
// do not immediately encrypt all email addresses.
// If the module is being installed not by config sync,
// create an encryption profile and encrypt all email addresses.
if (_dbee_create_encrypt_profile(DBEE_ENCRYPT_NAME, DBEE_DEFAULT_KEY_NAME, DBEE_DEFAULT_ENCRYPTION_METHOD, $is_syncing)) {
// Don't recreate the index.
// Encrypt all existing email addresses.
require_once \Drupal::service('extension.list.module')->getPath('dbee') . '/dbee.users.inc';
dbee_update_crypt_all('encrypt');
// Enable the dbee extra_field_only on the user view page.
if (!$is_syncing && ($display = \Drupal::entityTypeManager()
->getStorage('entity_view_display')
->load('user.user.default'))) {
if ($components = $display->getComponents()) {
if (empty($components['dbee'])) {
$options = ['weight' => 15];
$display->setComponent('dbee', $options);
$display->save();
}
}
}
}
@@ -330,7 +317,7 @@ function _dbee_ensure_encryption_key_exists($bytes, $filename) {
* TRUE if the Encryption Profile entity already exists or has been
* successfully created, or FALSE if an error has occurred.
*/
function _dbee_create_encryt_profile($encrypt_profile_id, $key_id, $encryption_method = 'real_aes', $is_syncing = FALSE) {
function _dbee_create_encrypt_profile($encrypt_profile_id, $key_id, $encryption_method = 'real_aes', $is_syncing = FALSE) {
if ($is_syncing) {
return FALSE;
}
@@ -363,3 +350,31 @@ function _dbee_create_encryt_profile($encrypt_profile_id, $key_id, $encryption_m
\Drupal::logger('dbee')->critical('Dbee encryption profile was not saved on install.');
return FALSE;
}
/**
* Increase the length of the user mail and init fields.
*
* We need to do this because the encrypted data can be longer
* than the length of a valid email address.
*/
function _dbee_lengthen_email_fields() {
$user_table = 'users_field_data';
$mail_index_name = 'user_field__mail';
// Drop the mail index.
/** @var \Drupal\Core\Database\Connection $connection */
$connection = \Drupal::service('database');
if ($connection->schema()->indexExists($user_table, $mail_index_name)) {
$connection->schema()->dropIndex($user_table, $mail_index_name);
}
$spec = [
'type' => 'text',
'length' => 600,
];
if ($connection->databaseType() === 'pgsql') {
unset($spec['length']);
}
// Increase the mail and init storage length.
foreach (['mail', 'init'] as $field) {
$connection->schema()->changeField($user_table, $field, $field, $spec, []);
}
}
Loading