@salesforce/plugin-user
Version:
Commands to interact with Users and Permission Sets
116 lines • 5.72 kB
JavaScript
/*
* Copyright 2026, Salesforce, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { envVars, 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');
const secretsMessages = Messages.loadMessages('@salesforce/plugin-user', 'secrets-redacted');
export class DisplayUserCommand extends SfCommand {
static deprecateAliases = true;
static aliases = ['force:user:display'];
static summary = messages.getMessage('summary');
static description = messages.getMessage('description');
static examples = messages.getMessages('examples');
static flags = {
'target-org': requiredOrgFlagWithDeprecations,
'api-version': orgApiVersionFlagWithDeprecations,
loglevel,
};
async run() {
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 = userAuthDataArray
.find((uat) => uat.getFields().username === username)
?.getFields(true);
const conn = 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(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(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 : ''}`);
}
// TODO: Remove env var workaround
const showSecretsEnvVarIsSet = envVars.getBoolean('SF_TEMP_SHOW_SECRETS', false);
const accessTokenRedacted = secretsMessages.getMessage('redacted.accessToken');
const passwordRedacted = secretsMessages.getMessage('redacted.userPassword');
const result = {
accessToken: showSecretsEnvVarIsSet ? conn.accessToken : accessTokenRedacted,
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 = showSecretsEnvVarIsSet ? userAuthData.password : passwordRedacted;
}
if (showSecretsEnvVarIsSet) {
this.warn(secretsMessages.getMessage('temp.envVarIsSet', ['sf org display user']));
this.warn(messages.getMessage('securityWarning'));
}
else {
this.warn(secretsMessages.getMessage('temp.envVarWorkaround', ['sf org display user']));
}
this.log('');
this.print(result);
return result;
}
print(result) {
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 }] : []),
],
title: 'User Description',
});
}
}
//# sourceMappingURL=user.js.map