Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EZP-29096: Do not store empty draft values in DB #1417

Merged
merged 12 commits into from
Mar 11, 2019
Merged
18 changes: 16 additions & 2 deletions kernel/classes/datatypes/ezuser/ezuser.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,24 @@ static function create( $contentObjectID )
return new eZUser( $row );
}

function store( $fieldFilters = null )
/**
* Only stores the entry if it has a Login value
*
* @param null $fieldFilters
pkamps marked this conversation as resolved.
Show resolved Hide resolved
*/
public function store( $fieldFilters = null )
{
$this->Email = trim( $this->Email );
$userID = $this->attribute( 'contentobject_id' );
// Clear memory cache
unset( $GLOBALS['eZUserObject_' . $userID] );
$GLOBALS['eZUserObject_' . $userID] = $this;
self::purgeUserCacheByUserId( $userID );
parent::store( $fieldFilters );

if ( $this->Login )
{
parent::store( $fieldFilters );
}
}

function originalPassword()
Expand Down Expand Up @@ -307,6 +316,11 @@ function setInformation( $id, $login, $email, $password, $passwordConfirm = fals
}
}

/**
* @param integer $id
* @param bool $asObject
* @return eZUser|null
*/
static function fetch( $id, $asObject = true )
{
if ( !$id )
Expand Down
37 changes: 27 additions & 10 deletions kernel/classes/datatypes/ezuser/ezusertype.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,22 @@ function storeObjectAttribute( $contentObjectAttribute )
}

// saving information in the object attribute data_text field to simulate a draft
$contentObjectAttribute->setAttribute( 'data_text', $this->serializeDraft( $user ) );
// only if the object version is a draft (status == 0)
if(
pkamps marked this conversation as resolved.
Show resolved Hide resolved
$user->Login &&
$contentObjectAttribute->attribute( 'object_version' )->attribute( 'status' ) == 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$contentObjectAttribute->attribute( 'object_version' )->attribute( 'status' ) == 0
$contentObjectAttribute->attribute( 'object_version' )->attribute( 'status' ) === 0

Or can this be a string? I would not think so.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hope you don't mind if I ignore it. Maybe it's a string and then it would hurt to be extra strict here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We ought to be strict, strictly speaking ;) eZContentObjectVersion says it's an integer. But I see many cases where we aren't strict, so I guess this can pass if others agree.

glye marked this conversation as resolved.
Show resolved Hide resolved
)
{
$contentObjectAttribute->setAttribute( 'data_text', $this->serializeDraft( $user ) );
}
}
}

/**
* @param $contentObjectAttribute
* @param eZContentObject $contentObject
* @param $publishedNodes
*/
function onPublish( $contentObjectAttribute, $contentObject, $publishedNodes )
{
/** @var eZContentObjectAttribute $contentObjectAttribute */
Expand All @@ -274,10 +286,13 @@ function onPublish( $contentObjectAttribute, $contentObject, $publishedNodes )
if ( !empty( $serializedDraft ) )
{
$user = $this->updateUserDraft( $user, $serializedDraft );
}
$user->store();
$contentObjectAttribute->setContent( $user );

$user->store();
$contentObjectAttribute->setContent( $user );
// Clear draft info
$contentObjectAttribute->setAttribute( 'data_text', '' );
$contentObjectAttribute->store();
}
}

/**
Expand Down Expand Up @@ -320,10 +335,13 @@ private function updateUserDraft( eZUser $user, $serializedDraft )
{
$draft = $this->unserializeDraft( $serializedDraft );

$user->setAttribute( 'login', $draft->login );
$user->setAttribute( 'password_hash', $draft->password_hash );
$user->setAttribute( 'email', $draft->email );
$user->setAttribute( 'password_hash_type', $draft->password_hash_type );
if( $draft )
pkamps marked this conversation as resolved.
Show resolved Hide resolved
{
$user->setAttribute( 'login', $draft->login );
$user->setAttribute( 'password_hash', $draft->password_hash );
$user->setAttribute( 'email', $draft->email );
$user->setAttribute( 'password_hash_type', $draft->password_hash_type );
}

return $user;
}
Expand Down Expand Up @@ -360,11 +378,10 @@ function objectAttributeContent( $contentObjectAttribute )
$GLOBALS['eZUserObject_' . $userID] = eZUser::fetch( $userID );
}

/** @var eZUser $user */
$user = eZUser::fetch( $userID );
eZDebugSetting::writeDebug( 'kernel-user', $user, 'user' );

// Looking for a "draft" and loading it's content
//Looking for a "draft" and loading it's content
pkamps marked this conversation as resolved.
Show resolved Hide resolved
$serializedDraft = $contentObjectAttribute->attribute( 'data_text' );

if ( !empty( $serializedDraft ) )
Expand Down
2 changes: 1 addition & 1 deletion kernel/user/ezuseroperationcollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ static public function activation( $userID, $userHash, $enableUser = false )
}

/**
* Change user password
* Change user password for an existing user
glye marked this conversation as resolved.
Show resolved Hide resolved
*
* @param int $userID
* @param string $newPassword
Expand Down
38 changes: 14 additions & 24 deletions tests/tests/kernel/datatypes/ezuser/ezusertype_regression.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,16 @@ public function setUp()
'parent_node_id' => $ini->variable( 'UserSettings', 'DefaultUserPlacement' ),
'attributes' => array(
'first_name' => 'foo',
'last_name' => 'bar' ),
'last_name' => 'bar',
'user_account' => "{$this->userLogin}|{$this->userEmail}|1234|md5_password|0",
),
);

$contentObject = eZContentFunctions::createAndPublishObject( $params );

// Re-fetch to get the object after it was published
$contentObject = eZContentObject::fetch( $contentObject->ID );

if( !$contentObject instanceof eZContentObject )
{
die( 'Impossible to create user object' );
Expand Down Expand Up @@ -110,8 +115,9 @@ public function testFromStringHandlesDisabledAccount()
$userAccount = $dataMap['user_account']->fromString(
join( '|', array( $tmpLogin, $tmpEmail, self::USER_PASSWORD_HASH, self::USER_PASSWORD_HASH_ID, 0 ) )
);
$userAccount->store();

$dataMap['user_account']->setContent( $userAccount );
$dataMap['user_account']->store();
glye marked this conversation as resolved.
Show resolved Hide resolved

$tempObject = eZContentObject::fetch( $this->userObject->attribute( 'id' ) );
$dataMap = $tempObject->dataMap();
Expand Down Expand Up @@ -151,8 +157,9 @@ public function testFromStringHandlesEnabledAccount()
$userAccount = $dataMap['user_account']->fromString(
join( '|', array( $tmpLogin, $tmpEmail, self::USER_PASSWORD_HASH, self::USER_PASSWORD_HASH_ID, 1 ) )
);
$userAccount->store();

$dataMap['user_account']->setContent( $userAccount );
$dataMap['user_account']->store();

$tempObject = eZContentObject::fetch( $this->userObject->attribute( 'id' ) );
$dataMap = $tempObject->dataMap();
Expand Down Expand Up @@ -188,11 +195,13 @@ public function testUpdatePasswordUpdatesSerializedData()
{
$userId = $this->userObject->attribute('id');

$passwordHash = $this->getSerializedPasswordHash($this->userObject);
$user = ezuser::fetch( $userId );
$passwordHash = $user->PasswordHash;
glye marked this conversation as resolved.
Show resolved Hide resolved

eZUserOperationCollection::password($userId, 'newpassword');

$updatedPasswordHash = $this->getSerializedPasswordHash( eZContentObject::fetch($userId) );
$user = ezuser::fetch( $userId );
$updatedPasswordHash = $user->PasswordHash;

self::assertNotEquals(
$passwordHash,
Expand All @@ -201,25 +210,6 @@ public function testUpdatePasswordUpdatesSerializedData()
);
}

/**
* @param \eZContentObject $userObject
*
* @return string The serialized password hash, or null if none is set
*/
private function getSerializedPasswordHash(eZContentObject $userObject)
{
$dataMap = $userObject->dataMap();

$userAccountAttributeText = $dataMap['user_account']->attribute('data_text');

// empty on initial version
if (!empty($userAccountAttributeText)) {
return json_decode($userAccountAttributeText)->password_hash;
}

return null;
}

/**
* Enables the current user
*/
Expand Down