UNPKG

@pnp/cli-microsoft365

Version:

Manage Microsoft 365 and SharePoint Framework projects on any platform

150 lines 7.87 kB
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _EntraGroupMemberAddCommand_instances, _EntraGroupMemberAddCommand_initTelemetry, _EntraGroupMemberAddCommand_initOptions, _EntraGroupMemberAddCommand_initValidators, _EntraGroupMemberAddCommand_initOptionSets, _EntraGroupMemberAddCommand_initTypes; import request from '../../../../request.js'; import { entraGroup } from '../../../../utils/entraGroup.js'; import { entraUser } from '../../../../utils/entraUser.js'; import { validation } from '../../../../utils/validation.js'; import GraphCommand from '../../../base/GraphCommand.js'; import commands from '../../commands.js'; class EntraGroupMemberAddCommand extends GraphCommand { get name() { return commands.GROUP_MEMBER_ADD; } get description() { return 'Adds members to a Microsoft Entra group'; } constructor() { super(); _EntraGroupMemberAddCommand_instances.add(this); this.roleValues = ['Owner', 'Member']; __classPrivateFieldGet(this, _EntraGroupMemberAddCommand_instances, "m", _EntraGroupMemberAddCommand_initTelemetry).call(this); __classPrivateFieldGet(this, _EntraGroupMemberAddCommand_instances, "m", _EntraGroupMemberAddCommand_initOptions).call(this); __classPrivateFieldGet(this, _EntraGroupMemberAddCommand_instances, "m", _EntraGroupMemberAddCommand_initValidators).call(this); __classPrivateFieldGet(this, _EntraGroupMemberAddCommand_instances, "m", _EntraGroupMemberAddCommand_initOptionSets).call(this); __classPrivateFieldGet(this, _EntraGroupMemberAddCommand_instances, "m", _EntraGroupMemberAddCommand_initTypes).call(this); } async commandAction(logger, args) { try { if (args.options.groupDisplayName) { await this.warn(logger, `Option 'groupDisplayName' is deprecated and will be removed in the next major release.`); } if (this.verbose) { await logger.logToStderr(`Adding member(s) ${args.options.ids || args.options.userNames} to group ${args.options.groupId || args.options.groupDisplayName || args.options.groupName}...`); } const groupId = await this.getGroupId(logger, args.options); const userIds = await this.getUserIds(logger, args.options); for (let i = 0; i < userIds.length; i += 400) { const userIdsBatch = userIds.slice(i, i + 400); const requestOptions = { url: `${this.resource}/v1.0/$batch`, headers: { 'content-type': 'application/json;odata.metadata=none' }, responseType: 'json', data: { requests: [] } }; for (let j = 0; j < userIdsBatch.length; j += 20) { const userIdsChunk = userIdsBatch.slice(j, j + 20); requestOptions.data.requests.push({ id: j + 1, method: 'PATCH', url: `/groups/${groupId}`, headers: { 'content-type': 'application/json;odata.metadata=none' }, body: { [`${args.options.role === 'Member' ? 'members' : 'owners'}@odata.bind`]: userIdsChunk.map(u => `${this.resource}/v1.0/directoryObjects/${u}`) } }); } const res = await request.post(requestOptions); for (const response of res.responses) { if (response.status !== 204) { throw response.body; } } } } catch (err) { this.handleRejectedODataJsonPromise(err); } } async getGroupId(logger, options) { if (options.groupId) { return options.groupId; } if (this.verbose) { await logger.logToStderr(`Retrieving ID of group ${options.groupDisplayName || options.groupName}...`); } return entraGroup.getGroupIdByDisplayName(options.groupDisplayName || options.groupName); } async getUserIds(logger, options) { if (options.ids) { return options.ids.split(',').map(i => i.trim()); } if (this.verbose) { await logger.logToStderr('Retrieving ID(s) of user(s)...'); } return entraUser.getUserIdsByUpns(options.userNames.split(',').map(u => u.trim())); } } _EntraGroupMemberAddCommand_instances = new WeakSet(), _EntraGroupMemberAddCommand_initTelemetry = function _EntraGroupMemberAddCommand_initTelemetry() { this.telemetry.push((args) => { Object.assign(this.telemetryProperties, { groupId: typeof args.options.groupId !== 'undefined', groupDisplayName: typeof args.options.groupDisplayName !== 'undefined', groupName: typeof args.options.groupName !== 'undefined', ids: typeof args.options.ids !== 'undefined', userNames: typeof args.options.userNames !== 'undefined' }); }); }, _EntraGroupMemberAddCommand_initOptions = function _EntraGroupMemberAddCommand_initOptions() { this.options.unshift({ option: '-i, --groupId [groupId]' }, { option: '--groupDisplayName [groupDisplayName]' }, { option: '-n, --groupName [groupName]' }, { option: '--ids [ids]' }, { option: '--userNames [userNames]' }, { option: '-r, --role <role>', autocomplete: this.roleValues }); }, _EntraGroupMemberAddCommand_initValidators = function _EntraGroupMemberAddCommand_initValidators() { this.validators.push(async (args) => { if (args.options.groupId && !validation.isValidGuid(args.options.groupId)) { return `${args.options.groupId} is not a valid GUID for option groupId.`; } if (args.options.ids) { const isValidGUIDArrayResult = validation.isValidGuidArray(args.options.ids); if (isValidGUIDArrayResult !== true) { return `The following GUIDs are invalid for the option 'ids': ${isValidGUIDArrayResult}.`; } } if (args.options.userNames) { const isValidUPNArrayResult = validation.isValidUserPrincipalNameArray(args.options.userNames); if (isValidUPNArrayResult !== true) { return `The following user principal names are invalid for the option 'userNames': ${isValidUPNArrayResult}.`; } } if (this.roleValues.indexOf(args.options.role) === -1) { return `Option 'role' must be one of the following values: ${this.roleValues.join(', ')}.`; } return true; }); }, _EntraGroupMemberAddCommand_initOptionSets = function _EntraGroupMemberAddCommand_initOptionSets() { this.optionSets.push({ options: ['groupId', 'groupDisplayName', 'groupName'] }, { options: ['ids', 'userNames'] }); }, _EntraGroupMemberAddCommand_initTypes = function _EntraGroupMemberAddCommand_initTypes() { this.types.string.push('groupId', 'groupDisplayName', 'groupName', 'ids', 'userNames', 'role'); }; export default new EntraGroupMemberAddCommand(); //# sourceMappingURL=group-member-add.js.map