_getUser($reference); if (empty($user)) { throw new UserNotFoundException(__d('CakeDC/Users', "User not found")); } if (Hash::get($options, 'checkActive')) { if ($user->active) { throw new UserAlreadyActiveException(__d('CakeDC/Users', "User account already validated")); } $user->active = false; $user->activation_date = null; } if (Hash::get($options, 'ensureActive')) { if (!$user['active']) { throw new UserNotActiveException(__d('CakeDC/Users', "User not active")); } } $user->updateToken($expiration); $saveResult = $this->_table->save($user); if (Hash::get($options, 'sendEmail')) { switch (Hash::get($options, 'type')) { case 'email': $this->_sendValidationEmail($user); break; case 'password': $this->_sendResetPasswordEmail($user); break; } } return $saveResult; } /** * Send the reset password related email link * * @param EntityInterface $user user * @return void */ protected function _sendResetPasswordEmail($user) { $this ->getMailer(Configure::read('Users.Email.mailerClass') ?: 'CakeDC/Users.Users') ->send('resetPassword', [$user]); } /** * Wrapper for mailer * * @param EntityInterface $user user * @return void */ protected function _sendValidationEmail($user) { $mailer = Configure::read('Users.Email.mailerClass') ?: 'CakeDC/Users.Users'; $this ->getMailer($mailer) ->send('validation', [$user]); } /** * Get the user by email or username * * @param string $reference reference could be either an email or username * @return mixed user entity if found */ protected function _getUser($reference) { return $this->_table->findByUsernameOrEmail($reference, $reference)->first(); } /** * Change password method * * @param EntityInterface $user user data. * @throws WrongPasswordException * @return mixed */ public function changePassword(EntityInterface $user) { try { $currentUser = $this->_table->get($user->id, [ 'contain' => [] ]); } catch (RecordNotFoundException $e) { throw new UserNotFoundException(__d('CakeDC/Users', "User not found")); } if (!empty($user->current_password)) { if (!$user->checkPassword($user->current_password, $currentUser->password)) { throw new WrongPasswordException(__d('CakeDC/Users', 'The current password does not match')); } if ($user->current_password === $user->password_confirm) { throw new WrongPasswordException(__d( 'CakeDC/Users', 'You cannot use the current password as the new one' )); } } $user = $this->_table->save($user); if (!empty($user)) { $user = $this->_removeValidationToken($user); } return $user; } }