item
request was:
the best is in entityManager -> fieldAttribute -> checkbox : isEncrypted

so it's outOfBox and we can decide witch is encrypted or not.
<?php
namespace Espo\Custom\Core\Utils;
class Encryption
{
private $secret_key = null;
private $iv = null;
protected $method = 'AES-256-CBC';
public function __construct($secret_key, $iv = null, $method = 'AES-256-CBC')
{
$this->secret_key = $secret_key;
if ($iv) {
$this->iv = hex2bin($iv);
}
}
public static function get_random_key($length)
{
return openssl_random_pseudo_bytes($length, $crypto_strong);
}
public function get_algorithms()
{
return $this->method;
}
public function set_algorithms($algorithm)
{
$methods = $this->available_algorithms();
if (isset($methods[$algorithm])) {
$this->method = $algorithm;
} else {
throw new \Exception('Encryption ['.$algorithm.'] method is not available', 1);
}
}
public function available_algorithms()
{
return openssl_get_cipher_methods();
}
public function encrypt($string_data)
{ // Use the openssl_encrypt to generate the ciphertext for the given $string_data and return the same.}
public function get_ivlen()
{
return $this->ivlen;
}
public function get_iv()
{
return bin2hex($this->iv);
}
public function decrypt($string_cypher)
{
// Use the openssl_decrypt to generate the string text for the given $string_cypher and return the same.
}
}
"personalData": {
"readOnly": false,
"maxLength": 11,
"type": "foreign",
"link": "personalData",
"field": "personalDataString",
"view": "custom:views/fields/foreign-varchar",
"tooltip": true,
"isCustom": true,
"exportDisabled": true,
"isEncrypted ": true,
"isPersonalData": true
},
Espo.define('custom:views/fields/foreign-varchar', 'views/fields/foreign-varchar', function (Dep) { return Dep.extend({ type: 'foreign', initInlineEdit: function () { this.$el.addClass('hidden'); var $cell = this.getCellElement(); var $editLink = $('<a href="javascript:" class="pull-right inline-edit-link hidden"><span class="fas fa-pencil-alt fa-sm"></span></a>'); if ($cell.length == 0) { this.listenToOnce(this, 'after:render', this.initInlineEdit, this); return; } $cell.prepend($editLink); $cell.on('mouseenter', function (e) { if (this.disabled || this.readOnly) { return; } }.bind(this)).on('mouseleave', function (e) { e.stopPropagation(); }.bind(this)); $editLink.on('click', function () { this.notify(this.translate('pleaseWait', 'messages')); this.ajaxGetRequest('API path to get the descrypted string' + this.model.id).done(function (response) { this.model.set('personalData', response.personalDataDecrypt); this.notify(false); }.bind(this)); this.inlineEdit(); }.bind(this)); }, inlineEditClose: function (dontReset) { this.trigger('inline-edit-off'); this._isInlineEditMode = false; if (this.mode != 'edit') { return; } this.setMode('detail'); this.once('after:render', function () { this.removeInlineEditLinks(); }, this); if (!dontReset) { this.model.set(this.initialAttributes); } this.reRender(true); this.trigger('after:inline-edit-off'); this.initInlineEdit(); } });
protected function afterUpdateEntity(Entity $entity, $data)
{
if (($entity->isAttributeChanged('personalData'))) {
$this->getRecordService('Contact')->encryptPersonalData($entity->get('personalData'));
}
}
public function getActionGetSSNSuffix($params, $data, $request)
{
return $this->getRecordService()->decryptPersonalData();
}
<?php namespace Espo\Custom\Hooks\Contact; use Espo\ORM\Entity; class ContactHooks extends \Espo\Core\Hooks\Base { public function beforeSave(Entity $entity, array $options=array()) { // get the un-encrypted description from the model $description = $entity->get('description'); // encrypt the description field prior to storing in database $fileM = new \Espo\Core\Utils\File\Manager(); $config = new \Espo\Core\Utils\Config($fileM); $descriptionHash = new \Espo\Core\Utils\PasswordHash($config); $encodedDescription = $descriptionHash->hash($description,true); // store the encoded description in the model $entity->set('description', $encodedDescription); } }
Leave a comment: