Skip to content

Commit

Permalink
chore: can assign a permset to a remote user
Browse files Browse the repository at this point in the history
  • Loading branch information
WillieRuemmele committed Jan 21, 2021
1 parent 9e476f1 commit 65d5292
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 26 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -- CLEAN

tmp/
# use yarn by default, so ignore npm
package-lock.json

Expand Down Expand Up @@ -32,4 +32,4 @@ node_modules

# os specific files
.DS_Store
.idea
.idea
21 changes: 11 additions & 10 deletions src/commands/force/user/permset/assign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

import * as os from 'os';
import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command';
import { Aliases, Connection, Messages, User, AuthInfo, Org, UserFields, SfdxError } from '@salesforce/core';
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 @@ -54,19 +55,21 @@ export class UserPermsetAssignCommand extends SfdxCommand {
} else {
this.usernames = [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;
const connection: Connection = await Connection.create({
authInfo: await AuthInfo.create({ username }),
});
const org = await Org.create({ connection });
const user: User = await User.create({ org });
const fields: UserFields = await user.retrieve(username);
// 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(
`SELECT Id FROM User WHERE Username='${username}'`
);
const userId = queryResult.records[0].Id;

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

this.print();

const result: Result = {
return {
successes: this.successes,
failures: this.failures,
};

return result;
}

private print(): void {
Expand Down
18 changes: 4 additions & 14 deletions test/commands/user/permset/assign.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,16 @@
*/

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

describe('force:user:permset:assign', () => {
let authInfoStub: StubbedType<AuthInfo>;
const testData = new MockTestOrgData();

async function prepareStubs(throws = false) {
const authFields = await testData.getConfig();
authInfoStub = stubInterface<AuthInfo>($$.SANDBOX, { getFields: () => authFields });

stubMethod($$.SANDBOX, AuthInfo, 'create').callsFake(async () => authInfoStub);
stubMethod($$.SANDBOX, Connection, 'create').callsFake(async () => Connection.prototype);
stubMethod($$.SANDBOX, Org.prototype, 'getConnection').returns(Connection.prototype);
stubMethod($$.SANDBOX, Connection.prototype, 'query').resolves({ records: [{ Id: 1234567890 }] });
stubMethod($$.SANDBOX, Org, 'create').callsFake(async () => Org.prototype);
stubMethod($$.SANDBOX, Org.prototype, 'getUsername').returns('[email protected]');
stubMethod($$.SANDBOX, User, 'create').callsFake(async () => User.prototype);
stubMethod($$.SANDBOX, User.prototype, 'retrieve').resolves({
id: '0052D0000043PawWWR',
});
if (throws) {
stubMethod($$.SANDBOX, User.prototype, 'assignPermissionSets').throws(
new Error('Permission set "abc" not found in target org. Do you need to push source?')
Expand Down

0 comments on commit 65d5292

Please sign in to comment.