Skip to content

Commit

Permalink
Fixed issue #19274: Attribute encryption can not be turned OFF, it ca…
Browse files Browse the repository at this point in the history
…n only be turned on (#3671)

Co-authored-by: lapiudevgit <devgit@lapiu.biz>
  • Loading branch information
gabrieljenik and lapiudevgit committed Jan 16, 2024
1 parent 1d20161 commit be215e4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
14 changes: 7 additions & 7 deletions application/controllers/admin/ParticipantsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -1437,23 +1437,23 @@ public function changeAttributeEncrypted()
$encrypted = Yii::app()->request->getPost('encrypted');
$encrypted_value = $encrypted == "true" ? 'Y' : 'N';
$attributeName = ParticipantAttributeName::model()->findByPk($attributeId);
$sEncryptedBeforeChange = $attributeName->encrypted;
$encryptedBeforeChange = $attributeName->isEncrypted();
$attributeName->encrypted = $encrypted_value;
$sEncryptedAfterChange = $attributeName->encrypted;
$encryptedAfterChange = $attributeName->isEncrypted();
$sDefaultname = $attributeName->defaultname;

// encryption/decryption MUST be done in a one synchronous step, either all succeeded or none
$oDB = Yii::app()->db;
$oTransaction = $oDB->beginTransaction();
try {
if ($attributeName->core_attribute == 'Y') {
if ($attributeName->isCoreAttribute()) {
// core participant attributes
$oParticipants = Participant::model()->findAll();
foreach ($oParticipants as $participant) {
$aUpdateData = array();
if ($sEncryptedBeforeChange == 'Y' && $sEncryptedAfterChange == 'N') {
if ($encryptedBeforeChange && !$encryptedAfterChange) {
$aUpdateData[$sDefaultname] = LSActiveRecord::decryptSingle($participant->$sDefaultname);
} elseif ($sEncryptedBeforeChange == 'N' && $sEncryptedAfterChange == 'Y') {
} elseif (!$encryptedBeforeChange && $encryptedAfterChange) {
$aUpdateData[$sDefaultname] = LSActiveRecord::encryptSingle($participant->$sDefaultname);
}
if (!empty($aUpdateData)) {
Expand All @@ -1468,9 +1468,9 @@ public function changeAttributeEncrypted()
);
foreach ($oAttributes as $attribute) {
$aUpdateData = array();
if ($sEncryptedBeforeChange == 'Y' && $sEncryptedAfterChange == 'N') {
if ($encryptedBeforeChange && !$encryptedAfterChange) {
$aUpdateData['value'] = LSActiveRecord::decryptSingle($attribute->value);
} elseif ($sEncryptedBeforeChange == 'N' && $sEncryptedAfterChange == 'Y') {
} elseif (!$encryptedBeforeChange && $encryptedAfterChange) {
$aUpdateData['value'] = LSActiveRecord::encryptSingle($attribute->value);
}
if (!empty($aUpdateData) && $aUpdateData['value'] !== null) {
Expand Down
18 changes: 18 additions & 0 deletions application/models/ParticipantAttributeName.php
Original file line number Diff line number Diff line change
Expand Up @@ -895,4 +895,22 @@ public function saveParticipantAttributeValue($data)
{
Yii::app()->db->createCommand()->insert('{{participant_attribute}}', $data);
}

/**
* Returns true if the attribute is encrypted
* @return bool
*/
public function isEncrypted()
{
return $this->encrypted == 'Y';
}

/**
* Returns true if the attribute is a core attribute
* @return bool
*/
public function isCoreAttribute()
{
return $this->core_attribute == 'Y';
}
}

0 comments on commit be215e4

Please sign in to comment.