Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add possibility to get the ServicePrincipal via IAppManager #1116

Merged
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
8 changes: 4 additions & 4 deletions docs/using-the-sdk/admin-sharepoint-apps.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,14 @@ After adding and deploying an app to the app catalog these API permissions need

```csharp
// List the permission requests that are pending approval or rejection
ServicePrincipal principal = new(context);
List<IPermissionRequest> permissionRequests = await principal.GetPermissionRequestsAsync();
var appManager = context.GetTenantAppManager();
List<IPermissionRequest> permissionRequests = await appManager.ServicePrincipal.GetPermissionRequestsAsync();

// Approve a permission request
var result = await principal.ApprovePermissionRequestAsync(permissionRequests.First().Id.ToString());
var result = await appManager.ServicePrincipal.ApprovePermissionRequestAsync(permissionRequests.First().Id.ToString());

// Deny a permission request
await principal.DenyPermissionRequestAsync(permissionRequests.First().Id.ToString());
await appManager.ServicePrincipal.DenyPermissionRequestAsync(permissionRequests.First().Id.ToString());
```

## Tenant app catalog specific operations
Expand Down
39 changes: 31 additions & 8 deletions src/sdk/PnP.Core.Admin.Test/SharePoint/PermissionRequestsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,14 @@ public async Task ApprovePermissionsRequestTest_Async()
app = appManager.Add(packagePath, true);
var deployResult = app.Deploy(false);

ServicePrincipal principal = new(context);
List<IPermissionRequest> permissionRequests = await principal.GetPermissionRequestsAsync();
Assert.IsTrue(deployResult);

var result = await principal.ApprovePermissionRequestAsync(permissionRequests.First().Id.ToString());
List<IPermissionRequest> permissionRequests =
await appManager.ServicePrincipal.GetPermissionRequestsAsync();

var result =
await appManager.ServicePrincipal.ApprovePermissionRequestAsync(permissionRequests.First().Id
.ToString());

Assert.IsNotNull(result);
Assert.IsTrue(!string.IsNullOrWhiteSpace(result.ObjectId));
Expand All @@ -105,12 +109,27 @@ public async Task GetPermissionsRequestsTest_Async()
//TestCommon.Instance.Mocking = false;
using (PnPContext context = await TestCommon.Instance.GetContextAsync(TestCommonBase.TestSite))
{
ServicePrincipal principal = new(context);
List<IPermissionRequest> permissionRequests = await principal.GetPermissionRequestsAsync();
ITenantApp app = null;
try
{
var appManager = context.GetTenantAppManager();
app = appManager.Add(packagePath, true);
var deployResult = app.Deploy(false);

Assert.IsTrue(deployResult);

List<IPermissionRequest> permissionRequests =
await appManager.ServicePrincipal.GetPermissionRequestsAsync();

Assert.IsNotNull(permissionRequests);
Assert.IsTrue(permissionRequests.Count > 0);
}
finally
{
var retractResult = app.Retract();
app.Remove();
}
}
}

[TestMethod]
Expand All @@ -125,10 +144,14 @@ public async Task DenyPermissionsRequestTest_Async()
var appManager = context.GetTenantAppManager();
app = appManager.Add(packagePath, true);
var deployResult = app.Deploy(false);
ServicePrincipal principal = new(context);
List<IPermissionRequest> permissionRequests = await principal.GetPermissionRequestsAsync();

await principal.DenyPermissionRequestAsync(permissionRequests.First().Id.ToString());
Assert.IsTrue(deployResult);

List<IPermissionRequest> permissionRequests =
await appManager.ServicePrincipal.GetPermissionRequestsAsync();

await appManager.ServicePrincipal.DenyPermissionRequestAsync(permissionRequests.First().Id
.ToString());
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,14 @@ public async Task<T> AddAsync(string path, bool overwrite = false)
return await AddAsync(bytes, fileInfo.Name, overwrite).ConfigureAwait(false);
}

public IServicePrincipal ServicePrincipal
{
get
{
return new ServicePrincipal(this.context);
}
}

/// <summary>
/// Executes an action and disposes <see cref="PnPContext" /> if it's not the current context
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,10 @@ public interface IAppManager<T> : IAppOperations where T : IApp
/// <param name="overwrite">If true will overwrite an existing entry.</param>
/// <returns>An instance of the app.</returns>
Task<T> AddAsync(string path, bool overwrite = false);

/// <summary>
/// Get the SharePoint Apps ServicePrincipal
/// </summary>
IServicePrincipal ServicePrincipal { get; }
}
}