153 lines
7.0 KiB
Plaintext
153 lines
7.0 KiB
Plaintext
@page "/templates"
|
|
@attribute [Authorize]
|
|
@inject IUserSessionService Session
|
|
@inject IUserContextAccessor UserContext
|
|
@inject ISessionManager SessionMgr
|
|
@inject IElevationCoordinator Elevation
|
|
@inject ITemplateService TemplateSvc
|
|
@inject SharepointToolbox.Web.Infrastructure.Persistence.TemplateRepository TemplateRepo
|
|
@rendermode InteractiveServer
|
|
|
|
<h1 class="page-title">Site Templates</h1>
|
|
<p class="page-subtitle">Capture site structure and apply to new sites.</p>
|
|
|
|
@if (!Session.HasProfile) { <NoProfilePrompt /> return; }
|
|
@if (UserContext.Role < UserRole.TechN1) { <WriteGuard /> return; }
|
|
|
|
<div style="display:grid;grid-template-columns:1fr 1fr;gap:16px">
|
|
<div class="card">
|
|
<div class="card-title">Capture Template</div>
|
|
<SitePicker Profile="Session.CurrentProfile!" @bind-SelectedSites="_captureSites" Single="true" />
|
|
<div class="form-group mt-8">
|
|
<label class="form-label">Template Name</label>
|
|
<input class="form-input" @bind="_captureName" placeholder="My Template" />
|
|
</div>
|
|
<div class="flex-row" style="flex-wrap:wrap">
|
|
<label><input type="checkbox" @bind="_capLibraries" /> Libraries</label>
|
|
<label><input type="checkbox" @bind="_capFolders" /> Folders</label>
|
|
<label><input type="checkbox" @bind="_capGroups" /> Permission groups</label>
|
|
</div>
|
|
<button class="btn btn-primary mt-8" @onclick="CaptureTemplate" disabled="@_running">
|
|
@(_running ? "Capturing…" : "Capture")
|
|
</button>
|
|
<ProgressPanel IsRunning="_running" StatusMessage="@_status" />
|
|
</div>
|
|
|
|
<div class="card">
|
|
<div class="card-title">Apply Template</div>
|
|
@if (_selectedTemplate == null)
|
|
{
|
|
<div class="alert alert-info">Select a template from the list below.</div>
|
|
}
|
|
else
|
|
{
|
|
<div class="alert alert-info">Template: <strong>@_selectedTemplate.Name</strong></div>
|
|
<div class="form-group">
|
|
<label class="form-label">New Site Title</label>
|
|
<input class="form-input" @bind="_newTitle" />
|
|
</div>
|
|
<div class="form-group">
|
|
<label class="form-label">New Site Alias</label>
|
|
<input class="form-input" @bind="_newAlias" />
|
|
</div>
|
|
<div class="form-group">
|
|
<label class="form-label">Admin Center URL</label>
|
|
<input class="form-input" @bind="_adminUrl" placeholder="https://contoso-admin.sharepoint.com" />
|
|
</div>
|
|
<button class="btn btn-primary" @onclick="ApplyTemplate" disabled="@_running">
|
|
@(_running ? "Applying…" : "Apply Template")
|
|
</button>
|
|
}
|
|
</div>
|
|
</div>
|
|
|
|
@if (!string.IsNullOrEmpty(_error)) { <div class="alert alert-error mt-8">@_error</div> }
|
|
@if (!string.IsNullOrEmpty(_successMsg)) { <div class="alert alert-success mt-8">@_successMsg</div> }
|
|
|
|
<div class="card" style="margin-top:16px">
|
|
<div class="card-title">Saved Templates</div>
|
|
@if (_templates.Count == 0)
|
|
{
|
|
<div class="text-muted">No templates saved.</div>
|
|
}
|
|
@foreach (var t in _templates)
|
|
{
|
|
<div class="flex-row" style="padding:8px 0;border-bottom:1px solid var(--border)">
|
|
<div>
|
|
<div style="font-weight:600">@t.Name</div>
|
|
<div class="text-muted">@t.SiteType · @t.CapturedAt.ToString("yyyy-MM-dd") · @t.Libraries.Count libraries</div>
|
|
</div>
|
|
<div class="spacer"></div>
|
|
<button class="btn btn-secondary btn-sm" @onclick="() => _selectedTemplate = t">Use</button>
|
|
<button class="btn btn-danger btn-sm" @onclick="() => DeleteTemplate(t)">Delete</button>
|
|
</div>
|
|
}
|
|
</div>
|
|
|
|
@code {
|
|
private List<SiteInfo> _captureSites = new();
|
|
private string _captureName = string.Empty;
|
|
private bool _capLibraries = true, _capFolders = true, _capGroups = true;
|
|
private SiteTemplate? _selectedTemplate;
|
|
private string _newTitle = string.Empty, _newAlias = string.Empty, _adminUrl = string.Empty;
|
|
private bool _running; private string _status = string.Empty, _error = string.Empty, _successMsg = string.Empty;
|
|
private List<SiteTemplate> _templates = new();
|
|
private CancellationTokenSource? _cts;
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
_templates = (await TemplateRepo.GetAllAsync()).ToList();
|
|
}
|
|
|
|
private async Task CaptureTemplate()
|
|
{
|
|
_error = string.Empty; _successMsg = string.Empty; _running = true;
|
|
_cts = new CancellationTokenSource();
|
|
var siteUrl = _captureSites.FirstOrDefault()?.Url ?? Session.CurrentProfile!.TenantUrl;
|
|
var progress = new Progress<OperationProgress>(p => { _status = p.Message; InvokeAsync(StateHasChanged); });
|
|
try
|
|
{
|
|
var opts = new SiteTemplateOptions { CaptureLibraries = _capLibraries, CaptureFolders = _capFolders, CapturePermissionGroups = _capGroups };
|
|
var template = await Elevation.RunAsync(async c =>
|
|
{
|
|
var ctx = await SessionMgr.GetOrCreateContextAsync(siteUrl, Session.CurrentProfile!, c);
|
|
return await TemplateSvc.CaptureTemplateAsync(ctx, opts, progress, c);
|
|
}, _cts.Token);
|
|
template.Name = string.IsNullOrWhiteSpace(_captureName) ? $"Template-{DateTime.Now:yyyyMMdd}" : _captureName;
|
|
await TemplateRepo.SaveAsync(template);
|
|
_templates = (await TemplateRepo.GetAllAsync()).ToList();
|
|
_successMsg = $"Template '{template.Name}' saved.";
|
|
}
|
|
catch (OperationCanceledException) { _status = "Cancelled."; }
|
|
catch (Exception ex) { _error = ex.Message; }
|
|
finally { _running = false; await InvokeAsync(StateHasChanged); }
|
|
}
|
|
|
|
private async Task ApplyTemplate()
|
|
{
|
|
if (_selectedTemplate == null) return;
|
|
_error = string.Empty; _successMsg = string.Empty; _running = true;
|
|
_cts = new CancellationTokenSource();
|
|
var adminUrl = string.IsNullOrWhiteSpace(_adminUrl)
|
|
? Session.CurrentProfile!.TenantUrl.Replace(".sharepoint.com", "-admin.sharepoint.com")
|
|
: _adminUrl.Trim();
|
|
var progress = new Progress<OperationProgress>(p => { _status = p.Message; InvokeAsync(StateHasChanged); });
|
|
try
|
|
{
|
|
var ctx = await SessionMgr.GetOrCreateContextAsync(adminUrl, Session.CurrentProfile!, _cts.Token);
|
|
var url = await TemplateSvc.ApplyTemplateAsync(ctx, _selectedTemplate, _newTitle, _newAlias, progress, _cts.Token);
|
|
_successMsg = $"Site created: {url}";
|
|
}
|
|
catch (OperationCanceledException) { _status = "Cancelled."; }
|
|
catch (Exception ex) { _error = ex.Message; }
|
|
finally { _running = false; await InvokeAsync(StateHasChanged); }
|
|
}
|
|
|
|
private async Task DeleteTemplate(SiteTemplate t)
|
|
{
|
|
await TemplateRepo.DeleteAsync(t.Id);
|
|
_templates.RemoveAll(x => x.Id == t.Id);
|
|
if (_selectedTemplate?.Id == t.Id) _selectedTemplate = null;
|
|
}
|
|
}
|