feat: Kh component library, record pages and profile security (M3, part 2)
Adds the building blocks every later screen uses, so modules are built from one set of components instead of ad hoc markup: - Form components (KhForm, KhTextBox, KhNumberBox, KhDateBox, KhSelect, KhCheck, KhSwitch, KhHotlink) with Italian number and date input, inline calculation (=10*3), field messages plus a summary that focuses the field, and service errors added by field name. - Display and window components (panels, tiles, badges, chips, money, tabs with Alt+1..9, steps, dialog, side drawer and DrawerService). - KhIconToolbar that keeps one line and moves less used commands into the "..." menu, KhDocHead, and the RecordPage/DocumentPage/MasterDataPage bases with standard commands, shortcuts and save/cancel/delete flow (#27, #29). - Development-only component catalog and sample document page. - Profile: personal data, password change that ends other sessions, TOTP enable/disable and recovery codes through ProfileService (#31). Refs #27 #29 #31 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
cf6f849cc1
commit
d958e7372d
57 files changed
+5249
-12
No files matched your search
@@ -0,0 +1,315 @@
|
||||
@page "/sviluppo/componenti"
|
||||
@attribute [Authorize]
|
||||
@inject IWebHostEnvironment Environment
|
||||
@inject ToastService Toasts
|
||||
@inject DialogService Dialogs
|
||||
@inject DrawerService Drawers
|
||||
@inject TimeProvider Clock
|
||||
|
||||
@* The catalog of the Kh components with all their states (issue #27): development only. *@
|
||||
<KhPageTitle Text="@T["Pages.Catalog.Title"]" Icon="grid" />
|
||||
|
||||
@if (!Environment.IsDevelopment())
|
||||
{
|
||||
<div class="kh-message">
|
||||
<h1>@T["Pages.Catalog.DevOnly"]</h1>
|
||||
<a class="btn" href="/">@T["Common.BackHome"]</a>
|
||||
</div>
|
||||
}
|
||||
else
|
||||
{
|
||||
<div class="phead">
|
||||
<div>
|
||||
<h1>@T["Pages.Catalog.Title"]</h1>
|
||||
<div class="sub">@T["Pages.Catalog.Subtitle"]</div>
|
||||
</div>
|
||||
<div class="grow"></div>
|
||||
<a class="btn" href="sviluppo/documento"><KhIcon Name="file" /> @T["Pages.Catalog.OpenDocument"]</a>
|
||||
</div>
|
||||
|
||||
<KhTabs Items="Sections" Active="@_section" ActiveChanged="s => _section = s" Label="@T["Pages.Catalog.Title"]" />
|
||||
|
||||
@switch (_section)
|
||||
{
|
||||
case "campi":
|
||||
<KhForm Validation="_validation" ReadOnly="_readOnly" Layout="KhFormLayout.Groups" OnValidSubmit="OnValid" @ref="_form">
|
||||
<KhFieldGroup Title="@T["Pages.Catalog.Fields.Document"]" Icon="file">
|
||||
<KhTextBox Label="@T["Pages.Catalog.Fields.Number"]" Name="number" @bind-Value="_document.Number" Required="true" Span="6" Mono="true" />
|
||||
<KhDateBox Label="@T["Pages.Catalog.Fields.Date"]" Name="date" @bind-Value="_document.Date" Required="true" Span="6" Hint="@T["Pages.Catalog.Fields.DateHint"]" />
|
||||
<KhSelect TValue="string" Label="@T["Pages.Catalog.Fields.Kind"]" Name="kind" @bind-Value="_kind" Options="Kinds" Required="true" Empty="" Span="12" />
|
||||
<KhHotlink TItem="SampleParty" Label="@T["Pages.Catalog.Fields.Party"]" Name="party" @bind-Value="_document.Party" Required="true" Span="12"
|
||||
Search="CatalogSamples.SearchAsync" CodeOf="p => p.Code" DescriptionOf="p => p.Name"
|
||||
OnRadar="Radar" OnOpen="OpenParty" OnCreate="CreateParty" Hint="@T["Pages.Catalog.Fields.PartyHint"]" />
|
||||
</KhFieldGroup>
|
||||
<KhFieldGroup Title="@T["Pages.Catalog.Fields.Conditions"]" Icon="coins">
|
||||
<KhNumberBox Label="@T["Pages.Catalog.Fields.Discount"]" Name="discount" @bind-Value="_document.Discount" Min="0" Max="100" Span="6" Hint="@T["Pages.Catalog.Fields.DiscountHint"]" />
|
||||
<KhNumberBox Label="@T["Pages.Catalog.Fields.Amount"]" Name="amount" @bind-Value="_amount" Span="6" Hint="@T["Pages.Catalog.Fields.AmountHint"]" />
|
||||
<KhTextBox Label="@T["Pages.Catalog.Fields.Email"]" Name="email" Type="email" @bind-Value="_email" Span="12" Rule="EmailRule" />
|
||||
<KhCheck Label="@T["Pages.Catalog.Fields.Split"]" Name="split" @bind-Value="_split" Span="6" />
|
||||
<KhSwitch Label="@T["Pages.Catalog.Fields.Stamp"]" Name="stamp" @bind-Value="_stamp" Span="6" />
|
||||
<KhTextArea Label="@T["Pages.Catalog.Fields.Notes"]" Name="notes" @bind-Value="_document.Notes" Span="12" Rows="2" />
|
||||
<KhTextBox Label="@T["Pages.Catalog.Fields.Disabled"]" Value="@T["Pages.Catalog.Fields.DisabledValue"]" Disabled="true" Span="12" />
|
||||
</KhFieldGroup>
|
||||
<section class="fgroup wide">
|
||||
<div class="fgh"><KhIcon Name="check-circle" /> @T["Pages.Catalog.Fields.States"]</div>
|
||||
<div class="kh-catalogbar">
|
||||
<button type="submit" class="btn primary"><KhIcon Name="check" /> @T["Pages.Catalog.Fields.Validate"]</button>
|
||||
<button type="button" class="btn" @onclick="ServerError"><KhIcon Name="alert" /> @T["Pages.Catalog.Fields.ServerError"]</button>
|
||||
<button type="button" class="btn" @onclick="() => _validation.Clear()">@T["Pages.Catalog.Fields.ClearMessages"]</button>
|
||||
<KhSwitch Label="@T["Pages.Catalog.Fields.ReadOnly"]" Value="_readOnly" ValueChanged="v => _readOnly = v" Span="0" />
|
||||
</div>
|
||||
</section>
|
||||
</KhForm>
|
||||
break;
|
||||
|
||||
case "pannelli":
|
||||
<div class="kh-catalog">
|
||||
<div class="cols cols-4">
|
||||
<KhTile Title="@T["Pages.Catalog.Panels.Revenue"]" Icon="euro" Value="@T.Money(128_450.30m)" Tone="KhTone.Accent">
|
||||
<Detail>@T["Pages.Catalog.Panels.RevenueDetail"]</Detail>
|
||||
</KhTile>
|
||||
<KhTile Title="@T["Pages.Catalog.Panels.Collected"]" Icon="check-circle" Value="@T.Money(96_120m)" Tone="KhTone.Ok" />
|
||||
<KhTile Title="@T["Pages.Catalog.Panels.Expiring"]" Icon="clock" Value="@T.Money(12_300m)" Tone="KhTone.Warning" />
|
||||
<KhTile Title="@T["Pages.Catalog.Panels.Overdue"]" Icon="alert" Value="@T.Money(4_870.55m)" Tone="KhTone.Error" Href="sviluppo/componenti" />
|
||||
</div>
|
||||
<KhStrip Items="Stats" Label="@T["Pages.Catalog.Panels.Strip"]" />
|
||||
<KhPanel Title="@T["Pages.Catalog.Panels.Panel"]" Icon="layers" LinkHref="sviluppo/componenti" LinkText="@T["Pages.Catalog.Panels.Link"]">
|
||||
<div class="kh-catalogrow">
|
||||
<KhBadge Tone="KhTone.Neutral">@T["Pages.Catalog.Panels.Draft"]</KhBadge>
|
||||
<KhBadge Tone="KhTone.Info">@T["Pages.Catalog.Panels.Sent"]</KhBadge>
|
||||
<KhBadge Tone="KhTone.Ok">@T["Pages.Catalog.Panels.Paid"]</KhBadge>
|
||||
<KhBadge Tone="KhTone.Warning">@T["Pages.Catalog.Panels.Expiring"]</KhBadge>
|
||||
<KhBadge Tone="KhTone.Error">@T["Pages.Catalog.Panels.Overdue"]</KhBadge>
|
||||
<KhBadge Tone="KhTone.Accent">@T["Pages.Catalog.Panels.Issued"]</KhBadge>
|
||||
</div>
|
||||
<div class="kh-catalogrow">
|
||||
<KhChip Icon="coins" Text="@T["Pages.Catalog.Panels.Payment"]" Value="@T["Pages.Catalog.Panels.PaymentValue"]" />
|
||||
<KhChip Icon="tag" Text="@T["Pages.Catalog.Panels.PriceList"]" Value="@T["Pages.Catalog.Panels.PriceListValue"]" Tone="KhTone.Accent" />
|
||||
<KhChip Icon="alert" Text="@T["Pages.Catalog.Panels.Credit"]" Tone="KhTone.Warning" />
|
||||
<KhChip Icon="x-circle" Text="@T["Pages.Catalog.Panels.Blocked"]" Tone="KhTone.Error" />
|
||||
@if (_filter)
|
||||
{
|
||||
<KhChip Text="@T["Pages.Catalog.Panels.Filter"]" Value="@T["Pages.Catalog.Panels.FilterValue"]" OnRemove="() => _filter = false" />
|
||||
}
|
||||
</div>
|
||||
<div class="kh-catalogrow">
|
||||
<KhMoney Value="1234.5m" /> <KhMoney Value="-87.2m" RedWhenNegative="true" /> <KhMoney Value="null" Empty="-" />
|
||||
</div>
|
||||
</KhPanel>
|
||||
</div>
|
||||
break;
|
||||
|
||||
case "navigazione":
|
||||
<div class="kh-catalog">
|
||||
<KhPanel Title="@T["Pages.Catalog.Navigation.Tabs"]" Icon="columns" Tight="true">
|
||||
<KhTabs Items="DemoTabs" Active="@_demoTab" ActiveChanged="t => _demoTab = t" Shortcuts="false" />
|
||||
<div class="pb">@T.Format("Pages.Catalog.Navigation.TabShown", DemoTabs.First(t => t.Id == _demoTab).Text)</div>
|
||||
</KhPanel>
|
||||
<KhPanel Title="@T["Pages.Catalog.Navigation.Steps"]" Icon="activity">
|
||||
<KhSteps Items="Steps" Current="_step" Label="@T["Pages.Catalog.Navigation.Steps"]" />
|
||||
<div class="cols cols-2">
|
||||
<KhVSteps Items="Steps" Current="_step" Label="@T["Pages.Catalog.Navigation.Steps"]" />
|
||||
<div class="kh-catalogrow">
|
||||
<button type="button" class="btn" disabled="@(_step == 0)" @onclick="() => _step--"><KhIcon Name="chevron-left" /> @T["Common.Back"]</button>
|
||||
<button type="button" class="btn primary" disabled="@(_step >= Steps.Count)" @onclick="() => _step++">@T["Common.Continue"] <KhIcon Name="chevron-right" /></button>
|
||||
</div>
|
||||
</div>
|
||||
</KhPanel>
|
||||
</div>
|
||||
break;
|
||||
|
||||
case "finestre":
|
||||
<div class="kh-catalog">
|
||||
<KhPanel Title="@T["Pages.Catalog.Windows.Title"]" Icon="layout">
|
||||
<div class="kh-catalogrow">
|
||||
<button type="button" class="btn" @onclick="() => _dialog = true"><KhIcon Name="layout" /> @T["Pages.Catalog.Windows.Dialog"]</button>
|
||||
<button type="button" class="btn" @onclick="() => _drawer = true"><KhIcon Name="sidebar" /> @T["Pages.Catalog.Windows.Drawer"]</button>
|
||||
<button type="button" class="btn" @onclick="ServiceDrawerAsync"><KhIcon Name="sidebar" /> @T["Pages.Catalog.Windows.DrawerService"]</button>
|
||||
<button type="button" class="btn danger" @onclick="ConfirmAsync"><KhIcon Name="trash" /> @T["Pages.Catalog.Windows.Confirm"]</button>
|
||||
</div>
|
||||
<div class="kh-catalogrow">
|
||||
@foreach (var kind in Enum.GetValues<ToastKind>())
|
||||
{
|
||||
<button type="button" class="btn" @onclick="() => ShowToast(kind)">@T[KindKey(kind)]</button>
|
||||
}
|
||||
</div>
|
||||
</KhPanel>
|
||||
</div>
|
||||
<KhDialog Open="_dialog" Title="@T["Pages.Catalog.Windows.Dialog"]" Icon="layout" OnClose="() => _dialog = false">
|
||||
<ChildContent>
|
||||
<p>@T["Pages.Catalog.Windows.DialogText"]</p>
|
||||
<KhForm Layout="KhFormLayout.Plain">
|
||||
<KhTextBox Label="@T["Pages.Catalog.Fields.Number"]" @bind-Value="_dialogText" Span="12" />
|
||||
</KhForm>
|
||||
</ChildContent>
|
||||
<Footer>
|
||||
<button type="button" class="btn" @onclick="() => _dialog = false">@T["Common.Cancel"]</button>
|
||||
<button type="button" class="btn primary" @onclick="() => _dialog = false">@T["Common.Save"]</button>
|
||||
</Footer>
|
||||
</KhDialog>
|
||||
<KhDrawer Open="_drawer" Title="@T["Pages.Catalog.Windows.Drawer"]" Icon="sidebar" OnClose="() => _drawer = false">
|
||||
<p>@T["Pages.Catalog.Windows.DrawerText"]</p>
|
||||
<KhStrip Items="Stats" />
|
||||
</KhDrawer>
|
||||
break;
|
||||
|
||||
case "toolbar":
|
||||
<div class="kh-catalog">
|
||||
<p class="muted">@T["Pages.Catalog.Toolbar.Intro"]</p>
|
||||
<div class="kh-resizable">
|
||||
<KhIconToolbar Items="DemoToolbar" Label="@T["Ui.Toolbar.Label"]" />
|
||||
</div>
|
||||
<KhDocHead KindIcon="file" KindLabel="@T["Pages.Catalog.Head.Kind"]" Number="@_document.Number"
|
||||
PartyName="@_document.Party?.Name" PartyCode="@_document.Party?.Code" PartyLines="PartyLines"
|
||||
TotalLabel="@T["Pages.Catalog.Head.Total"]" TotalValue="@T.Money(_document.Total)" SubValues="SubValues">
|
||||
<Facts>
|
||||
<span>@T["Pages.Catalog.Fields.Date"] <b>@(_document.Date is { } d ? T.Date(d) : null)</b></span>
|
||||
<span>@T["Pages.Catalog.Head.Section"] <b>A</b></span>
|
||||
</Facts>
|
||||
<State><KhBadge Tone="KhTone.Info">@T["Pages.Catalog.Panels.Draft"]</KhBadge></State>
|
||||
<Chips>
|
||||
<KhChip Icon="coins" Text="@T["Pages.Catalog.Panels.Payment"]" Value="@T["Pages.Catalog.Panels.PaymentValue"]" />
|
||||
<KhChip Icon="tag" Text="@T["Pages.Catalog.Panels.PriceList"]" Value="@T["Pages.Catalog.Panels.PriceListValue"]" Tone="KhTone.Accent" />
|
||||
<KhChip Icon="alert" Text="@T["Pages.Catalog.Panels.Credit"]" Tone="KhTone.Warning" />
|
||||
</Chips>
|
||||
</KhDocHead>
|
||||
</div>
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@code {
|
||||
private readonly KhValidation _validation = new();
|
||||
private SampleDocument _document = default!;
|
||||
private KhForm? _form;
|
||||
private string _section = "campi";
|
||||
private string _demoTab = "righe";
|
||||
private string? _kind;
|
||||
private string? _email;
|
||||
private string? _dialogText;
|
||||
private decimal? _amount;
|
||||
private bool _split;
|
||||
private bool _stamp = true;
|
||||
private bool _readOnly;
|
||||
private bool _filter = true;
|
||||
private bool _dialog;
|
||||
private bool _drawer;
|
||||
private int _step = 1;
|
||||
|
||||
private IReadOnlyList<KhTabItem> Sections =>
|
||||
[
|
||||
new("campi", "edit", T["Pages.Catalog.Sections.Fields"]),
|
||||
new("pannelli", "layers", T["Pages.Catalog.Sections.Panels"]),
|
||||
new("navigazione", "columns", T["Pages.Catalog.Sections.Navigation"]),
|
||||
new("finestre", "layout", T["Pages.Catalog.Sections.Windows"]),
|
||||
new("toolbar", "settings", T["Pages.Catalog.Sections.Toolbar"]),
|
||||
];
|
||||
|
||||
private IReadOnlyList<KhOption<string>> Kinds =>
|
||||
[
|
||||
new("TD01", T["Pages.Catalog.Fields.KindInvoice"]),
|
||||
new("TD04", T["Pages.Catalog.Fields.KindCredit"]),
|
||||
new("TD24", T["Pages.Catalog.Fields.KindDeferred"]),
|
||||
];
|
||||
|
||||
private IReadOnlyList<KhStat> Stats =>
|
||||
[
|
||||
new(T["Pages.Catalog.Panels.Documents"], "128"),
|
||||
new(T["Pages.Catalog.Panels.Taxable"], T.Money(104_000m)),
|
||||
new(T["Pages.Catalog.Panels.Collected"], T.Money(96_120m), KhTone.Ok),
|
||||
new(T["Pages.Catalog.Panels.Overdue"], T.Money(4_870.55m), KhTone.Error),
|
||||
];
|
||||
|
||||
private IReadOnlyList<KhTabItem> DemoTabs =>
|
||||
[
|
||||
new("righe", "list", T["Ui.Record.Tabs.Lines"], 3),
|
||||
new("testata", "file", T["Ui.Record.Tabs.Header"]),
|
||||
new("allegati", "paperclip", T["Ui.Record.Tabs.Attachments"], 2),
|
||||
new("storico", "history", T["Ui.Record.Tabs.History"]),
|
||||
];
|
||||
|
||||
private IReadOnlyList<KhStep> Steps =>
|
||||
[
|
||||
new(T["Pages.Catalog.Navigation.Step1"], T["Pages.Catalog.Navigation.Step1Detail"]),
|
||||
new(T["Pages.Catalog.Navigation.Step2"], T["Pages.Catalog.Navigation.Step2Detail"]),
|
||||
new(T["Pages.Catalog.Navigation.Step3"], T["Pages.Catalog.Navigation.Step3Detail"]),
|
||||
];
|
||||
|
||||
private IReadOnlyList<string> PartyLines => [T["Pages.Catalog.Head.Address"], T["Pages.Catalog.Head.Vat"]];
|
||||
|
||||
private IReadOnlyList<KhStat> SubValues =>
|
||||
[
|
||||
new(T["Pages.Catalog.Panels.Taxable"], T.Money(_document.Taxable)),
|
||||
new(T["Pages.Catalog.Head.VatAmount"], T.Money(_document.Vat)),
|
||||
new(T["Pages.Catalog.Head.Stamp"], T.Money(2m)),
|
||||
];
|
||||
|
||||
private IReadOnlyList<ToolbarItem> DemoToolbar =>
|
||||
[
|
||||
Item("new", "plus", "Ui.Toolbar.New", "Ctrl+N") with { Priority = 0 },
|
||||
Item("save", "save", "Ui.Toolbar.Save", "Ctrl+S") with { Primary = true, Priority = 0 },
|
||||
Item("cancel", "undo", "Ui.Toolbar.Cancel", null) with { Priority = 2 },
|
||||
Item("delete", "trash", "Ui.Toolbar.Delete", "Ctrl+Delete") with { Danger = true, Priority = 3 },
|
||||
Item("copy", "copy", "Ui.Toolbar.Duplicate", null) with { Group = 1, Priority = 4 },
|
||||
Item("print", "printer", "Ui.Toolbar.Print", "Ctrl+P") with { Group = 1, Priority = 4 },
|
||||
Item("mail", "mail", "Ui.Toolbar.Email", null) with { Group = 1, Priority = 5 },
|
||||
Item("export", "download", "Ui.Toolbar.Export", null) with { Group = 1, Priority = 5 },
|
||||
Item("attachments", "paperclip", "Ui.Toolbar.Attachments", null) with { Group = 2, Count = 2, Priority = 5 },
|
||||
Item("history", "history", "Ui.Toolbar.History", null) with { Group = 2, Priority = 6 },
|
||||
Item("links", "link", "Ui.Toolbar.Links", null) with { Group = 2, Count = 1, Priority = 6 },
|
||||
Item("issue", "send", "Pages.Catalog.Toolbar.Issue", null) with { Kind = ToolbarKind.Action, Primary = true, Priority = 1 },
|
||||
Item("xml", "file-check", "Pages.Catalog.Toolbar.Xml", null) with { Kind = ToolbarKind.Action, Priority = 3 },
|
||||
Item("post", "book", "Pages.Catalog.Toolbar.Post", null) with { Kind = ToolbarKind.Action, Tone = KhTone.Ok, Priority = 3 },
|
||||
Item("ddt", "truck", "Pages.Catalog.Toolbar.Ddt", null) with { Kind = ToolbarKind.Action, Priority = 4 },
|
||||
Item("final", "lock", "Pages.Catalog.Toolbar.Final", null) with { Kind = ToolbarKind.Action, Priority = 4 },
|
||||
Item("radar", "radar", "Ui.Toolbar.Radar", "F9") with { End = true, Priority = 6 },
|
||||
Item("previous", "chevron-up", "Ui.Toolbar.Previous", "Ctrl+ArrowUp") with { End = true, Priority = 7 },
|
||||
Item("next", "chevron-down", "Ui.Toolbar.Next", "Ctrl+ArrowDown") with { End = true, Priority = 7 },
|
||||
Item("settings", "settings", "Pages.Catalog.Toolbar.Settings", null) with { Kind = ToolbarKind.Menu },
|
||||
];
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void OnInitialized() =>
|
||||
_document = CatalogSamples.Document(DateOnly.FromDateTime(TimeZoneInfo.ConvertTime(Clock.GetUtcNow(), TimeZoneInfo.Local).DateTime));
|
||||
|
||||
private ToolbarItem Item(string id, string icon, string key, string? shortcut) =>
|
||||
new(id, icon, T[key]) { Shortcut = shortcut, OnClick = () => Clicked(key) };
|
||||
|
||||
private Task Clicked(string key)
|
||||
{
|
||||
Toasts.Show(ToastKind.Info, T.Format("Pages.Catalog.Toolbar.Clicked", T[key]));
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private string? EmailRule(string? value) =>
|
||||
string.IsNullOrEmpty(value) || value.Contains('@', StringComparison.Ordinal) ? null : T["Auth.Errors.email_invalid"];
|
||||
|
||||
private void OnValid() => Toasts.Show(ToastKind.Success, T["Pages.Catalog.Fields.Valid"]);
|
||||
|
||||
private void ServerError() => _validation.Add("number", T["Pages.Catalog.Fields.NumberTaken"]);
|
||||
|
||||
private void Radar(string text) => Toasts.Show(ToastKind.Info, T.Format("Pages.Catalog.Fields.RadarOpened", text));
|
||||
|
||||
private void OpenParty(SampleParty party) => Toasts.Show(ToastKind.Info, T.Format("Pages.Catalog.Fields.PartyOpened", party.Name));
|
||||
|
||||
private void CreateParty(string text)
|
||||
{
|
||||
_document.Party = new SampleParty("C9999", text);
|
||||
Toasts.Show(ToastKind.Success, T.Format("Pages.Catalog.Fields.PartyCreated", text));
|
||||
}
|
||||
|
||||
private Task ServiceDrawerAsync() => Drawers.ShowAsync(new DrawerRequest(
|
||||
T["Pages.Catalog.Windows.DrawerService"], "sidebar",
|
||||
@<div><p>@T["Pages.Catalog.Windows.DrawerServiceText"]</p><KhStrip Items="Stats" /></div>,
|
||||
@<button type="button" class="btn primary" @onclick="Drawers.Close">@T["Common.Close"]</button>));
|
||||
|
||||
private async Task ConfirmAsync()
|
||||
{
|
||||
var yes = await Dialogs.ConfirmAsync(new(T["Ui.Record.DeleteTitle"], T["Ui.Record.DeleteText"], T["Ui.Toolbar.Delete"], T["Common.Cancel"], Danger: true));
|
||||
Toasts.Show(yes ? ToastKind.Warning : ToastKind.Info, T[yes ? "Pages.Catalog.Windows.Confirmed" : "Pages.Catalog.Windows.Cancelled"]);
|
||||
}
|
||||
|
||||
private static string KindKey(ToastKind kind) => "Pages.Catalog.Windows.Toast." + kind;
|
||||
|
||||
private void ShowToast(ToastKind kind) => Toasts.Show(kind, T[KindKey(kind)], T["Pages.Catalog.Windows.ToastText"]);
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
@page "/sviluppo/documento"
|
||||
@attribute [Authorize]
|
||||
@inherits DocumentPage<SampleDocument, SampleLine>
|
||||
@inject IWebHostEnvironment Environment
|
||||
@inject TimeProvider Clock
|
||||
|
||||
@* A document page built on DocumentPage (issue #29): toolbar, header, tabs with Righe first, shortcuts. Fake data,
|
||||
development only. *@
|
||||
<KhPageTitle Text="@T["Pages.Catalog.Document"]" Icon="file" />
|
||||
|
||||
@if (!Environment.IsDevelopment() || Record is null)
|
||||
{
|
||||
<div class="kh-message"><h1>@T["Pages.Catalog.DevOnly"]</h1></div>
|
||||
}
|
||||
else
|
||||
{
|
||||
<KhRecordLayout Page="this">
|
||||
<Heading>
|
||||
<KhDocHead KindIcon="file" KindLabel="@T["Pages.Catalog.Head.Kind"]" Number="@Record.Number"
|
||||
PartyName="@Record.Party?.Name" PartyCode="@Record.Party?.Code" PartyLines="PartyLines"
|
||||
TotalLabel="@T["Pages.Catalog.Head.Total"]" TotalValue="@T.Money(Record.Total)" SubValues="SubValues">
|
||||
<Facts>
|
||||
<span>@T["Pages.Catalog.Fields.Date"] <b>@(Record.Date is { } d ? T.Date(d) : null)</b></span>
|
||||
</Facts>
|
||||
<State>
|
||||
@if (Record.Issued)
|
||||
{
|
||||
<KhBadge Tone="KhTone.Accent">@T["Pages.Catalog.Panels.Issued"]</KhBadge>
|
||||
}
|
||||
else
|
||||
{
|
||||
<KhBadge Tone="KhTone.Info">@T["Pages.Catalog.Panels.Draft"]</KhBadge>
|
||||
}
|
||||
@if (IsModified)
|
||||
{
|
||||
<KhBadge Tone="KhTone.Warning">@T["Pages.Catalog.Modified"]</KhBadge>
|
||||
}
|
||||
</State>
|
||||
<Chips>
|
||||
<KhChip Icon="coins" Text="@T["Pages.Catalog.Panels.Payment"]" Value="@T["Pages.Catalog.Panels.PaymentValue"]" />
|
||||
</Chips>
|
||||
</KhDocHead>
|
||||
</Heading>
|
||||
<ChildContent>
|
||||
@switch (ActiveTab)
|
||||
{
|
||||
case TabIds.Lines:
|
||||
<table class="grid">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">@T["Pages.Catalog.Lines.Code"]</th>
|
||||
<th scope="col">@T["Pages.Catalog.Lines.Description"]</th>
|
||||
<th scope="col" class="num">@T["Pages.Catalog.Lines.Quantity"]</th>
|
||||
<th scope="col" class="num">@T["Pages.Catalog.Lines.Price"]</th>
|
||||
<th scope="col" class="num">@T["Pages.Catalog.Lines.Amount"]</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var line in Record.Lines)
|
||||
{
|
||||
<tr>
|
||||
<td class="code">@line.Code</td>
|
||||
<td>@line.Description</td>
|
||||
<td class="num">@T.Number(line.Quantity ?? 0, 2)</td>
|
||||
<td class="num"><KhMoney Value="line.Price" /></td>
|
||||
<td class="num"><KhMoney Value="line.Amount" /></td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
break;
|
||||
|
||||
case TabIds.Header:
|
||||
<KhForm Validation="Validation" ReadOnly="IsReadOnly" Layout="KhFormLayout.Groups" @ref="Form">
|
||||
<KhFieldGroup Title="@T["Pages.Catalog.Fields.Document"]" Icon="file">
|
||||
<KhTextBox Label="@T["Pages.Catalog.Fields.Number"]" Name="number" Value="@Record.Number" ValueChanged="v => Change(() => Record.Number = v ?? string.Empty)" Required="true" Mono="true" />
|
||||
<KhDateBox Label="@T["Pages.Catalog.Fields.Date"]" Name="date" Value="Record.Date" ValueChanged="v => Change(() => Record.Date = v)" Required="true" />
|
||||
<KhHotlink TItem="SampleParty" Label="@T["Pages.Catalog.Fields.Party"]" Name="party" Value="Record.Party" ValueChanged="v => Change(() => Record.Party = v)"
|
||||
Required="true" Span="12" Search="CatalogSamples.SearchAsync" CodeOf="p => p.Code" DescriptionOf="p => p.Name" />
|
||||
</KhFieldGroup>
|
||||
<KhFieldGroup Title="@T["Pages.Catalog.Fields.Conditions"]" Icon="coins">
|
||||
<KhNumberBox Label="@T["Pages.Catalog.Fields.Discount"]" Name="discount" Value="Record.Discount" ValueChanged="v => Change(() => Record.Discount = v)" Min="0" Max="100" />
|
||||
<KhTextArea Label="@T["Pages.Catalog.Fields.Notes"]" Name="notes" Value="@Record.Notes" ValueChanged="v => Change(() => Record.Notes = v)" Span="12" Rows="2" />
|
||||
</KhFieldGroup>
|
||||
</KhForm>
|
||||
break;
|
||||
|
||||
default:
|
||||
<div class="pb"><p class="muted">@T["Pages.Catalog.TabPlaceholder"]</p></div>
|
||||
break;
|
||||
}
|
||||
</ChildContent>
|
||||
</KhRecordLayout>
|
||||
}
|
||||
|
||||
@code {
|
||||
private int _saved;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override IReadOnlyList<KhTabItem> Tabs => Record is null ? [] : StandardTabs(attachments: 2, links: 1);
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool IsReadOnly => Record?.Issued == true;
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override IList<SampleLine> Lines => Record?.Lines ?? [];
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override string? NewUrl => "sviluppo/documento";
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override int? AttachmentCount => 2;
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override bool CanMove => true;
|
||||
|
||||
private IReadOnlyList<string> PartyLines => [T["Pages.Catalog.Head.Address"], T["Pages.Catalog.Head.Vat"]];
|
||||
|
||||
private IReadOnlyList<KhStat> SubValues => Record is null ? [] :
|
||||
[
|
||||
new(T["Pages.Catalog.Panels.Taxable"], T.Money(Record.Taxable)),
|
||||
new(T["Pages.Catalog.Head.VatAmount"], T.Money(Record.Vat)),
|
||||
];
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
base.OnInitialized();
|
||||
Record = CatalogSamples.Document(Today);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override IEnumerable<ToolbarItem> ContextActions()
|
||||
{
|
||||
yield return new ToolbarItem("issue", "send", T["Pages.Catalog.Toolbar.Issue"])
|
||||
{
|
||||
Kind = ToolbarKind.Action, Primary = true, Disabled = IsReadOnly || IsModified, OnClick = IssueAsync,
|
||||
};
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override Task<bool> SaveCoreAsync()
|
||||
{
|
||||
// A pretend rule of the service: the number must keep the year prefix.
|
||||
if (Record is null || !Record.Number.StartsWith("2026/", StringComparison.Ordinal))
|
||||
{
|
||||
Validation.Add("number", T["Pages.Catalog.Fields.NumberTaken"]);
|
||||
return Task.FromResult(false);
|
||||
}
|
||||
|
||||
_saved++;
|
||||
return Task.FromResult(true);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override Task ReloadAsync()
|
||||
{
|
||||
Record = CatalogSamples.Document(Today);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override Task MoveAsync(int direction)
|
||||
{
|
||||
Toasts.Show(ToastKind.Info, T[direction < 0 ? "Ui.Toolbar.Previous" : "Ui.Toolbar.Next"]);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override Task RadarAsync()
|
||||
{
|
||||
Toasts.Show(ToastKind.Info, T["Ui.Toolbar.Radar"]);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private DateOnly Today => DateOnly.FromDateTime(TimeZoneInfo.ConvertTime(Clock.GetUtcNow(), TimeZoneInfo.Local).DateTime);
|
||||
|
||||
private void Change(Action change)
|
||||
{
|
||||
change();
|
||||
MarkModified();
|
||||
}
|
||||
|
||||
private Task IssueAsync()
|
||||
{
|
||||
if (Record is not null)
|
||||
{
|
||||
Record.Issued = true;
|
||||
Toasts.Show(ToastKind.Success, T["Pages.Catalog.Panels.Issued"]);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
@using Khaemhat.Application.Preferences
|
||||
@using Khaemhat.Application.Security
|
||||
@using Khaemhat.Web.Security
|
||||
@using Khaemhat.Web.Components.Pages.ProfileTabs
|
||||
@implements IAsyncDisposable
|
||||
@inject ShellSession Session
|
||||
@inject ThemeService Theme
|
||||
@@ -176,6 +177,14 @@
|
||||
</div>
|
||||
break;
|
||||
|
||||
case AccountSection:
|
||||
<ProfileAccount Owner="_owner" />
|
||||
break;
|
||||
|
||||
case SecuritySection:
|
||||
<ProfileSecurity Owner="_owner" CurrentSessionId="CurrentSession" />
|
||||
break;
|
||||
|
||||
case SessionsSection:
|
||||
<div class="pb">
|
||||
<p class="muted">@T["Pages.Profile.Sessions.Intro"]</p>
|
||||
@@ -238,9 +247,16 @@
|
||||
{
|
||||
<tr>
|
||||
<td>
|
||||
@foreach (var keys in shortcut.Keys)
|
||||
@if (shortcut.Display is { } display)
|
||||
{
|
||||
<kbd>@keys</kbd>
|
||||
<kbd>@display</kbd>
|
||||
}
|
||||
else
|
||||
{
|
||||
@foreach (var keys in shortcut.Keys)
|
||||
{
|
||||
<kbd>@KeyboardShortcuts.Pretty(keys)</kbd>
|
||||
}
|
||||
}
|
||||
</td>
|
||||
<td>@T[shortcut.TextKey]</td>
|
||||
@@ -260,11 +276,15 @@
|
||||
private const string StartSection = "pagina-iniziale";
|
||||
private const string ShortcutsSection = "scorciatoie";
|
||||
private const string SessionsSection = "sessioni";
|
||||
private const string AccountSection = "dati";
|
||||
private const string SecuritySection = "sicurezza";
|
||||
|
||||
private static readonly (string Id, string Icon, string Key)[] Sections =
|
||||
[
|
||||
(AppearanceSection, "palette", "Pages.Profile.Tabs.Appearance"),
|
||||
(StartSection, "home", "Pages.Profile.Tabs.Start"),
|
||||
(AccountSection, "user", "Pages.Profile.Tabs.Account"),
|
||||
(SecuritySection, "lock", "Pages.Profile.Tabs.Security"),
|
||||
(SessionsSection, "monitor", "Pages.Profile.Tabs.Sessions"),
|
||||
(ShortcutsSection, "keyboard", "Pages.Profile.Tabs.Shortcuts"),
|
||||
];
|
||||
@@ -281,7 +301,7 @@
|
||||
private SessionOwner? _owner;
|
||||
private long _currentSessionId;
|
||||
|
||||
/// <summary>The section: <c>aspetto</c>, <c>pagina-iniziale</c>, <c>sessioni</c>, <c>scorciatoie</c>.</summary>
|
||||
/// <summary>The section: <c>aspetto</c>, <c>pagina-iniziale</c>, <c>dati</c>, <c>sicurezza</c>, <c>sessioni</c>, <c>scorciatoie</c>.</summary>
|
||||
[SupplyParameterFromQuery(Name = "scheda")]
|
||||
public string? Section { get; set; }
|
||||
|
||||
@@ -293,6 +313,8 @@
|
||||
|
||||
private bool IsModified => Draft != Session.Preferences;
|
||||
|
||||
private long? CurrentSession => _currentSessionId == 0 ? null : _currentSessionId;
|
||||
|
||||
private IEnumerable<MenuItem> StartChoices => MenuCatalog.All.Where(i => i.Milestone is null && Session.CanSee(i));
|
||||
|
||||
/// <inheritdoc />
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
@using Khaemhat.Application.Security
|
||||
@using Khaemhat.Web.Security
|
||||
@inject ProfileService Profiles
|
||||
@inject ShellSession Session
|
||||
@inject ToastService Toasts
|
||||
@inject SecurityPolicyOptions Policy
|
||||
|
||||
@* "Dati personali" of the profile (SPEC §12, issue #31): the name and e-mail the user may change alone. *@
|
||||
@if (_info is null)
|
||||
{
|
||||
<div class="pb"><p class="muted">@T["Common.Loading"]</p></div>
|
||||
}
|
||||
else
|
||||
{
|
||||
<form class="fgroups" @onsubmit="SaveAsync" @onsubmit:preventDefault>
|
||||
<fieldset class="fgroup">
|
||||
<legend class="fgh"><KhIcon Name="user" /> @T["Pages.Profile.Account.Data"]</legend>
|
||||
<div class="form">
|
||||
<div class="f c12">
|
||||
<label for="kh-p-user">@T["Pages.Profile.Account.UserName"]</label>
|
||||
<input id="kh-p-user" class="in" value="@_info.UserName" readonly />
|
||||
</div>
|
||||
<div class="f c12">
|
||||
<label for="kh-p-name">@T["Pages.Profile.Account.DisplayName"] <span class="req">*</span></label>
|
||||
<input id="kh-p-name" class="in @(HasError(ProfileService.DisplayNameInvalid) ? "err" : null)" @bind="_displayName" @bind:event="oninput"
|
||||
maxlength="@ProfileService.DisplayNameMaxLength" autocomplete="name" required />
|
||||
</div>
|
||||
<div class="f c12">
|
||||
<label for="kh-p-mail">@T["Pages.Profile.Account.Email"]</label>
|
||||
<input id="kh-p-mail" type="email" class="in @(HasError(ProfileService.EmailInvalid) ? "err" : null)" @bind="_email" @bind:event="oninput"
|
||||
autocomplete="email" />
|
||||
<span class="hint">@T["Pages.Profile.Account.EmailHint"]</span>
|
||||
</div>
|
||||
@foreach (var message in _messages)
|
||||
{
|
||||
<div class="c12 kh-alert err" role="alert">@message</div>
|
||||
}
|
||||
<div class="c12 kh-formactions">
|
||||
<button type="submit" class="btn primary" disabled="@(!IsModified || _busy)"><KhIcon Name="save" /> @T["Pages.Profile.Save"]</button>
|
||||
</div>
|
||||
</div>
|
||||
</fieldset>
|
||||
|
||||
<fieldset class="fgroup">
|
||||
<legend class="fgh"><KhIcon Name="info" /> @T["Pages.Profile.Account.About"]</legend>
|
||||
<dl class="kh-facts">
|
||||
<dt>@T["Pages.Profile.Account.Kind"]</dt>
|
||||
<dd>@T[Session.LevelKey]</dd>
|
||||
<dt>@T["Pages.Profile.Account.Company"]</dt>
|
||||
<dd>@(Session.Company?.Name ?? T["Pages.Profile.Sessions.InstanceAccount"])</dd>
|
||||
<dt>@T["Pages.Profile.Account.LastLogin"]</dt>
|
||||
<dd>@(_info.LastLoginAt is { } last ? T.DateTime(last, TimeZoneInfo.Local) : T["Pages.Profile.Account.Never"])</dd>
|
||||
<dt>@T["Pages.Profile.Account.PasswordChanged"]</dt>
|
||||
<dd>@(_info.PasswordChangedAt is { } changed ? T.DateTime(changed, TimeZoneInfo.Local) : T["Pages.Profile.Account.Never"])</dd>
|
||||
<dt>@T["Pages.Profile.Account.TwoStep"]</dt>
|
||||
<dd>
|
||||
@if (_info.TotpEnabled)
|
||||
{
|
||||
<span class="badge green">@T["Pages.Profile.Security.TotpOn"]</span>
|
||||
}
|
||||
else
|
||||
{
|
||||
<span class="badge amber">@T["Pages.Profile.Security.TotpOff"]</span>
|
||||
}
|
||||
</dd>
|
||||
</dl>
|
||||
</fieldset>
|
||||
</form>
|
||||
}
|
||||
|
||||
@code {
|
||||
private ProfileInfo? _info;
|
||||
private string? _displayName;
|
||||
private string? _email;
|
||||
private IReadOnlyList<string> _errors = [];
|
||||
private IReadOnlyList<string> _messages = [];
|
||||
private bool _busy;
|
||||
private bool _reported;
|
||||
|
||||
/// <summary>The account of the session.</summary>
|
||||
[Parameter, EditorRequired]
|
||||
public SessionOwner? Owner { get; set; }
|
||||
|
||||
/// <summary>The tab of the page, to show the modified mark.</summary>
|
||||
[CascadingParameter]
|
||||
public KhTab? Tab { get; set; }
|
||||
|
||||
private bool IsModified => _info is not null && (_displayName != _info.DisplayName || (_email ?? string.Empty) != (_info.Email ?? string.Empty));
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
if (Owner is not null)
|
||||
{
|
||||
_info = await Profiles.GetAsync(Owner);
|
||||
_displayName = _info?.DisplayName;
|
||||
_email = _info?.Email;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void OnAfterRender(bool firstRender)
|
||||
{
|
||||
// Only a change of state is reported, so the mark set by another section of the page is left alone.
|
||||
if (IsModified != _reported)
|
||||
{
|
||||
_reported = IsModified;
|
||||
Tab?.SetModified(_reported);
|
||||
}
|
||||
}
|
||||
|
||||
private bool HasError(string code) => _errors.Contains(code);
|
||||
|
||||
private async Task SaveAsync()
|
||||
{
|
||||
if (Owner is null || !IsModified || _busy)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_busy = true;
|
||||
try
|
||||
{
|
||||
_errors = await Profiles.UpdateAsync(Owner, _displayName, _email);
|
||||
_messages = LoginFlow.Messages(T, Policy, string.Join(',', _errors));
|
||||
if (_errors.Count == 0)
|
||||
{
|
||||
_info = await Profiles.GetAsync(Owner);
|
||||
_displayName = _info?.DisplayName;
|
||||
_email = _info?.Email;
|
||||
Session.SetDisplayName(_displayName);
|
||||
Toasts.Show(ToastKind.Success, T["Pages.Profile.Account.Saved"]);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
_busy = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,258 @@
|
||||
@using Khaemhat.Application.Security
|
||||
@using Khaemhat.Web.Security
|
||||
@inject ProfileService Profiles
|
||||
@inject ShellSession Session
|
||||
@inject ToastService Toasts
|
||||
@inject DialogService Dialogs
|
||||
@inject SecurityPolicyOptions Policy
|
||||
|
||||
@* "Password e verifica in due passaggi" of the profile (SPEC §7.3, §12, issue #31). A password change closes the other
|
||||
sessions; turning the verification off and renewing the recovery codes ask for the password again. *@
|
||||
@if (_info is null)
|
||||
{
|
||||
<div class="pb"><p class="muted">@T["Common.Loading"]</p></div>
|
||||
}
|
||||
else
|
||||
{
|
||||
<div class="fgroups">
|
||||
@if (_info.HasPassword)
|
||||
{
|
||||
<form class="fgroup" @onsubmit="ChangePasswordAsync" @onsubmit:preventDefault>
|
||||
<div class="fgh"><KhIcon Name="lock" /> @T["Pages.Profile.Security.Password"]</div>
|
||||
<div class="form">
|
||||
<div class="f c12">
|
||||
<label for="kh-s-current">@T["Pages.Profile.Security.Current"]</label>
|
||||
<input id="kh-s-current" type="password" class="in" @bind="_current" autocomplete="current-password" required />
|
||||
</div>
|
||||
<div class="f c12">
|
||||
<label for="kh-s-new">@T["Pages.Profile.Security.New"]</label>
|
||||
<input id="kh-s-new" type="password" class="in" @bind="_new" @bind:event="oninput" autocomplete="new-password" required />
|
||||
<span class="hint">@T.Format("Pages.Profile.Security.Rules", Policy.MinimumLength, Policy.HistorySize)</span>
|
||||
</div>
|
||||
<div class="f c12">
|
||||
<label for="kh-s-repeat">@T["Pages.Profile.Security.Repeat"]</label>
|
||||
<input id="kh-s-repeat" type="password" class="in @(Mismatch ? "err" : null)" @bind="_repeat" @bind:event="oninput" autocomplete="new-password" required />
|
||||
@if (Mismatch)
|
||||
{
|
||||
<span class="hint err">@T["Pages.Profile.Security.Mismatch"]</span>
|
||||
}
|
||||
</div>
|
||||
@foreach (var message in _passwordMessages)
|
||||
{
|
||||
<div class="c12 kh-alert err" role="alert">@message</div>
|
||||
}
|
||||
<div class="c12 kh-formactions">
|
||||
<button type="submit" class="btn primary" disabled="@(!CanChangePassword)"><KhIcon Name="key" /> @T["Pages.Profile.Security.Change"]</button>
|
||||
</div>
|
||||
<p class="c12 muted">@T["Pages.Profile.Security.ChangeNote"]</p>
|
||||
</div>
|
||||
</form>
|
||||
}
|
||||
|
||||
<section class="fgroup">
|
||||
<div class="fgh"><KhIcon Name="shield" /> @T["Pages.Profile.Security.TwoStep"]</div>
|
||||
<div class="pb">
|
||||
<p>
|
||||
@if (_info.TotpEnabled)
|
||||
{
|
||||
<span class="badge green">@T["Pages.Profile.Security.TotpOn"]</span>
|
||||
}
|
||||
else
|
||||
{
|
||||
<span class="badge amber">@T["Pages.Profile.Security.TotpOff"]</span>
|
||||
}
|
||||
@if (_info.TotpRequired)
|
||||
{
|
||||
<span class="badge">@T["Pages.Profile.Security.TotpRequired"]</span>
|
||||
}
|
||||
</p>
|
||||
|
||||
@if (_recoveryCodes is not null)
|
||||
{
|
||||
<div class="callout warn"><KhIcon Name="alert" /> <span>@T["Pages.Profile.Security.CodesOnce"]</span></div>
|
||||
<ul class="kh-codes">
|
||||
@foreach (var recovery in _recoveryCodes)
|
||||
{
|
||||
<li><code>@recovery</code></li>
|
||||
}
|
||||
</ul>
|
||||
<button type="button" class="btn" @onclick="() => _recoveryCodes = null"><KhIcon Name="check" /> @T["Pages.Profile.Security.CodesDone"]</button>
|
||||
}
|
||||
else if (!_info.TotpEnabled && _setup is null)
|
||||
{
|
||||
<p class="muted">@T["Pages.Profile.Security.TotpIntro"]</p>
|
||||
<button type="button" class="btn primary" @onclick="BeginSetupAsync"><KhIcon Name="shield" /> @T["Pages.Profile.Security.Enable"]</button>
|
||||
}
|
||||
else if (!_info.TotpEnabled && _setup is not null)
|
||||
{
|
||||
<p class="muted">@T["Pages.Profile.Security.SetupText"]</p>
|
||||
<dl class="kh-secret">
|
||||
<dt>@T["Auth.TotpSetup.Secret"]</dt>
|
||||
<dd><code>@Grouped(_setup.Secret)</code></dd>
|
||||
<dt>@T["Auth.TotpSetup.Phone"]</dt>
|
||||
<dd><a href="@_setup.ProvisioningUri">@T["Auth.TotpSetup.OpenInApp"]</a></dd>
|
||||
</dl>
|
||||
<form class="form plain" @onsubmit="ConfirmSetupAsync" @onsubmit:preventDefault>
|
||||
<div class="f c6">
|
||||
<label for="kh-s-code">@T["Auth.TotpSetup.FirstCode"]</label>
|
||||
<input id="kh-s-code" class="in mono" @bind="_code" inputmode="numeric" autocomplete="one-time-code" maxlength="6" required />
|
||||
</div>
|
||||
@if (_codeWrong)
|
||||
{
|
||||
<div class="c12 kh-alert err" role="alert">@T["Auth.Errors.totp_invalid"]</div>
|
||||
}
|
||||
<div class="c12 kh-formactions">
|
||||
<button type="button" class="btn ghost" @onclick="() => _setup = null">@T["Common.Cancel"]</button>
|
||||
<button type="submit" class="btn primary"><KhIcon Name="check" /> @T["Auth.TotpSetup.Submit"]</button>
|
||||
</div>
|
||||
</form>
|
||||
}
|
||||
else
|
||||
{
|
||||
<p class="muted">@T.Format("Pages.Profile.Security.CodesLeft", _info.RecoveryCodesLeft)</p>
|
||||
<form class="form plain" @onsubmit:preventDefault>
|
||||
<div class="f c8">
|
||||
<label for="kh-s-confirm">@T["Pages.Profile.Security.ConfirmPassword"]</label>
|
||||
<input id="kh-s-confirm" type="password" class="in" @bind="_confirm" @bind:event="oninput" autocomplete="current-password" />
|
||||
</div>
|
||||
@foreach (var message in _totpMessages)
|
||||
{
|
||||
<div class="c12 kh-alert err" role="alert">@message</div>
|
||||
}
|
||||
<div class="c12 kh-formactions">
|
||||
<button type="button" class="btn" disabled="@string.IsNullOrEmpty(_confirm)" @onclick="RenewCodesAsync"><KhIcon Name="refresh" /> @T["Pages.Profile.Security.Renew"]</button>
|
||||
@if (!_info.TotpRequired)
|
||||
{
|
||||
<button type="button" class="btn danger" disabled="@string.IsNullOrEmpty(_confirm)" @onclick="DisableAsync"><KhIcon Name="x" /> @T["Pages.Profile.Security.Disable"]</button>
|
||||
}
|
||||
</div>
|
||||
</form>
|
||||
}
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
}
|
||||
|
||||
@code {
|
||||
private ProfileInfo? _info;
|
||||
private string? _current;
|
||||
private string? _new;
|
||||
private string? _repeat;
|
||||
private string? _code;
|
||||
private string? _confirm;
|
||||
private bool _codeWrong;
|
||||
private bool _busy;
|
||||
private TotpSetup? _setup;
|
||||
private IReadOnlyList<string>? _recoveryCodes;
|
||||
private IReadOnlyList<string> _passwordMessages = [];
|
||||
private IReadOnlyList<string> _totpMessages = [];
|
||||
|
||||
/// <summary>The account of the session.</summary>
|
||||
[Parameter, EditorRequired]
|
||||
public SessionOwner? Owner { get; set; }
|
||||
|
||||
/// <summary>The session that stays open after a password change.</summary>
|
||||
[Parameter]
|
||||
public long? CurrentSessionId { get; set; }
|
||||
|
||||
private bool Mismatch => !string.IsNullOrEmpty(_repeat) && _repeat != _new;
|
||||
|
||||
private bool CanChangePassword => !_busy && !string.IsNullOrEmpty(_current) && !string.IsNullOrEmpty(_new) && _new == _repeat;
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override Task OnInitializedAsync() => ReloadAsync();
|
||||
|
||||
private async Task ReloadAsync() => _info = Owner is null ? null : await Profiles.GetAsync(Owner);
|
||||
|
||||
private async Task ChangePasswordAsync()
|
||||
{
|
||||
if (Owner is null || !CanChangePassword)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_busy = true;
|
||||
try
|
||||
{
|
||||
var errors = await Profiles.ChangePasswordAsync(Owner, CurrentSessionId, _current!, _new!);
|
||||
_passwordMessages = LoginFlow.Messages(T, Policy, string.Join(',', errors));
|
||||
if (errors.Count == 0)
|
||||
{
|
||||
_current = _new = _repeat = null;
|
||||
await ReloadAsync();
|
||||
Toasts.Show(ToastKind.Success, T["Pages.Profile.Security.Changed"], T["Pages.Profile.Security.ChangedText"]);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
_busy = false;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task BeginSetupAsync()
|
||||
{
|
||||
if (Owner is not null)
|
||||
{
|
||||
_codeWrong = false;
|
||||
_setup = await Profiles.BeginTotpSetupAsync(Owner, Session.Company?.Code);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ConfirmSetupAsync()
|
||||
{
|
||||
if (Owner is null || string.IsNullOrWhiteSpace(_code))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var codes = await Profiles.ConfirmTotpSetupAsync(Owner, _code);
|
||||
_code = null;
|
||||
_codeWrong = codes is null;
|
||||
if (codes is not null)
|
||||
{
|
||||
_setup = null;
|
||||
_recoveryCodes = codes;
|
||||
await ReloadAsync();
|
||||
Toasts.Show(ToastKind.Success, T["Pages.Profile.Security.Enabled"]);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task RenewCodesAsync()
|
||||
{
|
||||
if (Owner is null || string.IsNullOrEmpty(_confirm))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var codes = await Profiles.RenewRecoveryCodesAsync(Owner, _confirm);
|
||||
_confirm = null;
|
||||
_totpMessages = codes is null ? LoginFlow.Messages(T, Policy, SecurityErrors.CurrentPasswordWrong) : [];
|
||||
if (codes is not null)
|
||||
{
|
||||
_recoveryCodes = codes;
|
||||
await ReloadAsync();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task DisableAsync()
|
||||
{
|
||||
if (Owner is null || string.IsNullOrEmpty(_confirm)
|
||||
|| !await Dialogs.ConfirmAsync(new(T["Pages.Profile.Security.DisableTitle"], T["Pages.Profile.Security.DisableText"],
|
||||
T["Pages.Profile.Security.Disable"], T["Common.Cancel"], Danger: true)))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var errors = await Profiles.DisableTotpAsync(Owner, _confirm);
|
||||
_confirm = null;
|
||||
_totpMessages = LoginFlow.Messages(T, Policy, string.Join(',', errors));
|
||||
if (errors.Count == 0)
|
||||
{
|
||||
await ReloadAsync();
|
||||
Toasts.Show(ToastKind.Warning, T["Pages.Profile.Security.Disabled"]);
|
||||
}
|
||||
}
|
||||
|
||||
private static string Grouped(string secret) =>
|
||||
string.Join(' ', Enumerable.Range(0, (secret.Length + 3) / 4).Select(i => secret.Substring(i * 4, Math.Min(4, secret.Length - (i * 4)))));
|
||||
}
|
||||
Reference in new issue
Block a user