created a way to manage users and their roles

This commit is contained in:
s459315 2022-08-20 14:23:25 +02:00
parent 7c40488938
commit 030b97e8a3
14 changed files with 294 additions and 2 deletions

View File

@ -5,6 +5,8 @@ using RMDataManagerLibrary.Models;
using System.Web;
using Microsoft.AspNet.Identity;
using System.Linq;
using Microsoft.AspNet.Identity.EntityFramework;
using RMDataManager.Models;
namespace RMDataManager.Controllers
{
@ -19,6 +21,40 @@ namespace RMDataManager.Controllers
UserData data = new UserData();
return data.GetUserById(userId).First();
}
}
[Authorize(Roles = "Admin")]
[HttpGet]
[Route("api/User/Admin/GetAllUsers")]
public List<ApplicationUserModel> GetAllUsers()
{
List<ApplicationUserModel> output = new List<ApplicationUserModel>();
using (var context = new ApplicationDbContext())
{
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
var users = userManager.Users.ToList();
var roles = context.Roles.ToList();
foreach (var user in users)
{
ApplicationUserModel u = new ApplicationUserModel
{
Id = user.Id,
EmailAddress = user.Email
};
foreach (var role in user.Roles)
{
u.Roles.Add(role.RoleId, roles.Where(x => x.Id == role.RoleId).First().Name);
}
output.Add(u);
}
}
return output;
}
}
}

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace RMDataManager.Models
{
public class ApplicationUserModel
{
public string Id { get; set; }
public string EmailAddress { get; set; }
public Dictionary<string, string> Roles { get; set; } = new Dictionary<string, string>();
}
}

View File

@ -222,6 +222,7 @@
</Compile>
<Compile Include="Models\AccountBindingModels.cs" />
<Compile Include="Models\AccountViewModels.cs" />
<Compile Include="Models\ApplicationUserModel.cs" />
<Compile Include="Models\IdentityModels.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Providers\ApplicationOAuthProvider.cs" />

View File

@ -0,0 +1,11 @@
using RMWPFInterfaceLibrary.Models;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace RMWPFInterfaceLibrary.Api
{
public interface IUserEndPoint
{
Task<List<UserModel>> GetAll();
}
}

View File

@ -0,0 +1,36 @@
using RMWPFInterfaceLibrary.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace RMWPFInterfaceLibrary.Api
{
public class UserEndPoint : IUserEndPoint
{
private readonly IAPIHelper _apiHelper;
public UserEndPoint(IAPIHelper apiHelper)
{
_apiHelper = apiHelper;
}
public async Task<List<UserModel>> GetAll()
{
using (HttpResponseMessage response = await _apiHelper.ApiClient.GetAsync("/api/User/Admin/GetAllUsers"))
{
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsAsync<List<UserModel>>();
return result;
}
else
{
throw new Exception(response.ReasonPhrase);
}
}
}
}
}

View File

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RMWPFInterfaceLibrary.Models
{
public class UserModel
{
public string Id { get; set; }
public string EmailAddress { get; set; }
public Dictionary<string, string> Roles { get; set; } = new Dictionary<string, string>();
public string RoleList
{
get
{
return string.Join(", ", Roles.Select(x => x.Value));
}
}
}
}

View File

@ -52,10 +52,13 @@
<Compile Include="Api\IAPIHelper.cs" />
<Compile Include="Api\IProductEndPoint.cs" />
<Compile Include="Api\ISaleEndPoint.cs" />
<Compile Include="Api\IUserEndPoint.cs" />
<Compile Include="Api\ProductEndPoint.cs" />
<Compile Include="Api\SaleEndPoint.cs" />
<Compile Include="Api\UserEndPoint.cs" />
<Compile Include="Helpers\ConfigHelper.cs" />
<Compile Include="Helpers\IConfigHelper.cs" />
<Compile Include="Models\UserModel.cs" />
<Compile Include="Models\AuthenticatedUser.cs" />
<Compile Include="Models\CartItemModel.cs" />
<Compile Include="Models\ILoggedInUserModel.cs" />

View File

@ -34,7 +34,9 @@ namespace RMWPFUserInterface
_container.Instance(_container)
.PerRequest<IProductEndPoint, ProductEndPoint>()
.PerRequest<ISaleEndPoint, SaleEndPoint>();
.PerRequest<ISaleEndPoint, SaleEndPoint>()
.PerRequest<IUserEndPoint, UserEndPoint>();
_container
.Singleton<IWindowManager, WindowManager>()

View File

@ -92,6 +92,7 @@
<Compile Include="ViewModels\SalesViewModel.cs" />
<Compile Include="ViewModels\ShellViewModel.cs" />
<Compile Include="ViewModels\StatusInfoViewModel.cs" />
<Compile Include="ViewModels\UserDisplayViewModel.cs" />
<Compile Include="Views\LoginView.xaml.cs">
<DependentUpon>LoginView.xaml</DependentUpon>
</Compile>
@ -108,6 +109,9 @@
<Compile Include="Views\StatusInfoView.xaml.cs">
<DependentUpon>StatusInfoView.xaml</DependentUpon>
</Compile>
<Compile Include="Views\UserDisplayView.xaml.cs">
<DependentUpon>UserDisplayView.xaml</DependentUpon>
</Compile>
<Page Include="Views\LoginView.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
@ -124,6 +128,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Views\UserDisplayView.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs">

View File

@ -50,6 +50,11 @@ namespace RMWPFUserInterface.ViewModels
TryCloseAsync();
}
public void UserManagement()
{
ActivateItemAsync(IoC.Get<UserDisplayViewModel>());
}
public void LogOut()
{
_user.ResetUserModel();

View File

@ -0,0 +1,76 @@
using Caliburn.Micro;
using RMWPFInterfaceLibrary.Api;
using RMWPFInterfaceLibrary.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace RMWPFUserInterface.ViewModels
{
public class UserDisplayViewModel : Screen
{
private readonly StatusInfoViewModel _status;
private readonly IWindowManager _window;
private readonly IUserEndPoint _userEndpoint;
private BindingList<UserModel> _users;
public BindingList<UserModel> Users
{
get
{
return _users;
}
set
{
_users = value;
NotifyOfPropertyChange(() => Users);
}
}
public UserDisplayViewModel(StatusInfoViewModel status, IWindowManager window, IUserEndPoint user)
{
_status = status;
_window = window;
_userEndpoint = user;
}
protected override async void OnViewLoaded(object view)
{
base.OnViewLoaded(view);
try
{
await LoadUsers();
}
catch (Exception ex)
{
dynamic settings = new ExpandoObject();
settings.WindowStartUpLocation = WindowStartupLocation.CenterOwner;
settings.ResizeMode = ResizeMode.NoResize;
settings.Title = "System Error";
if (ex.Message == "Unauthorized")
{
_status.UpdateMessage("Unauthorized Access", "You do not have permission to interact with Sales Form.");
_window.ShowDialogAsync(_status, null, settings);
}
else
{
_status.UpdateMessage("Fatal exception", ex.Message);
_window.ShowDialogAsync(_status, null, settings);
}
TryCloseAsync();
}
}
private async Task LoadUsers()
{
var userList = await _userEndpoint.GetAll();
Users = new BindingList<UserModel>(userList);
}
}
}

View File

@ -11,6 +11,9 @@
<MenuItem Header="_File">
<MenuItem x:Name="ExitApplication" Header="E_xit"/>
</MenuItem>
<MenuItem Header="_Users">
<MenuItem x:Name="UserManagement" Header="User_Management"/>
</MenuItem>
<MenuItem Header="_Account" Visibility="{Binding IsLoggedIn, Converter={StaticResource BooleanToVisibilityConverter}, FallbackValue=Collapsed}">
<MenuItem x:Name="LogOut" Header="_Log Out"/>
</MenuItem>

View File

@ -0,0 +1,46 @@
<UserControl x:Class="RMWPFUserInterface.Views.UserDisplayView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:RMWPFUserInterface.Views"
mc:Ignorable="d" Background="White" FontSize="24"
d:DesignHeight="550" d:DesignWidth="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<!-- Header -->
<TextBlock Text="User Administration" FontSize="48"
Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3"
Margin="0 0 0 30"/>
<!-- Column 0 -->
<TextBlock Text="Users" Grid.Row="1" Grid.Column="0"/>
<ListBox x:Name="Users" Grid.Row="2" Grid.Column="0"
MinHeight="200" MinWidth="150">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding EmailAddress}"/>
<TextBlock Text="{Binding RoleList}" FontSize="18" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</UserControl>

View File

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace RMWPFUserInterface.Views
{
/// <summary>
/// Interaction logic for UserDisplayView.xaml
/// </summary>
public partial class UserDisplayView : UserControl
{
public UserDisplayView()
{
InitializeComponent();
}
}
}