Skip to content

Commit

Permalink
Fixed issue #18184: cpd_importParticipants with encryption turned on,…
Browse files Browse the repository at this point in the history
… fails to save the participant encrypted in CPDB, causing 500 error to CPDB (#2464)

Co-authored-by: encuestabizdevgit <devgit@encuesta.biz>
Co-authored-by: Lapiu Dev <devgit@lapiu.biz>
  • Loading branch information
3 people committed Apr 3, 2023
1 parent 7c8ab1a commit 68296cc
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 12 deletions.
18 changes: 7 additions & 11 deletions application/helpers/remotecontrol/remotecontrol_handle.php
Original file line number Diff line number Diff line change
Expand Up @@ -3581,22 +3581,18 @@ public function cpd_importParticipants($sSessionKey, $participants, $update = fa
$model->modified = date('Y-m-d H:i:s');
}

if ($model->save()) {
if ($model->encryptSave()) {
foreach ($participant as $sLabel => $sAttributeValue) {
if (!in_array($sLabel, $aDefaultFields)) {
foreach ($aAttributeRecords as $sKey => $arValue) {
$aAttributes = $arValue->getAttributes();
if ($aAttributes['defaultname'] == $sLabel) {
$aAttributeData = array(
'participant_id' => $model->participant_id,
'attribute_id' => $aAttributes['attribute_id'],
'value' => $sAttributeValue
);
if ($scenario == 'insert') {
ParticipantAttributeName::model()->saveParticipantAttributeValue($aAttributeData);
} else { // update
ParticipantAttribute::model()->updateParticipantAttributeValue($aAttributeData);
}
$attribute = ParticipantAttribute::model();
$attribute->attribute_id = $aAttributes['attribute_id'];
$attribute->participant_id = $model->participant_id;
$attribute->value = $sAttributeValue;
$attribute->encrypt();
$attribute->updateParticipantAttributeValue($attribute->attributes);
}
}
}
Expand Down
88 changes: 87 additions & 1 deletion tests/unit/helpers/remotecontrol/CPDImportParticpantsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ public function testOneParticipantWithOwnIdImportedSuccessfully()

$max = \Participant::model()->findByPk('max');
$this->assertInstanceOf('Participant', $max);

}

public function testImportingParticipantFailsDueToSameFirstnameLastnameEmail()
Expand Down Expand Up @@ -194,4 +193,91 @@ public function testParticipantUpdatedSuccessfullyWhenUpdateTrue()
$attribute = $max->getParticipantAttribute('ea_1');
$this->assertEquals('http://www.example.org', $attribute);
}

public function testOneParticipantWithEncryptedCoreAttributesImportedSuccessfully()
{
\Yii::app()->session['adminlang'] = 'de';
$this->assertTrue(empty(\ParticipantAttributeName::model()->findAll()));

//Setting email attribute to be encrypted.
$result = \ParticipantAttributeName::model()->storeAttribute(array(
'attribute_type' => 'TB',
'defaultname' => 'email',
'visible' => 'TRUE',
'encrypted' => 'Y',
'core_attribute' => 'Y'
));
$this->assertTrue(intval($result) > 0);

//Setting lastname attribute to be encrypted.
$result = \ParticipantAttributeName::model()->storeAttribute(array(
'attribute_type' => 'TB',
'defaultname' => 'lastname',
'visible' => 'TRUE',
'encrypted' => 'Y',
'core_attribute' => 'Y'
));
$this->assertTrue(intval($result) > 0);

$participants = array(
array(
'participant_id' => 'max',
'firstname' => 'Max',
'lastname' => 'Mustermann',
'email' => 'max.mustermann@example.com',
'language' => 'de',
'blacklisted' => 'Y'
)
);

$sessionKey = $this->handler->get_session_key($this->getUsername(), $this->getPassword());
$result = $this->handler->cpd_importParticipants($sessionKey, $participants);
$this->assertArrayHasKey('ImportCount', $result);
$this->assertEquals(1, $result['ImportCount']);

$max = \Participant::model()->findByPk('max');
$this->assertInstanceOf(\Participant::class, $max);

//Not equal since it's encrypted.
$this->assertNotEquals($participants[0]['email'], $max->email);
$this->assertNotEquals($participants[0]['lastname'], $max->lastname);
}

public function testParticipantWithOneEncryptedAttributeImportedSucessfully()
{
\Yii::app()->session['adminlang'] = 'de';
$this->assertTrue(empty(\ParticipantAttributeName::model()->findAll()));
$result = \ParticipantAttributeName::model()->storeAttribute(array(
'attribute_type' => 'TB',
'defaultname' => 'passport',
'visible' => 'TRUE',
'attribute_name' => 'Passport',
'encrypted' => 'Y',
'core_attribute' => 'N'
));
$this->assertTrue(intval($result) > 0);

$participants = array(
array(
'participant_id' => 'max',
'firstname' => 'Max',
'lastname' => 'Mustermann',
'email' => 'max.mustermann@example.com',
'language' => 'de',
'blacklisted' => 'Y',
'passport' => '123456789',
)
);

$sessionKey = $this->handler->get_session_key($this->getUsername(), $this->getPassword());
$result = $this->handler->cpd_importParticipants($sessionKey, $participants);
$this->assertArrayHasKey('ImportCount', $result);
$this->assertEquals(1, $result['ImportCount']);

$max = \Participant::model()->findByPk('max');
$this->assertInstanceOf(\Participant::class, $max);

$attribute = $max->getParticipantAttribute('ea_1');
$this->assertEquals('123456789', $attribute);
}
}

0 comments on commit 68296cc

Please sign in to comment.