Source of file EncryptedDecimal.php
Size: 2,198 Bytes - Last Modified: 2021-12-23T10:50:52+00:00
/var/www/docs.ssmods.com/process/src/code/fieldtypes/EncryptedDecimal.php
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 | <?php require_once 'EncryptedFieldTrait.php'; //Traits are not auto-loaded by SilverStripe /** * Class EncryptedDatetime * @package EncryptAtRest\Fieldtypes * * This class wraps around a SS_Datetime, storing the value in the database as an encrypted string in a varchar field, but * returning it to SilverStripe as a decrypted SS_Datetime object. */ class EncryptedDecimal extends Decimal { use EncryptedFieldTrait; public $is_encrypted = true; /** * @var AtRestCryptoService */ protected $service; public function __construct($name) { parent::__construct($name); $this->service = Injector::inst()->get('AtRestCryptoService'); } public function setValue($value, $record = array()) { $this->setEncryptionKeyFromRecord($record); if (array_key_exists($this->name, $record) && $value === null) { $this->value = $record[$this->name]; } else { $this->value = $value; } } public function getDecryptedValue($value) { // Test if we're actually an encrypted value; if (ctype_xdigit($value) && strlen($value) > 130) { return $this->service->decrypt($value, $this->getEncryptionKey()); } return (float)$value; } public function getValue() { return $this->getDecryptedValue($this->value); // TODO: Change the autogenerated stub } public function requireField() { $values = array( 'type' => 'text', 'parts' => array( 'datatype' => 'text', // 'precision' => $this->service->calculateRequiredFieldSize(strlen('Y-m-d H:i:s')), 'null' => 'not null', 'default' => $this->defaultVal, 'arrayValue' => $this->arrayValue ) ); DB::require_field($this->tableName, $this->name, $values); } public function prepValueForDB($value) { $value = parent::prepValueForDB($value); $ciphertext = $this->service->encrypt($value, $this->getEncryptionKey()); $this->value = $ciphertext; return $ciphertext; } } |