Archived
1
0
Fork 0

app/models/Data/User.php: fixing a bug with snapshots and new rows in beforeSave()

This commit is contained in:
Henrik Hautakoski 2018-06-07 23:23:58 +02:00
parent 22b7e983f0
commit d81c1f4423

View file

@ -209,18 +209,32 @@ class User extends Model
public function beforeSave()
{
// Fire event on password create/changed.
$manager = $this->getEventsManager();
// EventManager exist and password field has changed.
if ($manager && $this->hasChanged('password')) {
// EventManager exist
if ($manager) {
$old_value = $this->getOldSnapshotData()['password'];
// If we have Snapshot data (existing row)
if ($this->hasSnapshotData()) {
$has_new_value = $this->hasChanged('password');
$old_value = $this->getOldSnapshotData()['password'];
}
// New row, check if we have a password.
else {
$has_new_value = strlen($this->password) > 0;
$old_value = null;
}
// Empty password before
if (strlen($old_value) < 1) {
$manager->fire('user:onPasswordCreated', $this);
} else {
$manager->fire('user:onPasswordChanged', $this);
// we have a new password.
if ($has_new_value) {
// Empty password before
if (strlen($old_value) < 1) {
$manager->fire('user:onPasswordCreated', $this);
} else {
$manager->fire('user:onPasswordChanged', $this);
}
}
}
}