Skip to content

Commit 074b950

Browse files
committed
Permission Management UI
1 parent 73bc4de commit 074b950

File tree

3 files changed

+1303
-0
lines changed

3 files changed

+1303
-0
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
@using FSH.Playground.Blazor.ApiClient
2+
@inject IIdentityClient IdentityClient
3+
@inject ISnackbar Snackbar
4+
5+
<MudDialog>
6+
<TitleContent>
7+
<MudStack Spacing="0">
8+
<MudText Typo="Typo.h6" Class="fw-600">@(IsEditMode ? "Edit Role" : "Create New Role")</MudText>
9+
<MudText Typo="Typo.caption" Class="text-muted">
10+
@(IsEditMode ? "Modify role details" : "Define a new role for your organization")
11+
</MudText>
12+
</MudStack>
13+
</TitleContent>
14+
<DialogContent>
15+
<MudForm @ref="_form" Model="_model">
16+
<MudStack Spacing="4" Class="mt-2">
17+
@* Role Name *@
18+
<MudTextField @bind-Value="_model.Name"
19+
Label="Role Name"
20+
Placeholder="Enter role name (e.g., Manager, Editor)"
21+
Required="true"
22+
RequiredError="Role name is required"
23+
Variant="Variant.Outlined"
24+
Adornment="Adornment.Start"
25+
AdornmentIcon="@Icons.Material.Filled.Badge"
26+
Immediate="true"
27+
Disabled="@(IsEditMode && IsSystemRole(_model.Name))"
28+
HelperText="@(IsEditMode && IsSystemRole(_model.Name) ? "System role names cannot be changed" : "Choose a descriptive name for the role")" />
29+
30+
@* Role Description *@
31+
<MudTextField @bind-Value="_model.Description"
32+
Label="Description"
33+
Placeholder="Describe the purpose and scope of this role"
34+
Variant="Variant.Outlined"
35+
Adornment="Adornment.Start"
36+
AdornmentIcon="@Icons.Material.Filled.Description"
37+
Lines="3"
38+
HelperText="Help others understand what this role is used for" />
39+
40+
@* Info Alert *@
41+
<MudAlert Severity="Severity.Info" Variant="Variant.Outlined" Dense="true">
42+
<MudStack Row="true" AlignItems="AlignItems.Center" Spacing="2">
43+
<MudIcon Icon="@Icons.Material.Filled.Info" Size="Size.Small" />
44+
<MudText Typo="Typo.body2">
45+
@if (IsEditMode)
46+
{
47+
<span>After saving, you can manage permissions for this role from the Roles page.</span>
48+
}
49+
else
50+
{
51+
<span>After creating the role, you can assign permissions to define what users with this role can do.</span>
52+
}
53+
</MudText>
54+
</MudStack>
55+
</MudAlert>
56+
</MudStack>
57+
</MudForm>
58+
</DialogContent>
59+
<DialogActions>
60+
<MudButton Variant="Variant.Text"
61+
Color="Color.Default"
62+
OnClick="Cancel"
63+
Disabled="_busy">
64+
Cancel
65+
</MudButton>
66+
<MudButton Variant="Variant.Filled"
67+
Color="Color.Primary"
68+
OnClick="Submit"
69+
Disabled="_busy"
70+
StartIcon="@(_busy ? null : (IsEditMode ? Icons.Material.Filled.Save : Icons.Material.Filled.Add))">
71+
@if (_busy)
72+
{
73+
<MudProgressCircular Size="Size.Small" Indeterminate="true" Class="mr-2" />
74+
<span>@(IsEditMode ? "Saving..." : "Creating...")</span>
75+
}
76+
else
77+
{
78+
<span>@(IsEditMode ? "Save Changes" : "Create Role")</span>
79+
}
80+
</MudButton>
81+
</DialogActions>
82+
</MudDialog>
83+
84+
@code {
85+
[CascadingParameter]
86+
private IMudDialogInstance MudDialog { get; set; } = default!;
87+
88+
[Parameter]
89+
public RoleDto? ExistingRole { get; set; }
90+
91+
private MudForm? _form;
92+
private UpsertRoleCommand _model = new();
93+
private bool _busy;
94+
95+
private bool IsEditMode => ExistingRole is not null;
96+
97+
private static readonly HashSet<string> SystemRoles = new(StringComparer.OrdinalIgnoreCase)
98+
{
99+
"Admin", "Administrator", "Basic"
100+
};
101+
102+
protected override void OnInitialized()
103+
{
104+
if (ExistingRole is not null)
105+
{
106+
_model = new UpsertRoleCommand
107+
{
108+
Id = ExistingRole.Id,
109+
Name = ExistingRole.Name,
110+
Description = ExistingRole.Description
111+
};
112+
}
113+
}
114+
115+
private static bool IsSystemRole(string? roleName) =>
116+
!string.IsNullOrEmpty(roleName) && SystemRoles.Contains(roleName);
117+
118+
private void Cancel()
119+
{
120+
MudDialog.Cancel();
121+
}
122+
123+
private async Task Submit()
124+
{
125+
if (_form is not null)
126+
{
127+
await _form.Validate();
128+
if (!_form.IsValid)
129+
{
130+
return;
131+
}
132+
}
133+
134+
if (string.IsNullOrWhiteSpace(_model.Name))
135+
{
136+
Snackbar.Add("Please enter a role name.", Severity.Warning);
137+
return;
138+
}
139+
140+
_busy = true;
141+
try
142+
{
143+
await IdentityClient.RolesPostAsync(_model);
144+
Snackbar.Add($"Role '{_model.Name}' {(IsEditMode ? "updated" : "created")} successfully!", Severity.Success);
145+
MudDialog.Close(DialogResult.Ok(true));
146+
}
147+
catch (Exception ex)
148+
{
149+
Snackbar.Add($"Failed to {(IsEditMode ? "update" : "create")} role: {ex.Message}", Severity.Error);
150+
}
151+
finally
152+
{
153+
_busy = false;
154+
}
155+
}
156+
}

0 commit comments

Comments
 (0)