Skip to content

Commit

Permalink
feat(roles): add specific roles for assets and devices (#365)
Browse files Browse the repository at this point in the history
* ci(doc): fix major version in CI doc step

* feat(roles): add new specific roles for assets and devices

* refactor(asset roles): removed roleAssetDeviceAssociation as it was a duplicate of device role

* fix(roles): Move update right from creation role to configuration role as it updates only metadata

* Update lib/modules/device/roles/specificRoles/RoleDevicesAssetAssociation.ts

typo

Co-authored-by: Ricky <[email protected]>

* Update lib/modules/device/roles/specificRoles/RoleDevicesConfiguration.ts

typo

Co-authored-by: Ricky <[email protected]>

---------

Co-authored-by: Florian Maunier <[email protected]>
Co-authored-by: Ricky <[email protected]>
  • Loading branch information
3 people authored Aug 30, 2024
1 parent 8a14207 commit a01982c
Show file tree
Hide file tree
Showing 12 changed files with 183 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/modules/asset/AssetModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { RoleAssetsAdmin } from "./roles/RoleAssetsAdmin";
import { RoleAssetsReader } from "./roles/RoleAssetsReader";
import { RoleAssetsGroupsAdmin } from "./roles/RoleAssetsGroupsAdmin";
import { RoleAssetsGroupsReader } from "./roles/RoleAssetsGroupsReader";
import * as specificRoles from "./roles/specificRoles";

export class AssetModule extends Module {
private assetService: AssetService;
Expand All @@ -30,7 +31,12 @@ export class AssetModule extends Module {
RoleAssetsAdmin.definition;
this.plugin.imports.roles[RoleAssetsReader.name] =
RoleAssetsReader.definition;

for (const role in specificRoles) {
if (specificRoles[role].name && specificRoles[role].definition) {
this.plugin.imports.roles[specificRoles[role].name] =
specificRoles[role].definition;
}
}
this.plugin.imports.roles[RoleAssetsGroupsAdmin.name] =
RoleAssetsGroupsAdmin.definition;
this.plugin.imports.roles[RoleAssetsGroupsReader.name] =
Expand Down
18 changes: 18 additions & 0 deletions lib/modules/asset/roles/specificRoles/RoleAssetsConfiguration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { KuzzleRole } from "../../../shared/types/KuzzleRole";

/**
* This role allows to configurate an asset metadata.
*
* It's a tenant role.
*
*/
export const RoleAssetsConfiguration: KuzzleRole = {
definition: {
controllers: {
"device-manager/assets": {
actions: { replaceMetadata: true, update: true },
},
},
},
name: "assets.configuration",
};
22 changes: 22 additions & 0 deletions lib/modules/asset/roles/specificRoles/RoleAssetsCreation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { KuzzleRole } from "../../../shared/types/KuzzleRole";

/**
* This role allows to create, update and delete an asset.
*
* It's a tenant role.
*
*/
export const RoleAssetsCreation: KuzzleRole = {
definition: {
controllers: {
"device-manager/assets": {
actions: {
create: true,
delete: true,
upsert: true,
},
},
},
},
name: "assets.creation",
};
2 changes: 2 additions & 0 deletions lib/modules/asset/roles/specificRoles/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./RoleAssetsConfiguration";
export * from "./RoleAssetsCreation";
8 changes: 7 additions & 1 deletion lib/modules/device/DeviceModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DeviceService } from "./DeviceService";
import { RoleDevicesAdmin } from "./roles/RoleDevicesAdmin";
import { RoleDevicesPlatformAdmin } from "./roles/RoleDevicesPlatformAdmin";
import { RoleDevicesReader } from "./roles/RoleDevicesReader";

import * as specificRoles from "./roles/specificRoles";
export class DeviceModule extends Module {
private deviceService: DeviceService;
private deviceController: DevicesController;
Expand All @@ -26,5 +26,11 @@ export class DeviceModule extends Module {
RoleDevicesPlatformAdmin.definition;
this.plugin.imports.roles[RoleDevicesReader.name] =
RoleDevicesReader.definition;
for (const role in specificRoles) {
if (specificRoles[role].name && specificRoles[role].definition) {
this.plugin.imports.roles[specificRoles[role].name] =
specificRoles[role].definition;
}
}
}
}
39 changes: 39 additions & 0 deletions lib/modules/device/DeviceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,45 @@ export class DeviceService extends BaseService {
deviceId,
);
}
/**
* Replace a device metadata
*/
public async replaceMetadata(
engineId: string,
deviceId: string,
metadata: Metadata,
request: KuzzleRequest,
): Promise<KDocument<DeviceContent>> {
const device = await this.get(engineId, deviceId, request);

for (const key in metadata) {
if (key in device._source.metadata) {
device._source.metadata[key] = metadata[key];
}
}

const updatedPayload = await this.app.trigger<EventDeviceUpdateBefore>(
"device-manager:device:update:before",
{ device: device, metadata },
);

const updatedDevice = await this.sdk.document.replace<DeviceContent>(
engineId,
InternalCollection.DEVICES,
deviceId,
updatedPayload.device._source,
);

await this.app.trigger<EventDeviceUpdateAfter>(
"device-manager:device:update:after",
{
device: updatedDevice,
metadata: updatedPayload.metadata,
},
);

return updatedDevice;
}

/**
* Update or Create an device metadata
Expand Down
25 changes: 25 additions & 0 deletions lib/modules/device/DevicesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
ApiDeviceUpdateResult,
ApiDeviceGetMeasuresResult,
ApiDeviceUpsertResult,
ApiDeviceMetadataReplaceResult,
} from "./types/DeviceApi";

export class DevicesController {
Expand Down Expand Up @@ -63,6 +64,15 @@ export class DevicesController {
{ path: "device-manager/:engineId/devices/:_id", verb: "post" },
],
},
replaceMetadata: {
handler: this.replaceMetadata.bind(this),
http: [
{
path: "device-manager/:engineId/devices/:_id/metadata",
verb: "patch",
},
],
},
search: {
handler: this.search.bind(this),
http: [
Expand Down Expand Up @@ -196,7 +206,22 @@ export class DevicesController {

return DeviceSerializer.serialize(updatedDevice);
}
async replaceMetadata(
request: KuzzleRequest,
): Promise<ApiDeviceMetadataReplaceResult> {
const deviceId = request.getId();
const engineId = request.getString("engineId");
const metadata = request.getBodyObject("metadata");

const updatedDevice = await this.deviceService.replaceMetadata(
engineId,
deviceId,
metadata,
request,
);

return DeviceSerializer.serialize(updatedDevice);
}
async delete(request: KuzzleRequest): Promise<ApiDeviceDeleteResult> {
const engineId = request.getString("engineId");
const deviceId = request.getId();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { KuzzleRole } from "../../../shared/types/KuzzleRole";

/**
* This role allows to associate assets and devices.
*
* It's a tenant role.
*
*/
export const RoleDevicesAssetAssociation: KuzzleRole = {
definition: {
controllers: {
"device-manager/devices": {
actions: { linkAsset: true, unlinkAsset: true },
},
},
},
name: "devices.assetAssociation",
};
18 changes: 18 additions & 0 deletions lib/modules/device/roles/specificRoles/RoleDevicesConfiguration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { KuzzleRole } from "../../../shared/types/KuzzleRole";

/**
* This role allows to configure a device metadata.
*
* It's a tenant role.
*
*/
export const RoleDevicesConfiguration: KuzzleRole = {
definition: {
controllers: {
"device-manager/devices": {
actions: { replaceMetadata: true, update: true },
},
},
},
name: "devices.configuration",
};
22 changes: 22 additions & 0 deletions lib/modules/device/roles/specificRoles/RoleDevicesCreation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { KuzzleRole } from "../../../shared/types/KuzzleRole";

/**
* This role allows to create, update and delete a device.
*
* It's a tenant role.
*
*/
export const RoleDevicesCreation: KuzzleRole = {
definition: {
controllers: {
"device-manager/devices": {
actions: {
create: true,
delete: true,
upsert: true,
},
},
},
},
name: "devices.creation",
};
3 changes: 3 additions & 0 deletions lib/modules/device/roles/specificRoles/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./RoleDevicesAssetAssociation";
export * from "./RoleDevicesConfiguration";
export * from "./RoleDevicesCreation";
2 changes: 2 additions & 0 deletions lib/modules/device/types/DeviceApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export interface ApiDeviceUpdateRequest extends DevicesControllerRequest {
}
export type ApiDeviceUpdateResult = KDocument<DeviceContent>;

export type ApiDeviceMetadataReplaceResult = KDocument<DeviceContent>;

export interface ApiDeviceUpsertRequest extends DevicesControllerRequest {
action: "upsert";

Expand Down

0 comments on commit a01982c

Please sign in to comment.