Skip to content

Commit

Permalink
Merge pull request #49 from salesforcecli/sm/fix-permset-assign-user-…
Browse files Browse the repository at this point in the history
…not-found

Sm/fix permset assign user not found
  • Loading branch information
WillieRuemmele authored Feb 18, 2021
2 parents 4e101af + 0509fda commit 08cd7e5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
15 changes: 6 additions & 9 deletions src/commands/force/user/permset/assign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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<Result> {
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,
Expand Down
4 changes: 2 additions & 2 deletions test/commands/user/permset/assign.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('force:user:permset:assign', () => {
stubMethod($$.SANDBOX, User.prototype, 'assignPermissionSets').resolves();
}

stubMethod($$.SANDBOX, Aliases, 'fetch').withArgs('[email protected]').resolves('testAlias');
stubMethod($$.SANDBOX, Aliases, 'fetch').withArgs('testAlias').resolves('[email protected]');
}

test
Expand All @@ -36,7 +36,7 @@ describe('force:user:permset:assign', () => {
'force:user:permset:assign',
'--json',
'--onbehalfof',
'[email protected], [email protected]',
'testAlias, [email protected]',
'--permsetname',
'DreamHouse',
])
Expand Down
1 change: 1 addition & 0 deletions test/df17AppBuilding
Submodule df17AppBuilding added at 505722

0 comments on commit 08cd7e5

Please sign in to comment.