began implementing Request-Scoped beans (#123)

* example

* switched to copy constructor

* switched to Supplier

* expanded example
This commit is contained in:
Jake Potrebic 2020-09-06 10:16:59 -07:00 committed by GitHub
parent e92a68fb3a
commit 44681dc302
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 138 additions and 101 deletions

View File

@ -12,7 +12,6 @@ import io.papermc.hangar.db.dao.ProjectDao;
import io.papermc.hangar.db.dao.UserDao;
import io.papermc.hangar.db.model.OrganizationsTable;
import io.papermc.hangar.db.model.ProjectOwner;
import io.papermc.hangar.db.model.ProjectVersionsTable;
import io.papermc.hangar.db.model.ProjectsTable;
import io.papermc.hangar.db.model.UserProjectRolesTable;
import io.papermc.hangar.db.model.UsersTable;
@ -82,6 +81,7 @@ import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@ -135,17 +135,27 @@ public class ProjectsController extends HangarController {
@Bean
@RequestScope
ProjectsTable projectsTable() {
Supplier<ProjectsTable> projectsTable() {
Map<String, String> pathParams = (Map<String, String>) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
if (!pathParams.keySet().containsAll(Set.of("author", "slug"))) {
return null;
ProjectsTable pt;
if (pathParams.keySet().containsAll(Set.of("author", "slug"))) {
pt = projectService.getProjectsTable(pathParams.get("author"), pathParams.get("slug"));
} else if (pathParams.containsKey("pluginId")) {
pt = projectService.getProjectsTable(pathParams.get("pluginId"));
} else {
ProjectsTable projectsTable = projectService.getProjectsTable(pathParams.get("author"), pathParams.get("slug"));
if (projectsTable == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
}
return projectsTable;
return () -> null;
}
if (pt == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
}
return () -> pt;
}
@Bean
@RequestScope
Supplier<ProjectData> projectData() {
//noinspection SpringConfigurationProxyMethods
return () -> projectService.getProjectData(projectsTable().get());
}
@Secured("ROLE_USER")

View File

@ -40,7 +40,6 @@ import io.papermc.hangar.util.HangarException;
import io.papermc.hangar.util.RequestUtil;
import io.papermc.hangar.util.Routes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
@ -79,6 +78,7 @@ import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.function.Supplier;
@Controller
public class VersionsController extends HangarController {
@ -102,11 +102,13 @@ public class VersionsController extends HangarController {
private final HttpServletRequest request;
private final HttpServletResponse response;
private final ProjectVersionsTable projectVersionsTable;
private final ProjectsTable projectsTable;
private final Supplier<ProjectVersionsTable> projectVersionsTable;
private final Supplier<VersionData> versionData;
private final Supplier<ProjectsTable> projectsTable;
private final Supplier<ProjectData> projectData;
@Autowired(required = false)
public VersionsController(ProjectService projectService, VersionService versionService, ProjectFactory projectFactory, UserService userService, PluginUploadService pluginUploadService, ChannelService channelService, DownloadsService downloadsService, UserActionLogService userActionLogService, CacheManager cacheManager, HangarConfig hangarConfig, HangarDao<ProjectDao> projectDao, ProjectFiles projectFiles, HangarDao<ProjectVersionDownloadWarningDao> downloadWarningDao, MessageSource messageSource, ObjectMapper mapper, HttpServletRequest request, HttpServletResponse response, ProjectVersionsTable projectVersionsTable, ProjectsTable projectsTable) {
public VersionsController(ProjectService projectService, VersionService versionService, ProjectFactory projectFactory, UserService userService, PluginUploadService pluginUploadService, ChannelService channelService, DownloadsService downloadsService, UserActionLogService userActionLogService, CacheManager cacheManager, HangarConfig hangarConfig, HangarDao<ProjectDao> projectDao, ProjectFiles projectFiles, HangarDao<ProjectVersionDownloadWarningDao> downloadWarningDao, MessageSource messageSource, ObjectMapper mapper, HttpServletRequest request, HttpServletResponse response, Supplier<ProjectVersionsTable> projectVersionsTable, Supplier<VersionData> versionData, Supplier<ProjectsTable> projectsTable, Supplier<ProjectData> projectData) {
this.projectService = projectService;
this.versionService = versionService;
this.projectFactory = projectFactory;
@ -125,48 +127,54 @@ public class VersionsController extends HangarController {
this.request = request;
this.response = response;
this.projectVersionsTable = projectVersionsTable;
this.versionData = versionData;
this.projectsTable = projectsTable;
this.projectData = projectData;
}
@Bean
@RequestScope
ProjectVersionsTable projectVersionsTable() {
Supplier<ProjectVersionsTable> projectVersionsTable() {
Map<String, String> pathParams = (Map<String, String>) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
if (!pathParams.keySet().containsAll(Set.of("author", "slug", "version"))) {
return null;
return () -> null;
} else {
ProjectVersionsTable projectVersionsTable = versionService.getVersion(pathParams.get("author"), pathParams.get("slug"), pathParams.get("version"));
if (projectVersionsTable == null) {
ProjectVersionsTable pvt = versionService.getVersion(pathParams.get("author"), pathParams.get("slug"), pathParams.get("version"));
if (pvt == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
}
return projectVersionsTable;
return () -> pvt;
}
}
@Bean
@RequestScope
Supplier<VersionData> versionData() {
//noinspection SpringConfigurationProxyMethods
return () -> versionService.getVersionData(projectData.get(), projectVersionsTable().get());
}
@GetMapping(value = "/api/project/{pluginId}/versions/recommended/download", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
@ResponseBody
public Object downloadRecommendedJarById(@PathVariable String pluginId, @RequestParam(required = false) String token) {
ProjectsTable project = projectDao.get().getByPluginId(pluginId);
Long recommendedVersionId = project.getRecommendedVersionId();
Long recommendedVersionId = projectsTable.get().getRecommendedVersionId();
if (recommendedVersionId == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
} else {
ProjectVersionsTable versionsTable = versionService.getVersion(project.getId(), recommendedVersionId);// TODO we need to check visibility here, the query currently doesnt do that
return sendVersion(project, versionsTable, token, true);
ProjectVersionsTable versionsTable = versionService.getVersion(projectsTable.get().getId(), recommendedVersionId);// TODO we need to check visibility here, the query currently doesnt do that
return sendVersion(projectsTable.get(), versionsTable, token, true);
}
}
@GetMapping(value = "/api/project/{pluginId}/versions/{name}/download", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
@ResponseBody
public Object downloadJarById(@PathVariable String pluginId, @PathVariable String name, @RequestParam(required = false) String token) {
ProjectsTable project = projectDao.get().getByPluginId(pluginId);
ProjectVersionsTable versionsTable = versionService.getVersion(project.getId(), name);// TODO we need to check visibility here, the query currently doesnt do that
ProjectVersionsTable versionsTable = versionService.getVersion(projectsTable.get().getId(), name);// TODO we need to check visibility here, the query currently doesnt do that
if (token != null) {
// TODO confirmDownload0(version.id, Some(DownloadType.JarFile.value), Some(token)).orElseFail(notFound) *>
return sendJar(project, versionsTable, token, true);
return sendJar(projectsTable.get(), versionsTable, token, true);
} else {
return sendJar(project, versionsTable, null, true);
return sendJar(projectsTable.get(), versionsTable, null, true);
}
}
@ -175,22 +183,19 @@ public class VersionsController extends HangarController {
@GetMapping("/{author}/{slug}/versionLog")
public ModelAndView showLog(@PathVariable String author, @PathVariable String slug, @RequestParam String versionString) {
ModelAndView mv = new ModelAndView("projects/versions/log");
ProjectData projectData = projectService.getProjectData(author, slug);
ProjectVersionsTable version = versionService.getVersion(projectData.getProject().getId(), versionString);
mv.addObject("project", projectData);
mv.addObject("version", version);
mv.addObject("visibilityChanges", versionService.getVersionVisibilityChanges(version.getId()));
mv.addObject("project", projectData.get());
mv.addObject("version", projectVersionsTable.get());
mv.addObject("visibilityChanges", versionService.getVersionVisibilityChanges(projectVersionsTable.get().getId()));
return fillModel(mv);
}
@GetMapping("/{author}/{slug}/versions")
public ModelAndView showList(@PathVariable String author, @PathVariable String slug) {
ModelAndView mav = new ModelAndView("projects/versions/list");
ProjectData projectData = projectService.getProjectData(author, slug);
ScopedProjectData sp = projectService.getScopedProjectData(projectData.getProject().getId());
ScopedProjectData sp = projectService.getScopedProjectData(projectData.get().getProject().getId());
mav.addObject("sp", sp);
mav.addObject("p", projectData);
mav.addObject("channels", channelService.getProjectChannels(projectData.getProject().getId()));
mav.addObject("p", projectData.get());
mav.addObject("channels", channelService.getProjectChannels(projectData.get().getProject().getId()));
return fillModel(mav);
}
@ -217,10 +222,9 @@ public class VersionsController extends HangarController {
return fillModel(mav);
}
ProjectData projectData = projectService.getProjectData(author, slug);
PendingVersion pendingVersion;
try {
pendingVersion = pluginUploadService.processSubsequentPluginUpload(file, projectData.getProjectOwner(), projectData.getProject());
pendingVersion = pluginUploadService.processSubsequentPluginUpload(file, projectData.get().getProjectOwner(), projectData.get().getProject());
} catch (HangarException e) {
ModelAndView mav = _showCreator(author, slug, null);
AlertUtil.showAlert(mav, AlertUtil.AlertType.ERROR, e.getMessageKey(), e.getArgs());
@ -232,8 +236,7 @@ public class VersionsController extends HangarController {
@Secured("ROLE_USER")
@GetMapping("/{author}/{slug}/versions/new/{versionName}")
public ModelAndView showCreatorWithMeta(@PathVariable String author, @PathVariable String slug, @PathVariable String versionName) {
ProjectData projectData = projectService.getProjectData(author, slug);
PendingVersion pendingVersion = cacheManager.getCache(CacheConfig.PENDING_VERSION_CACHE).get(projectData.getProject().getId() + "/" + versionName, PendingVersion.class);
PendingVersion pendingVersion = cacheManager.getCache(CacheConfig.PENDING_VERSION_CACHE).get(projectData.get().getProject().getId() + "/" + versionName, PendingVersion.class);
if (pendingVersion == null) {
ModelAndView mav = _showCreator(author, slug, null);
@ -245,44 +248,39 @@ public class VersionsController extends HangarController {
}
private ModelAndView _showCreator(String author, String slug, PendingVersion pendingVersion) {
ProjectData projectData = projectService.getProjectData(author, slug);
ModelAndView mav = new ModelAndView("projects/versions/create");
mav.addObject("projectName", projectData.getProject().getName());
mav.addObject("pluginId", projectData.getProject().getPluginId());
mav.addObject("projectName", projectData.get().getProject().getName());
mav.addObject("pluginId", projectData.get().getProject().getPluginId());
mav.addObject("projectSlug", slug);
mav.addObject("ownerName", author);
mav.addObject("projectDescription", projectData.getProject().getDescription());
mav.addObject("forumSync", projectData.getProject().getForumSync());
mav.addObject("projectDescription", projectData.get().getProject().getDescription());
mav.addObject("forumSync", projectData.get().getProject().getForumSync());
mav.addObject("pending", pendingVersion);
mav.addObject("channels", channelService.getProjectChannels(projectData.getProject().getId()));
mav.addObject("channels", channelService.getProjectChannels(projectData.get().getProject().getId()));
return fillModel(mav);
}
@GetMapping(value = "/{author}/{slug}/versions/recommended/download", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
@ResponseBody
public Object downloadRecommended(@PathVariable String author, @PathVariable String slug, @RequestParam(required = false) String token) {
ProjectsTable project = projectDao.get().getBySlug(author, slug);
Long recommendedVersionId = project.getRecommendedVersionId();
Long recommendedVersionId = projectsTable.get().getRecommendedVersionId();
if (recommendedVersionId == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
} else {
ProjectVersionsTable versionsTable = versionService.getVersion(project.getId(), recommendedVersionId);// TODO we need to check visibility here, the query currently doesnt do that
return sendVersion(project, versionsTable, token, false);
ProjectVersionsTable versionsTable = versionService.getVersion(projectsTable.get().getId(), recommendedVersionId);// TODO we need to check visibility here, the query currently doesnt do that
return sendVersion(projectsTable.get(), versionsTable, token, false);
}
}
@GetMapping(value = "/{author}/{slug}/versions/recommended/jar", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
@ResponseBody
public Object downloadRecommendedJar(@PathVariable String author, @PathVariable String slug, @RequestParam(required = false) String token) {
ProjectsTable project = projectDao.get().getBySlug(author, slug);
Long recommendedVersionId = project.getRecommendedVersionId();
Long recommendedVersionId = projectsTable.get().getRecommendedVersionId();
if (recommendedVersionId == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
} else {
ProjectVersionsTable versionsTable = versionService.getVersion(project.getId(), recommendedVersionId);// TODO we need to check visibility here, the query currently doesnt do that
return sendJar(project, versionsTable, token, false);
ProjectVersionsTable versionsTable = versionService.getVersion(projectsTable.get().getId(), recommendedVersionId);// TODO we need to check visibility here, the query currently doesnt do that
return sendJar(projectsTable.get(), versionsTable, token, false);
}
}
@ -300,14 +298,13 @@ public class VersionsController extends HangarController {
@RequestParam(required = false) String content,
RedirectAttributes attributes) {
Color channelColor = channelColorInput == null ? hangarConfig.channels.getColorDefault() : channelColorInput;
ProjectData projectData = projectService.getProjectData(author, slug);
PendingVersion pendingVersion = cacheManager.getCache(CacheConfig.PENDING_VERSION_CACHE).get(projectData.getProject().getId() + "/" + versionName, PendingVersion.class);
PendingVersion pendingVersion = cacheManager.getCache(CacheConfig.PENDING_VERSION_CACHE).get(projectData.get().getProject().getId() + "/" + versionName, PendingVersion.class);
if (pendingVersion == null) {
AlertUtil.showAlert(attributes, AlertUtil.AlertType.ERROR, "error.plugin.timeout");
return Routes.VERSIONS_SHOW_CREATOR.getRedirect(author, slug);
}
List<ProjectChannelsTable> projectChannels = channelService.getProjectChannels(projectData.getProject().getId());
List<ProjectChannelsTable> projectChannels = channelService.getProjectChannels(projectData.get().getProject().getId());
String alertMsg = null;
String[] alertArgs = new String[0];
Optional<ProjectChannelsTable> channelOptional = projectChannels.stream().filter(ch -> ch.getName().equals(channelInput.trim())).findAny();
@ -324,7 +321,7 @@ public class VersionsController extends HangarController {
AlertUtil.showAlert(attributes, AlertUtil.AlertType.ERROR, alertMsg, alertArgs);
return Routes.VERSIONS_SHOW_CREATOR.getRedirect(author, slug);
}
channel = channelService.addProjectChannel(projectData.getProject().getId(), channelInput.trim(), channelColor);
channel = channelService.addProjectChannel(projectData.get().getProject().getId(), channelInput.trim(), channelColor);
} else {
channel = channelOptional.get();
}
@ -343,22 +340,22 @@ public class VersionsController extends HangarController {
ProjectVersionsTable version;
try {
version = newPendingVersion.complete(request, projectData, projectFactory);
version = newPendingVersion.complete(request, projectData.get(), projectFactory);
} catch (HangarException e) {
AlertUtil.showAlert(attributes, AlertUtil.AlertType.ERROR, e.getMessage(), e.getArgs());
return Routes.VERSIONS_SHOW_CREATOR.getRedirect(author, slug);
}
if (recommended) {
projectData.getProject().setRecommendedVersionId(version.getId());
projectDao.get().update(projectData.getProject());
projectData.get().getProject().setRecommendedVersionId(version.getId());
projectDao.get().update(projectData.get().getProject());
}
if (unstable) {
versionService.addUnstableTag(version.getId());
}
userActionLogService.version(request, LoggedActionType.VERSION_UPLOADED.with(VersionContext.of(projectData.getProject().getId(), version.getId())), "published", "");
userActionLogService.version(request, LoggedActionType.VERSION_UPLOADED.with(VersionContext.of(projectData.get().getProject().getId(), version.getId())), "published", "");
return Routes.VERSIONS_SHOW.getRedirect(author, slug, versionName);
}
@ -367,9 +364,8 @@ public class VersionsController extends HangarController {
public ModelAndView show(@PathVariable String author, @PathVariable String slug, @PathVariable String version, ModelMap modelMap) {
ModelAndView mav = new ModelAndView("projects/versions/view");
AlertUtil.transferAlerts(mav, modelMap);
VersionData v = versionService.getVersionData(author, slug, version);
ScopedProjectData sp = projectService.getScopedProjectData(v.getP().getProject().getId());
mav.addObject("v", v);
ScopedProjectData sp = projectService.getScopedProjectData(versionData.get().getP().getProject().getId());
mav.addObject("v", versionData.get());
mav.addObject("sp", sp);
return fillModel(mav);
}
@ -378,32 +374,34 @@ public class VersionsController extends HangarController {
@Secured("ROLE_USER")
@PostMapping("/{author}/{slug}/versions/{version}/approve")
public ModelAndView approve(@PathVariable String author, @PathVariable String slug, @PathVariable String version) {
return _approve(author, slug, version, false);
return _approve(projectVersionsTable.get(), author, slug, version, false);
}
@GlobalPermission(NamedPermission.REVIEWER)
@Secured("ROLE_USER")
@PostMapping("/{author}/{slug}/versions/{version}/approvePartial")
public ModelAndView approvePartial(@PathVariable String author, @PathVariable String slug, @PathVariable String version) {
return _approve(author, slug, version, true);
return _approve(projectVersionsTable.get(), author, slug, version, true);
}
private ModelAndView _approve(String author, String slug, String version, boolean partial) {
private ModelAndView _approve(ProjectVersionsTable projectVersion, String author, String slug, String version, boolean partial) {
if (projectVersion == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
}
ReviewState newState = partial ? ReviewState.PARTIALLY_REVIEWED : ReviewState.REVIEWED;
ProjectVersionsTable versionsTable = versionService.getVersion(author, slug, version);
ReviewState oldState = versionsTable.getReviewState();
versionsTable.setReviewState(newState);
versionsTable.setReviewerId(userService.getCurrentUser().getId());
versionsTable.setApprovedAt(OffsetDateTime.now());
versionsTable.setVisibility(Visibility.PUBLIC);
versionService.update(versionsTable);
userActionLogService.version(request, LoggedActionType.VERSION_REVIEW_STATE_CHANGED.with(VersionContext.of(versionsTable.getProjectId(), versionsTable.getId())), newState.name(), oldState.name());
ReviewState oldState = projectVersion.getReviewState();
projectVersion.setReviewState(newState);
projectVersion.setReviewerId(userService.getCurrentUser().getId());
projectVersion.setApprovedAt(OffsetDateTime.now());
projectVersion.setVisibility(Visibility.PUBLIC);
versionService.update(projectVersion);
userActionLogService.version(request, LoggedActionType.VERSION_REVIEW_STATE_CHANGED.with(VersionContext.of(projectVersion.getProjectId(), projectVersion.getId())), newState.name(), oldState.name());
return Routes.VERSIONS_SHOW.getRedirect(author, slug, version);
}
@GetMapping("/{author}/{slug}/versions/{version}/confirm")
public Object showDownloadConfirm(@PathVariable String author, @PathVariable String slug, @PathVariable String version, @RequestParam(defaultValue = "0") DownloadType downloadType, @RequestParam Optional<Boolean> api, @RequestParam(required = false) String dummy) {
if (projectVersionsTable.getReviewState() == ReviewState.REVIEWED) {
if (projectVersionsTable.get().getReviewState() == ReviewState.REVIEWED) {
return AlertUtil.showAlert(Routes.PROJECTS_SHOW.getRedirect(author, slug), AlertType.ERROR, "error.plugin.stateChanged");
}
OffsetDateTime expiration = OffsetDateTime.now().plus(hangarConfig.projects.getUnsafeDownloadMaxAge().toMillis(), ChronoUnit.MILLIS);
@ -411,7 +409,7 @@ public class VersionsController extends HangarController {
String address = RequestUtil.getRemoteAddress(request);
String apiMsgKey = "version.download.confirm.body.api";
if (projectVersionsTable.getReviewState() == ReviewState.PARTIALLY_REVIEWED) {
if (projectVersionsTable.get().getReviewState() == ReviewState.PARTIALLY_REVIEWED) {
apiMsgKey = "version.download.confirmPartial.api";
}
String apiMsg = messageSource.getMessage(apiMsgKey, null, LocaleContextHolder.getLocale());
@ -430,7 +428,7 @@ public class VersionsController extends HangarController {
ObjectNode objectNode = mapper.createObjectNode();
objectNode.put("message", apiMsg);
objectNode.put("post", Routes.VERSIONS_CONFIRM_DOWNLOAD.getRouteUrl(author, slug, version, downloadType.ordinal() + "", token, null));
objectNode.put("url", Routes.VERSIONS_DOWNLOAD_JAR_BY_ID.getRouteUrl(projectsTable.getPluginId(), projectVersionsTable.getVersionString(), token));
objectNode.put("url", Routes.VERSIONS_DOWNLOAD_JAR_BY_ID.getRouteUrl(projectsTable.get().getPluginId(), projectVersionsTable.get().getVersionString(), token));
objectNode.put("curl", curlInstruction);
objectNode.put("token", token);
return new ResponseEntity<>(objectNode.toPrettyString(), headers, HttpStatus.MULTIPLE_CHOICES);
@ -442,11 +440,11 @@ public class VersionsController extends HangarController {
removeAddWarnings(address, expiration, token);
return new ResponseEntity<>(apiMsg + "\n" + curlInstruction + "\n", headers, HttpStatus.MULTIPLE_CHOICES);
} else {
ProjectChannelsTable channelsTable = channelService.getVersionsChannel(projectVersionsTable.getProjectId(), projectVersionsTable.getId());
response.addCookie(new Cookie(ProjectVersionDownloadWarningsTable.cookieKey(projectVersionsTable.getId()), "set"));
ProjectChannelsTable channelsTable = channelService.getVersionsChannel(projectVersionsTable.get().getProjectId(), projectVersionsTable.get().getId());
response.addCookie(new Cookie(ProjectVersionDownloadWarningsTable.cookieKey(projectVersionsTable.get().getId()), "set"));
ModelAndView mav = new ModelAndView("projects/versions/unsafeDownload");
mav.addObject("project", projectsTable);
mav.addObject("target", projectVersionsTable);
mav.addObject("project", projectsTable.get());
mav.addObject("target", projectVersionsTable.get());
mav.addObject("isTargetChannelNonReviewed", channelsTable.getIsNonReviewed());
mav.addObject("downloadType", downloadType);
return fillModel(mav);
@ -455,11 +453,11 @@ public class VersionsController extends HangarController {
}
private void removeAddWarnings(String address, OffsetDateTime expiration, String token) {
downloadWarningDao.get().deleteInvalid(address, projectVersionsTable.getId());
downloadWarningDao.get().deleteInvalid(address, projectVersionsTable.get().getId());
downloadWarningDao.get().insert(new ProjectVersionDownloadWarningsTable(
expiration,
token,
projectVersionsTable.getId(),
projectVersionsTable.get().getId(),
RequestUtil.getRemoteInetAddress(request)
));
}
@ -467,7 +465,7 @@ public class VersionsController extends HangarController {
@PostMapping(value = "/{author}/{slug}/versions/{version}/confirm", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
@ResponseBody
public Object confirmDownload(@PathVariable String author, @PathVariable String slug, @PathVariable String version, @RequestParam(defaultValue = "0") DownloadType downloadType, @RequestParam Optional<String> token, @RequestParam(required = false) String dummy) {
if (projectVersionsTable.getReviewState() == ReviewState.REVIEWED) {
if (projectVersionsTable.get().getReviewState() == ReviewState.REVIEWED) {
return Routes.PROJECTS_SHOW.getRedirect(author, slug);
}
ProjectVersionUnsafeDownloadsTable download;
@ -488,7 +486,7 @@ public class VersionsController extends HangarController {
private ProjectVersionUnsafeDownloadsTable confirmDownload0(DownloadType downloadType, Optional<String> token) {
if (token.isPresent()) {
ProjectVersionDownloadWarningsTable warning = downloadsService.findUnconfirmedWarning(RequestUtil.getRemoteInetAddress(request), token.get(), projectVersionsTable.getId());
ProjectVersionDownloadWarningsTable warning = downloadsService.findUnconfirmedWarning(RequestUtil.getRemoteInetAddress(request), token.get(), projectVersionsTable.get().getId());
if (warning == null) {
throw new HangarException("error.plugin.noConfirmDownload");
} else if (warning.hasExpired()) {
@ -500,7 +498,7 @@ public class VersionsController extends HangarController {
return download;
}
} else {
String cookieKey = ProjectVersionDownloadWarningsTable.cookieKey(projectVersionsTable.getId());
String cookieKey = ProjectVersionDownloadWarningsTable.cookieKey(projectVersionsTable.get().getId());
Cookie cookie = WebUtils.getCookie(request, cookieKey);
if (cookie != null && cookie.getValue().contains("set")) {
cookie.setValue("confirmed");
@ -532,9 +530,8 @@ public class VersionsController extends HangarController {
@GetMapping(value = "/{author}/{slug}/versions/{version}/download", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
@ResponseBody
public Object download(@PathVariable String author, @PathVariable String slug, @PathVariable String version, @RequestParam(required = false) String token, @RequestParam(defaultValue = "false") boolean confirm) {
ProjectsTable project = projectDao.get().getBySlug(author, slug);
ProjectVersionsTable versionsTable = versionService.getVersion(project.getId(), version);// TODO we need to check visibility here, the query currently doesnt do that
return sendVersion(project, versionsTable, token, confirm);
ProjectVersionsTable versionsTable = versionService.getVersion(projectsTable.get().getId(), version);// TODO we need to check visibility here, the query currently doesnt do that
return sendVersion(projectsTable.get(), versionsTable, token, confirm);
}
private Object sendVersion(ProjectsTable project, ProjectVersionsTable version, String token, boolean confirm) {
@ -597,9 +594,8 @@ public class VersionsController extends HangarController {
@GetMapping(value = "/{author}/{slug}/versions/{version}/jar", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
@ResponseBody
public Object downloadJar(@PathVariable String author, @PathVariable String slug, @PathVariable String version, @RequestParam(required = false) String token) {
ProjectsTable project = projectDao.get().getBySlug(author, slug);
ProjectVersionsTable versionsTable = versionService.getVersion(project.getId(), version);// TODO we need to check visibility here, the query currently doesnt do that
return sendJar(project, versionsTable, token, false);
ProjectVersionsTable versionsTable = versionService.getVersion(projectsTable.get().getId(), version);// TODO we need to check visibility here, the query currently doesnt do that
return sendJar(projectsTable.get(), versionsTable, token, false);
}
private Object sendJar(ProjectsTable project, ProjectVersionsTable version, String token, boolean api) {

View File

@ -8,7 +8,6 @@ import org.jdbi.v3.sqlobject.statement.GetGeneratedKeys;
import org.jdbi.v3.sqlobject.statement.SqlQuery;
import org.jdbi.v3.sqlobject.statement.SqlUpdate;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import java.net.InetAddress;

View File

@ -199,4 +199,26 @@ public class ProjectVersionsTable {
this.postId = postId;
}
@Override
public String toString() {
return "ProjectVersionsTable{" +
"id=" + id +
", createdAt=" + createdAt +
", versionString='" + versionString + '\'' +
", dependencies=" + dependencies +
", description='" + description + '\'' +
", projectId=" + projectId +
", channelId=" + channelId +
", fileSize=" + fileSize +
", hash='" + hash + '\'' +
", fileName='" + fileName + '\'' +
", reviewerId=" + reviewerId +
", approvedAt=" + approvedAt +
", authorId=" + authorId +
", visibility=" + visibility +
", reviewState=" + reviewState +
", createForumPost=" + createForumPost +
", postId=" + postId +
'}';
}
}

View File

@ -110,6 +110,10 @@ public class VersionService {
public VersionData getVersionData(String author, String slug, String versionString) {
ProjectData projectData = projectService.getProjectData(author, slug);
ProjectVersionsTable projectVersion = getVersion(projectData.getProject().getId(), versionString);
return getVersionData(projectData, projectVersion);
}
public VersionData getVersionData(ProjectData projectData, ProjectVersionsTable projectVersion) {
ProjectChannelsTable projectChannel = channelService.getProjectChannel(projectData.getProject().getId(), projectVersion.getChannelId());
String approvedBy = null;
if (projectVersion.getReviewerId() != null) {

View File

@ -144,6 +144,10 @@ public class ProjectService {
return projectDao.get().getBySlug(author, name);
}
public ProjectsTable getProjectsTable(String pluginId) {
return projectDao.get().getByPluginId(pluginId);
}
@Secured("ROLE_USER")
public void changeVisibility(ProjectsTable project, Visibility newVisibility, String comment) {
Preconditions.checkNotNull(project, "project");

View File

@ -2,13 +2,15 @@
<#import "*/utils/hangar.ftlh" as hangar />
<#import "*/layout/base.ftlh" as base />
<#assign message><@spring.messageArgs code="version.log.logger.title" args=[project.namespace] /></#assign>
<#assign message><@spring.message "version.log.logger.title"/></#assign>
<@base.base title=message>
<div class="row">
<div class="col-md-12">
<h1><@spring.message "version.log.visibility.title" /> <a
href="${Routes.VERSIONS_SHOW.getRouteUrl(project.project.name, project.project.slug, version.versionString)}">${project.project.name}
/${project.project.slug}/versions/${version.versionString}</a></h1>
<h1><@spring.message "version.log.visibility.title" />
<a href="${Routes.VERSIONS_SHOW.getRouteUrl(project.ownerName, project.project.slug, version.versionString)}">
${project.ownerName}/${project.project.slug}/versions/${version.versionString}
</a>
</h1>
</div>
</div>
<div class="row">