UNPKG

@pnp/cli-microsoft365

Version:

Manage Microsoft 365 and SharePoint Framework projects on any platform

125 lines 5.27 kB
import { z } from 'zod'; import { globalOptionsZod } from '../../../../Command.js'; import request from '../../../../request.js'; import { formatting } from '../../../../utils/formatting.js'; import GraphCommand from '../../../base/GraphCommand.js'; import commands from '../../commands.js'; export const options = z.strictObject({ ...globalOptionsZod.shape, appId: z.uuid().optional().alias('i'), appDisplayName: z.string().optional().alias('n'), appObjectId: z.uuid().optional() }); class EntraAppRoleAssignmentListCommand extends GraphCommand { get name() { return commands.APPROLEASSIGNMENT_LIST; } get description() { return 'Lists app role assignments for the specified application registration'; } get schema() { return options; } getRefinedSchema(schema) { return schema .refine(options => [options.appId, options.appObjectId, options.appDisplayName].filter(o => o !== undefined).length === 1, { error: 'Specify either appId, appObjectId, or appDisplayName', params: { customCode: 'optionSet', options: ['appId', 'appObjectId', 'appDisplayName'] } }); } defaultProperties() { return ['resourceDisplayName', 'roleName']; } async commandAction(logger, args) { try { const spAppRoleAssignments = await this.getAppRoleAssignments(args.options); // the role assignment has an appRoleId but no name. To get the name, // we need to get all the roles from the resource. the resource is // a service principal. Multiple roles may have same resource id. const resourceIds = spAppRoleAssignments.map((item) => item.resourceId); const tasks = []; for (let i = 0; i < resourceIds.length; i++) { tasks.push(this.getServicePrincipal(resourceIds[i])); } const resources = await Promise.all(tasks); // loop through all appRoleAssignments for the servicePrincipal // and lookup the appRole.Id in the resources[resourceId].appRoles array... const results = []; spAppRoleAssignments.map((appRoleAssignment) => { const resource = resources.find((r) => r.id === appRoleAssignment.resourceId); if (resource) { const appRole = resource.appRoles.find((r) => r.id === appRoleAssignment.appRoleId); if (appRole) { results.push({ appRoleId: appRoleAssignment.appRoleId, resourceDisplayName: appRoleAssignment.resourceDisplayName, resourceId: appRoleAssignment.resourceId, roleId: appRole.id, roleName: appRole.value, created: appRoleAssignment.createdDateTime, deleted: appRoleAssignment.deletedDateTime }); } } }); await logger.log(results); } catch (err) { this.handleRejectedODataJsonPromise(err); } } async getAppRoleAssignments(argOptions) { if (argOptions.appObjectId) { const spAppRoleAssignments = await this.getSPAppRoleAssignments(argOptions.appObjectId); if (!spAppRoleAssignments.value.length) { throw 'no app role assignments found'; } return spAppRoleAssignments.value; } else { const spMatchQuery = argOptions.appId ? `appId eq '${formatting.encodeQueryParameter(argOptions.appId)}'` : `displayName eq '${formatting.encodeQueryParameter(argOptions.appDisplayName)}'`; const resp = await this.getServicePrincipalForApp(spMatchQuery); if (!resp.value.length) { throw 'app registration not found'; } return resp.value[0].appRoleAssignments; } } async getSPAppRoleAssignments(spId) { const spRequestOptions = { url: `${this.resource}/v1.0/servicePrincipals/${spId}/appRoleAssignments`, headers: { accept: 'application/json' }, responseType: 'json' }; return request.get(spRequestOptions); } async getServicePrincipalForApp(filterParam) { const spRequestOptions = { url: `${this.resource}/v1.0/servicePrincipals?$expand=appRoleAssignments&$filter=${filterParam}`, headers: { accept: 'application/json' }, responseType: 'json' }; return request.get(spRequestOptions); } async getServicePrincipal(spId) { const spRequestOptions = { url: `${this.resource}/v1.0/servicePrincipals/${spId}`, headers: { accept: 'application/json' }, responseType: 'json' }; return request.get(spRequestOptions); } } export default new EntraAppRoleAssignmentListCommand(); //# sourceMappingURL=approleassignment-list.js.map