28 lines
1.1 KiB
C#
28 lines
1.1 KiB
C#
using Microsoft.Online.SharePoint.TenantAdministration;
|
|
using Microsoft.SharePoint.Client;
|
|
using SharepointToolbox.Web.Services.Audit;
|
|
|
|
namespace SharepointToolbox.Web.Services;
|
|
|
|
public class OwnershipElevationService : IOwnershipElevationService
|
|
{
|
|
private readonly IAuditService _audit;
|
|
|
|
public OwnershipElevationService(IAuditService audit) { _audit = audit; }
|
|
|
|
public async Task ElevateAsync(ClientContext tenantAdminCtx, string siteUrl, string loginName, CancellationToken ct)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(loginName))
|
|
{
|
|
tenantAdminCtx.Load(tenantAdminCtx.Web.CurrentUser, u => u.LoginName);
|
|
await tenantAdminCtx.ExecuteQueryAsync();
|
|
loginName = tenantAdminCtx.Web.CurrentUser.LoginName;
|
|
}
|
|
var tenant = new Tenant(tenantAdminCtx);
|
|
tenant.SetSiteAdmin(siteUrl, loginName, isSiteAdmin: true);
|
|
await tenantAdminCtx.ExecuteQueryAsync();
|
|
await _audit.LogAsync("ElevateOwnership", tenantAdminCtx.Url, new[] { siteUrl },
|
|
$"Site admin granted to {loginName}");
|
|
}
|
|
}
|