@@ -4,12 +4,13 @@ import {
44 PluginContext ,
55 PluginImplementationError ,
66} from "kuzzle" ;
7- import { snakeCase } from "../../modules/shared" ;
87
98import { Decoder , DecoderContent } from "../../modules/decoder" ;
9+ import { DeviceManagerPlugin } from ".." ;
1010
1111export 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}
0 commit comments