working on Autocomplete field for member addition to projects

This commit is contained in:
2025-07-21 23:47:28 +02:00
parent cad74d1b78
commit 7eca9dd08e
7 changed files with 123 additions and 28 deletions

View File

@@ -7,12 +7,11 @@ import static de.srsoftware.tools.Strings.uuid;
import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
import static de.srsoftware.umbrella.core.Constants.*;
import static de.srsoftware.umbrella.core.Constants.CODE;
import static de.srsoftware.umbrella.core.Paths.LIST;
import static de.srsoftware.umbrella.core.Paths.LOGOUT;
import static de.srsoftware.umbrella.core.Constants.TOKEN;
import static de.srsoftware.umbrella.core.Paths.*;
import static de.srsoftware.umbrella.core.ResponseCode.*;
import static de.srsoftware.umbrella.core.ResponseCode.HTTP_SERVER_ERROR;
import static de.srsoftware.umbrella.core.Util.open;
import static de.srsoftware.umbrella.core.Util.request;
import static de.srsoftware.umbrella.core.Util.*;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
import static de.srsoftware.umbrella.user.Constants.*;
import static de.srsoftware.umbrella.user.Paths.*;
@@ -257,6 +256,7 @@ public class UserModule extends BaseHandler implements UserService {
case OIDC -> postOIDC(ex, path);
case IMPERSONATE -> impersonate(ex, targetId);
case LOGIN -> postLogin(ex);
case SEARCH -> postSearch(ex);
case RESET_PW -> postResetPassword(ex);
case null, default -> super.doPost(path,ex);
};
@@ -449,6 +449,17 @@ public class UserModule extends BaseHandler implements UserService {
}
}
private boolean patchService(HttpExchange ex, String serviceName, UmbrellaUser requestingUser) throws IOException, UmbrellaException {
if (!(requestingUser instanceof DbUser user && user.permissions().contains(MANAGE_LOGIN_SERVICES))) throw forbidden("You are not allowed to manage that service!");
var json = json(ex);
if (!json.has(NAME) || !(json.get(NAME) instanceof String name) || name.isBlank()) throw missingFieldException(NAME);
if (!json.has(URL) || !(json.get(URL) instanceof String url) || url.isBlank()) throw missingFieldException(URL);
if (!json.has(CLIENT_ID) || !(json.get(CLIENT_ID) instanceof String clientId) || clientId.isBlank()) throw missingFieldException(CLIENT_ID);
if (!json.has(CLIENT_SECRET) || !(json.get(CLIENT_SECRET) instanceof String secret) || secret.isBlank()) throw missingFieldException(CLIENT_SECRET);
var service = logins.save(new LoginService(name,url,clientId,secret, DEFAULT_FIELD));
return sendContent(ex,service.toMap());
}
@Override
public PostBox postBox() {
return messages;
@@ -496,17 +507,6 @@ public class UserModule extends BaseHandler implements UserService {
return ok(ex);
}
private boolean patchService(HttpExchange ex, String serviceName, UmbrellaUser requestingUser) throws IOException, UmbrellaException {
if (!(requestingUser instanceof DbUser user && user.permissions().contains(MANAGE_LOGIN_SERVICES))) throw forbidden("You are not allowed to manage that service!");
var json = json(ex);
if (!json.has(NAME) || !(json.get(NAME) instanceof String name) || name.isBlank()) throw missingFieldException(NAME);
if (!json.has(URL) || !(json.get(URL) instanceof String url) || url.isBlank()) throw missingFieldException(URL);
if (!json.has(CLIENT_ID) || !(json.get(CLIENT_ID) instanceof String clientId) || clientId.isBlank()) throw missingFieldException(CLIENT_ID);
if (!json.has(CLIENT_SECRET) || !(json.get(CLIENT_SECRET) instanceof String secret) || secret.isBlank()) throw missingFieldException(CLIENT_SECRET);
var service = logins.save(new LoginService(name,url,clientId,secret, DEFAULT_FIELD));
return sendContent(ex,service.toMap());
}
private boolean postLogin(HttpExchange ex) throws IOException {
var json = json(ex);
if (!(json.has(USERNAME) && json.get(USERNAME) instanceof String username)) return sendContent(ex, HTTP_UNPROCESSABLE,"Username missing");
@@ -522,7 +522,12 @@ public class UserModule extends BaseHandler implements UserService {
}
}
private boolean postSearch(HttpExchange ex) throws IOException {
var requestingUser = loadUser(ex);
if (!(requestingUser.isPresent() && requestingUser.get() instanceof DbUser dbUser)) return unauthorized(ex);
var key = body(ex);
return sendContent(ex,mapValues(users.search(key)));
}
static int score(String password){
if (password == null) return 0;

View File

@@ -17,6 +17,8 @@ public interface UserDb {
Boolean dropSession(Token token) throws UmbrellaException;
Session extend(Session session) throws UmbrellaException;
/**
* Get a session for the provided user.
* @param user
@@ -24,8 +26,6 @@ public interface UserDb {
*/
Session getSession(UmbrellaUser user) throws UmbrellaException;
Session extend(Session session) throws UmbrellaException;
Map<Long, UmbrellaUser> list(Integer start, Integer limit, Collection<Long> ids) throws UmbrellaException;
Session load(Token token) throws UmbrellaException;
@@ -39,4 +39,6 @@ public interface UserDb {
UmbrellaUser load(String key, Password password) throws UmbrellaException;
UmbrellaUser save(DbUser user) throws UmbrellaException;
Map<Long,DbUser> search(String key);
}

View File

@@ -5,6 +5,7 @@ import static de.srsoftware.tools.jdbc.Condition.*;
import static de.srsoftware.tools.jdbc.Query.*;
import static de.srsoftware.tools.jdbc.Query.SelectQuery.ALL;
import static de.srsoftware.umbrella.core.Constants.*;
import static de.srsoftware.umbrella.core.ResponseCode.HTTP_SERVER_ERROR;
import static de.srsoftware.umbrella.user.Constants.*;
import static de.srsoftware.umbrella.user.model.DbUser.ADMIN_PERMISSIONS;
import static java.lang.System.Logger.Level.*;
@@ -499,6 +500,22 @@ CREATE TABLE IF NOT EXISTS {0} (
return user;
}
@Override
public Map<Long, DbUser> search(String key) {
try {
var rs = select(ALL).from(TABLE_USERS).where(LOGIN,like("%"+key+"%")).exec(db);
var users = new HashMap<Long,DbUser>();
while (rs.next()){
var user = toUser(rs);
users.put(user.id(),user);
}
rs.close();
return users;
} catch (SQLException e){
throw new UmbrellaException(HTTP_SERVER_ERROR,"Failed to search for user by key = {0}",key);
}
}
public Instant then(){
return LocalDateTime.now().plus(DEFAULT_SESSION_DURATION).toInstant(ZoneOffset.UTC);
}