From a4ef96374d1dd0f2c772b3153ad3a209dcf380bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Berkefeld?= Date: Thu, 23 Mar 2023 13:03:23 +0100 Subject: [PATCH] #139: catch no roles situation --- lib/metadataTypes/User.js | 63 ++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 24 deletions(-) diff --git a/lib/metadataTypes/User.js b/lib/metadataTypes/User.js index 998d9b48a..529b6249e 100644 --- a/lib/metadataTypes/User.js +++ b/lib/metadataTypes/User.js @@ -413,14 +413,22 @@ class User extends MetadataType { // delete global roles from user that were not in the c__RoleNamesGlobal array / Roles.Role const deletedRoles = []; const deletedRoleNames = []; + if (item.after?.Roles?.Role && !Array.isArray(item.after.Roles.Role)) { + item.after.Roles.Role = [item.after.Roles.Role]; + } if (item.before?.Roles?.Role) { + if (!Array.isArray(item.before.Roles.Role)) { + item.before.Roles.Role = [item.before.Roles.Role]; + } for (const oldRole of item.before.Roles.Role) { // check if oldRole is missing in list of new roles --> removing role let oldRoleFound = false; - for (const newRole of item.after.Roles.Role) { - if (newRole.ObjectID == oldRole.ObjectID) { - oldRoleFound = true; - break; + if (item.after.Roles?.Role) { + for (const newRole of item.after.Roles.Role) { + if (newRole.ObjectID == oldRole.ObjectID) { + oldRoleFound = true; + break; + } } } if (!oldRoleFound && !oldRole.Name.startsWith('Individual role for ')) { @@ -431,31 +439,33 @@ class User extends MetadataType { } } const addedRoleNames = []; - for (const newRole of item.after.Roles.Role) { - // check if newRole is missing in list of old roles --> adding role - let roleAlreadyExisting = false; - if (item.before?.Roles?.Role) { - for (const oldRole of item.before.Roles.Role) { - if (newRole.ObjectID == oldRole.ObjectID) { - roleAlreadyExisting = true; - break; + if (item.after?.Roles?.Role) { + for (const newRole of item.after.Roles.Role) { + // check if newRole is missing in list of old roles --> adding role + let roleAlreadyExisting = false; + if (item.before?.Roles?.Role) { + for (const oldRole of item.before.Roles.Role) { + if (newRole.ObjectID == oldRole.ObjectID) { + roleAlreadyExisting = true; + break; + } } } + if (!roleAlreadyExisting) { + addedRoleNames.push(newRole.Name); + // Note: no AssignmentConfigurations property is needed to ADD global roles + } } - if (!roleAlreadyExisting) { - addedRoleNames.push(newRole.Name); - // Note: no AssignmentConfigurations property is needed to ADD global roles + if (addedRoleNames.length) { + Util.logger.info( + Util.getGrayMsg( + ` - adding role-assignment (${item.after.CustomerKey} / ${ + item.after.Name + }): ${addedRoleNames.join(', ')}` + ) + ); } } - if (addedRoleNames.length) { - Util.logger.info( - Util.getGrayMsg( - ` - adding role-assignment (${item.after.CustomerKey} / ${ - item.after.Name - }): ${addedRoleNames.join(', ')}` - ) - ); - } if (deletedRoles.length) { Util.logger.info( Util.getGrayMsg( @@ -465,6 +475,11 @@ class User extends MetadataType { ) ); // add deleted roles to payload with IsDelete=true + if (!item.after.Roles) { + item.after.Roles = { Role: [] }; + } else if (!item.after.Roles.Role) { + item.after.Roles.Role = []; + } item.after.Roles.Role.push( ...deletedRoles.map((role) => this._getRoleObjectForDeploy(