Skip to content

Bugfix in running AdminCmdlet changing context in a specific scenario #1611

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 2 commits into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Fixed `Add-PnPFileToSiteTemplate` when used alongside `Get-PnPFile` where the FileStream tried to overwrite an already open filestream when a file was located in the same directory as the template file itself.
- Fixed `Get-PnPMessageCenterAnnouncement` returning an error [#1607](https://github.com/pnp/powershell/pull/1607)
- Fixed `New-PnPTeamsTeam` issue when adding Owners and Members.
- Fixed running an admin cmdlet not always returning to the same context as before running the cmdlet [#1611](https://github.com/pnp/powershell/pull/1611)

### Removed

Expand Down
48 changes: 33 additions & 15 deletions src/Commands/Base/PnPAdminCmdlet.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using System;
using System.Linq;
using System.Management.Automation;
using System.Net.Http;
using Microsoft.Online.SharePoint.TenantAdministration;
using Microsoft.SharePoint.Client;
using PnP.PowerShell.Commands.Enums;
using Resources = PnP.PowerShell.Commands.Properties.Resources;

namespace PnP.PowerShell.Commands.Base
{
/// <summary>
/// Base cmdlet for cmdlets that require running on against the admin site collection
/// </summary>
public abstract class PnPAdminCmdlet : PnPSharePointCmdlet
{
private Tenant _tenant;
Expand All @@ -28,8 +30,28 @@ public Tenant Tenant

public Uri BaseUri => _baseUri;

/// <summary>
/// ClientContext which was the active context before elevating to the admin context
/// </summary>
internal ClientContext SiteContext;

/// <summary>
/// Checks if the current context has been set up using a device login. In that case we cannot elevate to an admin context.
/// </summary>
private void IsDeviceLogin(string tenantAdminUrl)
{
if (PnPConnection.Current.ConnectionMethod == Model.ConnectionMethod.DeviceLogin)
{
if (tenantAdminUrl != PnPConnection.Current.Url)
{
throw new PSInvalidOperationException($"You used a device login connection to authenticate to SharePoint. We do not support automatically switching context to the tenant administration site which is required to execute this cmdlet. Please use Connect-PnPOnline and connect to '{tenantAdminUrl}' with the appropriate connection parameters");
}
}
}

/// <summary>
/// Executed before executing the specific admin cmdlet logic
/// </summary>
protected override void BeginProcessing()
{
base.BeginProcessing();
Expand All @@ -42,12 +64,14 @@ protected override void BeginProcessing()
{
throw new InvalidOperationException(Resources.NoSharePointConnection);
}

// Keep an instance of the client context which is currently active before elevating to an admin client context so we can restore it afterwards
SiteContext = PnPConnection.Current.Context;

PnPConnection.Current.CacheContext();

if (PnPConnection.Current.TenantAdminUrl != null &&
(PnPConnection.Current.ConnectionType == ConnectionType.O365))
(PnPConnection.Current.ConnectionType == ConnectionType.O365))
{
var uri = new Uri(PnPConnection.Current.Url);
var uriParts = uri.Host.Split('.');
Expand Down Expand Up @@ -86,21 +110,15 @@ protected override void BeginProcessing()
}
}

private void IsDeviceLogin(string tenantAdminUrl)
{
if (PnPConnection.Current.ConnectionMethod == Model.ConnectionMethod.DeviceLogin)
{
if (tenantAdminUrl != PnPConnection.Current.Url)
{
throw new PSInvalidOperationException($"You used a device login connection to authenticate to SharePoint. We do not support automatically switching context to the tenant administration site which is required to execute this cmdlet. Please use Connect-PnPOnline and connect to '{tenantAdminUrl}' with the appropriate connection parameters");
}
}
}

/// <summary>
/// Executed after completing the specific admin cmdlet logic
/// </summary>
protected override void EndProcessing()
{
base.EndProcessing();
PnPConnection.Current.RestoreCachedContext(PnPConnection.Current.Url);

// Restore the client context to the context which was used before the admin context elevation
PnPConnection.Current.Context = SiteContext;
}
}
}
}