100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
UI updates: Added auto provisioning page to the Manager UI
Rich Turner242c63c2b038
diff --git a/manager/src/main/java/org/openremote/manager/provisioning/ProvisioningResourceImpl.java b/manager/src/main/java/org/openremote/manager/provisioning/ProvisioningResourceImpl.javaindex 321d2f8e7..14c970902 100644--- a/manager/src/main/java/org/openremote/manager/provisioning/ProvisioningResourceImpl.java+++ b/manager/src/main/java/org/openremote/manager/provisioning/ProvisioningResourceImpl.java@@ -19,38 +19,56 @@*/package org.openremote.manager.provisioning;+import org.openremote.container.timer.TimerService;import org.openremote.manager.security.ManagerIdentityService;+import org.openremote.manager.web.ManagerWebResource;import org.openremote.model.http.RequestParams;import org.openremote.model.provisioning.ProvisioningConfig;import org.openremote.model.provisioning.ProvisioningResource;-public class ProvisioningResourceImpl implements ProvisioningResource {+import javax.ws.rs.ForbiddenException;++public class ProvisioningResourceImpl extends ManagerWebResource implements ProvisioningResource {protected final ProvisioningService provisioningService;protected final ManagerIdentityService identityService;- public ProvisioningResourceImpl(ProvisioningService provisioningService, ManagerIdentityService identityService) {+ public ProvisioningResourceImpl(ProvisioningService provisioningService, TimerService timerService, ManagerIdentityService identityService) {+ super(timerService, identityService);this.provisioningService = provisioningService;this.identityService = identityService;}@Override- public ProvisioningConfig<?, ?> getProvisioningConfigs() {- return null;+ public ProvisioningConfig<?, ?>[] getProvisioningConfigs() {+ if (!isSuperUser()) {+ throw new ForbiddenException("Only super admin can get provisioning configurations");+ }+ return provisioningService.getProvisioningConfigs().toArray(new ProvisioningConfig[0]);}@Overridepublic long createProvisioningConfig(ProvisioningConfig<?, ?> provisioningConfig) {- return 0;+ if (!isSuperUser()) {+ throw new ForbiddenException("Only super admin can create provisioning configurations");+ }+ provisioningConfig = provisioningService.merge(provisioningConfig);+ return provisioningConfig.getId();}@Overridepublic void updateProvisioningConfig(RequestParams requestParams, Long id, ProvisioningConfig<?, ?> provisioningConfig) {-+ if (!isSuperUser()) {+ throw new ForbiddenException("Only super admin can modify provisioning configurations");+ }+ provisioningService.merge(provisioningConfig);}@Overridepublic void deleteProvisioningConfig(RequestParams requestParams, Long id) {-+ if (!isSuperUser()) {+ throw new ForbiddenException("Only super admin can delete provisioning configurations");+ }+ provisioningService.delete(id);}}diff --git a/manager/src/main/java/org/openremote/manager/provisioning/ProvisioningService.java b/manager/src/main/java/org/openremote/manager/provisioning/ProvisioningService.javaindex db40fb190..34179752a 100644--- a/manager/src/main/java/org/openremote/manager/provisioning/ProvisioningService.java+++ b/manager/src/main/java/org/openremote/manager/provisioning/ProvisioningService.java@@ -20,6 +20,7 @@package org.openremote.manager.provisioning;import org.openremote.container.persistence.PersistenceService;+import org.openremote.container.timer.TimerService;import org.openremote.manager.security.ManagerIdentityService;import org.openremote.manager.web.ManagerWebService;import org.openremote.model.Container;@@ -29,7 +30,6 @@ import org.openremote.model.util.ValueUtil;import javax.validation.ConstraintViolation;import javax.validation.ConstraintViolationException;-import java.security.cert.CertificateFactory;import java.util.List;import java.util.Set;import java.util.logging.Level;@@ -40,7 +40,6 @@ public class ProvisioningService implements ContainerService {protected static final Logger LOG = Logger.getLogger(ProvisioningService.class.getName());protected PersistenceService persistenceService;protected ManagerIdentityService identityService;- protected CertificateFactory certificateFactory;@Overridepublic int getPriority() {@@ -51,10 +50,10 @@ public class ProvisioningService implements ContainerService {public void init(Container container) throws Exception {persistenceService = container.getService(PersistenceService.class);identityService = container.getService(ManagerIdentityService.class);-+ TimerService timerService = container.getService(TimerService.class);container.getService(ManagerWebService.class).getApiSingletons().add(- new ProvisioningResourceImpl(this, identityService)+ new ProvisioningResourceImpl(this, timerService, identityService));}diff --git a/manager/src/web/shared/locales/en/or.json b/manager/src/web/shared/locales/en/or.jsonindex cc7b4a734..689ac4b38 100644--- a/manager/src/web/shared/locales/en/or.json+++ b/manager/src/web/shared/locales/en/or.json@@ -427,5 +427,13 @@"invalidJSON": "Invalid JSON","invalidDate": "Invalid date","keyAlreadyExists": "Key already exists"- }+ },+ "autoProvisioning": "Auto provisioning",+ "provisioningConfigs": "Provisioning configurations",+ "loadFailedProvisioningConfigs": "Failed to load provisioning configurations",+ "deleteProvisioningConfigConfirm":"Are you sure you want to delete this provisioning config?\nClients that use this config will no longer be able to connect.",+ "assetTemplate": "Asset template",+ "createAsRestrictedUser": "Create restricted user",+ "ignoreExpiryDate": "Ignore expiry date",+ "CACertPem": "CA Certificate (PEM)"}diff --git a/ui/app/manager/src/headers.ts b/ui/app/manager/src/headers.tsindex 3c0bb5fd5..b5f907f87 100644--- a/ui/app/manager/src/headers.ts+++ b/ui/app/manager/src/headers.ts@@ -123,4 +123,14 @@ export function headerItemExport<S extends AppStateKeyed, A extends AnyAction>(ohref: "data-export",text: "dataExport"};-}\ No newline at end of file+}++export function headerItemProvisioning<S extends AppStateKeyed, A extends AnyAction>(orApp: OrApp<S>): HeaderItem {+ return {+ icon: "cellphone-cog",+ value: "provisioning",+ href: "provisioning",+ text: "autoProvisioning",+ roles: () => manager.isSuperUser()+ };+}diff --git a/ui/app/manager/src/index.ts b/ui/app/manager/src/index.tsindex e66a9e4a6..a4ca51aca 100644--- a/ui/app/manager/src/index.ts+++ b/ui/app/manager/src/index.ts@@ -1,4 +1,6 @@// Declare require method which we'll use for importing webpack resources (using ES6 imports will confuse typescript parser)+import {pageProvisioningProvider} from "./pages/page-provisioning";+declare var CONFIG_URL_PREFIX: string;import {combineReducers, configureStore} from "@reduxjs/toolkit";@@ -24,7 +26,8 @@ import {headerItemRoles,headerItemRealms,headerItemInsights,- headerItemExport} from "./headers";+ headerItemExport, headerItemProvisioning+} from "./headers";import "./pages/page-map";import {pageMapReducer, pageMapProvider, PageMapConfig} from "./pages/page-map";import "./pages/page-assets";@@ -97,7 +100,8 @@ export const DefaultPagesConfig: PageProvider<any>[] = [pageRolesProvider(store),pageUsersProvider(store),pageRealmsProvider(store),- pageExportProvider(store)+ pageExportProvider(store),+ pageProvisioningProvider(store)];export const DefaultHeaderMainMenu: {[name: string]: HeaderItem} = {@@ -116,6 +120,7 @@ export const DefaultHeaderSecondaryMenu: {[name: string]: HeaderItem} = {roles: headerItemRoles(orApp),realms: headerItemRealms(orApp),export: headerItemExport(orApp),+ provisioning: headerItemProvisioning(orApp),logout: headerItemLogout(orApp)};diff --git a/ui/app/manager/src/pages/page-provisioning.ts b/ui/app/manager/src/pages/page-provisioning.tsnew file mode 100644index 000000000..7e3c45088--- /dev/null+++ b/ui/app/manager/src/pages/page-provisioning.ts@@ -0,0 +1,466 @@+import {css, html, TemplateResult, unsafeCSS} from "lit";+import {customElement, state} from "lit/decorators.js";+import manager, {DefaultColor3} from "@openremote/core";+import "@openremote/or-components/or-panel";+import "@openremote/or-translate";+import {EnhancedStore} from "@reduxjs/toolkit";+import {AppStateKeyed, Page, PageProvider} from "@openremote/or-app";+import {ClientRole, ProvisioningConfig, X509ProvisioningData} from "@openremote/model";+import {i18next} from "@openremote/or-translate";+import {OrIcon} from "@openremote/or-icon";+import {InputType, OrInputChangedEvent} from "@openremote/or-mwc-components/or-mwc-input";+import {showOkCancelDialog} from "@openremote/or-mwc-components/or-mwc-dialog";+import {showSnackbar} from "@openremote/or-mwc-components/or-mwc-snackbar";+import {GenericAxiosResponse} from "@openremote/rest";++const tableStyle = require("@material/data-table/dist/mdc.data-table.css");++export function pageProvisioningProvider<S extends AppStateKeyed>(store: EnhancedStore<S>): PageProvider<S> {+ return {+ name: "provisioning",+ routes: ["provisioning"],+ pageCreator: () => {+ return new PageProvisioning(store);+ },+ };+}++@customElement("page-provisioning")+class PageProvisioning<S extends AppStateKeyed> extends Page<S> {++ static get styles() {+ // language=CSS+ return [+ unsafeCSS(tableStyle),+ css`+ #wrapper {+ height: 100%;+ width: 100%;+ display: flex;+ flex-direction: column;+ overflow: auto;+ }++ #title {+ padding: 0 20px;+ font-size: 18px;+ font-weight: bold;+ width: calc(100% - 40px);+ max-width: 1360px;+ margin: 20px auto;+ align-items: center;+ display: flex;+ }++ #title or-icon {+ margin-right: 10px;+ margin-left: 14px;+ }++ .panel {+ width: calc(100% - 90px);+ max-width: 1310px;+ background-color: white;+ border: 1px solid #e5e5e5;+ border-radius: 5px;+ position: relative;+ margin: 5px auto;+ padding: 24px;+ }++ .panel-title {+ text-transform: uppercase;+ font-weight: bolder;+ line-height: 1em;+ color: var(--internal-or-asset-viewer-title-text-color);+ margin-bottom: 20px;+ margin-top: 0;+ flex: 0 0 auto;+ letter-spacing: 0.025em;+ }++ #table-users,+ #table-users table {+ width: 100%;+ white-space: nowrap;+ }++ .mdc-data-table__row {+ cursor: pointer;+ border-top-color: #D3D3D3;+ }++ td, th {+ width: 25%+ }++ or-mwc-input {+ margin-bottom: 20px;+ margin-right: 16px;+ }++ or-icon {+ vertical-align: middle;+ --or-icon-width: 20px;+ --or-icon-height: 20px;+ margin-right: 2px;+ margin-left: -5px;+ }++ .row {+ display: flex;+ flex-direction: row;+ margin: 10px 0;+ flex: 1 1 0;+ }++ .column {+ display: flex;+ flex-direction: column;+ margin: 0px;+ flex: 1 1 0;+ max-width: 50%;+ }++ .mdc-data-table__header-cell {+ font-weight: bold;+ color: ${unsafeCSS(DefaultColor3)};+ }++ .mdc-data-table__header-cell:first-child {+ padding-left: 36px;+ }++ .item-row td {+ padding: 0;+ }++ .item-row-content {+ flex-direction: row;+ overflow: hidden;+ max-height: 0;+ padding-left: 16px;+ }++ .item-row.expanded .item-row-content {+ overflow: visible;+ max-height: unset;+ }++ .button {+ cursor: pointer;+ display: flex;+ flex-direction: row;+ align-content: center;+ padding: 16px;+ align-items: center;+ font-size: 14px;+ text-transform: uppercase;+ color: var(--or-app-color4);+ }++ .hidden {+ display: none;+ }++ @media screen and (max-width: 768px) {+ #title {+ padding: 0;+ width: 100%;+ }++ .hide-mobile {+ display: none;+ }++ .row {+ display: block;+ flex-direction: column;+ }++ .panel {+ border-radius: 0;+ border-left: 0px;+ border-right: 0px;+ width: calc(100% - 48px);+ }++ td, th {+ width: 50%+ }+ }+ `,+ ];+ }++ @state()+ protected _configs?: ProvisioningConfig<any, any>[];+ @state()+ protected _roleOptions?: [string, string][];+ @state()+ protected _realmOptions?: string[];++ get name(): string {+ return "autoProvisioning";+ }++ public connectedCallback() {+ super.connectedCallback();+ this._loadConfigs();+ }++ stateChanged(state: S): void {+ }++ protected render(): TemplateResult | void {+ if (!manager.authenticated) {+ return html`+ <or-translate value="notAuthenticated"></or-translate>+ `;+ }++ if (!this._configs) {+ return html``;+ }++ return html`+ <div id="wrapper">+ <div id="title">+ <or-icon icon="cellphone-cog"></or-icon>+ ${i18next.t("provisioningConfigs")}+ </div>++ <div class="panel">+ <div id="table-users" class="mdc-data-table">+ <table class="mdc-data-table__table" aria-label="attribute list">+ <thead>+ <tr class="mdc-data-table__header-row">+ <th class="mdc-data-table__header-cell" role="columnheader" scope="col">+ <or-translate value="name"></or-translate>+ </th>+ <th class="mdc-data-table__header-cell hide-mobile" role="columnheader" scope="col">+ <or-translate value="realm"></or-translate>+ </th>+ <th class="mdc-data-table__header-cell hide-mobile" role="columnheader" scope="col">+ <or-translate value="type"></or-translate>+ </th>+ <th class="mdc-data-table__header-cell hide-mobile" role="columnheader" scope="col">+ <or-translate value="enabled"></or-translate>+ </th>+ </tr>+ </thead>+ <tbody class="mdc-data-table__content">+ ${this._configs.map((config, index) => this._getConfigTemplate(() => {+ this._configs.pop(); this._configs = [...this._configs];+ }, config))}+ ${(this._configs.length === 0 || (this._configs.length > 0 && !!this._configs[this._configs.length - 1].id)) ? html`+ <tr class="mdc-data-table__row" @click="${() => {+ this._configs = [...this._configs, {type: "x509"}];+ }}">+ <td colspan="100%">+ <a class="button"><or-icon icon="plus"></or-icon>${i18next.t("add")}</a>+ </td>+ </tr>+ ` : ``}+ </tbody>+ </table>+ </div>+ </div>+ </div>+ `;+ }++ protected async _loadConfigs() {++ this._configs = undefined;+ this._roleOptions = undefined;+ this._realmOptions = undefined;++ const responseAndStateOK: <T extends GenericAxiosResponse<any[]>>(response: T, errorMsg: string) => boolean = (response, errorMsg) => {++ // After async op check that the response still matches current state and that the component is still loaded in the UI+ if (!this.isConnected) {+ // Component disconnected - don't continue+ return false;+ }++ if (!response.data) {+ showSnackbar(undefined, errorMsg, i18next.t("dismiss"));+ console.error(errorMsg + ": response = " + response.statusText);+ return false;+ }++ return true;+ };++ const realmResponse = await manager.rest.api.TenantResource.getAll();++ if (!responseAndStateOK(realmResponse, i18next.t("loadFailedRealms"))) {+ return;+ }++ const rolesResponse = await manager.rest.api.UserResource.getRoles(manager.displayRealm);++ if (!responseAndStateOK(rolesResponse, i18next.t("loadFailedRoles"))) {+ return;+ }++ const provisioningResponse = await manager.rest.api.ProvisioningResource.getProvisioningConfigs();++ if (!responseAndStateOK(provisioningResponse, i18next.t("loadFailedProvisioningConfigs"))) {+ return;+ }++ this._realmOptions = realmResponse.data.map(r => r.realm);+ this._roleOptions = rolesResponse.data.filter(role => !role.composite).map(r => [r.name, r.name] as [string, string]).sort();+ this._configs = provisioningResponse.data;+ }++ protected async _createUpdateConfig(config: ProvisioningConfig<any, any>) {+ if (config.id) {+ const response = await manager.rest.api.ProvisioningResource.updateProvisioningConfig(config.id, config);+ } else {+ const response = await manager.rest.api.ProvisioningResource.createProvisioningConfig(config);+ config.id = response.data;+ this._configs = [...this._configs];+ }+ }++ protected _deleteConfig(config: ProvisioningConfig<any, any>) {+ showOkCancelDialog(i18next.t("delete"), i18next.t("deleteProvisioningConfigConfirm"), i18next.t("delete"))+ .then((ok) => {+ if (ok) {+ this._doDelete(config);+ }+ });+ }++ protected _doDelete(config: ProvisioningConfig<any, any>) {+ manager.rest.api.ProvisioningResource.deleteProvisioningConfig(config.id).then(response => {+ this._configs = [...this._configs.filter(c => c.id !== config.id)];+ });+ }++ protected _toggleConfigExpand(ev: MouseEvent) {+ const trElem = ev.currentTarget as HTMLTableRowElement;+ const expanderIcon = trElem.getElementsByTagName("or-icon")[0] as OrIcon;+ const userRow = (trElem.parentElement! as HTMLTableElement).rows[trElem.rowIndex];++ if (expanderIcon.icon === "chevron-right") {+ expanderIcon.icon = "chevron-down";+ userRow.classList.add("expanded");+ } else {+ expanderIcon.icon = "chevron-right";+ userRow.classList.remove("expanded");+ }+ }++ protected _getConfigTemplate(addCancel: () => void, config: ProvisioningConfig<any, any>): TemplateResult {++ let typeColumnContents: TemplateResult;++ if (config.type === "x509") {+ if (!config.data) {+ config.data = {};+ }+ const data = config.data as X509ProvisioningData;++ typeColumnContents = html`+ <or-mwc-input .label="${i18next.t("CACertPem")}" required+ .type="${InputType.TEXTAREA}"+ .value="${data.CACertPEM}"+ @or-mwc-input-changed="${(e: OrInputChangedEvent) => data.CACertPEM = e.detail.value}"></or-mwc-input>+ <or-mwc-input .label="${i18next.t("ignoreExpiryDate")}"+ .type="${InputType.CHECKBOX}"+ .value="${data.ignoreExpiryDate}"+ @or-mwc-input-changed="${(e: OrInputChangedEvent) => config.disabled = !e.detail.value}"+ style="height: 56px;"></or-mwc-input>+ `;+ } else {+ typeColumnContents = html`+ <or-translate .value="notSupported"></or-translate>+ `;+ }++ return html`+ <tr class="mdc-data-table__row" @click="${(ev) => this._toggleConfigExpand(ev)}">+ <td class="padded-cell mdc-data-table__cell">+ <or-icon icon="chevron-right"></or-icon>+ <span>${!config.id ? "" : config.name}</span>+ </td>+ <td class="padded-cell mdc-data-table__cell">+ ${!config.id ? "" : config.realm}+ </td>+ <td class="padded-cell mdc-data-table__cell">+ ${!config.id ? "" : config.type}+ </td>+ <td class="padded-cell mdc-data-table__cell">+ <or-input .type="${InputType.CHECKBOX}" .value="${!config.id ? "" : !config.disabled}"></or-input>+ </td>+ </tr>+ <tr class="item-row${!config.id ? " expanded" : ""}">+ <td colspan="4">+ <div class="item-row-content">+ <div class="row">+ <div class="column">+ <or-mwc-input .label="${i18next.t("name")}"+ .type="${InputType.TEXT}" min="1" required+ .value="${config.name}"+ @or-mwc-input-changed="${(e: OrInputChangedEvent) => config.name = e.detail.value}"></or-mwc-input>+ <or-mwc-input ?disabled="${!!config.id}" required+ .label="${i18next.t("type")}"+ .type="${InputType.SELECT}"+ .options="${[["x509", "X.509"]]}"+ .value="${!config.type ? "x509" : config.type}"+ @or-mwc-input-changed="${(e: OrInputChangedEvent) => config.type = e.detail.value}"></or-mwc-input>+ <or-mwc-input ?disabled="${!!config.id}" required+ .label="${i18next.t("realm")}"+ .type="${InputType.SELECT}"+ .options="${this._realmOptions}"+ .value="${config.realm}"+ @or-mwc-input-changed="${(e: OrInputChangedEvent) => config.realm = e.detail.value}"></or-mwc-input>+ <or-mwc-input ?disabled="${!!config.id}"+ .value="${config.userRoles}"+ .type="${InputType.SELECT}" multiple+ .options="${this._roleOptions}"+ .label="${i18next.t("role_plural")}"+ @or-mwc-input-changed="${(e: OrInputChangedEvent) => config.userRoles = e.detail.value as ClientRole[]}"></or-mwc-input>+ <or-mwc-input .label="${i18next.t("assetTemplate")}"+ .type="${InputType.JSON}"+ .value="${config.assetTemplate}"+ @or-mwc-input-changed="${(e: OrInputChangedEvent) => config.assetTemplate = e.detail.value}"></or-mwc-input>+ <or-mwc-input .label="${i18next.t("createAsRestrictedUser")}"+ .type="${InputType.CHECKBOX}"+ .value="${config.restrictedUser}"+ @or-mwc-input-changed="${(e: OrInputChangedEvent) => config.disabled = e.detail.value}"></or-mwc-input>+ <or-mwc-input .label="${i18next.t("disabled")}"+ .type="${InputType.CHECKBOX}"+ .value="${config.disabled}"+ @or-mwc-input-changed="${(e: OrInputChangedEvent) => config.disabled = e.detail.value}"></or-mwc-input>+ </div>++ <div class="column">+ ${typeColumnContents}+ </div>+ </div>++ <div class="row" style="margin-bottom: 0;">+ ${config.id ? html`<or-mwc-input .label="${i18next.t("delete")}"+ .type="${InputType.BUTTON}"+ @click="${() => this._deleteConfig(config)}"></or-mwc-input>+ ` : ``}+ ${!config.id ? html`<or-mwc-input .label="${i18next.t("cancel")}"+ .type="${InputType.BUTTON}"+ @click="${() => addCancel()}"></or-mwc-input>+ ` : ``}+ <or-mwc-input class="savebtn" style="margin-left: auto;"+ .label="${i18next.t(config.id ? "save" : "create")}"+ .type="${InputType.BUTTON}"+ @click="${() => this._createUpdateConfig(config)}"></or-mwc-input>+ </div>+ </div>+ </td>+ </tr>+ `+ }+}diff --git a/ui/component/core/src/util.ts b/ui/component/core/src/util.tsindex 91df65761..1e054bc27 100644--- a/ui/component/core/src/util.ts+++ b/ui/component/core/src/util.ts@@ -281,7 +281,7 @@ export function camelCaseToSentenceCase(str: string | undefined): string {if (!v) return v;// If first block then capitalise 1st letter regardless- if (!startDone){+ if (!startDone) {startDone = true;return v.charAt(0).toUpperCase() + v.slice(1);}