Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bulkify permset assign command #102

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,20 @@ sfdx plugins
## Commands

<!-- commands -->
* [`sfdx force:user:create [name=value...] [-a <string>] [-f <string>] [-s] [-v <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-forceusercreate-namevalue--a-string--f-string--s--v-string--u-string---apiversion-string---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal)
* [`sfdx force:user:display [-v <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-forceuserdisplay--v-string--u-string---apiversion-string---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal)
* [`sfdx force:user:list [-v <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-forceuserlist--v-string--u-string---apiversion-string---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal)
* [`sfdx force:user:password:generate [-o <array>] [-v <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-forceuserpasswordgenerate--o-array--v-string--u-string---apiversion-string---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal)
* [`sfdx force:user:permset:assign -n <string> [-o <array>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-forceuserpermsetassign--n-string--o-array--u-string---apiversion-string---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal)

- [`sfdx force:user:create [name=value...] [-a <string>] [-f <string>] [-s] [-v <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-forceusercreate-namevalue--a-string--f-string--s--v-string--u-string---apiversion-string---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal)
- [`sfdx force:user:display [-v <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-forceuserdisplay--v-string--u-string---apiversion-string---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal)
- [`sfdx force:user:list [-v <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-forceuserlist--v-string--u-string---apiversion-string---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal)
- [`sfdx force:user:password:generate [-o <array>] [-v <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-forceuserpasswordgenerate--o-array--v-string--u-string---apiversion-string---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal)
- [`sfdx force:user:permset:assign -n <array> [-o <array>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-forceuserpermsetassign--n-string--o-array--u-string---apiversion-string---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal)

## `sfdx force:user:create [name=value...] [-a <string>] [-f <string>] [-s] [-v <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`

create a user for a scratch org

```
USAGE
$ sfdx force:user:create [name=value...] [-a <string>] [-f <string>] [-s] [-v <string>] [-u <string>] [--apiversion
$ sfdx force:user:create [name=value...] [-a <string>] [-f <string>] [-s] [-v <string>] [-u <string>] [--apiversion
<string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]

OPTIONS
Expand Down Expand Up @@ -110,7 +111,7 @@ OPTIONS
this command invocation

DESCRIPTION
Create a user for a scratch org, optionally setting an alias for use by the CLI, assigning permission sets (e.g.,
Create 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
Expand All @@ -128,7 +129,7 @@ displays information about a user of a scratch org

```
USAGE
$ sfdx force:user:display [-v <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel
$ sfdx force:user:display [-v <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel
trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]

OPTIONS
Expand Down Expand Up @@ -162,7 +163,7 @@ list all authenticated users of an org

```
USAGE
$ sfdx force:user:list [-v <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel
$ sfdx force:user:list [-v <string>] [-u <string>] [--apiversion <string>] [--json] [--loglevel
trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]

OPTIONS
Expand Down Expand Up @@ -197,7 +198,7 @@ generate a password for scratch org users

```
USAGE
$ sfdx force:user:password:generate [-o <array>] [-v <string>] [-u <string>] [--apiversion <string>] [--json]
$ sfdx force:user:password:generate [-o <array>] [-v <string>] [-u <string>] [--apiversion <string>] [--json]
[--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]

OPTIONS
Expand All @@ -219,10 +220,10 @@ OPTIONS
this command invocation

DESCRIPTION
Generates and sets a random password for one or more scratch org users. Targets the usernames listed with the
Generates 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.

If you haven’t set a default Dev Hub, or if your scratch org isn’t associated with your default Dev Hub,
If you haven’t set a default Dev Hub, or if your scratch org isn’t associated with your default Dev Hub,
--targetdevhubusername is required.

To see a password that was previously generated, run "sfdx force:user:display".
Expand All @@ -235,18 +236,18 @@ EXAMPLES

_See code: [src/commands/force/user/password/generate.ts](https://github.com/salesforcecli/plugin-user/blob/v1.2.7/src/commands/force/user/password/generate.ts)_

## `sfdx force:user:permset:assign -n <string> [-o <array>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`
## `sfdx force:user:permset:assign -n <array> [-o <array>] [-u <string>] [--apiversion <string>] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`

assign a permission set to one or more users of an org

```
USAGE
$ sfdx force:user:permset:assign -n <string> [-o <array>] [-u <string>] [--apiversion <string>] [--json] [--loglevel
$ sfdx force:user:permset:assign -n <array> [-o <array>] [-u <string>] [--apiversion <string>] [--json] [--loglevel
trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]

OPTIONS
-n, --permsetname=permsetname (required) the name of the
permission set to assign
-n, --permsetname=permsetname (required) comma-separated list of
permission sets to assign

-o, --onbehalfof=onbehalfof comma-separated list of usernames or
aliases to assign the permission set
Expand All @@ -266,8 +267,10 @@ OPTIONS
EXAMPLES
sfdx force:user:permset:assign -n DreamHouse
sfdx force:user:permset:assign -n DreamHouse -u [email protected]
sfdx force:user:permset:assign -n "DreamHouse,LargeDreamHouse" -u [email protected]
sfdx force:user:permset:assign -n DreamHouse -o "[email protected],user2,user3"
```

_See code: [src/commands/force/user/permset/assign.ts](https://github.com/salesforcecli/plugin-user/blob/v1.2.7/src/commands/force/user/permset/assign.ts)_

<!-- commandsstop -->
4 changes: 2 additions & 2 deletions messages/permset.assign.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"description": "assign a permission set to one or more users of an org",
"examples": [
"sfdx force:user:permset:assign -n DreamHouse",
"sfdx force:user:permset:assign -n \"DreamHouse, LargeDreamHouse\"",
"sfdx force:user:permset:assign -n DreamHouse -u [email protected]",
"sfdx force:user:permset:assign -n DreamHouse -o \"[email protected],user2,user3\""
],
"flags": {
"onBehalfOf": "comma-separated list of usernames or aliases to assign the permission set to",
"permsetName": "the name of the permission set to assign"
"permsetName": "comma-separated list of permission sets to assign"
}
}
8 changes: 4 additions & 4 deletions src/commands/force/user/permset/assign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class UserPermsetAssignCommand extends SfdxCommand {
public static readonly examples = messages.getMessage('examples').split(os.EOL);
public static readonly requiresUsername = true;
public static readonly flagsConfig: FlagsConfig = {
permsetname: flags.string({
permsetname: flags.array({
char: 'n',
description: messages.getMessage('flags.permsetName'),
required: true,
Expand Down Expand Up @@ -60,12 +60,12 @@ export class UserPermsetAssignCommand extends SfdxCommand {
const queryResult = await connection.singleRecordQuery<{ Id: string }>(
`SELECT Id FROM User WHERE Username='${username}'`
);

const permsetArray = this.flags.permsetname as string[];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if there's a standard around this, but it's been suggested to me to use asArray or asArray<string> if necessary instead of as string[]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to change it, this pattern is also used at line 50 and in other files in the code base.
Want me to change others?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eslint complains at me when I run build

63:56  error  Array type using 'Array<string>' is forbidden for simple types. Use 'string[]' instead  @typescript-eslint/array-type

I guess I should just leave it?

try {
await user.assignPermissionSets(queryResult.Id, [this.flags.permsetname]);
await user.assignPermissionSets(queryResult.Id, permsetArray);
this.successes.push({
name: aliasOrUsername,
value: this.flags.permsetname as string,
value: permsetArray.join(','),
});
} catch (e) {
const err = e as SfdxError;
Expand Down
8 changes: 4 additions & 4 deletions test/commands/user/permset/assign.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ describe('force:user:permset:assign', () => {
'--onbehalfof',
'testAlias, [email protected]',
'--permsetname',
'DreamHouse',
'DreamHouse, LargeDreamHouse',
])
.it('should assign the one permset to both users', (ctx) => {
.it('should assign both permsets to both users', (ctx) => {
// [email protected] is aliased to testUser
const expected = [
{
name: 'testAlias',
value: 'DreamHouse',
value: 'DreamHouse,LargeDreamHouse',
},
{
name: '[email protected]',
value: 'DreamHouse',
value: 'DreamHouse,LargeDreamHouse',
},
];
const result = JSON.parse(ctx.stdout).result;
Expand Down