Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Added extensive /dashboard/query endpoint (#1272)

Martin Peeters8a87aa3445b5
diff --git a/manager/src/main/java/org/openremote/manager/dashboard/DashboardResourceImpl.java b/manager/src/main/java/org/openremote/manager/dashboard/DashboardResourceImpl.java
index 27f942844..be492c58d 100644
--- a/manager/src/main/java/org/openremote/manager/dashboard/DashboardResourceImpl.java
+++ b/manager/src/main/java/org/openremote/manager/dashboard/DashboardResourceImpl.java
@@ -1,3 +1,22 @@
+/*
+ * Copyright 2024, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
package org.openremote.manager.dashboard;
import jakarta.ws.rs.WebApplicationException;
@@ -10,15 +29,23 @@ import org.openremote.model.dashboard.Dashboard;
import org.openremote.model.dashboard.DashboardAccess;
import org.openremote.model.dashboard.DashboardResource;
import org.openremote.model.http.RequestParams;
+import org.openremote.model.query.DashboardQuery;
+import org.openremote.model.query.filter.RealmPredicate;
import org.openremote.model.security.ClientRole;
import org.openremote.model.util.ValueUtil;
+import java.util.Arrays;
import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.logging.Logger;
import static jakarta.ws.rs.core.Response.Status.*;
public class DashboardResourceImpl extends ManagerWebResource implements DashboardResource {
+ public static final Logger LOG = Logger.getLogger(DashboardResourceImpl.class.getName());
+
protected final DashboardStorageService dashboardStorageService;
protected final MessageBrokerService messageBrokerService;
@@ -28,59 +55,51 @@ public class DashboardResourceImpl extends ManagerWebResource implements Dashboa
this.messageBrokerService = messageBrokerService;
}
-
@Override
public Dashboard[] getAllRealmDashboards(RequestParams requestParams, String realm) {
- boolean publicOnly = true;
if(realm == null) {
throw new WebApplicationException(BAD_REQUEST);
}
- try {
- // Realm should be enabled. Also takes unauthenticated users into count.
- if(!isRealmActiveAndAccessible(realm)) {
- throw new WebApplicationException(FORBIDDEN);
- }
- // if not authenticated, or having no INSIGHTS access, only fetch public dashboards
- if(isAuthenticated()) {
- publicOnly = (!hasResourceRole(ClientRole.READ_INSIGHTS.getValue(), Constants.KEYCLOAK_CLIENT_ID));
- }
- return this.dashboardStorageService.query(null, realm, getUserId(), publicOnly, false);
-
- } catch (IllegalStateException ex) {
- ex.printStackTrace();
- throw new WebApplicationException(ex, INTERNAL_SERVER_ERROR);
+ if(!isRealmActiveAndAccessible(realm)) {
+ throw new WebApplicationException(FORBIDDEN);
}
+ return dashboardStorageService.query(this.createDashboardQuery(realm), getUserId());
}
@Override
public Dashboard get(RequestParams requestParams, String realm, String dashboardId) {
- boolean publicOnly = true;
if(realm == null) {
throw new WebApplicationException(BAD_REQUEST);
}
- try {
- // Realm should be enabled. Also takes unauthenticated users into count.
- if(!isRealmActiveAndAccessible(realm)) {
+ Dashboard[] dashboards = dashboardStorageService.query(
+ this.createDashboardQuery(realm).ids(dashboardId).limit(1),
+ getUserId()
+ );
+ // If no dashboards were returned, check whether it existed, and return a different response.
+ if(dashboards.length == 0) {
+ if(this.dashboardStorageService.exists(dashboardId, realm)) {
throw new WebApplicationException(FORBIDDEN);
+ } else {
+ throw new WebApplicationException(NOT_FOUND);
}
- // if not authenticated, or having no INSIGHTS access, only take public dashboards into count
- if(isAuthenticated()) {
- publicOnly = (!hasResourceRole(ClientRole.READ_INSIGHTS.getValue(), Constants.KEYCLOAK_CLIENT_ID));
- }
- Dashboard[] dashboards = this.dashboardStorageService.query(Collections.singletonList(dashboardId), realm, getUserId(), publicOnly, false);
- if(dashboards.length == 0) {
- if(this.dashboardStorageService.exists(dashboardId, realm)) {
- throw new WebApplicationException(FORBIDDEN); // when no dashboard returned from query, but it does exist.
- } else {
- throw new WebApplicationException(NOT_FOUND); // aka it does not exist
- }
- }
- return dashboards[0]; // only return first entry
+ }
- } catch (IllegalStateException ex) {
- ex.printStackTrace();
- throw new WebApplicationException(ex, INTERNAL_SERVER_ERROR);
+ return dashboards[0];
+ }
+
+ @Override
+ public Dashboard[] query(RequestParams requestParams, DashboardQuery query) {
+ if(query == null) {
+ query = this.createDashboardQuery(getRequestRealmName());
+ }
+ if(query.realm == null || query.realm.name == null) {
+ query.realm(new RealmPredicate(getRequestRealmName()));
+ }
+ if(isAuthenticated() && !isSuperUser() && !(getAuthenticatedRealmName().equals(query.realm.name))) {
+ throw new WebApplicationException(FORBIDDEN);
}
+
+ return dashboardStorageService.query(query, getUserId());
}
@Override
@@ -141,4 +160,62 @@ public class DashboardResourceImpl extends ManagerWebResource implements Dashboa
throw new WebApplicationException(ex, INTERNAL_SERVER_ERROR);
}
}
+
+ /**
+ * Function that builds an {@link DashboardQuery} object, based on user permissions.
+ * Automatically fills NULL values, adjusts access filters based on roles, etc.
+ *
+ * @param realm
+ * @return An instantiated DashboardQuery with the correct permissions based on the user.
+ */
+ protected DashboardQuery createDashboardQuery(String realm) {
+ if(realm == null) {
+ realm = getRequestRealmName();
+ }
+ DashboardQuery query = new DashboardQuery().realm(new RealmPredicate(realm));
+ Set<DashboardAccess> userViewAccess = new HashSet<>(Set.of(query.conditions.getDashboard().getViewAccess()));
+ Set<DashboardAccess> userEditAccess = new HashSet<>(Set.of(query.conditions.getDashboard().getEditAccess()));
+ Set<DashboardQuery.AssetAccess> assetAccess = new HashSet<>(Set.of(query.conditions.getAsset().getAccess()));
+
+ // Detect cross realm access
+ if(isAuthenticated() && !isSuperUser() && !realm.equals(getAuthenticatedRealmName())) {
+ throw new WebApplicationException(FORBIDDEN);
+ }
+
+ // User always has access to public dashboards
+ userViewAccess.add(DashboardAccess.PUBLIC);
+ userEditAccess.add(DashboardAccess.PUBLIC);
+ assetAccess.add(DashboardQuery.AssetAccess.REALM);
+
+ // Adjust query object based on user roles/permissions
+ if (isAuthenticated()) {
+ assetAccess.add(DashboardQuery.AssetAccess.LINKED);
+ if (hasResourceRole(ClientRole.READ_INSIGHTS.getValue(), Constants.KEYCLOAK_CLIENT_ID)) {
+ Collections.addAll(userViewAccess, DashboardAccess.SHARED, DashboardAccess.PRIVATE);
+ }
+ if (hasResourceRole(ClientRole.WRITE_INSIGHTS.getValue(), Constants.KEYCLOAK_CLIENT_ID)) {
+ Collections.addAll(userEditAccess, DashboardAccess.SHARED, DashboardAccess.PRIVATE);
+ }
+ if (isRestrictedUser()) {
+ assetAccess = new HashSet<>(Set.of(DashboardQuery.AssetAccess.RESTRICTED));
+ }
+ }
+
+ // If not logged in, force only public read/write access
+ else {
+ userViewAccess = new HashSet<>(Set.of(DashboardAccess.PUBLIC));
+ userEditAccess = new HashSet<>(Set.of(DashboardAccess.PUBLIC));
+ assetAccess = new HashSet<>(Set.of(DashboardQuery.AssetAccess.REALM));
+ }
+
+ // Build query object and return
+ return query.conditions(new DashboardQuery.Conditions(
+ new DashboardQuery.DashboardConditions()
+ .viewAccess(userViewAccess.toArray(new DashboardAccess[0]))
+ .editAccess(userEditAccess.toArray(new DashboardAccess[0])),
+ new DashboardQuery.AssetConditions()
+ .access(assetAccess.toArray(new DashboardQuery.AssetAccess[0]))
+ )
+ );
+ }
}
diff --git a/manager/src/main/java/org/openremote/manager/dashboard/DashboardStorageService.java b/manager/src/main/java/org/openremote/manager/dashboard/DashboardStorageService.java
index d43a551ee..d6c7bb3f1 100644
--- a/manager/src/main/java/org/openremote/manager/dashboard/DashboardStorageService.java
+++ b/manager/src/main/java/org/openremote/manager/dashboard/DashboardStorageService.java
@@ -1,32 +1,54 @@
+/*
+ * Copyright 2024, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
package org.openremote.manager.dashboard;
import jakarta.persistence.Query;
-import jakarta.persistence.criteria.CriteriaBuilder;
-import jakarta.persistence.criteria.CriteriaQuery;
-import jakarta.persistence.criteria.Predicate;
-import jakarta.persistence.criteria.Root;
import org.apache.camel.builder.RouteBuilder;
import org.openremote.container.message.MessageBrokerService;
import org.openremote.container.persistence.PersistenceService;
import org.openremote.container.timer.TimerService;
+import org.openremote.manager.asset.AssetStorageService;
import org.openremote.manager.security.ManagerIdentityService;
import org.openremote.manager.web.ManagerWebService;
import org.openremote.model.Container;
import org.openremote.model.ContainerService;
+import org.openremote.model.asset.UserAssetLink;
import org.openremote.model.dashboard.Dashboard;
import org.openremote.model.dashboard.DashboardAccess;
+import org.openremote.model.query.DashboardQuery;
+import org.openremote.model.query.filter.RealmPredicate;
+import org.openremote.model.query.filter.StringPredicate;
import java.util.*;
+import java.util.logging.Logger;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
public class DashboardStorageService extends RouteBuilder implements ContainerService {
+ protected static final Logger LOG = Logger.getLogger(DashboardStorageService.class.getName());
+
protected ManagerIdentityService identityService;
protected PersistenceService persistenceService;
-
- @Override
- public int getPriority() {
- return ContainerService.DEFAULT_PRIORITY;
- }
+ protected AssetStorageService assetStorageService;
+ protected TimerService timerService;
@Override
public void configure() throws Exception {
@@ -37,6 +59,8 @@ public class DashboardStorageService extends RouteBuilder implements ContainerSe
public void init(Container container) throws Exception {
identityService = container.getService(ManagerIdentityService.class);
persistenceService = container.getService(PersistenceService.class);
+ assetStorageService = container.getService(AssetStorageService.class);
+ timerService = container.getService(TimerService.class);
container.getService(ManagerWebService.class).addApiSingleton(
new DashboardResourceImpl(
container.getService(TimerService.class),
@@ -58,59 +82,169 @@ public class DashboardStorageService extends RouteBuilder implements ContainerSe
}
+ /**
+ * Pulls dashboards from the database based on the query object.
+ * Useful to specifically filter and request dashboards.
+ * @param dashboardQuery see {@link DashboardQuery} for specification
+ * @return List of dashboards present in the database
+ */
+ protected Dashboard[] query(DashboardQuery dashboardQuery, String userId) {
- /* -------------------------- */
+ return (Dashboard[]) persistenceService.doReturningTransaction((em) -> {
+
+ StringBuilder sql = new StringBuilder("SELECT * FROM Dashboard WHERE realm LIKE :realm");
+ Map<String, Object> parameters = new HashMap<>();
+ parameters.put("realm", dashboardQuery.realm.name);
+
+ if(dashboardQuery.ids != null) {
+ this.appendSqlIdFilter(sql, dashboardQuery, parameters);
+ }
+ if(dashboardQuery.names != null) {
+ this.appendSqlNamesFilter(sql, dashboardQuery, parameters);
+ }
+ if(dashboardQuery.userIds != null) {
+ this.appendSqlUserIdsFilter(sql, dashboardQuery, parameters);
+ }
+ if(dashboardQuery.conditions.getDashboard() != null) {
+ this.appendSqlDashboardConditionsFilter(sql, dashboardQuery, parameters, userId);
+ }
+ if(dashboardQuery.conditions.getAsset() != null) {
+ this.appendSqlAssetConditionsFilter(sql, dashboardQuery, parameters, userId);
+ }
+
+ /** TODO: Implement SELECT filtering {@link org.openremote.model.query.DashboardQuery.Select} */
+
+ // Apply pagination
+ if(dashboardQuery.start != null) {
+ sql.append(" OFFSET :start");
+ parameters.put("start", dashboardQuery.start);
+ }
+ if(dashboardQuery.limit != null) {
+ sql.append(" LIMIT :limit");
+ parameters.put("limit", dashboardQuery.limit);
+ }
+
+ // Create query object and apply parameters
+ Query query = em.createNativeQuery(sql.toString(), Dashboard.class);
+ parameters.forEach(query::setParameter);
+
+ return query.getResultList().toArray(new Dashboard[0]);
+ });
+ }
+
+ /**
+ * Builds SQL section for filtering dashboards by ID,
+ * and applies the necessary data to the sqlBuilder and sqlParams parameters.
+ * @return {@link StringBuilder} used for building
+ */
+ protected StringBuilder appendSqlIdFilter(StringBuilder sqlBuilder, DashboardQuery query, Map<String, Object> sqlParams) {
+ sqlBuilder.append(" AND id IN (:ids)");
+ sqlParams.put("ids", List.of(query.ids));
+ return sqlBuilder;
+ }
+
+ /**
+ * Builds SQL section for filtering dashboards by display name using {@link StringPredicate},
+ * and applies the necessary data to the sqlBuilder and sqlParams parameters.
+ * @return {@link StringBuilder} used for building
+ */
+ protected StringBuilder appendSqlNamesFilter(StringBuilder sqlBuilder, DashboardQuery query, Map<String, Object> sqlParams) {
+ IntStream.range(0, query.names.length).forEach(index -> {
+ String key = "name" + index;
+ StringPredicate pred = query.names[index];
+ sqlBuilder.append(" AND ").append(pred.caseSensitive ? "display_name" : "UPPER(display_name)").append(pred.negate ? " NOT" : "");
+ switch (pred.match) {
+ case BEGIN -> sqlBuilder.append(" LIKE :").append(key).append(" || '%'");
+ case CONTAINS -> sqlBuilder.append(" LIKE '%' || :").append(key).append(" || '%'");
+ case END -> sqlBuilder.append(" LIKE '%' || :").append(key);
+ default -> sqlBuilder.append(" = :").append(key);
+ }
+ sqlParams.put(key, pred.value);
+ });
+ return sqlBuilder;
+ }
+
+ protected StringBuilder appendSqlUserIdsFilter(StringBuilder sqlBuilder, DashboardQuery query, Map<String, Object> sqlParams) {
+ sqlBuilder.append(" AND owner_id IN (:ownerIds)");
+ sqlParams.put("ownerIds", List.of(query.userIds));
+ return sqlBuilder;
+ }
+
+ /**
+ * Builds SQL section for filtering dashboards by dashboard conditions, such as {@link DashboardAccess} (PRIVATE, SHARED, PUBLIC),
+ * and applies the necessary data to the sqlBuilder and sqlParams parameters.
+ * @return {@link StringBuilder} used for building
+ */
+ protected StringBuilder appendSqlDashboardConditionsFilter(StringBuilder sqlBuilder, DashboardQuery query, Map<String, Object> sqlParams, String userId) {
+ var dashboardConditions = query.conditions.getDashboard();
+ if(dashboardConditions.getViewAccess() != null || dashboardConditions.getEditAccess() != null) {
+
+ List<DashboardAccess> viewAccess = new ArrayList<>(Arrays.asList(dashboardConditions.getViewAccess()));
+ List<DashboardAccess> editAccess = new ArrayList<>(Arrays.asList(dashboardConditions.getEditAccess()));
+ sqlBuilder.append(" AND (");
+
+ // When requesting PRIVATE dashboards, check whether the user is also the owner
+ if(userId != null && viewAccess.contains(DashboardAccess.PRIVATE)) {
+ viewAccess.remove(DashboardAccess.PRIVATE);
+ sqlBuilder.append("(view_access = 2 AND owner_id = :userId) OR ");
+ sqlParams.put("userId", userId);
+ }
+ if(userId != null && editAccess.contains(DashboardAccess.PRIVATE)) {
+ editAccess.remove(DashboardAccess.PRIVATE);
+ sqlBuilder.append("(edit_access = 2 AND owner_id = :userId) OR ");
+ sqlParams.put("userId", userId);
+ }
+
+ // Append simple SQL filter by dashboard access
+ sqlBuilder.append("(view_access IN (:viewAccess) OR edit_access IN (:editAccess)))");
+
+ sqlParams.put("viewAccess", viewAccess.stream().map(DashboardAccess::ordinal).collect(Collectors.toList()));
+ sqlParams.put("editAccess", editAccess.stream().map(DashboardAccess::ordinal).collect(Collectors.toList()));
+
+ /** TODO: Implement filtering by {@link org.openremote.model.query.DashboardQuery.DashboardConditions.minWidgets} */
- // Querying dashboards from the database
- // userId is required for checking dashboard ownership. If userId is NULL, we assume the user is not logged in.
- // editable can be used to only return dashboards where the user has edit access.
- public Dashboard[] query(List<String> dashboardIds, String realm, String userId, Boolean publicOnly, Boolean editable) {
- if(realm == null) {
- throw new IllegalArgumentException("No realm is specified.");
}
- return persistenceService.doReturningTransaction(em -> {
- try {
- CriteriaBuilder cb = em.getCriteriaBuilder();
- CriteriaQuery<Dashboard> cq = cb.createQuery(Dashboard.class);
- Root<Dashboard> root = cq.from(Dashboard.class);
+ return sqlBuilder;
+ }
- List<Predicate> predicates = new ArrayList<>();
- predicates.add(cb.like(root.get("realm"), realm));
+ /**
+ * Builds SQL section for filtering dashboards by assets present in the widgets.
+ * For example, to only query dashboards when the user has access to the assets used.
+ * Using {@link org.openremote.model.query.DashboardQuery.AssetAccess},
+ * it applies the necessary data to the sqlBuilder and sqlParams parameters.
+ * @return {@link StringBuilder} used for building
+ */
+ protected StringBuilder appendSqlAssetConditionsFilter(StringBuilder sqlBuilder, DashboardQuery query, Map<String, Object> sqlParams, String userId) {
+ var assetConditions = query.conditions.getAsset();
+ if(assetConditions.access != null) {
+ List<DashboardQuery.AssetAccess> levels = Arrays.asList(assetConditions.access);
+ if (levels.size() == 1 && levels.contains(DashboardQuery.AssetAccess.RESTRICTED)) {
- if(dashboardIds != null) {
- predicates.add(root.get("id").in(dashboardIds));
- }
- // Apply EDIT ACCESS filters; always return PUBLIC dashboards, SHARED dashboards if access to the realm,
- // and PRIVATE if you are the creator (ownerId) of the dashboard.
- if(Boolean.TRUE.equals(editable)) {
- if(publicOnly) {
- predicates.add(cb.equal(root.get("editAccess"), DashboardAccess.PUBLIC));
- } else {
- predicates.add(cb.or(
- root.get("editAccess").in(DashboardAccess.PUBLIC, (userId != null ? DashboardAccess.SHARED : null)),
- cb.and(root.get("editAccess").in(DashboardAccess.PRIVATE), root.get("ownerId").in(userId))
- ));
- }
+ // Gather asset ids the user is linked to
+ List<UserAssetLink> userAssetLinks = assetStorageService.findUserAssetLinks(query.realm.name, userId, null);
+ List<String> assetIds = userAssetLinks.stream().map(ua -> ua.getId().getAssetId()).collect(Collectors.toList());
+
+ // AT_LEAST_ONE - When user has access to the assets of at least 1 widget
+ if(assetConditions.minAmount == DashboardQuery.ConditionMinAmount.AT_LEAST_ONE) {
+ sqlBuilder.append(" AND (template IS NULL OR template->'widgets' IS NULL OR EXISTS (");
+ sqlBuilder.append("SELECT 1 FROM jsonb_array_elements(COALESCE(template->'widgets', '[]')) AS j(widget) ");
+ sqlBuilder.append("LEFT JOIN jsonb_array_elements(COALESCE(widget->'widgetConfig'->'attributeRefs', '[]')) AS a(attributeRef) ");
+ sqlBuilder.append("ON a->>'id' IN (:assetIds)))");
}
- // Apply VIEW ACCESS filters; always return PUBLIC dashboards, SHARED dashboards if access to the realm,
- // and PRIVATE if you are the creator (ownerId) of the dashboard.
- if(publicOnly) {
- predicates.add(cb.equal(root.get("viewAccess"), DashboardAccess.PUBLIC));
- } else {
- predicates.add(cb.or(
- root.get("viewAccess").in(DashboardAccess.PUBLIC, (userId != null ? DashboardAccess.SHARED : null)),
- cb.and(root.get("viewAccess").in(DashboardAccess.PRIVATE), root.get("ownerId").in(userId))
- ));
+ // ALL - User needs access to the assets of ALL widgets (or the dashboard has no widgets)
+ else if(assetConditions.minAmount == DashboardQuery.ConditionMinAmount.ALL) {
+ sqlBuilder.append(" AND NOT EXISTS (");
+ sqlBuilder.append("SELECT 1 FROM jsonb_array_elements(template->'widgets') AS j(widget), ");
+ sqlBuilder.append("jsonb_array_elements(widget->'widgetConfig'->'attributeRefs') AS a(attributeRef) ");
+ sqlBuilder.append("WHERE a->>'id' NOT IN (:assetIds))");
}
+ sqlParams.put("assetIds", assetIds);
+ }
+ }
- CriteriaQuery<Dashboard> all = cq.select(root).where(predicates.toArray(new Predicate[]{}));
- return em.createQuery(all).getResultList().toArray(new Dashboard[0]);
+ /** TODO: Implement filtering by ({@link org.openremote.model.query.DashboardQuery.AssetConditions.parents} */
- } catch (Exception e) {
- e.printStackTrace();
- }
- return new Dashboard[0]; // Empty array if nothing found.
- });
+ return sqlBuilder;
}
// Method to check if a dashboardId actually exists in the database
@@ -122,15 +256,11 @@ public class DashboardStorageService extends RouteBuilder implements ContainerSe
if(realm == null) {
throw new IllegalArgumentException("No realm is specified.");
}
- return persistenceService.doReturningTransaction(em -> {
- CriteriaBuilder cb = em.getCriteriaBuilder();
- CriteriaQuery<Dashboard> cq = cb.createQuery(Dashboard.class);
- Root<Dashboard> root = cq.from(Dashboard.class);
- return em.createQuery(cq.select(root)
- .where(cb.like(root.get("realm"), realm))
- .where(cb.like(root.get("id"), dashboardId))
- );
- }) != null;
+ return this.query(new DashboardQuery()
+ .realm(new RealmPredicate(realm))
+ .ids(dashboardId),
+ null
+ ).length > 0;
}
@@ -161,7 +291,18 @@ public class DashboardStorageService extends RouteBuilder implements ContainerSe
if(userId == null) {
throw new IllegalArgumentException("No userId is specified.");
}
- Dashboard[] dashboards = this.query(Collections.singletonList(dashboard.getId()), realm, userId, false, true); // Get dashboards that userId is able to EDIT.
+ // Query the dashboards with the same ID (which is only 1), and that userId is able to EDIT
+ var query = new DashboardQuery()
+ .ids(dashboard.getId())
+ .realm(new RealmPredicate(realm))
+ .userIds(userId)
+ .limit(1)
+ .conditions(new DashboardQuery.Conditions().dashboard(
+ new DashboardQuery.DashboardConditions()
+ .viewAccess(new DashboardAccess[]{})
+ .editAccess(new DashboardAccess[]{DashboardAccess.SHARED, DashboardAccess.PRIVATE})
+ ));
+ Dashboard[] dashboards = this.query(query, userId);
if(dashboards != null && dashboards.length > 0) {
Dashboard d = dashboards[0];
return persistenceService.doReturningTransaction(em -> {
@@ -186,12 +327,22 @@ public class DashboardStorageService extends RouteBuilder implements ContainerSe
return persistenceService.doReturningTransaction(em -> {
// Query the dashboards with the same ID (which is only 1), and that userId is able to EDIT
- Dashboard[] dashboards = this.query(Collections.singletonList(dashboardId), realm, userId, false, true);
+ Dashboard[] dashboards = this.query(new DashboardQuery()
+ .ids(dashboardId)
+ .realm(new RealmPredicate(realm))
+ .userIds(userId)
+ .limit(1)
+ .conditions(new DashboardQuery.Conditions().dashboard(
+ new DashboardQuery.DashboardConditions().editAccess(new DashboardAccess[]{ DashboardAccess.SHARED, DashboardAccess.PRIVATE })
+ )),
+ userId
+ );
if(dashboards == null || dashboards.length == 0) {
throw new IllegalArgumentException("No dashboards could be found.");
}
- Query query = em.createQuery("DELETE from Dashboard d where d.id=?1");
+ Query query = em.createQuery("DELETE from Dashboard d where d.id=?1 and d.realm =?2");
query.setParameter(1, dashboardId);
+ query.setParameter(2, realm);
query.executeUpdate();
return true;
});
diff --git a/model/src/main/java/org/openremote/model/dashboard/DashboardGridItem.java b/model/src/main/java/org/openremote/model/dashboard/DashboardGridItem.java
index 34ad56b1c..cb98aadcb 100644
--- a/model/src/main/java/org/openremote/model/dashboard/DashboardGridItem.java
+++ b/model/src/main/java/org/openremote/model/dashboard/DashboardGridItem.java
@@ -22,4 +22,21 @@ public class DashboardGridItem {
protected boolean noResize;
protected boolean noMove;
protected boolean locked;
+
+ public DashboardGridItem setX(int x) {
+ this.x = x;
+ return this;
+ }
+ public DashboardGridItem setY(int y) {
+ this.y = y;
+ return this;
+ }
+ public DashboardGridItem setW(int w) {
+ this.w = w;
+ return this;
+ }
+ public DashboardGridItem setH(int h) {
+ this.h = h;
+ return this;
+ }
}
diff --git a/model/src/main/java/org/openremote/model/dashboard/DashboardResource.java b/model/src/main/java/org/openremote/model/dashboard/DashboardResource.java
index 565cc1ea6..f4d877192 100644
--- a/model/src/main/java/org/openremote/model/dashboard/DashboardResource.java
+++ b/model/src/main/java/org/openremote/model/dashboard/DashboardResource.java
@@ -1,14 +1,12 @@
package org.openremote.model.dashboard;
import io.swagger.v3.oas.annotations.tags.Tag;
-import org.openremote.model.Constants;
-import org.openremote.model.http.RequestParams;
-
import jakarta.annotation.security.RolesAllowed;
import jakarta.validation.Valid;
import jakarta.ws.rs.*;
-
-import java.util.List;
+import org.openremote.model.Constants;
+import org.openremote.model.http.RequestParams;
+import org.openremote.model.query.DashboardQuery;
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;
@@ -16,16 +14,37 @@ import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;
@Path("dashboard")
public interface DashboardResource {
+ /**
+ * Retrieve all dashboards from a realm, where the user has access to.
+ * @return An array of {@link Dashboard} from the realm
+ */
@GET
@Path("all/{realm}")
@Produces(APPLICATION_JSON)
Dashboard[] getAllRealmDashboards(@BeanParam RequestParams requestParams, @PathParam("realm") String realm);
+ /**
+ * Queries a specific {@link Dashboard} by its ID and realm, if a user has access to it.
+ * @return {@link Dashboard}
+ */
@GET
@Path("{realm}/{dashboardId}")
@Produces(APPLICATION_JSON)
Dashboard get(@BeanParam RequestParams requestParams, @PathParam("realm") String realm, @PathParam("dashboardId") String dashboardId);
+ /**
+ * Advanced query endpoint for retrieving {@link Dashboard} from the database.
+ * Based on the {@link DashboardQuery} given, it will specifically filter the entries that are returned.
+ * For example filtering by displayName, ID, or dashboard access.
+ *
+ * @return An array of {@link Dashboard}
+ */
+ @POST
+ @Path("query")
+ @Consumes(APPLICATION_JSON)
+ @Produces(APPLICATION_JSON)
+ Dashboard[] query(@BeanParam RequestParams requestParams, @Valid DashboardQuery dashboardQuery);
+
@POST
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
diff --git a/model/src/main/java/org/openremote/model/dashboard/DashboardTemplate.java b/model/src/main/java/org/openremote/model/dashboard/DashboardTemplate.java
index 47cedb2ea..711dbaf76 100644
--- a/model/src/main/java/org/openremote/model/dashboard/DashboardTemplate.java
+++ b/model/src/main/java/org/openremote/model/dashboard/DashboardTemplate.java
@@ -32,6 +32,7 @@ public class DashboardTemplate {
public DashboardTemplate() {
}
+
public DashboardTemplate(DashboardScreenPreset[] screenPresets) {
this.columns = 1;
this.maxScreenWidth = 1;
@@ -39,17 +40,57 @@ public class DashboardTemplate {
this.screenPresets = screenPresets;
}
- public void setId(String id) { this.id = id; }
- public void setColumns(int columns) { this.columns = columns; }
- public void setMaxScreenWidth(int maxScreenWidth) { this.maxScreenWidth = maxScreenWidth; }
- public void setRefreshInterval(DashboardRefreshInterval interval) { this.refreshInterval = interval; }
- public void setScreenPresets(DashboardScreenPreset[] screenPresets) { this.screenPresets = screenPresets; }
- public void setWidgets(DashboardWidget[] widgets) { this.widgets = widgets; }
+ public DashboardTemplate setId(String id) {
+ this.id = id;
+ return this;
+ }
+
+ public DashboardTemplate setColumns(int columns) {
+ this.columns = columns;
+ return this;
+ }
+
+ public DashboardTemplate setMaxScreenWidth(int maxScreenWidth) {
+ this.maxScreenWidth = maxScreenWidth;
+ return this;
+ }
+
+ public DashboardTemplate setRefreshInterval(DashboardRefreshInterval interval) {
+ this.refreshInterval = interval;
+ return this;
+ }
+
+ public DashboardTemplate setScreenPresets(DashboardScreenPreset[] screenPresets) {
+ this.screenPresets = screenPresets;
+ return this;
+ }
+
+ public DashboardTemplate setWidgets(DashboardWidget[] widgets) {
+ this.widgets = widgets;
+ return this;
+ }
+
+ public String getId() {
+ return id;
+ }
+
+ public int getColumns() {
+ return columns;
+ }
+
+ public int getMaxScreenWidth() {
+ return maxScreenWidth;
+ }
+
+ public DashboardRefreshInterval getRefreshInterval() {
+ return this.refreshInterval;
+ }
- public String getId() { return id; }
- public int getColumns() { return columns; }
- public int getMaxScreenWidth() { return maxScreenWidth; }
- public DashboardRefreshInterval getRefreshInterval() { return this.refreshInterval; }
- public DashboardScreenPreset[] getScreenPresets() { return screenPresets; }
- public DashboardWidget[] getWidgets() { return widgets; }
+ public DashboardScreenPreset[] getScreenPresets() {
+ return screenPresets;
+ }
+
+ public DashboardWidget[] getWidgets() {
+ return widgets;
+ }
}
diff --git a/model/src/main/java/org/openremote/model/dashboard/DashboardWidget.java b/model/src/main/java/org/openremote/model/dashboard/DashboardWidget.java
index 050066bb9..5e83a5295 100644
--- a/model/src/main/java/org/openremote/model/dashboard/DashboardWidget.java
+++ b/model/src/main/java/org/openremote/model/dashboard/DashboardWidget.java
@@ -25,15 +25,48 @@ public class DashboardWidget {
/* ------------------------------ */
- public void setId(@NotNull @NotEmpty String id) { this.id = id; }
- public void setDisplayName(@NotNull String displayName) { this.displayName = displayName; }
- public void setGridItem(@NotNull DashboardGridItem gridItem) { this.gridItem = gridItem; }
- public void setWidgetTypeId(@NotNull String widgetTypeId) { this.widgetTypeId = widgetTypeId; }
- public void setWidgetConfig(Object widgetConfig) { this.widgetConfig = widgetConfig; }
-
- public String getId() { return id; }
- public String getDisplayName() { return displayName; }
- public DashboardGridItem getGridItem() { return gridItem; }
- public String getWidgetTypeId() { return widgetTypeId; }
- public Object getWidgetConfig() { return widgetConfig; }
+ public DashboardWidget setId(@NotNull @NotEmpty String id) {
+ this.id = id;
+ return this;
+ }
+
+ public DashboardWidget setDisplayName(@NotNull String displayName) {
+ this.displayName = displayName;
+ return this;
+ }
+
+ public DashboardWidget setGridItem(@NotNull DashboardGridItem gridItem) {
+ this.gridItem = gridItem;
+ return this;
+ }
+
+ public DashboardWidget setWidgetTypeId(@NotNull String widgetTypeId) {
+ this.widgetTypeId = widgetTypeId;
+ return this;
+ }
+
+ public DashboardWidget setWidgetConfig(Object widgetConfig) {
+ this.widgetConfig = widgetConfig;
+ return this;
+ }
+
+ public String getId() {
+ return id;
+ }
+
+ public String getDisplayName() {
+ return displayName;
+ }
+
+ public DashboardGridItem getGridItem() {
+ return gridItem;
+ }
+
+ public String getWidgetTypeId() {
+ return widgetTypeId;
+ }
+
+ public Object getWidgetConfig() {
+ return widgetConfig;
+ }
}
diff --git a/model/src/main/java/org/openremote/model/query/DashboardQuery.java b/model/src/main/java/org/openremote/model/query/DashboardQuery.java
new file mode 100644
index 000000000..64d7705b7
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/query/DashboardQuery.java
@@ -0,0 +1,284 @@
+/*
+ * Copyright 2024, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.model.query;
+
+import org.openremote.model.dashboard.DashboardAccess;
+import org.openremote.model.query.filter.ParentPredicate;
+import org.openremote.model.query.filter.RealmPredicate;
+import org.openremote.model.query.filter.StringPredicate;
+
+import java.io.Serializable;
+import java.util.Arrays;
+
+public class DashboardQuery implements Serializable {
+
+ /**
+ * A top-level object of {@link DashboardQuery}, that is meant for filtering its output.
+ * For example used to only return metadata of dashboards, instead of ALL data.
+ */
+ public static class Select {
+ protected boolean metadata;
+ protected boolean template;
+
+ public Select() {
+ this.metadata = true;
+ this.template = true;
+ }
+
+ public Select(boolean excludeMetadata, boolean excludeTemplate) {
+ this.metadata = !excludeMetadata;
+ this.template = !excludeTemplate;
+ }
+
+ public Select excludeMetadata(boolean excludeMetadata) {
+ this.metadata = !excludeMetadata;
+ return this;
+ }
+
+ public Select excludeTemplate(boolean excludeTemplate) {
+ this.template = !excludeTemplate;
+ return this;
+ }
+
+ public boolean hasMetadata() {
+ return metadata;
+ }
+
+ public boolean hasTemplateData() {
+ return template;
+ }
+ }
+
+ /**
+ *
+ */
+ public static class DashboardConditions {
+ protected DashboardAccess[] viewAccess;
+ protected DashboardAccess[] editAccess;
+ protected Integer minWidgets;
+
+ public DashboardConditions() {
+ this.viewAccess = new DashboardAccess[]{DashboardAccess.PUBLIC, DashboardAccess.SHARED, DashboardAccess.PRIVATE};
+ this.editAccess = new DashboardAccess[]{DashboardAccess.PUBLIC, DashboardAccess.SHARED, DashboardAccess.PRIVATE};
+ }
+
+ public DashboardConditions(DashboardAccess[] viewAccess, DashboardAccess[] editAccess, Integer minWidgets) {
+ this.viewAccess = viewAccess;
+ this.editAccess = editAccess;
+ this.minWidgets = minWidgets;
+ }
+
+ public DashboardConditions viewAccess(DashboardAccess[] access) {
+ this.viewAccess = access;
+ return this;
+ }
+
+ public DashboardConditions editAccess(DashboardAccess[] editAccess) {
+ this.editAccess = editAccess;
+ return this;
+ }
+
+ public DashboardConditions minWidgets(Integer minWidgets) {
+ this.minWidgets = minWidgets;
+ return this;
+ }
+
+ public DashboardAccess[] getViewAccess() {
+ return viewAccess;
+ }
+
+ public DashboardAccess[] getEditAccess() {
+ return editAccess;
+ }
+
+ public Integer getMinWidgets() {
+ return minWidgets;
+ }
+ }
+
+ /**
+ *
+ */
+ public enum AssetAccess {
+ RESTRICTED, LINKED, REALM
+ }
+
+ /**
+ *
+ */
+ public enum ConditionMinAmount {
+ AT_LEAST_ONE, ALL, NONE
+ }
+
+ /**
+ *
+ */
+ public static class AssetConditions {
+ public AssetAccess[] access;
+ public ConditionMinAmount minAmount;
+ public ParentPredicate[] parents;
+
+ public AssetConditions() {
+ this.access = new AssetAccess[]{AssetAccess.REALM, AssetAccess.LINKED};
+ this.minAmount = ConditionMinAmount.ALL;
+ this.parents = new ParentPredicate[]{};
+ }
+
+ public AssetConditions(AssetAccess[] access, ConditionMinAmount minAmount, ParentPredicate[] parents) {
+ this.access = access;
+ this.minAmount = minAmount;
+ this.parents = parents;
+ }
+
+ public AssetConditions access(AssetAccess... access) {
+ this.access = access;
+ return this;
+ }
+
+ public AssetConditions minAmount(ConditionMinAmount minAmount) {
+ this.minAmount = minAmount;
+ return this;
+ }
+
+ public AssetConditions parents(ParentPredicate... parents) {
+ this.parents = parents;
+ return this;
+ }
+
+ public AssetAccess[] getAccess() {
+ return access;
+ }
+
+ public ConditionMinAmount getMinAmount() {
+ return minAmount;
+ }
+
+ public ParentPredicate[] getParents() {
+ return parents;
+ }
+ }
+
+ public static class Conditions {
+ protected DashboardConditions dashboard;
+ protected AssetConditions asset;
+
+ public Conditions() {
+ this(new DashboardConditions(), new AssetConditions());
+ }
+ public Conditions(DashboardConditions dashboard) {
+ this(dashboard, null);
+ }
+ public Conditions(AssetConditions asset) {
+ this(null, asset);
+ }
+ public Conditions(DashboardConditions dashboard, AssetConditions asset) {
+ this.dashboard = dashboard;
+ this.asset = asset;
+ }
+
+ public Conditions dashboard(DashboardConditions dashboard) {
+ this.dashboard = dashboard;
+ return this;
+ }
+
+ public Conditions asset(AssetConditions asset) {
+ this.asset = asset;
+ return this;
+ }
+
+ public DashboardConditions getDashboard() {
+ return dashboard;
+ }
+
+ public AssetConditions getAsset() {
+ return asset;
+ }
+ }
+
+ // Response mapping
+ public Select select;
+
+ // Filtering predicates
+ public Conditions conditions;
+ public String[] ids;
+ public StringPredicate[] names;
+ public String[] userIds;
+ public RealmPredicate realm;
+
+ // Pagination fields
+ public Integer start;
+ public Integer limit;
+
+ public DashboardQuery() {
+ this.select = new Select();
+ this.conditions = new Conditions();
+ this.start = 0;
+ this.limit = 50;
+ }
+
+ public DashboardQuery select(Select select) {
+ this.select = select;
+ return this;
+ }
+
+ public DashboardQuery conditions(Conditions conditions) {
+ this.conditions = conditions;
+ return this;
+ }
+
+ public DashboardQuery ids(String... ids) {
+ this.ids = ids;
+ return this;
+ }
+
+ public DashboardQuery names(String... names) {
+ if (names == null || names.length == 0) {
+ this.names = null;
+ return this;
+ }
+ this.names = Arrays.stream(names).map(StringPredicate::new).toArray(StringPredicate[]::new);
+ return this;
+ }
+
+ public DashboardQuery names(StringPredicate... names) {
+ this.names = names;
+ return this;
+ }
+
+ public DashboardQuery userIds(String... ids) {
+ this.userIds = ids;
+ return this;
+ }
+
+ public DashboardQuery realm(RealmPredicate realm) {
+ this.realm = realm;
+ return this;
+ }
+
+ public DashboardQuery start(Integer start) {
+ this.start = start;
+ return this;
+ }
+
+ public DashboardQuery limit(Integer limit) {
+ this.limit = limit;
+ return this;
+ }
+}
diff --git a/test/src/test/groovy/org/openremote/test/dashboard/DashboardTest.groovy b/test/src/test/groovy/org/openremote/test/dashboard/DashboardTest.groovy
index 0cba892ba..22efe81ac 100644
--- a/test/src/test/groovy/org/openremote/test/dashboard/DashboardTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/dashboard/DashboardTest.groovy
@@ -2,8 +2,17 @@ package org.openremote.test.dashboard
import jakarta.ws.rs.ForbiddenException
import jakarta.ws.rs.NotFoundException
+import org.openremote.manager.asset.AssetStorageService
import org.openremote.manager.setup.SetupService
+import org.openremote.model.asset.AssetResource
+import org.openremote.model.asset.UserAssetLink
+import org.openremote.model.asset.impl.LightAsset
+import org.openremote.model.attribute.MetaItem
import org.openremote.model.dashboard.*
+import org.openremote.model.query.DashboardQuery
+import org.openremote.model.query.filter.RealmPredicate
+import org.openremote.model.util.ValueUtil
+import org.openremote.model.value.MetaItemType
import org.openremote.setup.integration.KeycloakTestSetup
import org.openremote.test.ManagerContainerTrait
import spock.lang.Specification
@@ -42,6 +51,7 @@ class DashboardTest extends Specification implements ManagerContainerTrait {
def adminUserDashboardResource = getClientApiTarget(serverUri, MASTER_REALM, adminUserAccessToken).proxy(DashboardResource.class)
def privateUser1DashboardResource = getClientApiTarget(serverUri, MASTER_REALM, privateUser1AccessToken).proxy(DashboardResource.class)
def publicUserDashboardResource = getClientApiTarget(serverUri, MASTER_REALM).proxy(DashboardResource.class)
+ def adminUserAssetResource = getClientApiTarget(serverUri, MASTER_REALM, adminUserAccessToken).proxy(AssetResource.class)
/* ----------------------- */
@@ -66,6 +76,18 @@ class DashboardTest extends Specification implements ManagerContainerTrait {
/* ----------------------- */
+ // Test to verify user can query by displayName
+
+ when: "admin user tries to query dashboard by display name"
+ def displayNameQuery = new DashboardQuery().names("dashboard1")
+ def displayNameDashboards = adminUserDashboardResource.query(null, displayNameQuery)
+
+ then: "the correct dashboard should be returned"
+ assert displayNameDashboards.length == 1
+ assert displayNameDashboards[0].displayName == "dashboard1"
+
+ /* ----------------------- */
+
// Test to verify "EDIT ACCESS" logic
when: "dashboard1 is made 'edit private' by its original owner"
@@ -123,9 +145,104 @@ class DashboardTest extends Specification implements ManagerContainerTrait {
// Test to verify private dashboards cannot be fetched by public/unauthenticated users, even if ID is specified
when: "when a public user specifically fetches a private dashboard"
- publicUserDashboardResource.get(null, MASTER_REALM, adminDashboard.id);
+ publicUserDashboardResource.get(null, MASTER_REALM, adminDashboard.id)
then: "it should throw exception"
thrown ForbiddenException
+
+ /* ------------------------- */
+
+ // Test to check whether dashboards are queried when no assets are accessible
+
+ when: "a new asset is created in the building realm"
+ def lightAsset = new LightAsset("admin light").setRealm(keycloakTestSetup.realmBuilding.name)
+ lightAsset.getAttribute("brightness").ifPresent {
+ it -> it.addMeta(new MetaItem<>(MetaItemType.ACCESS_RESTRICTED_READ))
+ }
+ def adminLightAsset = adminUserAssetResource.create(null, lightAsset)
+
+ and: "a dashboard is created in the building realm using that asset"
+ def gridItem = new DashboardGridItem().setX(0).setY(0).setW(1).setH(1)
+ def widgetConfigStr = ('{"attributeRefs":[{"id":"assetId","name":"brightness"}]}').replace("assetId", adminLightAsset.id)
+ def widget = new DashboardWidget()
+ .setDisplayName("Light widget")
+ .setWidgetTypeId("test")
+ .setGridItem(gridItem)
+ .setWidgetConfig(ValueUtil.parse(widgetConfigStr, Object.class).orElse(null))
+ def template = new DashboardTemplate(screenPresets)
+ .setWidgets(new DashboardWidget[]{ widget })
+ def buildingDashboard = new Dashboard()
+ .setRealm(keycloakTestSetup.realmBuilding.name)
+ .setDisplayName("Dashboard with assets")
+ .setTemplate(template)
+
+ def newDashboard = adminUserDashboardResource.create(null, buildingDashboard)
+
+ and: "a restricted user authenticates for the building realm"
+ def restrictedUser1AccessToken = authenticate(
+ container,
+ keycloakTestSetup.realmBuilding.name,
+ KEYCLOAK_CLIENT_ID,
+ "testuser3",
+ "testuser3"
+ ).token
+ def restrictedUserDashboardResource = getClientApiTarget(serverUri, keycloakTestSetup.realmBuilding.name, restrictedUser1AccessToken).proxy(DashboardResource.class)
+
+ and: "a restricted user tries to query a dashboard without access to the assets shown"
+ def restrictedDashboards = restrictedUserDashboardResource.getAllRealmDashboards(null, keycloakTestSetup.realmBuilding.name)
+
+ then: "it should only return that dashboard"
+ assert restrictedDashboards.length == 0
+
+ when: "the user gets access to the asset"
+ def assetStorageService = container.getService(AssetStorageService.class)
+ assetStorageService.storeUserAssetLinks(Arrays.asList(
+ new UserAssetLink(keycloakTestSetup.realmBuilding.getName(), keycloakTestSetup.testuser3Id, adminLightAsset.getId())
+ ))
+
+ then: "the user is able to request the dashboard"
+ def restrictedDashboards2 = restrictedUserDashboardResource.getAllRealmDashboards(null, keycloakTestSetup.realmBuilding.name)
+ assert restrictedDashboards2.length == 1
+ assert restrictedDashboards2[0].displayName == "Dashboard with assets"
+
+
+ /* ------------------------- */
+
+ // Test for cross realm requests
+ when: "the admin superuser in master realm tries requesting a dashboard in the building realm"
+ def dashboardOtherRealmQuery = new DashboardQuery().realm(new RealmPredicate(keycloakTestSetup.realmBuilding.name))
+ def dashboardsInOtherRealm = adminUserDashboardResource.query(null, dashboardOtherRealmQuery)
+
+ then: "it returns the building realm dashboard"
+ assert dashboardsInOtherRealm.length == 1
+ assert dashboardsInOtherRealm[0].displayName == "Dashboard with assets"
+
+ when: "the private user in master realm tries requesting a dashboard in the building realm"
+ def sameDashboardOtherRealmQuery = new DashboardQuery().realm(new RealmPredicate(keycloakTestSetup.realmBuilding.name))
+ def sameDashboardsInOtherRealm = privateUser1DashboardResource.query(null, sameDashboardOtherRealmQuery)
+
+ then: "it should return 403 forbidden"
+ thrown ForbiddenException
+
+
+
+ /* ------------------------- */
+
+ // Test to delete the dashboard
+
+ when: "the admin user deletes the new dashboard"
+ adminUserDashboardResource.delete(null, keycloakTestSetup.realmBuilding.getName(), newDashboard.getId())
+
+ and: "tries to get the dashboard again"
+ adminUserDashboardResource.get(null, keycloakTestSetup.realmBuilding.getName(), newDashboard.getId())
+
+ then: "it cannot be found, since it got deleted"
+ thrown NotFoundException
+
+ when: "the restricted user tries to fetch the deleted dashboard"
+ restrictedUserDashboardResource.get(null, keycloakTestSetup.realmBuilding.getName(), newDashboard.getId())
+
+ then: "it cannot be found as well, since it got deleted"
+ thrown NotFoundException
}
}
diff --git a/ui/app/manager/src/pages/page-insights.ts b/ui/app/manager/src/pages/page-insights.ts
index 2790b253e..81bdcdd5b 100644
--- a/ui/app/manager/src/pages/page-insights.ts
+++ b/ui/app/manager/src/pages/page-insights.ts
@@ -122,7 +122,7 @@ export class PageInsights extends Page<AppStateKeyed> {
stateChanged(state: AppStateKeyed) {
// State is only utilised for initial loading
this.getRealmState(state); // Order is important here!
- this._editMode = (state.app.params && state.app.params.editMode) ? (state.app.params.editMode == "true") : false;
+ this._editMode = (state.app.params && state.app.params.editMode) ? (state.app.params.editMode === "true" && manager.hasRole(ClientRole.WRITE_INSIGHTS)) : false;
this._dashboardId = (state.app.params && state.app.params.id) ? state.app.params.id : undefined;
}