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: super user now gets full manager app functionality independent of config

Rich Turner565ec69edf62
diff --git a/manager/src/web/shared/locales/en/or.json b/manager/src/web/shared/locales/en/or.json
index 056e34ec1..89f42bcc8 100644
--- a/manager/src/web/shared/locales/en/or.json
+++ b/manager/src/web/shared/locales/en/or.json
@@ -233,6 +233,11 @@
"EVENTS_CONNECTION_ERROR": "Event bus connection error (see console logs)",
"TRANSLATION_ERROR": "Translations error (see console logs)"
},
+ "appError": {
+ "noConfig": "No app config provided",
+ "noReduxStore": "No Redux store provided",
+ "noPages": "No pages provided"
+ },
"couldNotRetrieveAttribute": "Could not retrieve attribute information",
"pressEnterToSend": "Press Enter to send",
"sending": "Sending",
diff --git a/ui/app/manager/src/index.ts b/ui/app/manager/src/index.ts
index 068668727..d90f6e0f0 100644
--- a/ui/app/manager/src/index.ts
+++ b/ui/app/manager/src/index.ts
@@ -137,91 +137,12 @@ const configURL = (CONFIG_URL_PREFIX || "") + "/manager_config.json";
fetch(configURL).then(async (result) => {
if (!result.ok) {
- return [DefaultAppConfig, undefined];
+ return undefined;
}
- const appConfig = await result.json() as ManagerAppConfig;
+ return await result.json() as ManagerAppConfig;
- if (!appConfig) {
- return [DefaultAppConfig, undefined];
- }
-
- // Build pages
- let pages: PageProvider<any>[] = [...DefaultPagesConfig];
-
- if (appConfig.pages) {
-
- // Replace any supplied page configs
- if (appConfig.pages) {
- pages = pages.map(pageProvider => {
- const config = appConfig.pages[pageProvider.name];
-
- switch (pageProvider.name) {
- case "map": {
- pageProvider = config ? pageMapProvider(store, config as PageMapConfig) : pageProvider;
- break;
- }
- case "assets": {
- pageProvider = config ? pageAssetsProvider(store, config as PageAssetsConfig) : pageProvider;
- break;
- }
- case "rules": {
- pageProvider = config ? pageRulesProvider(store, config as PageRulesConfig) : pageProvider;
- break;
- }
- case "insights": {
- pageProvider = config ? pageInsightsProvider(store, config as PageInsightsConfig) : pageProvider;
- break;
- }
- case "logs": {
- pageProvider = config ? pageLogsProvider(store, config as PageLogsConfig) : pageProvider;
- break;
- }
- }
-
- return pageProvider;
- });
- }
- }
-
- const orAppConfig: AppConfig<RootState> = {
- pages: pages,
- superUserHeader: DefaultHeaderConfig
- };
-
- // Configure realms
- if (!appConfig.realms) {
- orAppConfig.realms = {
- default: {...DefaultRealmConfig, header: DefaultHeaderConfig}
- };
- } else {
- orAppConfig.realms = {};
- const defaultRealm = appConfig.realms.default ? {...DefaultRealmConfig,...appConfig.realms.default} : DefaultRealmConfig;
- orAppConfig.realms.default = defaultRealm;
-
- Object.entries(appConfig.realms).forEach(([name, realmConfig]) => {
-
- const normalisedConfig = {...defaultRealm,...realmConfig};
- let headers = DefaultHeaderConfig;
-
- if (normalisedConfig.headers) {
- headers = {
- mainMenu: [],
- secondaryMenu: []
- };
- normalisedConfig.headers.forEach((pageName) => {
- // Insert header
- if (DefaultHeaderMainMenu.hasOwnProperty(pageName)) {
- headers.mainMenu.push(DefaultHeaderMainMenu[pageName]);
- } else if (DefaultHeaderSecondaryMenu.hasOwnProperty(pageName)) {
- headers.secondaryMenu!.push(DefaultHeaderSecondaryMenu[pageName]);
- }
- });
- }
-
- orAppConfig.realms[name] = {...defaultRealm, header: headers,...(realmConfig as RealmAppConfig)};
- });
- }
+}).then((appConfig: ManagerAppConfig) => {
// Set locales and load path
if (!appConfig.manager) {
@@ -236,25 +157,111 @@ fetch(configURL).then(async (result) => {
}
}
- // Add config prefix if defined
+ // Add config prefix if defined (used in dev)
if (CONFIG_URL_PREFIX) {
- Object.values(orAppConfig.realms).forEach((realmConfig) => {
- if (typeof (realmConfig.logo) === "string") {
- realmConfig.logo = CONFIG_URL_PREFIX + realmConfig.logo;
- }
- if (typeof (realmConfig.logoMobile) === "string") {
- realmConfig.logoMobile = CONFIG_URL_PREFIX + realmConfig.logoMobile;
- }
- });
if (appConfig.manager.translationsLoadPath) {
appConfig.manager.translationsLoadPath = CONFIG_URL_PREFIX + appConfig.manager.translationsLoadPath;
}
}
- return [orAppConfig, appConfig.manager];
+ orApp.managerConfig = appConfig.manager;
+
+ orApp.appConfigProvider = (manager) => {
+
+ if (manager.isSuperUser()) {
+ return DefaultAppConfig;
+ }
+
+ // Build pages
+ let pages: PageProvider<any>[] = [...DefaultPagesConfig];
+
+ if (appConfig.pages) {
+
+ // Replace any supplied page configs
+ if (appConfig.pages) {
+ pages = pages.map(pageProvider => {
+ const config = appConfig.pages[pageProvider.name];
+
+ switch (pageProvider.name) {
+ case "map": {
+ pageProvider = config ? pageMapProvider(store, config as PageMapConfig) : pageProvider;
+ break;
+ }
+ case "assets": {
+ pageProvider = config ? pageAssetsProvider(store, config as PageAssetsConfig) : pageProvider;
+ break;
+ }
+ case "rules": {
+ pageProvider = config ? pageRulesProvider(store, config as PageRulesConfig) : pageProvider;
+ break;
+ }
+ case "insights": {
+ pageProvider = config ? pageInsightsProvider(store, config as PageInsightsConfig) : pageProvider;
+ break;
+ }
+ case "logs": {
+ pageProvider = config ? pageLogsProvider(store, config as PageLogsConfig) : pageProvider;
+ break;
+ }
+ }
+
+ return pageProvider;
+ });
+ }
+ }
+
+ const orAppConfig: AppConfig<RootState> = {
+ pages: pages,
+ superUserHeader: DefaultHeaderConfig
+ };
+
+ // Configure realms
+ if (!appConfig.realms) {
+ orAppConfig.realms = {
+ default: {...DefaultRealmConfig, header: DefaultHeaderConfig}
+ };
+ } else {
+ orAppConfig.realms = {};
+ const defaultRealm = appConfig.realms.default ? {...DefaultRealmConfig,...appConfig.realms.default} : DefaultRealmConfig;
+ orAppConfig.realms.default = defaultRealm;
+
+ Object.entries(appConfig.realms).forEach(([name, realmConfig]) => {
+
+ const normalisedConfig = {...defaultRealm,...realmConfig};
+ let headers = DefaultHeaderConfig;
+
+ if (normalisedConfig.headers) {
+ headers = {
+ mainMenu: [],
+ secondaryMenu: []
+ };
+ normalisedConfig.headers.forEach((pageName) => {
+ // Insert header
+ if (DefaultHeaderMainMenu.hasOwnProperty(pageName)) {
+ headers.mainMenu.push(DefaultHeaderMainMenu[pageName]);
+ } else if (DefaultHeaderSecondaryMenu.hasOwnProperty(pageName)) {
+ headers.secondaryMenu!.push(DefaultHeaderSecondaryMenu[pageName]);
+ }
+ });
+ }
+
+ orAppConfig.realms[name] = {...defaultRealm, header: headers,...(realmConfig as RealmAppConfig)};
+ });
+ }
+
+ // Add config prefix if defined (used in dev)
+ if (CONFIG_URL_PREFIX) {
+ Object.values(orAppConfig.realms).forEach((realmConfig) => {
+ if (typeof (realmConfig.logo) === "string") {
+ realmConfig.logo = CONFIG_URL_PREFIX + realmConfig.logo;
+ }
+ if (typeof (realmConfig.logoMobile) === "string") {
+ realmConfig.logoMobile = CONFIG_URL_PREFIX + realmConfig.logoMobile;
+ }
+ });
+ }
+
+ };
-}).then((orAppConfig: [AppConfig<RootState>, ManagerConfig | undefined]) => {
- orApp.appConfig = orAppConfig[0]!;
- orApp.managerConfig = orAppConfig[1];
document.body.appendChild(orApp);
});
diff --git a/ui/component/core/src/index.ts b/ui/component/core/src/index.ts
index b5fc68023..b0bf3d33f 100644
--- a/ui/component/core/src/index.ts
+++ b/ui/component/core/src/index.ts
@@ -130,6 +130,68 @@ export interface ManagerConfig {
basicLoginProvider?: (username: string | undefined, password: string | undefined) => PromiseLike<BasicLoginResult>;
}
+export function normaliseConfig(config: ManagerConfig): ManagerConfig {
+ const normalisedConfig: ManagerConfig = config ? Object.assign({}, config) : {};
+
+ if (!normalisedConfig.managerUrl || normalisedConfig.managerUrl === "") {
+ // Assume manager is running on same host as this code
+ normalisedConfig.managerUrl = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ":" + window.location.port : "");
+ } else {
+ // Normalise by stripping any trailing slashes
+ normalisedConfig.managerUrl = normalisedConfig.managerUrl.replace(/\/+$/, "");
+ }
+
+ if (!normalisedConfig.realm || normalisedConfig.realm === "") {
+ // Assume master realm
+ normalisedConfig.realm = "master";
+ }
+
+ if (normalisedConfig.auth === Auth.KEYCLOAK) {
+ // Determine URL of keycloak server
+ if (!normalisedConfig.keycloakUrl || normalisedConfig.keycloakUrl === "") {
+ // Assume keycloak is running on same host as the manager
+ normalisedConfig.keycloakUrl = normalisedConfig.managerUrl + "/auth";
+ } else {
+ // Normalise by stripping any trailing slashes
+ normalisedConfig.keycloakUrl = normalisedConfig.keycloakUrl.replace(/\/+$/, "");
+ }
+ }
+
+ if (normalisedConfig.consoleAutoEnable === undefined) {
+ normalisedConfig.consoleAutoEnable = true;
+ }
+
+ if (!normalisedConfig.eventProviderType) {
+ normalisedConfig.eventProviderType = EventProviderType.WEBSOCKET;
+ }
+
+ if (!normalisedConfig.pollingIntervalMillis || normalisedConfig.pollingIntervalMillis < 5000) {
+ normalisedConfig.pollingIntervalMillis = 10000;
+ }
+
+ if (normalisedConfig.loadIcons === undefined) {
+ normalisedConfig.loadIcons = true;
+ }
+
+ if (normalisedConfig.loadTranslations === undefined) {
+ normalisedConfig.loadTranslations = ["or"];
+ }
+
+ if (normalisedConfig.translationsLoadPath === undefined) {
+ normalisedConfig.translationsLoadPath = "locales/{{lng}}/{{ns}}.json";
+ }
+
+ if (normalisedConfig.loadDescriptors === undefined) {
+ normalisedConfig.loadDescriptors = true;
+ }
+
+ if (normalisedConfig.clientId === undefined) {
+ normalisedConfig.clientId = "openremote";
+ }
+
+ return normalisedConfig;
+}
+
export class IconSetAddedEvent extends CustomEvent<void> {
public static readonly NAME = "or-iconset-added";
@@ -522,68 +584,6 @@ export class Manager implements EventProviderFactory {
return this._config.mapType || MapType.VECTOR;
}
- protected static normaliseConfig(config: ManagerConfig): ManagerConfig {
- const normalisedConfig: ManagerConfig = Object.assign({}, config);
-
- if (!normalisedConfig.managerUrl || normalisedConfig.managerUrl === "") {
- // Assume manager is running on same host as this code
- normalisedConfig.managerUrl = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ":" + window.location.port : "");
- } else {
- // Normalise by stripping any trailing slashes
- normalisedConfig.managerUrl = normalisedConfig.managerUrl.replace(/\/+$/, "");
- }
-
- if (!normalisedConfig.realm || normalisedConfig.realm === "") {
- // Assume master realm
- normalisedConfig.realm = "master";
- }
-
- if (normalisedConfig.auth === Auth.KEYCLOAK) {
- // Determine URL of keycloak server
- if (!normalisedConfig.keycloakUrl || normalisedConfig.keycloakUrl === "") {
- // Assume keycloak is running on same host as the manager
- normalisedConfig.keycloakUrl = normalisedConfig.managerUrl + "/auth";
- } else {
- // Normalise by stripping any trailing slashes
- normalisedConfig.keycloakUrl = normalisedConfig.keycloakUrl.replace(/\/+$/, "");
- }
- }
-
- if (normalisedConfig.consoleAutoEnable === undefined) {
- normalisedConfig.consoleAutoEnable = true;
- }
-
- if (!normalisedConfig.eventProviderType) {
- normalisedConfig.eventProviderType = EventProviderType.WEBSOCKET;
- }
-
- if (!normalisedConfig.pollingIntervalMillis || normalisedConfig.pollingIntervalMillis < 5000) {
- normalisedConfig.pollingIntervalMillis = 10000;
- }
-
- if (normalisedConfig.loadIcons === undefined) {
- normalisedConfig.loadIcons = true;
- }
-
- if (normalisedConfig.loadTranslations === undefined) {
- normalisedConfig.loadTranslations = ["or"];
- }
-
- if (normalisedConfig.translationsLoadPath === undefined) {
- normalisedConfig.translationsLoadPath = "locales/{{lng}}/{{ns}}.json";
- }
-
- if (normalisedConfig.loadDescriptors === undefined) {
- normalisedConfig.loadDescriptors = true;
- }
-
- if (normalisedConfig.clientId === undefined) {
- normalisedConfig.clientId = "openremote";
- }
-
- return normalisedConfig;
- }
-
private _error?: ORError;
private _config!: ManagerConfig;
private _authenticated: boolean = false;
@@ -636,7 +636,7 @@ export class Manager implements EventProviderFactory {
console.log("Already initialised");
}
- this._config = Manager.normaliseConfig(config);
+ this._config = normaliseConfig(config);
let success = await this.doAuthInit();
diff --git a/ui/component/or-app/src/index.ts b/ui/component/or-app/src/index.ts
index 133acb770..2153032d4 100644
--- a/ui/component/or-app/src/index.ts
+++ b/ui/component/or-app/src/index.ts
@@ -18,7 +18,7 @@ import "./or-header";
import "@openremote/or-icon";
import {updateMetadata} from "pwa-helpers/metadata";
import i18next from "i18next";
-import manager, {Auth, DefaultColor2, DefaultColor3, ManagerConfig, Util, BasicLoginResult, OREvent} from "@openremote/core";
+import manager, {Auth, DefaultColor2, DefaultColor3, ManagerConfig, Util, BasicLoginResult, OREvent, normaliseConfig, Manager} from "@openremote/core";
import {DEFAULT_LANGUAGES, HeaderConfig, HeaderItem, Languages} from "./or-header";
import {DialogConfig, OrMwcDialog, showErrorDialog, showDialog} from "@openremote/or-mwc-components/or-mwc-dialog";
import {OrMwcSnackbar} from "@openremote/or-mwc-components/or-mwc-snackbar";
@@ -54,6 +54,10 @@ export function getRealmQueryParameter(): string | undefined {
}
}
+export function getDefaultManagerConfig() {
+ return normaliseConfig(DEFAULT_MANAGER_CONFIG);
+}
+
const DEFAULT_MANAGER_CONFIG: ManagerConfig = {
managerUrl: MANAGER_URL,
keycloakUrl: KEYCLOAK_URL,
@@ -68,7 +72,9 @@ const DEFAULT_MANAGER_CONFIG: ManagerConfig = {
export class OrApp<S extends AppStateKeyed> extends LitElement {
@property({type: Object})
- public appConfig!: AppConfig<S>;
+ public appConfig?: AppConfig<S>;
+
+ public appConfigProvider?: (manager: Manager) => AppConfig<S>;
@property({type: Object})
public managerConfig?: ManagerConfig;
@@ -175,31 +181,6 @@ export class OrApp<S extends AppStateKeyed> extends LitElement {
protected firstUpdated(_changedProperties: Map<PropertyKey, unknown>): void {
super.firstUpdated(_changedProperties);
- if (!this.appConfig) {
- console.error("No AppConfig supplied");
- return;
- }
-
- if (!this._store) {
- console.error("No Redux store supplied");
- return;
- }
-
- if (!this.appConfig.pages || Object.keys(this.appConfig.pages).length === 0) {
- console.error("No page providers");
- return;
- }
-
- const realm = getRealmQueryParameter();
- const config = this._getConfig(realm);
-
- if (!config) {
- console.error("No default AppConfig or realm specific config for requested realm: " + realm);
- return;
- } else {
- this._config = config;
- }
-
const managerConfig = this.managerConfig ? {...DEFAULT_MANAGER_CONFIG,...this.managerConfig} : DEFAULT_MANAGER_CONFIG;
managerConfig.basicLoginProvider = (u, p) => this.doBasicLogin(u, p);
manager.addListener(this._onManagerEvent);
@@ -208,7 +189,36 @@ export class OrApp<S extends AppStateKeyed> extends LitElement {
manager.init(managerConfig).then((success) => {
if (success) {
+ this.appConfig = this.appConfig || (this.appConfigProvider ? this.appConfigProvider(manager) : undefined);
+
+ if (!this.appConfig) {
+ showErrorDialog("appError.noConfig", document.body);
+ console.error("No AppConfig supplied");
+ return;
+ }
+
+ if (!this._store) {
+ showErrorDialog("appError.noReduxStore", document.body);
+ console.error("No Redux store supplied");
+ return;
+ }
+
+ if (!this.appConfig.pages || Object.keys(this.appConfig.pages).length === 0) {
+ showErrorDialog("appError.noPages", document.body);
+ console.error("No page providers");
+ return;
+ }
+
this._initialised = true;
+ const realm = getRealmQueryParameter();
+ const config = this._getConfig(realm);
+
+ if (!config) {
+ console.error("No default AppConfig or realm specific config for requested realm: " + realm);
+ return;
+ } else {
+ this._config = config;
+ }
this.appConfig.pages.forEach((pageProvider, index) => {
if (pageProvider.routes) {
@@ -224,7 +234,7 @@ export class OrApp<S extends AppStateKeyed> extends LitElement {
if (this.appConfig.pages.length > 0) {
router.on("*", (params, query) => {
- this._store.dispatch(updatePage(this.appConfig.pages[0].name));
+ this._store.dispatch(updatePage(this.appConfig!.pages[0].name));
});
}
@@ -298,7 +308,7 @@ export class OrApp<S extends AppStateKeyed> extends LitElement {
this._mainElem.firstElementChild.remove();
}
if (this._page) {
- const pageProvider = this.appConfig.pages.find((page) => page.name === this._page);
+ const pageProvider = this.appConfig!.pages.find((page) => page.name === this._page);
if (pageProvider) {
const pageElem = pageProvider.pageCreator();
this._mainElem.appendChild(pageElem);
@@ -383,8 +393,8 @@ export class OrApp<S extends AppStateKeyed> extends LitElement {
protected _getConfig(realm: string | undefined): RealmAppConfig {
realm = realm || "default";
- const defaultConfig = this.appConfig.realms ? this.appConfig.realms.default : {};
- let realmConfig = this.appConfig.realms ? this.appConfig.realms![realm] : undefined;
+ const defaultConfig = this.appConfig!.realms ? this.appConfig!.realms.default : {};
+ let realmConfig = this.appConfig!.realms ? this.appConfig!.realms![realm] : undefined;
realmConfig = Util.mergeObjects(defaultConfig, realmConfig, false);
if (this.appConfig && this.appConfig.superUserHeader && manager.isSuperUser()) {
diff --git a/ui/component/or-mwc-components/src/or-mwc-dialog.ts b/ui/component/or-mwc-components/src/or-mwc-dialog.ts
index 072fca725..664943dfa 100644
--- a/ui/component/or-mwc-components/src/or-mwc-dialog.ts
+++ b/ui/component/or-mwc-components/src/or-mwc-dialog.ts
@@ -60,7 +60,7 @@ declare global {
}
}
-export async function showErrorDialog(errorMessage: string) {
+export async function showErrorDialog(errorMessage: string, hostElement?: HTMLElement) {
const deferred = new Util.Deferred<void>();
showDialog({
@@ -83,7 +83,7 @@ export async function showErrorDialog(errorMessage: string) {
default: true,
action: (dialog) => deferred.resolve()
}]
- });
+ }, hostElement);
await deferred.promise;
}