Improved the way to manage user roles

This commit is contained in:
s459315 2022-08-22 00:12:27 +02:00
parent 030b97e8a3
commit 62dfccc162
8 changed files with 238 additions and 1 deletions

View File

@ -56,5 +56,44 @@ namespace RMDataManager.Controllers
return output;
}
[Authorize(Roles = "Admin")]
[HttpGet]
[Route("api/User/Admin/GetAllRoles")]
public Dictionary<string, string> GetAllRoles()
{
using (var context = new ApplicationDbContext())
{
var roles = context.Roles.ToDictionary(x => x.Id, x => x.Name);
return roles;
}
}
[Authorize(Roles = "Admin")]
[HttpPost]
[Route("api/User/Admin/AddRole")]
public void AddRole(UserRolePairModel pair)
{
using (var context = new ApplicationDbContext())
{
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
userManager.AddToRole(pair.UserId, pair.RoleName);
}
}
[Authorize(Roles = "Admin")]
[HttpPost]
[Route("api/User/Admin/RemoveRole")]
public void RemoveRole(UserRolePairModel pair)
{
using (var context = new ApplicationDbContext())
{
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
userManager.RemoveFromRole(pair.UserId, pair.RoleName);
}
}
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace RMDataManager.Models
{
public class UserRolePairModel
{
public string UserId { get; set; }
public string RoleName { get; set; }
}
}

View File

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

View File

@ -7,5 +7,9 @@ namespace RMWPFInterfaceLibrary.Api
public interface IUserEndPoint
{
Task<List<UserModel>> GetAll();
Task<Dictionary<string, string>> GetAllRoles();
Task AddUserToRole(string userId, string roleName);
Task RemoveUserFromRole(string userId, string roleName);
}
}

View File

@ -32,5 +32,48 @@ namespace RMWPFInterfaceLibrary.Api
}
}
}
public async Task<Dictionary<string,string>> GetAllRoles()
{
using (HttpResponseMessage response = await _apiHelper.ApiClient.GetAsync("/api/User/Admin/GetAllRoles"))
{
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsAsync<Dictionary<string,string>>();
return result;
}
else
{
throw new Exception(response.ReasonPhrase);
}
}
}
public async Task AddUserToRole(string userId, string roleName)
{
var data = new { userId, roleName };
using (HttpResponseMessage response = await _apiHelper.ApiClient.PostAsJsonAsync("/api/User/Admin/AddRole", data))
{
if (response.IsSuccessStatusCode == false)
{
throw new Exception(response.ReasonPhrase);
}
}
}
public async Task RemoveUserFromRole(string userId, string roleName)
{
var data = new { userId, roleName };
using (HttpResponseMessage response = await _apiHelper.ApiClient.PostAsJsonAsync("/api/User/Admin/RemoveRole", data))
{
if (response.IsSuccessStatusCode == false)
{
throw new Exception(response.ReasonPhrase);
}
}
}
}
}

View File

@ -31,6 +31,86 @@ namespace RMWPFUserInterface.ViewModels
NotifyOfPropertyChange(() => Users);
}
}
private UserModel _selectedUser;
public UserModel SelectedUser
{
get { return _selectedUser; }
set
{
_selectedUser = value;
SelectedUserName = value.EmailAddress;
UserRoles = new BindingList<string>(value.Roles.Select(x => x.Value).ToList());
LoadRoles();
NotifyOfPropertyChange(() => SelectedUser);
}
}
private string _selectedUserName;
public string SelectedUserName
{
get
{
return _selectedUserName;
}
set
{
_selectedUserName = value;
NotifyOfPropertyChange(() => SelectedUserName);
}
}
private BindingList<string> _userRoles = new BindingList<string>();
public BindingList<string> UserRoles
{
get { return _userRoles; }
set
{
_userRoles = value;
NotifyOfPropertyChange(() => UserRoles);
}
}
private BindingList<string> _availableRoles = new BindingList<string>();
public BindingList<string> AvailableRoles
{
get { return _availableRoles; }
set
{
_availableRoles = value;
NotifyOfPropertyChange(() => AvailableRoles);
}
}
private string _selectedUserRole;
public string SelectedUserRole
{
get { return _selectedUserRole; }
set
{
_selectedUserRole = value;
NotifyOfPropertyChange(() => SelectedUserRole);
}
}
private string _selectedAvailableRole;
public string SelectedAvailableRole
{
get { return _selectedAvailableRole; }
set
{
_selectedAvailableRole = value;
NotifyOfPropertyChange(() => SelectedAvailableRole);
}
}
public UserDisplayViewModel(StatusInfoViewModel status, IWindowManager window, IUserEndPoint user)
{
_status = status;
@ -72,5 +152,33 @@ namespace RMWPFUserInterface.ViewModels
var userList = await _userEndpoint.GetAll();
Users = new BindingList<UserModel>(userList);
}
private async Task LoadRoles()
{
var roles = await _userEndpoint.GetAllRoles();
foreach (var role in roles)
{
if (UserRoles.IndexOf(role.Value) < 0)
{
AvailableRoles.Add(role.Value);
}
}
}
public async void AddSelectedRole()
{
await _userEndpoint.AddUserToRole(SelectedUser.Id, SelectedAvailableRole);
UserRoles.Add(SelectedAvailableRole);
AvailableRoles.Remove(SelectedAvailableRole);
}
public async void RemoveSelectedRole()
{
await _userEndpoint.RemoveUserFromRole(SelectedUser.Id, SelectedUserRole);
AvailableRoles.Add(SelectedUserRole);
UserRoles.Remove(SelectedUserRole);
}
}
}

View File

@ -32,7 +32,7 @@
<!-- 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">
MinHeight="200" MinWidth="150" SelectedItem="SelectedUser">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
@ -42,5 +42,21 @@
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<!-- Column 1 -->
<StackPanel Orientation="Vertical" Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2"
Margin="20 0 0 0">
<TextBlock x:Name="SelectedUserName" Margin="0 0 0 10"/>
<TextBlock Text="Current Roles" />
<StackPanel Orientation="Horizontal" Margin="0 0 0 10">
<ListBox x:Name="UserRoles" />
<Button x:Name="RemoveSelectedRole" Content="Remove" Margin="5 0 0 0"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<ComboBox x:Name="AvailableRoles"/>
<Button x:Name="AddSelectedRole" Content="Add Role" Margin="5 0 0 0"/>
</StackPanel>
</StackPanel>
</Grid>
</UserControl>

View File

@ -13,6 +13,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RMDataManagerLibrary", "RMD
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RMWPFInterfaceLibrary", "RMWPFInterfaceLibrary\RMWPFInterfaceLibrary.csproj", "{10B4A580-F9CF-4483-BF8B-02C5B20F182D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Database", "Database", "{E89BEB7D-166A-41CE-B1EA-81BF13B5FB97}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "API", "API", "{F05CC07D-AB17-49CE-8374-00AA27A7D263}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "WPF", "WPF", "{E8119F10-49C2-465A-8B1D-04AA7A1243CE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -45,6 +51,13 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{E884BD5C-FE2D-4410-9E00-1D8024B75BFC} = {F05CC07D-AB17-49CE-8374-00AA27A7D263}
{0098FB45-9267-42D0-81B1-9948E2BAB28F} = {E89BEB7D-166A-41CE-B1EA-81BF13B5FB97}
{90936557-936E-4310-90C1-45254FEEA7B1} = {E8119F10-49C2-465A-8B1D-04AA7A1243CE}
{6669F7DC-4B07-497F-BDEE-5333DB3EDBF4} = {F05CC07D-AB17-49CE-8374-00AA27A7D263}
{10B4A580-F9CF-4483-BF8B-02C5B20F182D} = {E8119F10-49C2-465A-8B1D-04AA7A1243CE}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {990E8270-2147-49DA-A012-A34F4A232918}
EndGlobalSection