Skip to content

armgit / core

Public Imported from GitHub
0

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

Added new write:attributes role to allow attribute writes without writing assets (implements #213)

Rich Turnerdb1b0fe94b1f
diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java b/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java
index 2615282a7..2e9e2e6c2 100644
--- a/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java
+++ b/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java
@@ -274,8 +274,8 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer
throw new AssetProcessingException(INSUFFICIENT_ACCESS);
}
- // Regular user must have write assets role
- if (!authContext.hasResourceRoleOrIsSuperUser(ClientRole.WRITE_ASSETS.getValue(),
+ // Regular user must have write attributes role
+ if (!authContext.hasResourceRoleOrIsSuperUser(ClientRole.WRITE_ATTRIBUTES.getValue(),
Constants.KEYCLOAK_CLIENT_ID)) {
throw new AssetProcessingException(INSUFFICIENT_ACCESS);
}
diff --git a/manager/src/main/java/org/openremote/manager/setup/AbstractKeycloakSetup.java b/manager/src/main/java/org/openremote/manager/setup/AbstractKeycloakSetup.java
index ea49a3f15..53736e532 100644
--- a/manager/src/main/java/org/openremote/manager/setup/AbstractKeycloakSetup.java
+++ b/manager/src/main/java/org/openremote/manager/setup/AbstractKeycloakSetup.java
@@ -45,6 +45,7 @@ public abstract class AbstractKeycloakSetup implements Setup {
ClientRole.READ_USERS,
ClientRole.READ_RULES,
ClientRole.WRITE_ASSETS,
+ ClientRole.WRITE_ATTRIBUTES,
ClientRole.WRITE_RULES
};
diff --git a/manager/src/main/java/org/openremote/manager/setup/AbstractManagerSetup.java b/manager/src/main/java/org/openremote/manager/setup/AbstractManagerSetup.java
index 1aea78e0a..f83fe9f75 100644
--- a/manager/src/main/java/org/openremote/manager/setup/AbstractManagerSetup.java
+++ b/manager/src/main/java/org/openremote/manager/setup/AbstractManagerSetup.java
@@ -528,30 +528,60 @@ public abstract class AbstractManagerSetup implements Setup {
Asset peopleCounterAsset = new Asset(name, PEOPLE_COUNTER, area).addAttributes(
new AssetAttribute(AttributeType.LOCATION, location.toValue()).addMeta(SHOW_ON_DASHBOARD)
);
- peopleCounterAsset.getAttribute("peopleCountIn").ifPresent(assetAttribute -> assetAttribute.addMeta(
- new MetaItem(RULE_STATE),
- new MetaItem(STORE_DATA_POINTS)
- ));
- peopleCounterAsset.getAttribute("peopleCountOut").ifPresent(assetAttribute -> assetAttribute.addMeta(
- new MetaItem(RULE_STATE),
- new MetaItem(STORE_DATA_POINTS)
- ));
- peopleCounterAsset.getAttribute("peopleCountInMinute").ifPresent(assetAttribute -> assetAttribute.addMeta(
- new MetaItem(RULE_STATE),
- new MetaItem(STORE_DATA_POINTS)
- ));
- peopleCounterAsset.getAttribute("peopleCountOutMinute").ifPresent(assetAttribute -> assetAttribute.addMeta(
- new MetaItem(RULE_STATE),
- new MetaItem(STORE_DATA_POINTS)
- ));
- peopleCounterAsset.getAttribute("peopleCountTotal").ifPresent(assetAttribute -> assetAttribute.addMeta(
- new MetaItem(RULE_STATE),
- new MetaItem(STORE_DATA_POINTS)
- ));
- peopleCounterAsset.getAttribute("peopleCountGrowth").ifPresent(assetAttribute -> assetAttribute.addMeta(
- new MetaItem(RULE_STATE),
- new MetaItem(STORE_DATA_POINTS)
- ));
+ peopleCounterAsset.getAttribute("peopleCountIn").ifPresent(assetAttribute -> {
+ assetAttribute.addMeta(
+ new MetaItem(RULE_STATE),
+ new MetaItem(STORE_DATA_POINTS)
+ );
+ if (agentLinker != null) {
+ assetAttribute.addMeta(agentLinker.get());
+ }
+ });
+ peopleCounterAsset.getAttribute("peopleCountOut").ifPresent(assetAttribute -> {
+ assetAttribute.addMeta(
+ new MetaItem(RULE_STATE),
+ new MetaItem(STORE_DATA_POINTS)
+ );
+ if (agentLinker != null) {
+ assetAttribute.addMeta(agentLinker.get());
+ }
+ });
+ peopleCounterAsset.getAttribute("peopleCountInMinute").ifPresent(assetAttribute -> {
+ assetAttribute.addMeta(
+ new MetaItem(RULE_STATE),
+ new MetaItem(STORE_DATA_POINTS)
+ );
+ if (agentLinker != null) {
+ assetAttribute.addMeta(agentLinker.get());
+ }
+ });
+ peopleCounterAsset.getAttribute("peopleCountOutMinute").ifPresent(assetAttribute -> {
+ assetAttribute.addMeta(
+ new MetaItem(RULE_STATE),
+ new MetaItem(STORE_DATA_POINTS)
+ );
+ if (agentLinker != null) {
+ assetAttribute.addMeta(agentLinker.get());
+ }
+ });
+ peopleCounterAsset.getAttribute("peopleCountTotal").ifPresent(assetAttribute -> {
+ assetAttribute.addMeta(
+ new MetaItem(RULE_STATE),
+ new MetaItem(STORE_DATA_POINTS)
+ );
+ if (agentLinker != null) {
+ assetAttribute.addMeta(agentLinker.get());
+ }
+ });
+ peopleCounterAsset.getAttribute("peopleCountGrowth").ifPresent(assetAttribute -> {
+ assetAttribute.addMeta(
+ new MetaItem(RULE_STATE),
+ new MetaItem(STORE_DATA_POINTS)
+ );
+ if (agentLinker != null) {
+ assetAttribute.addMeta(agentLinker.get());
+ }
+ });
return peopleCounterAsset;
}
@@ -560,10 +590,16 @@ public abstract class AbstractManagerSetup implements Setup {
Asset microphoneAsset = new Asset(name, MICROPHONE, area).addAttributes(
new AssetAttribute(AttributeType.LOCATION, location.toValue()).addMeta(SHOW_ON_DASHBOARD)
);
- microphoneAsset.getAttribute("microphoneLevel").ifPresent(assetAttribute -> assetAttribute.addMeta(
- new MetaItem(RULE_STATE),
- new MetaItem(STORE_DATA_POINTS)
- ));
+ microphoneAsset.getAttribute("microphoneLevel").ifPresent(assetAttribute -> {
+ assetAttribute.addMeta(
+ new MetaItem(RULE_STATE),
+ new MetaItem(STORE_DATA_POINTS)
+ );
+ if (agentLinker != null) {
+ assetAttribute.addMeta(agentLinker.get());
+ }
+ });
+
return microphoneAsset;
}
@@ -572,26 +608,51 @@ public abstract class AbstractManagerSetup implements Setup {
Asset soundEventAsset = new Asset(name, SOUND_EVENT, area).addAttributes(
new AssetAttribute(AttributeType.LOCATION, location.toValue()).addMeta(SHOW_ON_DASHBOARD)
);
- soundEventAsset.getAttribute("lastAggressionEvent").ifPresent(assetAttribute -> assetAttribute.addMeta(
- new MetaItem(RULE_STATE),
- new MetaItem(STORE_DATA_POINTS)
- ));
- soundEventAsset.getAttribute("lastGunshotEvent").ifPresent(assetAttribute -> assetAttribute.addMeta(
- new MetaItem(RULE_STATE),
- new MetaItem(STORE_DATA_POINTS)
- ));
- soundEventAsset.getAttribute("lastBreakingGlassEvent").ifPresent(assetAttribute -> assetAttribute.addMeta(
- new MetaItem(RULE_STATE),
- new MetaItem(STORE_DATA_POINTS)
- ));
- soundEventAsset.getAttribute("lastIntensityEvent").ifPresent(assetAttribute -> assetAttribute.addMeta(
- new MetaItem(RULE_STATE),
- new MetaItem(STORE_DATA_POINTS)
- ));
- soundEventAsset.getAttribute("lastEvent").ifPresent(assetAttribute -> assetAttribute.addMeta(
- new MetaItem(RULE_STATE),
- new MetaItem(STORE_DATA_POINTS)
- ));
+ soundEventAsset.getAttribute("lastAggressionEvent").ifPresent(assetAttribute -> {
+ assetAttribute.addMeta(
+ new MetaItem(RULE_STATE),
+ new MetaItem(STORE_DATA_POINTS)
+ );
+ if (agentLinker != null) {
+ assetAttribute.addMeta(agentLinker.get());
+ }
+ });
+ soundEventAsset.getAttribute("lastGunshotEvent").ifPresent(assetAttribute -> {
+ assetAttribute.addMeta(
+ new MetaItem(RULE_STATE),
+ new MetaItem(STORE_DATA_POINTS)
+ );
+ if (agentLinker != null) {
+ assetAttribute.addMeta(agentLinker.get());
+ }
+ });
+ soundEventAsset.getAttribute("lastBreakingGlassEvent").ifPresent(assetAttribute -> {
+ assetAttribute.addMeta(
+ new MetaItem(RULE_STATE),
+ new MetaItem(STORE_DATA_POINTS)
+ );
+ if (agentLinker != null) {
+ assetAttribute.addMeta(agentLinker.get());
+ }
+ });
+ soundEventAsset.getAttribute("lastIntensityEvent").ifPresent(assetAttribute -> {
+ assetAttribute.addMeta(
+ new MetaItem(RULE_STATE),
+ new MetaItem(STORE_DATA_POINTS)
+ );
+ if (agentLinker != null) {
+ assetAttribute.addMeta(agentLinker.get());
+ }
+ });
+ soundEventAsset.getAttribute("lastEvent").ifPresent(assetAttribute -> {
+ assetAttribute.addMeta(
+ new MetaItem(RULE_STATE),
+ new MetaItem(STORE_DATA_POINTS)
+ );
+ if (agentLinker != null) {
+ assetAttribute.addMeta(agentLinker.get());
+ }
+ });
return soundEventAsset;
}
diff --git a/manager/src/main/java/org/openremote/manager/setup/builtin/KeycloakDemoSetup.java b/manager/src/main/java/org/openremote/manager/setup/builtin/KeycloakDemoSetup.java
index e8ee12786..41329dcac 100644
--- a/manager/src/main/java/org/openremote/manager/setup/builtin/KeycloakDemoSetup.java
+++ b/manager/src/main/java/org/openremote/manager/setup/builtin/KeycloakDemoSetup.java
@@ -19,6 +19,7 @@
*/
package org.openremote.manager.setup.builtin;
+import org.cyclopsgroup.jcli.spi.Cli;
import org.keycloak.representations.idm.ClientRepresentation;
import org.openremote.container.Container;
import org.openremote.container.util.UniqueIdentifierGenerator;
@@ -29,6 +30,7 @@ import org.openremote.model.security.ClientRole;
import org.openremote.model.security.Tenant;
import org.openremote.model.security.User;
+import java.util.Arrays;
import java.util.logging.Logger;
import static org.openremote.model.Constants.KEYCLOAK_CLIENT_ID;
@@ -72,8 +74,14 @@ public class KeycloakDemoSetup extends AbstractKeycloakSetup {
tenantBuilding = createTenant("building", "Building");
tenantCity = createTenant("smartcity", "Smart City");
+ // Don't allow demo users to write assets
+ ClientRole[] demoUserRoles = Arrays.stream(REGULAR_USER_ROLES)
+ .filter(clientRole -> clientRole != ClientRole.WRITE_ASSETS)
+ .toArray(ClientRole[]::new);
+
+
// Users
- User testuser1 = createUser(MASTER_REALM, "testuser1", "testuser1", "DemoMaster", "DemoLast", null, true, REGULAR_USER_ROLES);
+ User testuser1 = createUser(MASTER_REALM, "testuser1", "testuser1", "DemoMaster", "DemoLast", null, true, container.isDevMode() ? REGULAR_USER_ROLES : demoUserRoles);
this.testuser1Id = testuser1.getId();
keycloakProvider.updateRoles(MASTER_REALM, testuser1Id, "account"); // Remove all roles for account client
User testuser2 = createUser(tenantBuilding.getRealm(), "testuser2", "testuser2", "DemoA2", "DemoLast", "[email protected]", true, new ClientRole[] {
@@ -83,32 +91,34 @@ public class KeycloakDemoSetup extends AbstractKeycloakSetup {
});
this.testuser2Id = testuser2.getId();
keycloakProvider.updateRoles(tenantBuilding.getRealm(), testuser2Id, "account"); // Remove all roles for account client
- User testuser3 = createUser(tenantBuilding.getRealm(), "testuser3", "testuser3", "DemoA3", "DemoLast", "[email protected]", true, REGULAR_USER_ROLES);
+ User testuser3 = createUser(tenantBuilding.getRealm(), "testuser3", "testuser3", "DemoA3", "DemoLast", "[email protected]", true, container.isDevMode() ? REGULAR_USER_ROLES : demoUserRoles);
this.testuser3Id = testuser3.getId();
keycloakProvider.updateRoles(tenantBuilding.getRealm(), testuser3Id, "account"); // Remove all roles for account client
- User buildingUser = createUser(tenantBuilding.getRealm(), "building", "building", "Building", "User", "[email protected]", true, REGULAR_USER_ROLES);
+ User buildingUser = createUser(tenantBuilding.getRealm(), "building", "building", "Building", "User", "[email protected]", true, demoUserRoles);
this.buildingUserId = buildingUser.getId();
keycloakProvider.updateRoles(tenantBuilding.getRealm(), buildingUserId, "account"); // Remove all roles for account client
- User smartCityUser = createUser(tenantCity.getRealm(), "smartcity", "smartcity", "Smart", "City", null, true, REGULAR_USER_ROLES);
+ User smartCityUser = createUser(tenantCity.getRealm(), "smartcity", "smartcity", "Smart", "City", null, true, demoUserRoles);
this.smartCityUserId = smartCityUser.getId();
keycloakProvider.updateRoles(tenantCity.getRealm(), smartCityUserId, "account"); // Remove all roles for account client
- /*
- * MQTT Client
- */
- ClientRepresentation mqttClient = new ClientRepresentation();
- String buildingMqttClientId = MqttBrokerService.MQTT_CLIENT_ID_PREFIX + UniqueIdentifierGenerator.generateId(tenantBuilding.getRealm());
- mqttClient.setClientId(buildingMqttClientId);
- mqttClient.setName("MQTT");
- mqttClient.setStandardFlowEnabled(false);
- mqttClient.setImplicitFlowEnabled(false);
- mqttClient.setDirectAccessGrantsEnabled(false);
- mqttClient.setServiceAccountsEnabled(true);
- mqttClient.setSecret(UniqueIdentifierGenerator.generateId(tenantBuilding.getRealm()));
- mqttClient = keycloakProvider.createClient(tenantBuilding.getRealm(), mqttClient);
+ if (container.isDevMode()) {
+ /*
+ * MQTT Client
+ */
+ ClientRepresentation mqttClient = new ClientRepresentation();
+ String buildingMqttClientId = MqttBrokerService.MQTT_CLIENT_ID_PREFIX + UniqueIdentifierGenerator.generateId(tenantBuilding.getRealm());
+ mqttClient.setClientId(buildingMqttClientId);
+ mqttClient.setName("MQTT");
+ mqttClient.setStandardFlowEnabled(false);
+ mqttClient.setImplicitFlowEnabled(false);
+ mqttClient.setDirectAccessGrantsEnabled(false);
+ mqttClient.setServiceAccountsEnabled(true);
+ mqttClient.setSecret(UniqueIdentifierGenerator.generateId(tenantBuilding.getRealm()));
+ mqttClient = keycloakProvider.createClient(tenantBuilding.getRealm(), mqttClient);
- // Add asset RW roles to service user
- User serviceUser = keycloakProvider.getClientServiceUser(tenantBuilding.getRealm(), mqttClient.getClientId());
- keycloakProvider.updateRoles(tenantBuilding.getRealm(), serviceUser.getId(), KEYCLOAK_CLIENT_ID, ClientRole.READ_ASSETS.getValue(), ClientRole.WRITE_ASSETS.getValue());
+ // Add asset RW roles to service user
+ User serviceUser = keycloakProvider.getClientServiceUser(tenantBuilding.getRealm(), mqttClient.getClientId());
+ keycloakProvider.updateRoles(tenantBuilding.getRealm(), serviceUser.getId(), KEYCLOAK_CLIENT_ID, ClientRole.READ_ASSETS.getValue(), ClientRole.WRITE_ASSETS.getValue(), ClientRole.WRITE_ATTRIBUTES.getValue());
+ }
}
}
diff --git a/manager/src/main/java/org/openremote/manager/setup/builtin/ManagerDemoSetup.java b/manager/src/main/java/org/openremote/manager/setup/builtin/ManagerDemoSetup.java
index d4f6f2e31..54f1f2c55 100644
--- a/manager/src/main/java/org/openremote/manager/setup/builtin/ManagerDemoSetup.java
+++ b/manager/src/main/java/org/openremote/manager/setup/builtin/ManagerDemoSetup.java
@@ -54,7 +54,7 @@ public class ManagerDemoSetup extends AbstractManagerSetup {
public static final int DEMO_RULE_STATES_APARTMENT_3 = 0;
public static final int DEMO_RULE_STATES_SMART_OFFICE = 1;
public static final int DEMO_RULE_STATES_SMART_BUILDING = DEMO_RULE_STATES_APARTMENT_1 + DEMO_RULE_STATES_APARTMENT_2 + DEMO_RULE_STATES_APARTMENT_3;
- public static final int DEMO_RULE_STATES_SMART_CITY = 45;
+ public static final int DEMO_RULE_STATES_SMART_CITY = 44;
public static final int DEMO_RULE_STATES_GLOBAL = DEMO_RULE_STATES_SMART_BUILDING + DEMO_RULE_STATES_SMART_OFFICE + DEMO_RULE_STATES_SMART_CITY;
public static final int DEMO_RULE_STATES_APARTMENT_1_WITH_SCENES = DEMO_RULE_STATES_APARTMENT_1 + 28;
public static final int DEMO_RULE_STATES_SMART_BUILDING_WITH_SCENES = DEMO_RULE_STATES_APARTMENT_1_WITH_SCENES + DEMO_RULE_STATES_APARTMENT_2 + DEMO_RULE_STATES_APARTMENT_3;
diff --git a/model/src/main/java/org/openremote/model/Constants.java b/model/src/main/java/org/openremote/model/Constants.java
index bda732c45..d876208f4 100644
--- a/model/src/main/java/org/openremote/model/Constants.java
+++ b/model/src/main/java/org/openremote/model/Constants.java
@@ -36,6 +36,7 @@ public interface Constants {
String WRITE_ADMIN_ROLE = "write:admin";
String WRITE_LOGS_ROLE = "write:logs";
String WRITE_ASSETS_ROLE = "write:assets";
+ String WRITE_ATTRIBUTES_ROLE = "write:attributes";
String WRITE_RULES_ROLE = "write:rules";
String AUTH_CONTEXT = "AUTH_CONTEXT";
int ACCESS_TOKEN_LIFESPAN_SECONDS = 60; // 1 minute
diff --git a/model/src/main/java/org/openremote/model/security/ClientRole.java b/model/src/main/java/org/openremote/model/security/ClientRole.java
index c989f82a5..df818d147 100644
--- a/model/src/main/java/org/openremote/model/security/ClientRole.java
+++ b/model/src/main/java/org/openremote/model/security/ClientRole.java
@@ -45,6 +45,7 @@ public enum ClientRole {
WRITE_ADMIN(Constants.WRITE_ADMIN_ROLE, "Write system settings, tenants, and users"),
WRITE_LOGS(Constants.WRITE_LOGS_ROLE, "Write log settings"),
WRITE_ASSETS(Constants.WRITE_ASSETS_ROLE, "Write asset data"),
+ WRITE_ATTRIBUTES(Constants.WRITE_ATTRIBUTES_ROLE, "Write attribute data"),
WRITE_RULES(Constants.WRITE_RULES_ROLE, "Write rulesets (NOTE: effectively super-user access!)"),
READ("read", "Read all data", new ClientRole[]{
@@ -62,6 +63,7 @@ public enum ClientRole {
WRITE_ADMIN,
WRITE_LOGS,
WRITE_ASSETS,
+ WRITE_ATTRIBUTES,
WRITE_RULES
});
diff --git a/ui/component/or-asset-tree/src/index.ts b/ui/component/or-asset-tree/src/index.ts
index 75053b631..e30ec1f3c 100644
--- a/ui/component/or-asset-tree/src/index.ts
+++ b/ui/component/or-asset-tree/src/index.ts
@@ -331,6 +331,7 @@ export class OrAssetTree extends subscribe(manager)(LitElement) {
}
];
const selectedAssetTypeOption = getAssetTypeOption(this._selectedAssetTypeOption)
+ const hasAssetWriteRole = manager.hasRole("write:assets");
return html`
<div id="header">
<div id="title-container">
@@ -339,8 +340,8 @@ export class OrAssetTree extends subscribe(manager)(LitElement) {
<div id="header-btns">
<or-input style="display: none;" ?hidden="${this._isReadonly() || !this.selectedIds || this.selectedIds.length === 0}" type="${InputType.BUTTON}" icon="content-copy" @click="${() => this._onCopyClicked()}"></or-input>
- <or-input ?hidden="${this._isReadonly() || !this.selectedIds || this.selectedIds.length === 0}" type="${InputType.BUTTON}" icon="delete" @click="${() => this._onDeleteClicked()}"></or-input>
- <or-input ?hidden="${this._isReadonly() || !this.selectedIds || this.selectedIds.length === 0}" type="${InputType.BUTTON}" icon="plus" @click="${() => this._onAddClicked()}"></or-input>
+ <or-input ?hidden="${this._isReadonly() || !hasAssetWriteRole || !this.selectedIds || this.selectedIds.length === 0}" type="${InputType.BUTTON}" icon="delete" @click="${() => this._onDeleteClicked()}"></or-input>
+ <or-input ?hidden="${this._isReadonly() || !hasAssetWriteRole || !this.selectedIds || this.selectedIds.length === 0}" type="${InputType.BUTTON}" icon="plus" @click="${() => this._onAddClicked()}"></or-input>
<or-input hidden type="${InputType.BUTTON}" icon="magnify" @click="${() => this._onSearchClicked()}"></or-input>
${getContentWithMenuTemplate(