Browse Source
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>feature/brute_force_protection
4 changed files with 103 additions and 9 deletions
@ -0,0 +1,75 @@ |
|||||||
|
/* © SRSoftware 2025 */ |
||||||
|
package de.srsoftware.umbrella.legacy; |
||||||
|
|
||||||
|
|
||||||
|
import static de.srsoftware.tools.Optionals.nullable; |
||||||
|
import static de.srsoftware.umbrella.core.Constants.*; |
||||||
|
import static de.srsoftware.umbrella.core.Paths.JSON; |
||||||
|
import static de.srsoftware.umbrella.core.Util.mapValues; |
||||||
|
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.invalidFieldException; |
||||||
|
|
||||||
|
import com.sun.net.httpserver.HttpExchange; |
||||||
|
import de.srsoftware.configuration.Configuration; |
||||||
|
import de.srsoftware.tools.Path; |
||||||
|
import de.srsoftware.tools.SessionToken; |
||||||
|
import de.srsoftware.umbrella.core.BaseHandler; |
||||||
|
import de.srsoftware.umbrella.core.ModuleRegistry; |
||||||
|
import de.srsoftware.umbrella.core.model.Token; |
||||||
|
import java.io.IOException; |
||||||
|
import java.util.Map; |
||||||
|
import java.util.Optional; |
||||||
|
|
||||||
|
import de.srsoftware.umbrella.core.model.UmbrellaUser; |
||||||
|
import org.json.JSONObject; |
||||||
|
|
||||||
|
public class ProjectLegacy extends BaseHandler { |
||||||
|
private final ModuleRegistry registry; |
||||||
|
private final Configuration config; |
||||||
|
|
||||||
|
public ProjectLegacy(ModuleRegistry registry, Configuration config) { |
||||||
|
this.registry = registry; |
||||||
|
this.config = config.subset("umbrella.modules").orElseThrow(() -> new RuntimeException("Missing configuration: umbrella.modules")); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean doDelete(Path path, HttpExchange ex) throws IOException { |
||||||
|
return super.doDelete(path, ex); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean doGet(Path path, HttpExchange ex) throws IOException { |
||||||
|
if (path.empty()) return sendRedirect(ex, url(ex).replaceAll("/legacy/.*","")); |
||||||
|
return super.doGet(path, ex); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean doOptions(Path path, HttpExchange ex) throws IOException { |
||||||
|
return super.doOptions(path, ex); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean doPatch(Path path, HttpExchange ex) throws IOException { |
||||||
|
return super.doPatch(path, ex); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean doPost(Path path, HttpExchange ex) throws IOException{ |
||||||
|
var params = formData(ex); |
||||||
|
|
||||||
|
Optional<Token> token = SessionToken.from(ex).map(Token::of); |
||||||
|
if (token.isEmpty()) token = nullable(params.get(TOKEN)).map(Object::toString).map(Token::of); |
||||||
|
var user = registry.userService().loadUser(token); |
||||||
|
if (user.isEmpty()) return unauthorized(ex); |
||||||
|
|
||||||
|
return switch (path.pop()){ |
||||||
|
case JSON -> postProjectJson(ex,params,user.get()); |
||||||
|
default -> super.doPost(path, ex); |
||||||
|
}; |
||||||
|
} |
||||||
|
|
||||||
|
private boolean postProjectJson(HttpExchange ex, Map<String, Object> params, UmbrellaUser user) throws IOException { |
||||||
|
var includeUsers = "1".equals(params.get(USERS)); |
||||||
|
var projects = registry.projectService().listUserProjects(user.id(), false); |
||||||
|
return sendContent(ex, mapValues(projects)); |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue