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: Added support for header roles config to take arrow function

Rich Turner717a2548cb1c
diff --git a/ui/app/or-app/src/index.ts b/ui/app/or-app/src/index.ts
index 019b82875..944d0843d 100644
--- a/ui/app/or-app/src/index.ts
+++ b/ui/app/or-app/src/index.ts
@@ -144,7 +144,7 @@ export function headerItemRealms<S extends AppStateKeyed, A extends AnyAction>(o
value: "realms",
href: "realms",
text: "realm management",
- roles: {"master-realm": ["manage-clients"]}
+ roles: () => manager.isSuperUser()
};
}
diff --git a/ui/app/or-app/src/or-header.ts b/ui/app/or-app/src/or-header.ts
index 07c690535..62fcb1101 100644
--- a/ui/app/or-app/src/or-header.ts
+++ b/ui/app/or-app/src/or-header.ts
@@ -1,6 +1,7 @@
import {css, customElement, html, LitElement, property, PropertyValues, query, TemplateResult, unsafeCSS} from "lit-element";
import {until} from "lit-html/directives/until";
import manager, {
+ Util,
DefaultBoxShadowBottom,
DefaultColor1,
DefaultColor2,
@@ -27,7 +28,7 @@ export interface HeaderItem {
absolute?: boolean;
action?: () => void;
hideMobile?: boolean;
- roles?: string[] | {[client: string]: string[]};
+ roles?: string[] | {[client: string]: string[]} | (() => boolean);
}
export interface Languages {
@@ -57,10 +58,15 @@ function hasRequiredRole(option: HeaderItem): boolean {
if (!option.roles) {
return true;
}
+
if (Array.isArray(option.roles)) {
return option.roles.some((r) => manager.hasRole(r));
}
+ if (Util.isFunction(option.roles)) {
+ return (option.roles as () => boolean)();
+ }
+
return Object.entries(option.roles).some(([client, roles]) => roles.some((r) => manager.hasRole(r, client)));
}
diff --git a/ui/component/core/src/index.ts b/ui/component/core/src/index.ts
index 6262add0a..ef4ad6e69 100644
--- a/ui/component/core/src/index.ts
+++ b/ui/component/core/src/index.ts
@@ -1016,8 +1016,8 @@ export class Manager implements EventProviderFactory {
this._setAuthenticated(authenticated);
}
- public isSuperUser() {
- return this.getRealm() && this.getRealm() === "master" && this.hasRealmRole("admin");
+ public isSuperUser(): boolean {
+ return !!(this.getRealm() && this.getRealm() === "master" && this.hasRealmRole("admin"));
}
public getApiBaseUrl() {
diff --git a/ui/component/core/src/util.ts b/ui/component/core/src/util.ts
index 8c754b01d..e7e0a6dc3 100644
--- a/ui/component/core/src/util.ts
+++ b/ui/component/core/src/util.ts
@@ -204,6 +204,10 @@ export function isObject(object: any): boolean {
return false;
}
+export function isFunction(object: any): boolean {
+ return !!(object && object.constructor && object.call && object.apply);
+}
+
export function objectsEqual(obj1?: any, obj2?: any, deep: boolean = true): boolean {
if (obj1 === null || obj1 === undefined || obj2 === null || obj2 === undefined) {
return obj1 === obj2;