Skip to content

Commit

Permalink
Send username with download link
Browse files Browse the repository at this point in the history
Closes #981
  • Loading branch information
theotherp committed Jan 5, 2025
1 parent 6b19985 commit 5f64289
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@

import lombok.extern.slf4j.Slf4j;
import org.nzbhydra.config.ConfigProvider;
import org.nzbhydra.config.HistoryUserInfoType;
import org.nzbhydra.config.MainConfig;
import org.nzbhydra.config.downloading.DownloadType;
import org.nzbhydra.logging.LoggingMarkers;
import org.nzbhydra.searching.db.SearchResultEntity;
import org.nzbhydra.web.SessionStorage;
import org.nzbhydra.web.UrlCalculator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -78,6 +80,10 @@ private String getDownloadLink(Long searchResultId, boolean internal, DownloadTy
builder.path("/" + searchResultId);
builder.queryParam("apikey", main.getApiKey());
}
HistoryUserInfoType infoType = configProvider.getBaseConfig().getMain().getLogging().getHistoryUserInfoType();
if (infoType.isLogUserInfo() && SessionStorage.username.get() != null) {
builder.queryParam("username", SessionStorage.username.get());
}
return builder.toUriString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public ResponseEntity<Object> downloadNzbForUsers(@PathVariable("guid") long gui
@RequestMapping(value = "/getnzb/api/{guid}", produces = "application/x-nzb")
public ResponseEntity downloadNzbWithApikey(@PathVariable("guid") long guid, @RequestParam(required = false) String apikey) throws WrongApiKeyException {
logger.debug("downloadNzbWithApikey guid: {}", guid);

BaseConfig baseConfig = configProvider.getBaseConfig();
if ((apikey == null || !apikey.equals(baseConfig.getMain().getApiKey())) && !noApiKeyNeeded) {
logger.error("Received NZB API download call with wrong API key");
Expand Down
16 changes: 12 additions & 4 deletions core/src/main/java/org/nzbhydra/web/Interceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,19 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
if (configProvider.getBaseConfig().getMain().getLogging().isLogIpAddresses()) {
MDC.put("IPADDRESS", ip);
}
if (configProvider.getBaseConfig().getMain().getLogging().isLogUsername() && !Strings.isNullOrEmpty(request.getRemoteUser())) {
MDC.put("USERNAME", request.getRemoteUser());
}

SessionStorage.IP.set(ip);
SessionStorage.username.set(request.getRemoteUser());

if (request.getRemoteUser() != null) {
SessionStorage.username.set(request.getRemoteUser());
} else if (request.getParameter("username") != null) {
SessionStorage.username.set(request.getParameter("username"));
}
if (configProvider.getBaseConfig().getMain().getLogging().isLogUsername()) {
if (!Strings.isNullOrEmpty(SessionStorage.username.get())) {
MDC.put("USERNAME", SessionStorage.username.get());
}
}
SessionStorage.userAgent.set(userAgentMapper.getUserAgent(request.getHeader("User-Agent")));
SessionStorage.requestUrl.set(request.getRequestURI());

Expand Down
2 changes: 2 additions & 0 deletions core/src/main/resources/changelog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
- version: "v7.12.0"
date: "2025-01-05"
changes:
- type: "feature"
text: "If enabled log and/or store username for NZB links. This way when sending a link to be downloaded from hydra the username that caused the download will be known. See #981"
- type: "fix"
text: "Fix paging for search / download / notification history."
- type: "fix"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@ public enum HistoryUserInfoType {
BOTH,
IP,
USERNAME,
NONE
NONE;

public boolean isLogUserInfo() {
return this == BOTH || this == USERNAME;
}
}

0 comments on commit 5f64289

Please sign in to comment.