introduced ModuleRegistry to easy inter-module dependencies
This commit is contained in:
@@ -30,7 +30,7 @@ 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.api.PostBox;
|
||||
import de.srsoftware.umbrella.core.ModuleRegistry;
|
||||
import de.srsoftware.umbrella.core.api.UserService;
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import de.srsoftware.umbrella.core.model.*;
|
||||
@@ -59,6 +59,7 @@ public class UserModule extends BaseHandler implements UserService {
|
||||
|
||||
|
||||
private record State(LoginService loginService, JSONObject config){
|
||||
|
||||
public static State of(LoginService loginService, JSONObject config) {
|
||||
return new State(loginService,config);
|
||||
}
|
||||
@@ -68,9 +69,9 @@ public class UserModule extends BaseHandler implements UserService {
|
||||
private static final System.Logger LOG = System.getLogger("User");
|
||||
private final UserDb users;
|
||||
private final LoginServiceDb logins;
|
||||
private final ModuleRegistry registry;
|
||||
private final HashMap<String, State> stateMap = new HashMap<>(); // map from state to OIDC provider name
|
||||
private final HashMap<String,String> tokenMap = new HashMap<>();
|
||||
private final PostBox messages;
|
||||
|
||||
static {
|
||||
try {
|
||||
@@ -80,16 +81,12 @@ public class UserModule extends BaseHandler implements UserService {
|
||||
}
|
||||
}
|
||||
|
||||
public UserModule(Configuration config, PostBox messageSystem) throws UmbrellaException {
|
||||
public UserModule(ModuleRegistry registry, Configuration config) throws UmbrellaException {
|
||||
var dbFile = config.get(CONFIG_DATABASE).orElseThrow(() -> missingConfigException(CONFIG_DATABASE));
|
||||
// may be splitted in separate db files later
|
||||
logins = new SqliteDB(connect(dbFile));
|
||||
messages = messageSystem;
|
||||
users = new SqliteDB(connect(dbFile));
|
||||
}
|
||||
|
||||
public UserDb userDb(){
|
||||
return users;
|
||||
logins = new SqliteDB(connect(dbFile));
|
||||
users = new SqliteDB(connect(dbFile));
|
||||
this.registry = registry.add(this);
|
||||
}
|
||||
|
||||
private boolean deleteOIDC(HttpExchange ex, UmbrellaUser user, Path path) throws IOException {
|
||||
@@ -137,10 +134,15 @@ public class UserModule extends BaseHandler implements UserService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Long, UmbrellaUser> list(Collection<Long> ids) throws UmbrellaException {
|
||||
public Map<Long, UmbrellaUser> list(Integer start, Integer limit, Collection<Long> ids) throws UmbrellaException {
|
||||
return users.list(0,null,ids);
|
||||
}
|
||||
|
||||
public UmbrellaUser load(Session session) throws UmbrellaException{
|
||||
return users.load(session);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public UmbrellaUser loadUser(long userId) throws UmbrellaException {
|
||||
return users.load(userId);
|
||||
@@ -149,8 +151,8 @@ public class UserModule extends BaseHandler implements UserService {
|
||||
public Optional<UmbrellaUser> loadUser(Optional<Token> sessionToken) throws UmbrellaException {
|
||||
try {
|
||||
if (sessionToken.isEmpty()) return empty();
|
||||
var session = users.load(sessionToken.get());
|
||||
return Optional.of(users.load(session));
|
||||
var session = load(sessionToken.get());
|
||||
return Optional.of(load(session));
|
||||
} catch (UmbrellaException e) {
|
||||
return empty();
|
||||
}
|
||||
@@ -256,6 +258,12 @@ public class UserModule extends BaseHandler implements UserService {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dropSession(Token token) throws UmbrellaException{
|
||||
users.dropSession(token);
|
||||
}
|
||||
|
||||
|
||||
private boolean exchangeToken(HttpExchange ex) throws IOException {
|
||||
JSONObject params;
|
||||
try {
|
||||
@@ -296,6 +304,11 @@ public class UserModule extends BaseHandler implements UserService {
|
||||
}
|
||||
}
|
||||
|
||||
public Session extend(Session session) throws UmbrellaException{
|
||||
return users.extend(session);
|
||||
}
|
||||
|
||||
|
||||
private boolean getConnectedServices(HttpExchange ex, UmbrellaUser user) throws IOException {
|
||||
if (user == null) return unauthorized(ex);
|
||||
try {
|
||||
@@ -395,6 +408,11 @@ public class UserModule extends BaseHandler implements UserService {
|
||||
return sendContent(ex,targetUser.toMap());
|
||||
}
|
||||
|
||||
public Session load(Token token) throws UmbrellaException{
|
||||
return users.load(token);
|
||||
}
|
||||
|
||||
|
||||
public boolean logout(HttpExchange ex, Optional<Token> optToken) throws IOException {
|
||||
if (optToken.isPresent()){
|
||||
var token = optToken.get();
|
||||
@@ -451,11 +469,6 @@ public class UserModule extends BaseHandler implements UserService {
|
||||
return sendContent(ex,service.toMap());
|
||||
}
|
||||
|
||||
@Override
|
||||
public PostBox postBox() {
|
||||
return messages;
|
||||
}
|
||||
|
||||
private boolean postCreate(HttpExchange ex) throws IOException, UmbrellaException {
|
||||
var optUser = loadUser(ex);
|
||||
if (!(optUser.isPresent() && optUser.get() instanceof DbUser dbUser)) return unauthorized(ex);
|
||||
@@ -491,7 +504,7 @@ public class UserModule extends BaseHandler implements UserService {
|
||||
var fills = Map.of("url",url);
|
||||
var message = new Message(user,subject,content,fills,null);
|
||||
var envelope = new Envelope(message,user);
|
||||
messages.send(envelope);
|
||||
registry.postBox().send(envelope);
|
||||
} catch (UmbrellaException e){
|
||||
return send(ex,e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user