Skip to content

armgit / core

Public Imported from GitHub
0

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

UI updates: Assset tree updated to work for restricted user (Closes #539, Closes #540)

Rich Turner65beb2c8f39e
diff --git a/ui/app/manager/package.json b/ui/app/manager/package.json
index 762e51945..633b62681 100644
--- a/ui/app/manager/package.json
+++ b/ui/app/manager/package.json
@@ -11,7 +11,7 @@
"modelWatch": "npx orutil watch",
"build": "npx tsc -b -f && npx cross-env NODE_OPTIONS=--max_old_space_size=4096 webpack --mode production",
"serve": "npx cross-env NODE_OPTIONS=--max_old_space_size=4096 webpack serve --mode development",
- "watch": "npx tsc -b -w -f",
+ "watch": "npx tsc -b -w",
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
diff --git a/ui/app/manager/src/pages/page-map.ts b/ui/app/manager/src/pages/page-map.ts
index 7ded01eb7..edf0f7590 100644
--- a/ui/app/manager/src/pages/page-map.ts
+++ b/ui/app/manager/src/pages/page-map.ts
@@ -1,6 +1,6 @@
import {css, html} from "lit";
import {customElement, property, query} from "lit/decorators.js";
-import {Action, createSlice, EnhancedStore, PayloadAction, ThunkAction} from "@reduxjs/toolkit";
+import {createSlice, EnhancedStore, PayloadAction} from "@reduxjs/toolkit";
import "@openremote/or-map";
import {
MapAssetCardConfig,
@@ -10,12 +10,20 @@ import {
OrMapMarkerAsset,
OrMapMarkerClickedEvent
} from "@openremote/or-map";
-import manager, {OREvent, Util} from "@openremote/core";
+import manager, {Util} from "@openremote/core";
import {createSelector} from "reselect";
-import {Asset, AssetEvent, AssetEventCause, AttributeEvent, GeoJSONPoint, Attribute, WellknownMetaItems, WellknownAttributes, LogicGroupOperator} from "@openremote/model";
+import {
+ Asset,
+ AssetEvent,
+ AssetEventCause,
+ Attribute,
+ AttributeEvent,
+ GeoJSONPoint,
+ WellknownAttributes,
+ WellknownMetaItems
+} from "@openremote/model";
import {getAssetsRoute} from "./page-assets";
-import {Page, PageProvider, router} from "@openremote/or-app";
-import {AppStateKeyed} from "@openremote/or-app";
+import {AppStateKeyed, Page, PageProvider, router} from "@openremote/or-app";
export interface MapState {
assets: Asset[];
@@ -307,6 +315,9 @@ export class PageMap<S extends MapStateKeyed> extends Page<S> {
<or-map id="map" class="or-map">
${
this._assets.filter((asset) => {
+ if (!asset.attributes) {
+ return false;
+ }
const attr = asset.attributes[WellknownAttributes.LOCATION] as Attribute<GeoJSONPoint>;
const showOnMap = !attr.meta || !attr.meta.hasOwnProperty(WellknownMetaItems.SHOWONDASHBOARD) || !!Util.getMetaValue(WellknownMetaItems.SHOWONDASHBOARD, attr);
return showOnMap;
diff --git a/ui/app/manager/src/pages/page-users.ts b/ui/app/manager/src/pages/page-users.ts
index 366775835..9aa10888b 100644
--- a/ui/app/manager/src/pages/page-users.ts
+++ b/ui/app/manager/src/pages/page-users.ts
@@ -621,7 +621,12 @@ class PageUsers<S extends AppStateKeyed> extends Page<S> {
}
],
dismissAction: {
- actionName: "cancel"
+ actionName: "cancel",
+ action: () => {
+ user.userAssetLinks = user.previousAssetLinks;
+ user.previousAssetLinks = undefined;
+ openBtn.disabled = false;
+ }
}
});
}
diff --git a/ui/component/core/src/index.ts b/ui/component/core/src/index.ts
index 4cfe28cfd..514d303d3 100644
--- a/ui/component/core/src/index.ts
+++ b/ui/component/core/src/index.ts
@@ -1057,6 +1057,10 @@ export class Manager implements EventProviderFactory {
return !!(this.getRealm() && this.getRealm() === "master" && this.hasRealmRole("admin"));
}
+ public isRestrictedUser(): boolean {
+ return !!this.hasRealmRole("restricted_user");
+ }
+
public getApiBaseUrl(): string {
let baseUrl = this._config.managerUrl!;
baseUrl += "/api/" + this._config.realm + "/";
diff --git a/ui/component/or-asset-tree/src/index.ts b/ui/component/or-asset-tree/src/index.ts
index 5d8b9f693..2e702c333 100644
--- a/ui/component/or-asset-tree/src/index.ts
+++ b/ui/component/or-asset-tree/src/index.ts
@@ -58,6 +58,10 @@ export interface AssetTreeConfig {
};
}
+interface AssetWithReparentId extends Asset {
+ reparentId?: string | null;
+}
+
export interface UiAssetTreeNode extends AssetTreeNode {
selected: boolean;
expandable: boolean;
@@ -259,6 +263,7 @@ export class OrAssetTree extends subscribe(manager)(LitElement) {
@property({attribute: false})
protected _nodes?: UiAssetTreeNode[];
+ protected _loading: boolean = false;
protected _connected: boolean = false;
protected _selectedNodes: UiAssetTreeNode[] = [];
protected _initCallback?: EventCallback;
@@ -352,7 +357,7 @@ export class OrAssetTree extends subscribe(manager)(LitElement) {
this._nodes = undefined;
}
- if (!this._nodes || this._nodes.length === 0) {
+ if (!this._nodes) {
this._loadAssets();
return true;
}
@@ -783,13 +788,19 @@ export class OrAssetTree extends subscribe(manager)(LitElement) {
return;
}
+ if (this._loading) {
+ return;
+ }
+
+ this._loading = true;
+
const query: AssetQuery = {
tenant: {
realm: manager.isSuperUser() ? manager.displayRealm : manager.getRealm()
},
select: { // Just need the basic asset info
excludeAttributes: true,
- excludePath: true,
+ excludePath: !manager.isRestrictedUser(),
excludeParentInfo: true
}
};
@@ -810,9 +821,12 @@ export class OrAssetTree extends subscribe(manager)(LitElement) {
assetQuery: query
}
})
- .then((ev) =>
- this._buildTreeNodes((ev as AssetsEvent).assets!, sortFunction));
+ .then((ev) => {
+ this._loading = false;
+ this._buildTreeNodes((ev as AssetsEvent).assets!, sortFunction)
+ });
} else {
+ this._loading = false;
this._buildTreeNodes(this.assets, sortFunction);
}
}
@@ -879,6 +893,23 @@ export class OrAssetTree extends subscribe(manager)(LitElement) {
if (!assets || assets.length === 0) {
this._nodes = [];
} else {
+ if (manager.isRestrictedUser()) {
+ // Any assets whose parents aren't accessible need to be re-parented
+ assets.forEach(asset => {
+ if (!!asset.parentId && !!asset.path && assets.find(a => a.id === asset.parentId) === undefined) {
+ let reparentId = null;
+ for (let i = 2; i < asset.path!.length; i++) {
+ const ancestorId = asset.path![i];
+ if (assets.find(a => a.id === ancestorId) !== undefined) {
+ reparentId = ancestorId;
+ break;
+ }
+ }
+ (asset as AssetWithReparentId).reparentId = reparentId;
+ }
+ });
+ }
+
let rootAssetIds: string[] | undefined;
if (this.rootAssetIds) {
@@ -890,13 +921,13 @@ export class OrAssetTree extends subscribe(manager)(LitElement) {
let rootAssets: UiAssetTreeNode[];
if (rootAssetIds) {
- rootAssets = assets.filter((asset) => rootAssetIds!.indexOf(asset.id!) >= 0).map((asset) => {
+ rootAssets = assets.filter((asset: AssetWithReparentId) => rootAssetIds!.indexOf(asset.id!) >= 0 || asset.reparentId === null).map((asset) => {
return {
asset: asset
} as UiAssetTreeNode;
});
} else {
- rootAssets = assets.filter((asset) => !asset.parentId).map((asset) => {
+ rootAssets = assets.filter((asset: AssetWithReparentId) => !asset.parentId || asset.reparentId === null).map((asset) => {
return {
asset: asset
} as UiAssetTreeNode;
@@ -921,8 +952,8 @@ export class OrAssetTree extends subscribe(manager)(LitElement) {
}
}
- protected _buildChildTreeNodes(treeNode: UiAssetTreeNode, assets: Asset[], sortFunction: (a: UiAssetTreeNode, b: UiAssetTreeNode) => number) {
- treeNode.children = assets.filter((asset) => asset.parentId === treeNode.asset!.id).map((asset) => {
+ protected _buildChildTreeNodes(treeNode: UiAssetTreeNode, assets: AssetWithReparentId[], sortFunction: (a: UiAssetTreeNode, b: UiAssetTreeNode) => number) {
+ treeNode.children = assets.filter((asset) => asset.parentId === treeNode.asset!.id || asset.reparentId === treeNode.asset!.id).map((asset) => {
return {
asset: asset
} as UiAssetTreeNode;