Skip to content

armgit / core

Public Imported from GitHub
0

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

UI update: Asset viewer linked users panel

vlichtenberga8cdc9b93c22
diff --git a/manager/src/web/shared/locales/cn/or.json b/manager/src/web/shared/locales/cn/or.json
index 143f2456a..528ed8ef1 100644
--- a/manager/src/web/shared/locales/cn/or.json
+++ b/manager/src/web/shared/locales/cn/or.json
@@ -435,5 +435,7 @@
"invalidJSON": "Invalid JSON",
"invalidDate": "Invalid date",
"keyAlreadyExists": "Key already exists"
- }
+ },
+ "yes": "是的",
+ "no": "不"
}
diff --git a/manager/src/web/shared/locales/de/or.json b/manager/src/web/shared/locales/de/or.json
index 960d1a4b2..a50dbf9a4 100644
--- a/manager/src/web/shared/locales/de/or.json
+++ b/manager/src/web/shared/locales/de/or.json
@@ -461,5 +461,7 @@
"title": {
"indexedItem": "Item {{index}}"
}
- }
+ },
+ "yes": "Ja",
+ "no": "Nein"
}
diff --git a/manager/src/web/shared/locales/en/or.json b/manager/src/web/shared/locales/en/or.json
index 84ce671fa..a4f25ab38 100644
--- a/manager/src/web/shared/locales/en/or.json
+++ b/manager/src/web/shared/locales/en/or.json
@@ -178,6 +178,7 @@
"matchedOfType": "Matched of type {{type}}",
"unmatched": "Unmatched",
"wait": "Wait",
+ "linkedUsers": "Linked users",
"setup": "Setup",
"selectFile": "Select file",
"discoverAssets": "Discover assets",
@@ -467,5 +468,7 @@
"saveUserFailed": "Save user failed",
"invalidUsername": "Username must be 3-255 alpha numerical characters",
"invalidEmail": "Invalid email address",
+ "yes": "Yes",
+ "no": "No",
"linked": "Linked"
}
diff --git a/manager/src/web/shared/locales/es/or.json b/manager/src/web/shared/locales/es/or.json
index 447779d95..8c083edab 100644
--- a/manager/src/web/shared/locales/es/or.json
+++ b/manager/src/web/shared/locales/es/or.json
@@ -435,5 +435,7 @@
"invalidJSON": "JSON no válido",
"invalidDate": "Fecha no válida",
"keyAlreadyExists": "La clave ya existe"
- }
+ },
+ "yes": "Sí",
+ "no": "No"
}
diff --git a/manager/src/web/shared/locales/fr/or.json b/manager/src/web/shared/locales/fr/or.json
index 803dc44ec..3b73c4867 100644
--- a/manager/src/web/shared/locales/fr/or.json
+++ b/manager/src/web/shared/locales/fr/or.json
@@ -435,5 +435,7 @@
"invalidJSON": "JSON invalide",
"invalidDate": "Date invalide",
"keyAlreadyExists": "La clé existe déjà"
- }
+ },
+ "yes": "Oui",
+ "no": "Non"
}
diff --git a/manager/src/web/shared/locales/it/or.json b/manager/src/web/shared/locales/it/or.json
index 00aa320da..fdda402ae 100644
--- a/manager/src/web/shared/locales/it/or.json
+++ b/manager/src/web/shared/locales/it/or.json
@@ -435,5 +435,7 @@
"invalidJSON": "JSON non valido",
"invalidDate": "Fecha non valida",
"keyAlreadyExists": "La chiave non esiste"
- }
+ },
+ "yes": "Sì",
+ "no": "No"
}
diff --git a/manager/src/web/shared/locales/nl/or.json b/manager/src/web/shared/locales/nl/or.json
index 8db8c2a1b..262450a8b 100644
--- a/manager/src/web/shared/locales/nl/or.json
+++ b/manager/src/web/shared/locales/nl/or.json
@@ -177,6 +177,7 @@
"matchedOfType": "Hetzelfde type: {{type}}",
"unmatched": "Niet dezelfde",
"wait": "Wacht",
+ "linkedUsers": "Gekoppelde gebruikers",
"setup": "Instellingen",
"selectFile": "Kies bestand",
"discoverAssets": "Haal assets op",
@@ -462,5 +463,7 @@
"title": {
"indexedItem": "Item {{index}}"
}
- }
+ },
+ "yes": "Ja",
+ "no": "Nee"
}
diff --git a/manager/src/web/shared/locales/pt/or.json b/manager/src/web/shared/locales/pt/or.json
index 32efb795c..2baec3442 100644
--- a/manager/src/web/shared/locales/pt/or.json
+++ b/manager/src/web/shared/locales/pt/or.json
@@ -423,5 +423,7 @@
"invalidJSON": "JSON inválido",
"invalidDate": "Data inválida",
"keyAlreadyExists": "A chave já existe"
- }
+ },
+ "yes": "Sim",
+ "no": "Não"
}
diff --git a/ui/component/or-asset-viewer/src/index.ts b/ui/component/or-asset-viewer/src/index.ts
index 121eded71..cbe982e63 100644
--- a/ui/component/or-asset-viewer/src/index.ts
+++ b/ui/component/or-asset-viewer/src/index.ts
@@ -14,7 +14,7 @@ import "@openremote/or-mwc-components/or-mwc-dialog";
import {showOkCancelDialog} from "@openremote/or-mwc-components/or-mwc-dialog";
import "@openremote/or-mwc-components/or-mwc-list";
import {OrTranslate, translate} from "@openremote/or-translate";
-import {InputType, OrMwcInput, OrInputChangedEvent} from "@openremote/or-mwc-components/or-mwc-input";
+import {InputType, OrInputChangedEvent, OrMwcInput} from "@openremote/or-mwc-components/or-mwc-input";
import manager, {subscribe, Util} from "@openremote/core";
import {OrMwcTable} from "@openremote/or-mwc-components/or-mwc-table";
import {OrChartConfig, OrChartEvent} from "@openremote/or-chart";
@@ -23,6 +23,7 @@ import {
AgentDescriptor,
Asset,
AssetEvent,
+ AssetModelUtil,
Attribute,
AttributeEvent,
ClientRole,
@@ -31,13 +32,12 @@ import {
WellknownAssets,
WellknownAttributes,
WellknownMetaItems,
- AssetModelUtil
} from "@openremote/model";
import {panelStyles, style} from "./style";
-import i18next, {TOptions, InitOptions} from "i18next";
+import i18next, {InitOptions, TOptions} from "i18next";
import {styleMap} from "lit/directives/style-map.js";
import {classMap} from "lit/directives/class-map.js";
-import { GenericAxiosResponse } from "axios";
+import {GenericAxiosResponse} from "axios";
import {OrIcon} from "@openremote/or-icon";
import "./or-edit-asset-panel";
import {OrEditAssetModifiedEvent, OrEditAssetPanel, ValidatorResult} from "./or-edit-asset-panel";
@@ -45,7 +45,7 @@ import "@openremote/or-mwc-components/or-mwc-snackbar";
import {showSnackbar} from "@openremote/or-mwc-components/or-mwc-snackbar";
export interface PanelConfig {
- type?: "info" | "setup" | "history" | "group" | "survey" | "survey-results";
+ type?: "info" | "setup" | "history" | "group" | "survey" | "survey-results" | "linkedUsers";
title?: string;
hide?: boolean;
hideOnMobile?: boolean;
@@ -291,7 +291,9 @@ export function getPanelContent(panelName: string, asset: Asset, attributes: { [
}
}
- if (panelConfig && panelConfig.type === "info") {
+ if (!panelConfig) return undefined;
+
+ if (panelConfig.type === "info") {
// This type of panel shows attributes and/or properties of the asset
const infoConfig = panelConfig as InfoPanelConfig;
@@ -359,10 +361,10 @@ export function getPanelContent(panelName: string, asset: Asset, attributes: { [
})}`;
}
- if (panelConfig && panelConfig.type === "setup") {
+ if (panelConfig.type === "setup") {
const descriptor = AssetModelUtil.getAssetDescriptor(asset.type) as AgentDescriptor;
-
+
if (!descriptor || !asset.id || descriptor.descriptorType !== "agent" || !descriptor.assetDiscovery && !descriptor.assetImport) {
return;
}
@@ -372,13 +374,13 @@ export function getPanelContent(panelName: string, asset: Asset, attributes: { [
fileNameElem = hostElement.shadowRoot!.getElementById('filename-elem') as HTMLInputElement,
fileUploadBtn: OrMwcInput = hostElement.shadowRoot!.getElementById("fileupload-btn") as OrMwcInput,
str = fileInputElem.value;
-
+
if (!str) {
return;
}
-
+
fileUploadBtn.disabled = false;
-
+
let i;
if (str.lastIndexOf('\\')) {
i = str.lastIndexOf('\\') + 1;
@@ -391,7 +393,7 @@ export function getPanelContent(panelName: string, asset: Asset, attributes: { [
const discoverAssets = () => {
const discoverBtn: OrMwcInput = hostElement.shadowRoot!.getElementById("discover-btn") as OrMwcInput,
cancelBtn: OrMwcInput = hostElement.shadowRoot!.getElementById("cancel-discover-btn") as OrMwcInput;
-
+
if (!discoverBtn || !cancelBtn) {
return false;
}
@@ -399,7 +401,7 @@ export function getPanelContent(panelName: string, asset: Asset, attributes: { [
cancelBtn.hidden = false;
discoverBtn.disabled = true;
discoverBtn.label = i18next.t("discovering") + '...';
-
+
manager.rest.api.AgentResource.doProtocolAssetDiscovery(asset.id!)
.then(response => {
if (response.status !== 200) {
@@ -419,15 +421,15 @@ export function getPanelContent(panelName: string, asset: Asset, attributes: { [
discoverBtn.label = i18next.t("discoverAssets");
});
}
-
+
const cancelDiscovery = () => {
const discoverBtn: OrMwcInput = hostElement.shadowRoot!.getElementById("discover-btn") as OrMwcInput,
cancelBtn: OrMwcInput = hostElement.shadowRoot!.getElementById("cancel-discover-btn") as OrMwcInput;
-
+
discoverBtn.disabled = false;
discoverBtn.label = i18next.t("discoverAssets");
cancelBtn.hidden = true;
-
+
// TODO: cancel the request to the manager
}
@@ -439,14 +441,14 @@ export function getPanelContent(panelName: string, asset: Asset, attributes: { [
}
fileUploadBtn.disabled = true;
-
+
const fileInputElem = hostElement.shadowRoot!.getElementById('fileupload-elem') as HTMLInputElement;
if (fileInputElem) {
const reader = new FileReader();
if (fileInputElem.files && fileInputElem.files.length) {
reader.readAsDataURL(fileInputElem.files[0]); //convert to base64
}
-
+
reader.onload = () => {
if (!reader.result) {
showSnackbar(undefined, "Something went wrong, please try again", i18next.t("dismiss"));
@@ -478,12 +480,12 @@ export function getPanelContent(panelName: string, asset: Asset, attributes: { [
.finally(() => {
fileUploadBtn.disabled = false;
});
-
+
}
}
}
}
-
+
let content: TemplateResult = html``;
if (descriptor.assetImport) {
@@ -505,7 +507,7 @@ export function getPanelContent(panelName: string, asset: Asset, attributes: { [
} else {
showSnackbar(undefined, "agent type doesn't support a known protocol to add assets", i18next.t("dismiss"));
}
-
+
return html`
<style>
[hidden] {
@@ -514,11 +516,11 @@ export function getPanelContent(panelName: string, asset: Asset, attributes: { [
</style>
${content}
`;
-
+
}
// This is not something that should be part of the standard asset-viewer
- if (panelConfig && panelConfig.type === "survey") {
+ if (panelConfig.type === "survey") {
return html``;
// return html`
// <or-survey id="survey" .surveyId="${asset.id}"></or-survey>
@@ -526,14 +528,14 @@ export function getPanelContent(panelName: string, asset: Asset, attributes: { [
}
// This is not something that should be part of the standard asset-viewer
- if (panelConfig && panelConfig.type === "survey-results") {
+ if (panelConfig.type === "survey-results") {
return html``;
// return html`
// <or-survey-results id="survey-results" .survey="${asset}"></or-survey-results>
// `;
}
- if (panelConfig && panelConfig.type === "history") {
+ if (panelConfig.type === "history") {
// Special handling for history panel which shows an attribute selector and a graph/data table of historical values
const historyConfig = panelConfig as HistoryPanelConfig;
const includedAttributes = historyConfig.include ? historyConfig.include : undefined;
@@ -611,7 +613,7 @@ export function getPanelContent(panelName: string, asset: Asset, attributes: { [
`;
}
- if (panelConfig && panelConfig.type === "group") {
+ if (panelConfig.type === "group") {
if (asset.type !== "GroupAsset") {
return;
@@ -631,7 +633,7 @@ export function getPanelContent(panelName: string, asset: Asset, attributes: { [
let availableAttributes: string[] = [];
let selectedAttributes: string[] = [];
-
+
if (groupConfig.childAssetTypes && groupConfig.childAssetTypes[childAssetType]) {
availableAttributes = groupConfig.childAssetTypes[childAssetType].availableAttributes ? groupConfig.childAssetTypes[childAssetType].availableAttributes! : [];
selectedAttributes = groupConfig.childAssetTypes[childAssetType].selectedAttributes ? groupConfig.childAssetTypes[childAssetType].selectedAttributes! : [];
@@ -763,7 +765,28 @@ export function getPanelContent(panelName: string, asset: Asset, attributes: { [
`;
}
- return undefined;
+ if (panelConfig.type === "linkedUsers") {
+
+ const createUserTable = (rows: string[][]) => {
+ const userTable: OrMwcTable = hostElement.shadowRoot!.getElementById(panelName+"-user-table") as OrMwcTable;
+ userTable.options = {stickyFirstColumn:false};
+ userTable.headers = ['Username', 'Roles', 'Restricted user'];
+ userTable.rows = rows.sort(Util.sortByString(u => u[0]));
+ };
+
+ // Load users and rights, then update the table
+ getLinkedUsers(asset, hostElement).then((linkedUsers) => {
+ Promise.all(linkedUsers).then(res => {
+ if (!res || res.length === 0) {
+ hostElement.shadowRoot!.getElementById('linkedUsers-panel')!.hidden = true;
+ return;
+ }
+ createUserTable(res);
+ })
+ });
+
+ return html`<or-mwc-table .id="${panelName}-user-table"></or-mwc-table>`;
+ }
}
export function getAttributeTemplate(asset: Asset, attribute: Attribute<any>, hostElement: LitElement, viewerConfig: AssetViewerConfig, panelConfig: PanelConfig, itemConfig: InfoPanelItemConfig) {
@@ -891,6 +914,64 @@ async function getAssetChildren(id: string, childAssetType: string): Promise<Ass
return response.data.filter((asset) => asset.type === childAssetType);
}
+async function getLinkedUsers(asset: Asset, hostElement: LitElement): Promise<Promise<string[]>[]> {
+ let response: Promise<string[]>[];
+
+ try {
+ response = await manager.rest.api.AssetResource.getUserAssetLinks(
+ {realm: manager.displayRealm, assetId: asset.id}
+ ).then((userAssetLinksRes) => {
+ const userIds = userAssetLinksRes.data.map(e => e.id!.userId) as string[];
+ return userIds.map(async (userId) => {
+
+ let row: string[] = [];
+
+ await manager.rest.api.UserResource.get(manager.displayRealm, userId!)
+ .then((usersRes) => {
+ row.push(usersRes.data.username!);
+ });
+
+ await manager.rest.api.UserResource.getRoles(manager.displayRealm)
+ .then((rolesRes) => {
+ const roles = rolesRes.data.filter(role => !role.composite);
+ if (!manager.isSuperUser() && !roles.some(r => r.name === 'write:admin') && !roles.some(r => r.name === 'read:admin')) {
+ hostElement.shadowRoot!.getElementById('linkedUsers-panel')!.hidden = true;
+ return;
+ }
+ const compositeRoles = rolesRes.data.filter(role => role.composite).map(r => r.name);
+ const roleNames = compositeRoles.join(', ');
+ row.push(roleNames!);
+ })
+ .catch((err) => {
+ console.info('User not allowed to get roles', err);
+ hostElement.shadowRoot!.getElementById('linkedUsers-panel')!.hidden = true;
+ return;
+ });
+
+ await manager.rest.api.UserResource.getUserRealmRoles(manager.displayRealm, userId)
+ .then((rolesRes) => {
+ const hasRestrictedUser = rolesRes.data ? !!rolesRes.data.find(r => r.assigned && r.name === "restricted_user") : false;
+
+ let restrictedUser = hasRestrictedUser ? i18next.t('yes') : i18next.t('no');
+ row.push(restrictedUser);
+ });
+
+ return row;
+ });
+ });
+
+ } catch (e) {
+ console.log("Failed to get child assets: " + e);
+ return [];
+ }
+
+ if (!response) {
+ return [];
+ }
+
+ return response;
+}
+
export async function saveAsset(asset: Asset): Promise<SaveResult> {
const isUpdate = !!asset.id && asset.version !== undefined;
@@ -980,6 +1061,10 @@ export const DEFAULT_VIEWER_CONFIG: AssetViewerConfig = {
},
history: {
type: "history"
+ },
+ linkedUsers: {
+ type: "linkedUsers",
+ title: "linkedUsers",
}
}
};
@@ -1148,7 +1233,6 @@ export class OrAssetViewer extends subscribe(manager)(translate(i18next)(LitElem
content = html`
<div id="view-container" style="${this._viewerConfig.viewerStyles ? styleMap(this._viewerConfig.viewerStyles) : ""}">
${Object.entries(this._viewerConfig.panels).map(([name, panelConfig]) => {
-
if (panelConfig.hide) {
return ``;
}
diff --git a/ui/component/or-asset-viewer/src/style.ts b/ui/component/or-asset-viewer/src/style.ts
index d178146ec..68a50d248 100644
--- a/ui/component/or-asset-viewer/src/style.ts
+++ b/ui/component/or-asset-viewer/src/style.ts
@@ -56,6 +56,10 @@ export const panelStyles = css`
border-left: none;
}
}
+
+ #linkedUsers-panel {
+ min-height: 200px;
+ }
`;
// language=CSS