app/models/Data/User.php: fixing a bug with snapshots and new rows in beforeSave()
This commit is contained in:
parent
22b7e983f0
commit
d81c1f4423
1 changed files with 22 additions and 8 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue