_getUser($reference); if (empty($user)) { throw new UserNotFoundException(__d('cake_d_c/users', 'User not found')); } if ($options['checkActive'] ?? false) { if ($user->active) { throw new UserAlreadyActiveException(__d('cake_d_c/users', 'User account already validated')); } $user->active = false; $user->activation_date = null; } if ($options['ensureActive'] ?? false) { if (!$user['active']) { throw new UserNotActiveException(__d('cake_d_c/users', 'User not active')); } } $user->updateToken($expiration); $saveResult = $this->_table->save($user); if ($options['sendEmail'] ?? false) { switch ($options['type'] ?? null) { case 'email': $this->_sendValidationEmail($user); break; case 'password': $this->_sendResetPasswordEmail($user); break; } } return $saveResult; } /** * Send the reset password related email link * * @param \Cake\Datasource\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 \Cake\Datasource\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 \Cake\Datasource\EntityInterface $user user data. * @throws \CakeDC\Users\Exception\WrongPasswordException * @return mixed */ public function changePassword(EntityInterface $user) { try { $currentUser = $this->_table->get($user->id, [ 'contain' => [], ]); } catch (RecordNotFoundException $e) { throw new UserNotFoundException(__d('cake_d_c/users', 'User not found')); } if (!empty($user->current_password)) { if (!$user->checkPassword($user->current_password, $currentUser->password)) { throw new WrongPasswordException(__d('cake_d_c/users', 'The current password does not match')); } if ($user->current_password === $user->password_confirm) { throw new WrongPasswordException(__d( 'cake_d_c/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; } }