Skip to content

Commit

Permalink
Merge pull request #26 from salesforcecli/wr/removeProfileQuery
Browse files Browse the repository at this point in the history
fix: remove ProfileId query when we only need User Id
  • Loading branch information
mshanemc authored Jan 29, 2021
2 parents 67395c3 + 85af8dc commit 49af6de
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 10 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [1.0.12](https://github.com/salesforcecli/plugin-user/compare/v1.0.8...v1.0.12) (2021-01-29)


### Bug Fixes

* 1.0.11 ([450b6ee](https://github.com/salesforcecli/plugin-user/commit/450b6ee0837133d5629858cc3e4fe8c242b29add))
* add safety net around passwordGenerate in var args ([021a3f4](https://github.com/salesforcecli/plugin-user/commit/021a3f47baea85bddfbd6c04b901bb3a232af953))
* assign permset to new user ([2fde9ba](https://github.com/salesforcecli/plugin-user/commit/2fde9ba5d4f7a6336152d1879daeaccf2003ee89))
* bump to 1.0.9 to release ([88ce843](https://github.com/salesforcecli/plugin-user/commit/88ce8434ddd5b1f30dcf5de0cc4c7d40b579d9fd))
* code review 1 ([cb90a86](https://github.com/salesforcecli/plugin-user/commit/cb90a86eeac9c68d9052c9744f48a9642e8f9bdc))
* error message for display + accessToken ([3464ab3](https://github.com/salesforcecli/plugin-user/commit/3464ab3d697eb30f2f58186aeeaa59fe043ce23c))
* handled false generate(p|P)assword scenario ([1b9cc3c](https://github.com/salesforcecli/plugin-user/commit/1b9cc3c189cb9ab53ea9f4ec00f90b3dd396941d))
* save profileName and password to the authfile for the new user ([4b0b78d](https://github.com/salesforcecli/plugin-user/commit/4b0b78d41574fa30a53b4a741fa46ae3273fbfb4))
* update help for create, display, list ([f04b29e](https://github.com/salesforcecli/plugin-user/commit/f04b29e3dcf78bd7c7bdea0045e6ce41851f3795))
* update password:generate help ([d174f76](https://github.com/salesforcecli/plugin-user/commit/d174f763b4093ba160c3226d7a7af7d8e4d125af))
* use generatePassword in code, provide both in output ([8617114](https://github.com/salesforcecli/plugin-user/commit/861711430dc6cf31db10fe3f662f023dba9db410))

### [1.0.8](https://github.com/salesforcecli/plugin-user/compare/v1.0.7...v1.0.8) (2021-01-21)

### [1.0.7](https://github.com/salesforcecli/plugin-user/compare/v1.0.6...v1.0.7) (2021-01-14)
Expand Down
2 changes: 1 addition & 1 deletion messages/create.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"description": "create a user for a scratch org",
"description": "create a user for a scratch org\nCreate a user for a scratch org, optionally setting an alias for use by the CLI, assigning permission sets (e.g., permsets=ps1,ps2), generating a password (e.g., generatepassword=true), and setting User sObject fields.",
"examples": [
"sfdx force:user:create",
"sfdx force:user:create -a testuser1 -f config/project-user-def.json profileName='Chatter Free User'",
Expand Down
2 changes: 1 addition & 1 deletion messages/display.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"description": "displays information about a user of a scratch org",
"description": "displays information about a user of a scratch org\nOutput includes the profile name, org ID, access token, instance URL, login URL, and alias if applicable.",
"examples": ["sfdx force:user:display", "sfdx force:user:display -u [email protected] --json"],
"accessTokenError": "This command doesn't accept an access token for a username.",
"accessTokenAction": "Specify a username or an alias."
Expand Down
2 changes: 1 addition & 1 deletion messages/list.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"description": "list all authenticated users of an org",
"description": "list all authenticated users of an org\nThe original scratch org admin is marked with \"(A)\"",
"examples": [
"sfdx force:user:list",
"sfdx force:user:list -u [email protected] --json",
Expand Down
2 changes: 1 addition & 1 deletion messages/password.generate.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"description": "generate a password for scratch org users",
"description": "generate a password for scratch org users\nGenerates and sets a random password for one or more scratch org users. Targets the usernames listed with the --onbehalfof parameter or the --targetusername parameter. Defaults to the defaultusername.\n\nIf you haven’t set a default Dev Hub, or if your scratch org isn’t associated with your default Dev Hub, --targetdevhubusername is required.\n\nTo see a password that was previously generated, run \"sfdx force:user:display\".",
"examples": [
"sfdx force:user:password:generate",
"sfdx force:user:password:generate -u [email protected] --json",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@salesforce/plugin-user",
"description": "Commands to interact with Users and Permission Sets",
"version": "1.0.11",
"version": "1.0.12",
"author": "Salesforce",
"bugs": "https://github.com/forcedotcom/cli/issues",
"dependencies": {
Expand Down
13 changes: 10 additions & 3 deletions src/commands/force/user/password/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
import * as os from 'os';
import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command';
import { Aliases, AuthInfo, Connection, Messages, Org, SfdxError, User, UserFields } from '@salesforce/core';
import { Aliases, AuthInfo, Connection, Messages, Org, SfdxError, User } from '@salesforce/core';

Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/plugin-user', 'password.generate');
Expand Down Expand Up @@ -46,14 +46,21 @@ export class UserPasswordGenerateCommand extends SfdxCommand {
const org = await Org.create({ connection });
const user: User = await User.create({ org });
const password = User.generatePasswordUtf8();
const fields: UserFields = await user.retrieve(username);
// we only need the Id, so instead of User.retrieve we'll just query
// this avoids permission issues if ProfileId is restricted for the user querying for it
const result: { Id: string } = await connection.singleRecordQuery(
`SELECT Id FROM User WHERE Username='${username}'`
);

// userId is used by `assignPassword` so we need to set it here
authInfo.getFields().userId = fields.id;
authInfo.getFields().userId = result.Id;
await user.assignPassword(authInfo, password);

password.value((pass) => {
this.passwordData.push({ username: aliasOrUsername, password: pass.toString('utf-8') });
authInfo.update({ password: pass.toString('utf-8') });
});

await authInfo.save();
} catch (e) {
if (
Expand Down
8 changes: 6 additions & 2 deletions test/commands/user/password/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('force:user:password:generate', () => {
let authInfoStub: StubbedType<AuthInfo>;
let authInfoConfigStub: StubbedType<AuthInfoConfig>;
const testData = new MockTestOrgData();
let queryStub;

async function prepareStubs(throws = false) {
const authFields = await testData.getConfig();
Expand All @@ -32,8 +33,8 @@ describe('force:user:password:generate', () => {
stubMethod($$.SANDBOX, Org.prototype, 'getUsername').returns('[email protected]');
stubMethod($$.SANDBOX, Org.prototype, 'retrieveMaxApiVersion').returns('51.0');
stubMethod($$.SANDBOX, User, 'create').callsFake(async () => User.prototype);
stubMethod($$.SANDBOX, User.prototype, 'retrieve').resolves({
id: '0052D0000043PawWWR',
queryStub = stubMethod($$.SANDBOX, Connection.prototype, 'singleRecordQuery').resolves({
Id: '0052D0000043PawWWR',
});

const secureBuffer: SecureBuffer<void> = new SecureBuffer<void>();
Expand Down Expand Up @@ -69,6 +70,7 @@ describe('force:user:password:generate', () => {
const result = JSON.parse(ctx.stdout).result;
expect(result).to.deep.equal(expected);
expect(authInfoStub.update.callCount).to.equal(2);
expect(queryStub.callCount).to.equal(2);
});

test
Expand All @@ -80,6 +82,7 @@ describe('force:user:password:generate', () => {
const result = JSON.parse(ctx.stdout).result;
expect(result).to.deep.equal(expected);
expect(authInfoStub.update.callCount).to.equal(1);
expect(queryStub.callCount).to.equal(1);
});

test
Expand All @@ -93,5 +96,6 @@ describe('force:user:password:generate', () => {
expect(result.status).to.equal(1);
expect(result.name).to.equal('noSelfSetError');
expect(authInfoStub.update.callCount).to.equal(0);
expect(queryStub.callCount).to.equal(1);
});
});

0 comments on commit 49af6de

Please sign in to comment.