1+ using Microsoft . SharePoint . Client ;
2+ using PnP . PowerShell . Commands . Base ;
3+ using PnP . PowerShell . Commands . Base . PipeBinds ;
4+ using PnP . PowerShell . Commands . Utilities ;
5+ using PnP . PowerShell . Commands . Utilities . REST ;
6+ using System ;
7+ using System . Management . Automation ;
8+
9+ namespace PnP . PowerShell . Commands . PowerPlatform . PowerApps
10+ {
11+ [ Cmdlet ( VerbsCommon . Remove , "PnPPowerAppPermission" ) ]
12+ public class RemovePowerAppPermission : PnPAzureManagementApiCmdlet
13+ {
14+ [ Parameter ( Mandatory = false ) ]
15+ public PowerPlatformEnvironmentPipeBind Environment ;
16+
17+ [ Parameter ( Mandatory = true ) ]
18+ public PowerAppPipeBind Identity ;
19+
20+ [ Parameter ( Mandatory = false ) ]
21+ public string User ;
22+
23+ [ Parameter ( Mandatory = false ) ]
24+ public string Group ;
25+
26+ [ Parameter ( Mandatory = false ) ]
27+ public SwitchParameter Tenant ;
28+
29+ [ Parameter ( Mandatory = false ) ]
30+ public SwitchParameter AsAdmin ;
31+
32+ [ Parameter ( Mandatory = false ) ]
33+ public SwitchParameter Force ;
34+
35+ protected override void ExecuteCmdlet ( )
36+ {
37+ var environmentName = ParameterSpecified ( nameof ( Environment ) ) ? Environment . GetName ( ) : PowerPlatformUtility . GetDefaultEnvironment ( ArmRequestHelper , Connection . AzureEnvironment ) ? . Name ;
38+ if ( string . IsNullOrEmpty ( environmentName ) )
39+ {
40+ throw new PSArgumentException ( "Environment not found." , nameof ( Environment ) ) ;
41+ }
42+
43+ var appName = Identity . GetName ( ) ;
44+ if ( string . IsNullOrEmpty ( appName ) )
45+ {
46+ throw new PSArgumentException ( "PowerApp not found." , nameof ( Identity ) ) ;
47+ }
48+
49+ if ( string . IsNullOrEmpty ( User ) && string . IsNullOrEmpty ( Group ) && ! Tenant . IsPresent )
50+ {
51+ throw new PSArgumentException ( "Either User, Group, or Tenant must be specified." ) ;
52+ }
53+
54+ if ( ( Tenant . IsPresent && ( ! string . IsNullOrEmpty ( User ) || ! string . IsNullOrEmpty ( Group ) ) ) ||
55+ ( ! string . IsNullOrEmpty ( User ) && ! string . IsNullOrEmpty ( Group ) ) )
56+ {
57+ throw new PSArgumentException ( "Specify only one of User, Group, or Tenant." ) ;
58+ }
59+
60+ string graphAccessToken = TokenHandler . GetAccessToken ( $ "https://{ Connection . GraphEndPoint } /.default", Connection ) ;
61+ LogDebug ( "Microsoft Graph access token acquired" ) ;
62+
63+ var graphRequestHelper = new ApiRequestHelper ( GetType ( ) , Connection , $ "https://{ Connection . GraphEndPoint } /.default") ;
64+
65+ string entityId = null ;
66+
67+ if ( ! string . IsNullOrEmpty ( User ) )
68+ {
69+ LogDebug ( "Processing User parameter" ) ;
70+ Model . AzureAD . User graphUser ;
71+ if ( Guid . TryParse ( User , out Guid userGuid ) )
72+ {
73+ LogDebug ( $ "Looking up user through Microsoft Graph by user id { userGuid } ") ;
74+ graphUser = Utilities . AzureAdUtility . GetUser ( graphAccessToken , userGuid , azureEnvironment : Connection . AzureEnvironment ) ;
75+ }
76+ else
77+ {
78+ LogDebug ( $ "Looking up user through Microsoft Graph by user principal name { User } ") ;
79+ graphUser = Utilities . AzureAdUtility . GetUser ( graphAccessToken , User , azureEnvironment : Connection . AzureEnvironment ) ;
80+ }
81+
82+ if ( graphUser == null )
83+ {
84+ throw new PSArgumentException ( "User not found." , nameof ( User ) ) ;
85+ }
86+
87+ entityId = graphUser . Id . ToString ( ) ;
88+ }
89+ else if ( ! string . IsNullOrEmpty ( Group ) )
90+ {
91+ LogDebug ( "Processing Group parameter" ) ;
92+
93+ var graphGroup = Guid . TryParse ( Group , out Guid groupGuid )
94+ ? Utilities . AzureADGroupsUtility . GetGroup ( graphRequestHelper , groupGuid )
95+ : Utilities . AzureADGroupsUtility . GetGroup ( graphRequestHelper , Group ) ;
96+
97+ if ( graphGroup == null )
98+ {
99+ throw new PSArgumentException ( "Group not found." , nameof ( Group ) ) ;
100+ }
101+
102+ entityId = graphGroup . Id . ToString ( ) ;
103+ }
104+ else if ( Tenant . IsPresent )
105+ {
106+ LogDebug ( "Processing Tenant parameter" ) ;
107+
108+ string TenantGUID = TenantExtensions . GetTenantIdByUrl ( Connection . Url , Connection . AzureEnvironment ) ;
109+ entityId = $ "tenant-{ TenantGUID } ";
110+ LogDebug ( $ "Tenant ID resolved: { entityId } ") ;
111+ }
112+
113+ var payload = new
114+ {
115+ delete = new [ ]
116+ {
117+ new
118+ {
119+ id = entityId ,
120+ }
121+ }
122+ } ;
123+
124+ if ( Force || ShouldContinue ( $ "Remove PowerApp permission for entity with id '{ entityId } ' from app '{ appName } '?", Properties . Resources . Confirm ) )
125+ {
126+ string baseUrl = PowerPlatformUtility . GetPowerAppsEndpoint ( Connection . AzureEnvironment ) ;
127+ LogDebug ( $ "Removing entity { entityId } permissions from PowerApp { appName } in environment { environmentName } ") ;
128+ PowerAppsRequestHelper . Post ( $ "{ baseUrl } /providers/Microsoft.PowerApps{ ( AsAdmin ? "/scopes/admin/environments/" + environmentName : "" ) } /apps/{ appName } /modifyPermissions?api-version=2022-11-01", payload ) ;
129+ }
130+ }
131+ }
132+ }
0 commit comments