/** * Updates password in database * @return ErrorResponse|Response */ protected function update() { $missing_fields = UserController::validateJSONFormat($this->body, User::REQUIRED_PASSWORD_PUT_FIELD); // Check that required fields are not missing if (!$missing_fields) { $mapper = new UserDBMapper(); $json = $this->body; $user = User::fromResetPasswordQuery($this->id, $json); // If user is set if ($user) { $db_response = $mapper->resetPassword($user); if ($db_response instanceof DBError) { $response = new ErrorResponse($db_response); } else { $user = $mapper->getById($this->id); if ($user) { $response = new Response(json_encode($user->toArray(), JSON_PRETTY_PRINT), Response::STATUS_CODE_CREATED); } else { $response = new ErrorResponse(new NotFoundError()); } return $response; } } else { $response = new ErrorResponse(new ApplicationError("Reset password error", "There was a problem with the password")); } } else { $response = new ErrorResponse(new MalformedJSONFormatError($missing_fields)); } return $response; }
/** * @param $json * @param $email_type * @return ErrorResponse|null|Response */ public static function setNewPassword($json, $email_type) { $response = null; $missing_fields = UserController::validateJSONFormat($json, User::REQUIRED_PASSWORD_RESET_FIELD); // Check that required fields are not missing if (!$missing_fields) { $user_mapper = new UserDBMapper(); $email = $json['email']; $user = User::fromDBArray($user_mapper->getByEmail($email)); // Set random password $password = ResetPasswordController::getRandomString(ResetPasswordController::PASSWORD_LENGTH); $json['password'] = $password; $id = $user->getId(); $reset_password_user = User::fromResetPasswordQuery($id, $json); if ($reset_password_user) { $db_response = $user_mapper->resetPassword($reset_password_user); if ($db_response instanceof DBError) { $response = new ErrorResponse($db_response); } else { $reset_password_user = $user_mapper->getById($id); if ($reset_password_user) { EmailSender::sendEmail($email, $password, $email_type); // Sending Email notification $response = new Response(json_encode(array('message' => ResetPasswordController::RESET_PASSWORD_ACCEPTED_MESSAGE), JSON_PRETTY_PRINT), Response::STATUS_CODE_ACCEPTED); } else { $response = new ErrorResponse(new NotFoundError()); } } } return $response; } return new Response($response); }