Skip to content

Commit c274bc0

Browse files
authored
Merge pull request #264 from kuzzleio/2.1.0-proposal
# [2.1.0](https://github.com/kuzzleio/kuzzle-device-manager/releases/tag/2.1.0) (2023-02-07) #### Bug fixes - [ [#263](#263) ] Imports roles, profiles and users before Kuzzle starts ([Aschen](https://github.com/Aschen)) #### New features - [ [#262](#262) ] Add action to directly send formated measure a device ([Aschen](https://github.com/Aschen)) ---
2 parents e8e4dc1 + 2f41918 commit c274bc0

25 files changed

+618
-270
lines changed
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
Feature: Device Manager Decoders
22

33
Scenario: Creates default roles, profiles and users
4-
Then I am able to get a role with id "payload_gateway.dummy_temp"
5-
Then I am able to get a role with id "payload_gateway.dummy_temp_position"
6-
Then I am able to get a profile with id "payload_gateway.dummy_temp"
7-
Then I am able to get a profile with id "payload_gateway.dummy_temp_position"
4+
Then I am able to get a role with id "payload_gateway"
85
Then I am able to get a profile with id "payload_gateway"
9-
Then The user "payload_gateway.dummy_temp" exists
10-
Then The user "payload_gateway.dummy_temp_position" exists
116
Then The user "payload_gateway" exists

lib/core/DeviceManagerPlugin.ts

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
ModelModule,
2929
modelsMappings,
3030
} from "../modules/model";
31-
import { lock, KuzzleRole } from "../modules/shared";
31+
import { lock } from "../modules/shared";
3232

3333
import { DeviceManagerConfiguration } from "./DeviceManagerConfiguration";
3434
import { DeviceManagerEngine } from "./DeviceManagerEngine";
@@ -38,7 +38,6 @@ import { ModelsRegister } from "./registers/ModelsRegister";
3838

3939
export class DeviceManagerPlugin extends Plugin {
4040
public config: DeviceManagerConfiguration;
41-
public roles: KuzzleRole[] = [];
4241

4342
private deviceManagerEngine: DeviceManagerEngine;
4443
private adminConfigManager: ConfigManager;
@@ -160,7 +159,7 @@ export class DeviceManagerPlugin extends Plugin {
160159

161160
constructor() {
162161
super({
163-
kuzzleVersion: ">=2.19.5 <3",
162+
kuzzleVersion: ">=2.20.2 <3",
164163
});
165164

166165
/* eslint-disable sort-keys */
@@ -172,6 +171,12 @@ export class DeviceManagerPlugin extends Plugin {
172171
"generic:document:beforeDelete": [],
173172
};
174173
this.hooks = {};
174+
this.imports = {
175+
roles: {},
176+
users: {},
177+
profiles: {},
178+
onExistingUsers: "skip",
179+
};
175180

176181
this.config = {
177182
ignoreStartupErrors: false,
@@ -244,7 +249,7 @@ export class DeviceManagerPlugin extends Plugin {
244249
await this.measureModule.init();
245250
await this.modelModule.init();
246251

247-
this.decodersRegister.init(this.context);
252+
this.decodersRegister.init(this, this.context);
248253
this.modelsRegister.init(this);
249254

250255
this.adminConfigManager = new ConfigManager(this, {
@@ -282,21 +287,7 @@ export class DeviceManagerPlugin extends Plugin {
282287
this.deviceManagerEngine
283288
);
284289

285-
this.hooks["kuzzle:state:live"] = async () => {
286-
try {
287-
await this.decodersRegister.createDefaultRights();
288-
await this.createDefaultRoles();
289-
} catch (error) {
290-
if (this.config.ignoreStartupErrors) {
291-
this.context.log.warn(
292-
`WARNING: An error occured during plugin initialization: ${error.message}`
293-
);
294-
} else {
295-
throw error;
296-
}
297-
}
298-
};
299-
290+
this.decodersRegister.registerDefaultRights();
300291
this.decodersRegister.printDecoders();
301292

302293
try {
@@ -318,18 +309,6 @@ export class DeviceManagerPlugin extends Plugin {
318309
}
319310
}
320311

321-
private async createDefaultRoles() {
322-
const promises = [];
323-
324-
for (const role of this.roles) {
325-
promises.push(
326-
this.sdk.security.createOrReplaceRole(role.name, role.definition)
327-
);
328-
}
329-
330-
await Promise.all(promises);
331-
}
332-
333312
/**
334313
* Initialize the administration index of the plugin
335314
*/

lib/core/registers/DecodersRegister.ts

Lines changed: 24 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import {
44
PluginContext,
55
PluginImplementationError,
66
} from "kuzzle";
7-
import { snakeCase } from "../../modules/shared";
87

98
import { Decoder, DecoderContent } from "../../modules/decoder";
9+
import { DeviceManagerPlugin } from "..";
1010

1111
export class DecodersRegister {
1212
private context: PluginContext;
13+
private plugin: DeviceManagerPlugin;
1314

1415
/**
1516
* Map<deviceModel, Decoder>
@@ -24,7 +25,8 @@ export class DecodersRegister {
2425
return Array.from(this._decoders.values());
2526
}
2627

27-
init(context: PluginContext) {
28+
init(plugin: DeviceManagerPlugin, context: PluginContext) {
29+
this.plugin = plugin;
2830
this.context = context;
2931
}
3032

@@ -88,117 +90,50 @@ export class DecodersRegister {
8890
}
8991

9092
/**
91-
* Creates default roles, profiles and users associated to the generated actions
93+
* Register default role, profile and user associated to the generated actions
9294
* in the Payload controller.
93-
*
94-
* This method never returns a rejected promise.
9595
*/
96-
async createDefaultRights() {
96+
registerDefaultRights() {
9797
if (this.decoders.length === 0) {
9898
return;
9999
}
100100

101-
await this.createDefaultRoles();
102-
103-
await this.createDefaultProfiles();
101+
this.registerDefaultRole();
104102

105-
await this.createDefaultUsers();
103+
this.registerDefaultProfile();
106104

107-
this.context.log.info(
108-
"Default rights for payload controller has been registered."
109-
);
105+
this.registerDefaultUser();
110106
}
111107

112-
private async createDefaultUsers() {
113-
const promises = [];
108+
private registerDefaultUser() {
114109
const gatewayUser = {
115110
content: {
116-
profileIds: [],
111+
profileIds: ["payload_gateway"],
117112
},
118113
};
119114

120-
for (const decoder of this.decoders) {
121-
const userId = `payload_gateway.${snakeCase(decoder.action)}`;
122-
const user = {
123-
content: {
124-
// each created user has only the profile of the same name
125-
profileIds: [userId],
126-
},
127-
};
128-
129-
gatewayUser.content.profileIds.push(userId);
130-
131-
promises.push(
132-
this.sdk.security.createUser(userId, user).catch((error) => {
133-
if (error.id !== "security.user.already_exists") {
134-
throw error;
135-
}
136-
return this.sdk.security.updateUser(userId, user);
137-
})
138-
);
139-
}
140-
141-
promises.push(
142-
this.sdk.security
143-
.createUser("payload_gateway", gatewayUser)
144-
.catch((error) => {
145-
if (error.id !== "security.user.already_exists") {
146-
throw error;
147-
}
148-
return this.sdk.security.updateUser("payload_gateway", gatewayUser);
149-
})
150-
);
151-
152-
await Promise.all(promises);
115+
this.plugin.imports.users.payload_gateway = gatewayUser;
153116
}
154117

155-
private async createDefaultProfiles() {
156-
const promises = [];
118+
private registerDefaultProfile() {
157119
const gatewayProfile = {
158-
policies: [],
120+
policies: [{ roleId: "payload_gateway" }],
159121
};
160122

161-
for (const decoder of this.decoders) {
162-
const profileId = `payload_gateway.${snakeCase(decoder.action)}`;
163-
const profile = {
164-
// each created profile has only the role of the same name
165-
policies: [{ roleId: profileId }],
166-
};
167-
168-
gatewayProfile.policies.push({ roleId: profileId });
169-
promises.push(
170-
this.sdk.security.createOrReplaceProfile(profileId, profile)
171-
);
172-
}
173-
174-
promises.push(
175-
this.sdk.security.createOrReplaceProfile(
176-
"payload_gateway",
177-
gatewayProfile
178-
)
179-
);
180-
181-
await Promise.all(promises);
123+
this.plugin.imports.profiles.payload_gateway = gatewayProfile;
182124
}
183125

184-
private async createDefaultRoles() {
185-
const promises = [];
186-
187-
for (const decoder of this.decoders) {
188-
const roleId = `payload_gateway.${snakeCase(decoder.action)}`;
189-
const role = {
190-
controllers: {
191-
"device-manager/payloads": {
192-
actions: {
193-
[decoder.action]: true,
194-
},
126+
private registerDefaultRole() {
127+
const role = {
128+
controllers: {
129+
"device-manager/payloads": {
130+
actions: {
131+
"*": true,
195132
},
196133
},
197-
};
198-
199-
promises.push(this.sdk.security.createOrReplaceRole(roleId, role));
200-
}
134+
},
135+
};
201136

202-
await Promise.all(promises);
137+
this.plugin.imports.roles.payload_gateway = role;
203138
}
204139
}

lib/modules/asset/AssetModule.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ export class AssetModule extends Module {
1818

1919
this.plugin.api["device-manager/assets"] = this.assetController.definition;
2020

21-
this.plugin.roles.push(RoleAssetsAdmin);
22-
this.plugin.roles.push(RoleAssetsReader);
21+
this.plugin.imports.roles[RoleAssetsAdmin.name] =
22+
RoleAssetsAdmin.definition;
23+
this.plugin.imports.roles[RoleAssetsReader.name] =
24+
RoleAssetsReader.definition;
2325
}
2426
}

lib/modules/decoder/DecoderModule.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ export class DecoderModule extends Module {
3434
this.plugin.api["device-manager/payloads"] =
3535
this.payloadsController.definition;
3636

37-
this.plugin.roles.push(RoleDecodersAdmin);
38-
this.plugin.roles.push(RolePayloadsAll);
37+
this.plugin.imports.roles[RoleDecodersAdmin.name] =
38+
RoleDecodersAdmin.definition;
39+
this.plugin.imports.roles[RolePayloadsAll.name] =
40+
RolePayloadsAll.definition;
3941
}
4042
}

0 commit comments

Comments
 (0)