created a way to manage users and their roles
This commit is contained in:
parent
7c40488938
commit
030b97e8a3
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
14
RMDataManager/Models/ApplicationUserModel.cs
Normal file
14
RMDataManager/Models/ApplicationUserModel.cs
Normal 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>();
|
||||
}
|
||||
}
|
@ -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" />
|
||||
|
11
RMWPFInterfaceLibrary/Api/IUserEndPoint.cs
Normal file
11
RMWPFInterfaceLibrary/Api/IUserEndPoint.cs
Normal 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();
|
||||
}
|
||||
}
|
36
RMWPFInterfaceLibrary/Api/UserEndPoint.cs
Normal file
36
RMWPFInterfaceLibrary/Api/UserEndPoint.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
23
RMWPFInterfaceLibrary/Models/UserModel.cs
Normal file
23
RMWPFInterfaceLibrary/Models/UserModel.cs
Normal 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));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -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" />
|
||||
|
@ -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>()
|
||||
|
@ -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">
|
||||
|
@ -50,6 +50,11 @@ namespace RMWPFUserInterface.ViewModels
|
||||
TryCloseAsync();
|
||||
}
|
||||
|
||||
public void UserManagement()
|
||||
{
|
||||
ActivateItemAsync(IoC.Get<UserDisplayViewModel>());
|
||||
}
|
||||
|
||||
public void LogOut()
|
||||
{
|
||||
_user.ResetUserModel();
|
||||
|
76
RMWPFUserInterface/ViewModels/UserDisplayViewModel.cs
Normal file
76
RMWPFUserInterface/ViewModels/UserDisplayViewModel.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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>
|
||||
|
46
RMWPFUserInterface/Views/UserDisplayView.xaml
Normal file
46
RMWPFUserInterface/Views/UserDisplayView.xaml
Normal 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>
|
28
RMWPFUserInterface/Views/UserDisplayView.xaml.cs
Normal file
28
RMWPFUserInterface/Views/UserDisplayView.xaml.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user