generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
user.ts
132 lines (117 loc) · 4.68 KB
/
user.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { AuthFields, Connection, Logger, Messages, StateAggregator } from '@salesforce/core';
import { ensureString } from '@salesforce/ts-types';
import {
loglevel,
orgApiVersionFlagWithDeprecations,
requiredOrgFlagWithDeprecations,
SfCommand,
} from '@salesforce/sf-plugins-core';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-user', 'display');
export type DisplayUserResult = {
username: string;
profileName: string;
id: string;
orgId: string;
accessToken?: string;
instanceUrl?: string;
loginUrl?: string;
alias?: string;
password?: string;
};
export class DisplayUserCommand extends SfCommand<DisplayUserResult> {
public static readonly deprecateAliases = true;
public static readonly aliases = ['force:user:display'];
public static readonly summary = messages.getMessage('summary');
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessages('examples');
public static readonly flags = {
'target-org': requiredOrgFlagWithDeprecations,
'api-version': orgApiVersionFlagWithDeprecations,
loglevel,
};
public async run(): Promise<DisplayUserResult> {
const { flags } = await this.parse(DisplayUserCommand);
const username = ensureString(flags['target-org'].getUsername());
const userAuthDataArray = await flags['target-org'].readUserAuthFiles();
// userAuthDataArray contains all the Org's users AuthInfo, we just need the default or -o, which is in the username variable
const userAuthData: AuthFields | undefined = userAuthDataArray
.find((uat) => uat.getFields().username === username)
?.getFields(true);
const conn: Connection = flags['target-org'].getConnection(flags['api-version']);
let profileName = userAuthData?.userProfileName;
let userId = userAuthData?.userId;
try {
// the user executing this command may not have access to the Profile sObject.
if (!profileName) {
const PROFILE_NAME_QUERY = `SELECT name FROM Profile WHERE Id IN (SELECT ProfileId FROM User WHERE username='${username}')`;
profileName = (await conn.singleRecordQuery<{ Name: string }>(PROFILE_NAME_QUERY)).Name;
}
} catch (err) {
profileName = 'unknown';
const logger = await Logger.child(this.constructor.name);
logger.debug(
`Query for the profile name failed for username: ${username} with message: ${
err instanceof Error ? err.message : ''
}`
);
}
try {
if (!userId) {
const USER_QUERY = `SELECT id FROM User WHERE username='${username}'`;
userId = (await conn.singleRecordQuery<{ Id: string }>(USER_QUERY)).Id;
}
} catch (err) {
userId = 'unknown';
const logger = await Logger.child(this.constructor.name);
logger.debug(
`Query for the user ID failed for username: ${username} with message: ${
err instanceof Error ? err.message : ''
}`
);
}
const result: DisplayUserResult = {
accessToken: conn.accessToken as string,
id: userId,
instanceUrl: userAuthData?.instanceUrl,
loginUrl: userAuthData?.loginUrl,
orgId: flags['target-org'].getOrgId(),
profileName,
username,
};
const stateAggregator = await StateAggregator.getInstance();
const alias = stateAggregator.aliases.get(username);
if (alias) {
result.alias = alias;
}
if (userAuthData?.password) {
result.password = userAuthData.password;
}
this.warn(messages.getMessage('securityWarning'));
this.log('');
this.print(result);
return result;
}
private print(result: DisplayUserResult): void {
this.table({
data: [
{ key: 'Username', label: result.username ?? 'unknown' },
{ key: 'Profile Name', label: result.profileName },
{ key: 'Id', label: result.id },
{ key: 'Org Id', label: result.orgId },
...(result.accessToken ? [{ key: 'Access Token', label: result.accessToken }] : []),
...(result.instanceUrl ? [{ key: 'Instance Url', label: result.instanceUrl }] : []),
...(result.loginUrl ? [{ key: 'Login Url', label: result.loginUrl }] : []),
...(result.alias ? [{ key: 'Alias', label: result.alias }] : []),
...(result.password ? [{ key: 'Password', label: result.password }] : []),
] satisfies Array<{ key: string; label: string }>,
title: 'User Description',
});
}
}