UNPKG

@salesforce/plugin-user

Version:

Commands to interact with Users and Permission Sets

96 lines 4.57 kB
/* * 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 { 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 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 : ''}`); } const result = { accessToken: conn.accessToken, 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; } 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