Files
matrix/cygnus-onprem-app/src/main/java/matrix/services/commons/CommonService.java

142 lines
4.7 KiB
Java

package matrix.services.commons;
import com.cygnus.db.CygnusDbExecutor;
import jakarta.servlet.http.HttpSession;
import lib.models.Option;
import lib.models.UserSession;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import matrix.nimble.model.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class CommonService {
public static final String SESSION_ATTRIBUTE = "Sessvals";
public static final String USER_SESSION_ATTRIBUTE = "userSession";
private static final Pattern MENU_COMMAND = Pattern.compile(
"SubmitMenuCommand\\(\\s*'([^']+)'\\s*,",
Pattern.CASE_INSENSITIVE);
private final CygnusDbExecutor dbExecutor;
@Autowired
public CommonService(CygnusDbExecutor dbExecutor) {
this.dbExecutor = Objects.requireNonNull(dbExecutor, "dbExecutor");
}
/* Used by the session-only unit tests. Production construction is handled
by Spring through the CygnusDbExecutor constructor above. */
public CommonService() {
this.dbExecutor = null;
}
public Session getSession(HttpSession httpSession) {
if (httpSession == null) {
return null;
}
Object value = httpSession.getAttribute(SESSION_ATTRIBUTE);
return value instanceof Session session ? session : null;
}
public void storeSession(HttpSession httpSession, Session session) {
Objects.requireNonNull(httpSession, "httpSession");
httpSession.setAttribute(SESSION_ATTRIBUTE, Objects.requireNonNull(session, "session"));
}
public UserSession getUserSession(HttpSession httpSession) {
if (httpSession == null) {
return null;
}
Object value = httpSession.getAttribute(USER_SESSION_ATTRIBUTE);
return value instanceof UserSession session ? session : null;
}
public void storeUserSession(HttpSession httpSession, UserSession session) {
Objects.requireNonNull(httpSession, "httpSession");
httpSession.setAttribute(
USER_SESSION_ATTRIBUTE, Objects.requireNonNull(session, "session"));
}
public boolean hasPageAccess(HttpSession httpSession, String pageRoute) {
return hasPageAccess(getSession(httpSession), pageRoute);
}
public boolean hasPageAccess(Session session, String pageRoute) {
if (session == null || session.getMenuHtml() == null) {
return false;
}
String requiredRoute = normalizeRoute(pageRoute);
if (requiredRoute.isEmpty()) {
return false;
}
Matcher matcher = MENU_COMMAND.matcher(session.getMenuHtml());
while (matcher.find()) {
if (normalizeRoute(matcher.group(1)).equals(requiredRoute)) {
return true;
}
}
return false;
}
public boolean hasPageAccess(UserSession session, String pageRoute) {
return session != null && hasPageAccess(session.getMenuHtml(), pageRoute);
}
private boolean hasPageAccess(String menuHtml, String pageRoute) {
if (menuHtml == null) {
return false;
}
String requiredRoute = normalizeRoute(pageRoute);
if (requiredRoute.isEmpty()) {
return false;
}
Matcher matcher = MENU_COMMAND.matcher(menuHtml);
while (matcher.find()) {
if (normalizeRoute(matcher.group(1)).equals(requiredRoute)) {
return true;
}
}
return false;
}
private String normalizeRoute(String route) {
if (route == null) {
return "";
}
String normalized = route.trim().replace('\\', '/');
int query = normalized.indexOf('?');
if (query >= 0) {
normalized = normalized.substring(0, query);
}
int fragment = normalized.indexOf('#');
if (fragment >= 0) {
normalized = normalized.substring(0, fragment);
}
while (normalized.endsWith("/")) {
normalized = normalized.substring(0, normalized.length() - 1);
}
int slash = normalized.lastIndexOf('/');
return slash >= 0 ? normalized.substring(slash + 1) : normalized;
}
public List<Option> getOptions(Integer queryId, Object[] queryParams) {
if (queryId == null) {
return List.of();
}
if (dbExecutor == null) {
throw new IllegalStateException("CygnusDbExecutor is not configured");
}
Object[] parameters = queryParams == null ? new Object[0] : queryParams;
return dbExecutor.query(queryId, parameters, Option.class);
}
}