diff --git a/LANCommander/Models/RoleViewModel.cs b/LANCommander/Models/RoleViewModel.cs new file mode 100644 index 0000000..81f9530 --- /dev/null +++ b/LANCommander/Models/RoleViewModel.cs @@ -0,0 +1,10 @@ +namespace LANCommander.Models +{ + public class RoleViewModel + { + public string Name { get; set; } + public int Users { get; set; } + public int Collections { get; set; } + + } +} diff --git a/LANCommander/Pages/Settings/Roles/Index.razor b/LANCommander/Pages/Settings/Roles/Index.razor new file mode 100644 index 0000000..366774f --- /dev/null +++ b/LANCommander/Pages/Settings/Roles/Index.razor @@ -0,0 +1,144 @@ +@page "/Settings/Roles" +@using LANCommander.Models; +@layout SettingsLayout +@inject RoleManager RoleManager +@inject UserManager UserManager +@inject IMessageService MessageService +@inject NavigationManager NavigationManager +@attribute [Authorize(Roles = "Administrator")] + + + + + + + + + + + +
+ + + + + + + + @if (context.Name != "Administrator") + { + +
+
+ + +
+ + + +
+
+ +@code { + ICollection Roles { get; set; } + + LANCommanderSettings Settings = SettingService.GetSettings(); + bool Loading = true; + bool AddRoleDialogVisible = false; + + RoleViewModel NewRole = new RoleViewModel(); + + protected override async Task OnInitializedAsync() + { + Roles = new List(); + } + + protected override async Task OnAfterRenderAsync(bool firstRender) + { + if (firstRender) + await LoadData(); + } + + private async Task LoadData() + { + Roles = new List(); + + foreach (var role in RoleManager.Roles) + { + var users = await UserManager.GetUsersInRoleAsync(role.Name); + + Roles.Add(new RoleViewModel() + { + Name = role.Name, + Collections = role.Collections != null ? role.Collections.Count : 0, + Users = users != null ? users.Count : 0 + }); + } + + Loading = false; + + StateHasChanged(); + } + + private async Task DeleteRole(RoleViewModel roleViewModel) + { + var role = await RoleManager.FindByNameAsync(roleViewModel.Name); + + if (role.Name == "Administrator") + { + await MessageService.Error("Cannot delete the administrator role!"); + } + else + { + await RoleManager.DeleteAsync(role); + await LoadData(); + + await MessageService.Success($"Deleted {role.Name}!"); + } + } + + private void ShowNewRoleDialog() + { + NewRole = new RoleViewModel(); + + AddRoleDialogVisible = true; + } + + private async Task AddRole() + { + if (await RoleManager.RoleExistsAsync(NewRole.Name)) + { + MessageService.Error("A role with that name already exists!"); + AddRoleDialogVisible = false; + return; + } + + try + { + await RoleManager.CreateAsync(new Role() + { + Name = NewRole.Name + }); + + await LoadData(); + MessageService.Success("Role added!"); + } + catch (Exception ex) + { + MessageService.Error("Could not added role!"); + } + + } +} diff --git a/LANCommander/Pages/Settings/SettingsLayout.razor b/LANCommander/Pages/Settings/SettingsLayout.razor index 928bdde..a44eaf9 100644 --- a/LANCommander/Pages/Settings/SettingsLayout.razor +++ b/LANCommander/Pages/Settings/SettingsLayout.razor @@ -7,6 +7,7 @@ General Users + Roles Authentication User Saves Archives