mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-12-15 05:41:51 +08:00
Add user & version JSON endpoints (#2131)
This commit is contained in:
parent
1908ec1627
commit
2293760e7f
@ -60,6 +60,10 @@ public final class WebUser {
|
||||
return username;
|
||||
}
|
||||
|
||||
public Set<String> getPermissions() {
|
||||
return permissions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "WebUser{" +
|
||||
|
@ -58,7 +58,9 @@ public class RootJSONResolver {
|
||||
PlayerJSONResolver playerJSONResolver,
|
||||
NetworkJSONResolver networkJSONResolver,
|
||||
FiltersJSONResolver filtersJSONResolver,
|
||||
QueryJSONResolver queryJSONResolver
|
||||
QueryJSONResolver queryJSONResolver,
|
||||
UserJSONResolver userJSONResolver,
|
||||
VersionJSONResolver versionJSONResolver
|
||||
) {
|
||||
this.identifiers = identifiers;
|
||||
this.asyncJSONResolverService = asyncJSONResolverService;
|
||||
@ -80,6 +82,8 @@ public class RootJSONResolver {
|
||||
.add("filters", filtersJSONResolver)
|
||||
.add("query", queryJSONResolver)
|
||||
.add("errors", errorsJSONResolver)
|
||||
.add("user", userJSONResolver)
|
||||
.add("version", versionJSONResolver)
|
||||
.build();
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,76 @@
|
||||
/*
|
||||
* This file is part of Player Analytics (Plan).
|
||||
*
|
||||
* Plan is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License v3 as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Plan is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.djrapitops.plan.delivery.webserver.resolver.json;
|
||||
|
||||
import com.djrapitops.plan.delivery.web.resolver.Resolver;
|
||||
import com.djrapitops.plan.delivery.web.resolver.Response;
|
||||
import com.djrapitops.plan.delivery.web.resolver.request.Request;
|
||||
import com.djrapitops.plan.delivery.web.resolver.request.WebUser;
|
||||
import com.djrapitops.plan.delivery.webserver.WebServer;
|
||||
import com.djrapitops.plan.delivery.webserver.auth.FailReason;
|
||||
import com.djrapitops.plan.exceptions.WebUserAuthException;
|
||||
import dagger.Lazy;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Resolves requests for /v1/user
|
||||
*
|
||||
* @author Kopo942
|
||||
*/
|
||||
@Singleton
|
||||
public class UserJSONResolver implements Resolver {
|
||||
|
||||
private final Lazy<WebServer> webServer;
|
||||
|
||||
@Inject
|
||||
public UserJSONResolver(Lazy<WebServer> webServer) {
|
||||
this.webServer = webServer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canAccess(Request request) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Response> resolve(Request request) {
|
||||
return Optional.of(getResponse(request));
|
||||
}
|
||||
|
||||
private Response getResponse(Request request) {
|
||||
if (!webServer.get().isAuthRequired()) {
|
||||
return Response.builder()
|
||||
.setStatus(404)
|
||||
.setJSONContent("{}")
|
||||
.build();
|
||||
}
|
||||
|
||||
WebUser user = request.getUser().orElseThrow(() -> new WebUserAuthException(FailReason.NO_USER_PRESENT));
|
||||
Map<String, Object> json = new HashMap<>();
|
||||
|
||||
json.put("username", user.getUsername());
|
||||
json.put("linkedTo", user.getName());
|
||||
json.put("permissions", user.getPermissions());
|
||||
|
||||
return Response.builder().setJSONContent(json).build();
|
||||
}
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
/*
|
||||
* This file is part of Player Analytics (Plan).
|
||||
*
|
||||
* Plan is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License v3 as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Plan is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.djrapitops.plan.delivery.webserver.resolver.json;
|
||||
|
||||
import com.djrapitops.plan.delivery.web.resolver.Resolver;
|
||||
import com.djrapitops.plan.delivery.web.resolver.Response;
|
||||
import com.djrapitops.plan.delivery.web.resolver.request.Request;
|
||||
import com.djrapitops.plan.version.VersionChecker;
|
||||
import com.djrapitops.plan.version.VersionInfo;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Resolves requests for /v1/version.
|
||||
*
|
||||
* @author Kopo942
|
||||
*/
|
||||
public class VersionJSONResolver implements Resolver {
|
||||
|
||||
private final VersionChecker versionChecker;
|
||||
private final String currentVersion;
|
||||
|
||||
@Inject
|
||||
public VersionJSONResolver(
|
||||
@Named("currentVersion") String currentVersion,
|
||||
VersionChecker versionChecker
|
||||
) {
|
||||
this.currentVersion = currentVersion;
|
||||
this.versionChecker = versionChecker;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canAccess(Request request) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Response> resolve(Request request) {
|
||||
return Optional.of(getResponse());
|
||||
}
|
||||
|
||||
private Response getResponse() {
|
||||
Map<String, Object> json = new HashMap<>();
|
||||
Optional<VersionInfo> newVersion = versionChecker.getNewVersionAvailable();
|
||||
boolean updateAvailable = newVersion.isPresent();
|
||||
|
||||
json.put("currentVersion", this.currentVersion);
|
||||
json.put("updateAvailable", updateAvailable);
|
||||
|
||||
if (updateAvailable) {
|
||||
json.put("newVersion", newVersion.get().getVersion().asString());
|
||||
json.put("downloadUrl", newVersion.get().getDownloadUrl());
|
||||
json.put("changelogUrl", newVersion.get().getChangeLogUrl());
|
||||
json.put("isRelease", newVersion.get().isRelease());
|
||||
}
|
||||
|
||||
return Response.builder().setJSONContent(json).build();
|
||||
}
|
||||
}
|
@ -197,6 +197,8 @@ public class AccessControlTest {
|
||||
"/errors,200",
|
||||
"/v1/network/listServers,200",
|
||||
"/v1/network/performanceOverview?servers=[" + TestConstants.SERVER_UUID_STRING + "],200",
|
||||
"/v1/version,200",
|
||||
"/v1/user,200",
|
||||
})
|
||||
void levelZeroCanAccess(String resource, String expectedResponseCode) throws NoSuchAlgorithmException, IOException, KeyManagementException {
|
||||
int responseCode = access(resource, cookieLevel0);
|
||||
@ -259,6 +261,8 @@ public class AccessControlTest {
|
||||
"/errors,403",
|
||||
"/v1/network/listServers,403",
|
||||
"/v1/network/performanceOverview?servers=[" + TestConstants.SERVER_UUID_STRING + "],403",
|
||||
"/v1/version,200",
|
||||
"/v1/user,200",
|
||||
})
|
||||
void levelOneCanAccess(String resource, String expectedResponseCode) throws NoSuchAlgorithmException, IOException, KeyManagementException {
|
||||
int responseCode = access(resource, cookieLevel1);
|
||||
@ -321,6 +325,8 @@ public class AccessControlTest {
|
||||
"/errors,403",
|
||||
"/v1/network/listServers,403",
|
||||
"/v1/network/performanceOverview?servers=[" + TestConstants.SERVER_UUID_STRING + "],403",
|
||||
"/v1/version,200",
|
||||
"/v1/user,200",
|
||||
})
|
||||
void levelTwoCanAccess(String resource, String expectedResponseCode) throws NoSuchAlgorithmException, IOException, KeyManagementException {
|
||||
int responseCode = access(resource, cookieLevel2);
|
||||
@ -381,6 +387,8 @@ public class AccessControlTest {
|
||||
"/v1/query,403",
|
||||
"/v1/network/listServers,403",
|
||||
"/v1/network/performanceOverview?servers=[" + TestConstants.SERVER_UUID_STRING + "],403",
|
||||
"/v1/version,200",
|
||||
"/v1/user,200",
|
||||
})
|
||||
void levelHundredCanNotAccess(String resource, String expectedResponseCode) throws NoSuchAlgorithmException, IOException, KeyManagementException {
|
||||
int responseCode = access(resource, cookieLevel100);
|
||||
|
Loading…
Reference in New Issue
Block a user