Skip to content

Commit 65d5292

Browse files
chore: can assign a permset to a remote user
1 parent 9e476f1 commit 65d5292

File tree

3 files changed

+17
-26
lines changed

3 files changed

+17
-26
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -- CLEAN
2-
2+
tmp/
33
# use yarn by default, so ignore npm
44
package-lock.json
55

@@ -32,4 +32,4 @@ node_modules
3232

3333
# os specific files
3434
.DS_Store
35-
.idea
35+
.idea

src/commands/force/user/permset/assign.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
import * as os from 'os';
99
import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command';
10-
import { Aliases, Connection, Messages, User, AuthInfo, Org, UserFields, SfdxError } from '@salesforce/core';
10+
import { Aliases, Connection, Messages, Org, SfdxError, User } from '@salesforce/core';
11+
import { QueryResult } from 'jsforce';
1112

1213
Messages.importMessagesDirectory(__dirname);
1314
const messages = Messages.loadMessages('@salesforce/plugin-user', 'permset.assign');
@@ -54,19 +55,21 @@ export class UserPermsetAssignCommand extends SfdxCommand {
5455
} else {
5556
this.usernames = [this.org.getUsername()];
5657
}
58+
const connection: Connection = this.org.getConnection();
59+
const org = await Org.create({ connection });
5760

5861
for (const username of this.usernames) {
5962
// Convert any aliases to usernames
6063
const aliasOrUsername = (await Aliases.fetch(username)) || username;
61-
const connection: Connection = await Connection.create({
62-
authInfo: await AuthInfo.create({ username }),
63-
});
64-
const org = await Org.create({ connection });
6564
const user: User = await User.create({ org });
66-
const fields: UserFields = await user.retrieve(username);
65+
// get userId of whomever the permset will be assigned to via query to avoid AuthInfo if remote user
66+
const queryResult: QueryResult<{ Id: string }> = await connection.query(
67+
`SELECT Id FROM User WHERE Username='${username}'`
68+
);
69+
const userId = queryResult.records[0].Id;
6770

6871
try {
69-
await user.assignPermissionSets(fields.id, [this.flags.permsetname]);
72+
await user.assignPermissionSets(userId, [this.flags.permsetname]);
7073
this.successes.push({
7174
name: aliasOrUsername,
7275
value: this.flags.permsetname,
@@ -84,12 +87,10 @@ export class UserPermsetAssignCommand extends SfdxCommand {
8487

8588
this.print();
8689

87-
const result: Result = {
90+
return {
8891
successes: this.successes,
8992
failures: this.failures,
9093
};
91-
92-
return result;
9394
}
9495

9596
private print(): void {

test/commands/user/permset/assign.test.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,16 @@
66
*/
77

88
import { $$, expect, test } from '@salesforce/command/lib/test';
9-
import { Aliases, AuthInfo, Connection, Org, User } from '@salesforce/core';
10-
import { StubbedType, stubInterface, stubMethod } from '@salesforce/ts-sinon';
11-
import { MockTestOrgData } from '@salesforce/core/lib/testSetup';
9+
import { Aliases, Connection, Org, User } from '@salesforce/core';
10+
import { stubMethod } from '@salesforce/ts-sinon';
1211

1312
describe('force:user:permset:assign', () => {
14-
let authInfoStub: StubbedType<AuthInfo>;
15-
const testData = new MockTestOrgData();
16-
1713
async function prepareStubs(throws = false) {
18-
const authFields = await testData.getConfig();
19-
authInfoStub = stubInterface<AuthInfo>($$.SANDBOX, { getFields: () => authFields });
20-
21-
stubMethod($$.SANDBOX, AuthInfo, 'create').callsFake(async () => authInfoStub);
22-
stubMethod($$.SANDBOX, Connection, 'create').callsFake(async () => Connection.prototype);
14+
stubMethod($$.SANDBOX, Org.prototype, 'getConnection').returns(Connection.prototype);
15+
stubMethod($$.SANDBOX, Connection.prototype, 'query').resolves({ records: [{ Id: 1234567890 }] });
2316
stubMethod($$.SANDBOX, Org, 'create').callsFake(async () => Org.prototype);
2417
stubMethod($$.SANDBOX, Org.prototype, 'getUsername').returns('defaultusername@test.com');
2518
stubMethod($$.SANDBOX, User, 'create').callsFake(async () => User.prototype);
26-
stubMethod($$.SANDBOX, User.prototype, 'retrieve').resolves({
27-
id: '0052D0000043PawWWR',
28-
});
2919
if (throws) {
3020
stubMethod($$.SANDBOX, User.prototype, 'assignPermissionSets').throws(
3121
new Error('Permission set "abc" not found in target org. Do you need to push source?')

0 commit comments

Comments
 (0)