@pnp/cli-microsoft365
Version:
Manage Microsoft 365 and SharePoint Framework projects on any platform
74 lines • 2.93 kB
JavaScript
import { z } from 'zod';
import { globalOptionsZod } from '../../../../Command.js';
import commands from '../../commands.js';
import request from '../../../../request.js';
import { validation } from '../../../../utils/validation.js';
import { formatting } from '../../../../utils/formatting.js';
import { cli } from '../../../../cli/cli.js';
import GraphCommand from '../../../base/GraphCommand.js';
export const options = z.strictObject({
...globalOptionsZod.shape,
userId: z.uuid().optional(),
userName: z.string().refine(name => validation.isValidUserPrincipalName(name), {
error: e => `'${e.input}' is not a valid user principal name (UPN).`
}).optional(),
ids: z.string().refine(ids => !ids.split(',').some(e => !validation.isValidGuid(e)), {
error: e => `'${e.input}' contains one or more invalid GUIDs.`
}),
force: z.boolean().optional().alias('f')
});
class EntraUserLicenseRemoveCommand extends GraphCommand {
get name() {
return commands.USER_LICENSE_REMOVE;
}
get description() {
return 'Removes a license from a user';
}
get schema() {
return options;
}
getRefinedSchema(schema) {
return schema
.refine(options => [options.userId, options.userName].filter(o => o !== undefined).length === 1, {
error: `Specify either 'userId' or 'userName'.`,
params: {
customCode: 'optionSet',
options: ['userId', 'userName']
}
});
}
async commandAction(logger, args) {
if (this.verbose) {
await logger.logToStderr(`Removing the licenses for the user '${args.options.userId || args.options.userName}'...`);
}
if (args.options.force) {
await this.deleteUserLicenses(args);
}
else {
const result = await cli.promptForConfirmation({ message: `Are you sure you want to remove the licenses for the user '${args.options.userId || args.options.userName}'?` });
if (result) {
await this.deleteUserLicenses(args);
}
}
}
async deleteUserLicenses(args) {
const removeLicenses = args.options.ids.split(',');
const requestBody = { "addLicenses": [], "removeLicenses": removeLicenses };
const requestOptions = {
url: `${this.resource}/v1.0/users/${formatting.encodeQueryParameter(args.options.userId || args.options.userName)}/assignLicense`,
headers: {
accept: 'application/json;odata.metadata=none'
},
data: requestBody,
responseType: 'json'
};
try {
await request.post(requestOptions);
}
catch (err) {
this.handleRejectedODataJsonPromise(err);
}
}
}
export default new EntraUserLicenseRemoveCommand();
//# sourceMappingURL=user-license-remove.js.map