Skip to content

Commit

Permalink
Fix Asset and Device creation from backend (#252)
Browse files Browse the repository at this point in the history
Every modification of a Digital Twin (Asset or Device) through associated controllers is contextualized with the user who made the request to keep a trace in Kuzzle metadata.

This allows for example to know who has modified a metadata or link a new device to an asset by consulting history documents in assets-history collection.

When a request is made to those controllers from the backend, there is no user since it's the EmbeddedSDK executing the request and it was causing an error.

This PR fix that, when the EmbeddedSDK is used to execute one of those action, it will use the user if the sdk was contextualized with sdk.as method, otherwise the metadata will be set with the null user ID who represent the backend user.
  • Loading branch information
Aschen committed Jan 19, 2023
1 parent d792b04 commit 12d14f0
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 4 deletions.
6 changes: 6 additions & 0 deletions features/Asset/Controller.feature
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,9 @@ Feature: Asset Controller
Then I should receive a result matching:
| measures | [] |

Scenario: Create asset from backend side
When I successfully execute the action "tests":"createDigitalTwinFromBackend" with args:
| engineId | "engine-kuzzle" |
| body.reference | "foobar" |
Then The document "engine-kuzzle":"assets":"Container-foobar" exists
Then The document "engine-kuzzle":"devices":"DummyTemp-foobar" exists
5 changes: 4 additions & 1 deletion features/fixtures/application/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { Backend, KuzzleRequest } from "kuzzle";
import { DeviceManagerPlugin } from "../../../index";

import { DummyTempDecoder, DummyTempPositionDecoder } from "./decoders";
import { registerTestPipes } from "./testPipes";
import { registerTestPipes } from "./tests/pipes";
import { TestsController } from "./tests/controller";
import { containerAssetDefinition } from "./assets/Container";
import { warehouseAssetDefinition } from "./assets/Warehouse";

Expand Down Expand Up @@ -53,6 +54,8 @@ registerTestPipes(app);

app.plugin.use(deviceManager);

app.controller.use(new TestsController(app));

app.hook.register("request:onError", async (request: KuzzleRequest) => {
app.log.error(request.error);
});
Expand Down
45 changes: 45 additions & 0 deletions features/fixtures/application/tests/controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Backend, Controller, KuzzleRequest } from "kuzzle";
import { ApiAssetCreateRequest, ApiAssetCreateResult } from "lib/modules/asset";
import {
ApiDeviceCreateRequest,
ApiDeviceCreateResult,
} from "lib/modules/device";

export class TestsController extends Controller {
constructor(app: Backend) {
super(app);

this.definition = {
actions: {
createDigitalTwinFromBackend: {
handler: this.createDigitalTwinFromBackend,
},
},
};
}

async createDigitalTwinFromBackend(request: KuzzleRequest) {
const engineId = request.getString("engineId");
const reference = request.getBodyString("reference");

await this.app.sdk.query<ApiAssetCreateRequest, ApiAssetCreateResult>({
controller: "device-manager/assets",
action: "create",
engineId,
body: {
model: "Container",
reference,
},
});

await this.app.sdk.query<ApiDeviceCreateRequest, ApiDeviceCreateResult>({
controller: "device-manager/devices",
action: "create",
engineId,
body: {
model: "DummyTemp",
reference,
},
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
EventMeasureProcessBefore,
AssetContent,
DeviceContent,
} from "../../../index";
} from "../../../../index";

function checkEventWithDocument(app: Backend, event: string) {
app.pipe.register(event, async (payload) => {
Expand Down
8 changes: 7 additions & 1 deletion lib/modules/asset/AssetService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ export class AssetService {
}

private get impersonatedSdk() {
return (user: User) => this.sdk.as(user, { checkRights: false });
return (user: User) => {
if (user?._id) {
return this.sdk.as(user, { checkRights: false });
}

return this.sdk;
};
}

constructor(
Expand Down
8 changes: 7 additions & 1 deletion lib/modules/device/DeviceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ export class DeviceService {
}

private get impersonatedSdk() {
return (user: User) => this.sdk.as(user, { checkRights: false });
return (user: User) => {
if (user?._id) {
return this.sdk.as(user, { checkRights: false });
}

return this.sdk;
};
}

constructor(plugin: Plugin) {
Expand Down

0 comments on commit 12d14f0

Please sign in to comment.