-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathset.ts
68 lines (55 loc) · 2.14 KB
/
set.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { SfdxCommand } from '@salesforce/command';
import request = require('request-promise-native');
import userIdLookup = require('../../../../shared/userIdLookup');
import chalk from 'chalk';
export default class Set extends SfdxCommand {
public static description = 'Set the password for a user by first/last name';
public static examples = [
`sfdx shane:user:password:set -p sfdx1234 -g User -l User
// sets the password for User User to sfdx1234
`
];
protected static flagsConfig = {
firstname: { type: 'string', char: 'g', required: true, description: 'first (given) name of the user--keeping -f for file for consistency' },
lastname: { type: 'string', char: 'l', required: true, description: 'last name of the user' },
password: { type: 'string', char: 'p', required: true, description: 'local path of the photo to use' }
};
// Comment this out if your command does not require an org username
protected static requiresUsername = true;
public async run(): Promise<any> { // tslint:disable-line:no-any
const conn = this.org.getConnection();
let user;
try {
user = await userIdLookup.getUserId(conn, this.flags.lastname, this.flags.firstname);
} catch (e) {
throw new Error(e);
}
this.ux.log(`found user with id ${user.Id}`);
const resetResult = await request({
method: 'post',
uri: `${conn.instanceUrl}/services/data/v41.0/sobjects/User/${user.Id}/password`,
body: {
NewPassword: this.flags.password
},
headers: {
Authorization: `Bearer ${conn.accessToken}`
},
json: true,
resolveWithFullResponse: true
});
if (resetResult.statusCode === 204) {
this.ux.log(chalk.green(`Successfully set the password "${this.flags.password}" for user ${user.Username}.`));
this.ux.log(`You can see the password again by running "sfdx force:user:display -u ${user.Username}".`);
return {
password: this.flags.password,
username: user.Username
};
} else {
this.ux.error(chalk.red('Password not set correctly.'));
return {
status: 1,
result: resetResult
};
}
}
}