From 1a11a1f24d332b51b7af15bcc4d9af5773755099 Mon Sep 17 00:00:00 2001 From: mshanemc Date: Fri, 12 Feb 2021 16:01:35 -0600 Subject: [PATCH 1/2] fix: line 63 uses resolved username NOT alias --- src/commands/force/user/permset/assign.ts | 15 ++++++--------- test/df17AppBuilding | 1 + 2 files changed, 7 insertions(+), 9 deletions(-) create mode 160000 test/df17AppBuilding diff --git a/src/commands/force/user/permset/assign.ts b/src/commands/force/user/permset/assign.ts index de8ac0b5..ece0581e 100644 --- a/src/commands/force/user/permset/assign.ts +++ b/src/commands/force/user/permset/assign.ts @@ -8,7 +8,6 @@ import * as os from 'os'; import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command'; import { Aliases, Connection, Messages, Org, SfdxError, User } from '@salesforce/core'; -import { QueryResult } from 'jsforce'; Messages.importMessagesDirectory(__dirname); const messages = Messages.loadMessages('@salesforce/plugin-user', 'permset.assign'); @@ -43,29 +42,27 @@ export class UserPermsetAssignCommand extends SfdxCommand { description: messages.getMessage('flags.onBehalfOf'), }), }; - private usernames: string[]; private readonly successes: SuccessMsg[] = []; private readonly failures: FailureMsg[] = []; public async run(): Promise { try { - this.usernames = (this.flags.onbehalfof as string[]) ?? [this.org.getUsername()]; + const aliasOrUsernames = (this.flags.onbehalfof as string[]) ?? [this.org.getUsername()]; const connection: Connection = this.org.getConnection(); const org = await Org.create({ connection }); - for (const username of this.usernames) { - // Convert any aliases to usernames - const aliasOrUsername = (await Aliases.fetch(username)) || username; + for (const aliasOrUsername of aliasOrUsernames) { + // Attempt to convert any aliases to usernames. Not found alias will be **assumed** to be a username + const username = (await Aliases.fetch(aliasOrUsername)) || aliasOrUsername; const user: User = await User.create({ org }); // get userId of whomever the permset will be assigned to via query to avoid AuthInfo if remote user - const queryResult: QueryResult<{ Id: string }> = await connection.query( + const queryResult = await connection.singleRecordQuery<{ Id: string }>( `SELECT Id FROM User WHERE Username='${username}'` ); - const userId = queryResult.records[0].Id; try { - await user.assignPermissionSets(userId, [this.flags.permsetname]); + await user.assignPermissionSets(queryResult.Id, [this.flags.permsetname]); this.successes.push({ name: aliasOrUsername, value: this.flags.permsetname as string, diff --git a/test/df17AppBuilding b/test/df17AppBuilding new file mode 160000 index 00000000..505722ec --- /dev/null +++ b/test/df17AppBuilding @@ -0,0 +1 @@ +Subproject commit 505722ec16967c0d41296a0a2c47b6e16d88a771 From 0ff534462c91039e523d863562d67dd172f1f589 Mon Sep 17 00:00:00 2001 From: mshanemc Date: Fri, 12 Feb 2021 16:15:17 -0600 Subject: [PATCH 2/2] test: run onbehalfof with 1 username, 1 alias --- test/commands/user/permset/assign.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/commands/user/permset/assign.test.ts b/test/commands/user/permset/assign.test.ts index 7781ca02..6483a44e 100644 --- a/test/commands/user/permset/assign.test.ts +++ b/test/commands/user/permset/assign.test.ts @@ -24,7 +24,7 @@ describe('force:user:permset:assign', () => { stubMethod($$.SANDBOX, User.prototype, 'assignPermissionSets').resolves(); } - stubMethod($$.SANDBOX, Aliases, 'fetch').withArgs('testUser1@test.com').resolves('testAlias'); + stubMethod($$.SANDBOX, Aliases, 'fetch').withArgs('testAlias').resolves('testUser1@test.com'); } test @@ -36,7 +36,7 @@ describe('force:user:permset:assign', () => { 'force:user:permset:assign', '--json', '--onbehalfof', - 'testUser1@test.com, testUser2@test.com', + 'testAlias, testUser2@test.com', '--permsetname', 'DreamHouse', ])