diff --git a/app/models/Data/User.php b/app/models/Data/User.php index 1f1bc92..a676be5 100644 --- a/app/models/Data/User.php +++ b/app/models/Data/User.php @@ -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); + } } } }