use of com.djrapitops.plan.delivery.web.resolver.request.WebUser in project Plan by plan-player-analytics.
the class RequestHandler method buildRequest.
private Request buildRequest(HttpExchange exchange) {
String requestMethod = exchange.getRequestMethod();
URIPath path = new URIPath(exchange.getRequestURI().getPath());
URIQuery query = new URIQuery(exchange.getRequestURI().getRawQuery());
byte[] requestBody = readRequestBody(exchange);
WebUser user = getWebUser(exchange);
Map<String, String> headers = getRequestHeaders(exchange);
return new Request(requestMethod, path, query, user, headers, requestBody);
}
use of com.djrapitops.plan.delivery.web.resolver.request.WebUser in project Plan by plan-player-analytics.
the class PlayerJSONResolver method canAccess.
@Override
public boolean canAccess(Request request) {
WebUser user = request.getUser().orElse(new WebUser(""));
if (user.hasPermission("page.player.other"))
return true;
if (user.hasPermission("page.player.self")) {
try {
UUID webUserUUID = identifiers.getPlayerUUID(user.getName());
UUID playerUUID = identifiers.getPlayerUUID(request);
return playerUUID.equals(webUserUUID);
} catch (BadRequestException userDoesntExist) {
// Don't give away who has played on the server to someone with level 2 access
return false;
}
}
return false;
}
use of com.djrapitops.plan.delivery.web.resolver.request.WebUser in project Plan by plan-player-analytics.
the class ResponseResolver method tryToGetResponse.
/**
* @throws NotFoundException In some cases when page was not found, not all.
* @throws ForbiddenException If the user is not allowed to see the page
* @throws BadRequestException If the request did not have required things.
*/
private Response tryToGetResponse(Request request) {
if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS
return Response.builder().setStatus(204).build();
}
Optional<WebUser> user = request.getUser();
List<Resolver> foundResolvers = resolverService.getResolvers(request.getPath().asString());
if (foundResolvers.isEmpty())
return responseFactory.pageNotFound404();
for (Resolver resolver : foundResolvers) {
boolean isAuthRequired = webServer.get().isAuthRequired() && resolver.requiresAuth(request);
if (isAuthRequired) {
if (!user.isPresent()) {
if (webServer.get().isUsingHTTPS()) {
throw new WebUserAuthException(FailReason.NO_USER_PRESENT);
} else {
return responseFactory.forbidden403();
}
}
if (resolver.canAccess(request)) {
Optional<Response> resolved = resolver.resolve(request);
if (resolved.isPresent())
return resolved.get();
} else {
return responseFactory.forbidden403();
}
} else {
Optional<Response> resolved = resolver.resolve(request);
if (resolved.isPresent())
return resolved.get();
}
}
return responseFactory.pageNotFound404();
}
use of com.djrapitops.plan.delivery.web.resolver.request.WebUser in project Plan by plan-player-analytics.
the class PlayerPageResolver method canAccess.
@Override
public boolean canAccess(Request request) {
URIPath path = request.getPath();
WebUser user = request.getUser().orElse(new WebUser(""));
boolean isOwnPage = path.getPart(1).map(nameOrUUID -> {
// name matches user
if (user.getName().equalsIgnoreCase(nameOrUUID))
return true;
return // uuid matches user
uuidUtility.getNameOf(nameOrUUID).map(user.getName()::equalsIgnoreCase).orElse(// uuid or name don't match
false);
}).orElse(// No name or UUID given
true);
return user.hasPermission("page.player.other") || user.hasPermission("page.player.self") && isOwnPage;
}
use of com.djrapitops.plan.delivery.web.resolver.request.WebUser in project Plan by plan-player-analytics.
the class RootPageResolver method getResponse.
private Response getResponse(Request request) {
Server server = serverInfo.getServer();
if (!webServer.get().isAuthRequired()) {
String redirectTo = server.isProxy() ? "network" : "server/" + Html.encodeToURL(server.getIdentifiableName());
return responseFactory.redirectResponse(redirectTo);
}
WebUser user = request.getUser().orElseThrow(() -> new WebUserAuthException(FailReason.EXPIRED_COOKIE));
if (user.hasPermission("page.server")) {
return responseFactory.redirectResponse(server.isProxy() ? "network" : "server/" + Html.encodeToURL(server.getIdentifiableName()));
} else if (user.hasPermission("page.players")) {
return responseFactory.redirectResponse("players");
} else if (user.hasPermission("page.player.self")) {
return responseFactory.redirectResponse("player/" + Html.encodeToURL(user.getName()));
} else {
return responseFactory.forbidden403(user.getName() + " has insufficient permissions to be redirected to any page. Needs one of: 'page.server', 'page.players' or 'page.player.self'");
}
}
Aggregations