diff --git a/RMDataManager/Controllers/UserController.cs b/RMDataManager/Controllers/UserController.cs index 7bc22f5..c6253b7 100644 --- a/RMDataManager/Controllers/UserController.cs +++ b/RMDataManager/Controllers/UserController.cs @@ -56,5 +56,44 @@ namespace RMDataManager.Controllers return output; } + [Authorize(Roles = "Admin")] + [HttpGet] + [Route("api/User/Admin/GetAllRoles")] + public Dictionary 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(context); + var userManager = new UserManager(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(context); + var userManager = new UserManager(userStore); + + userManager.RemoveFromRole(pair.UserId, pair.RoleName); + } + } } } diff --git a/RMDataManager/Models/UserRolePairModel.cs b/RMDataManager/Models/UserRolePairModel.cs new file mode 100644 index 0000000..a362c99 --- /dev/null +++ b/RMDataManager/Models/UserRolePairModel.cs @@ -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; } + } +} \ No newline at end of file diff --git a/RMDataManager/RMDataManager.csproj b/RMDataManager/RMDataManager.csproj index 1092aec..abe1d15 100644 --- a/RMDataManager/RMDataManager.csproj +++ b/RMDataManager/RMDataManager.csproj @@ -224,6 +224,7 @@ + diff --git a/RMWPFInterfaceLibrary/Api/IUserEndPoint.cs b/RMWPFInterfaceLibrary/Api/IUserEndPoint.cs index 3b6052b..0157378 100644 --- a/RMWPFInterfaceLibrary/Api/IUserEndPoint.cs +++ b/RMWPFInterfaceLibrary/Api/IUserEndPoint.cs @@ -7,5 +7,9 @@ namespace RMWPFInterfaceLibrary.Api public interface IUserEndPoint { Task> GetAll(); + Task> GetAllRoles(); + Task AddUserToRole(string userId, string roleName); + Task RemoveUserFromRole(string userId, string roleName); + } } \ No newline at end of file diff --git a/RMWPFInterfaceLibrary/Api/UserEndPoint.cs b/RMWPFInterfaceLibrary/Api/UserEndPoint.cs index 6277bd0..2e2b58b 100644 --- a/RMWPFInterfaceLibrary/Api/UserEndPoint.cs +++ b/RMWPFInterfaceLibrary/Api/UserEndPoint.cs @@ -32,5 +32,48 @@ namespace RMWPFInterfaceLibrary.Api } } } + + public async Task> GetAllRoles() + { + using (HttpResponseMessage response = await _apiHelper.ApiClient.GetAsync("/api/User/Admin/GetAllRoles")) + { + if (response.IsSuccessStatusCode) + { + var result = await response.Content.ReadAsAsync>(); + + 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); + } + } + } } } diff --git a/RMWPFUserInterface/ViewModels/UserDisplayViewModel.cs b/RMWPFUserInterface/ViewModels/UserDisplayViewModel.cs index dec16e9..17fac46 100644 --- a/RMWPFUserInterface/ViewModels/UserDisplayViewModel.cs +++ b/RMWPFUserInterface/ViewModels/UserDisplayViewModel.cs @@ -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(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 _userRoles = new BindingList(); + + public BindingList UserRoles + { + get { return _userRoles; } + set + { + _userRoles = value; + NotifyOfPropertyChange(() => UserRoles); + } + } + + private BindingList _availableRoles = new BindingList(); + + public BindingList 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(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); + } } } diff --git a/RMWPFUserInterface/Views/UserDisplayView.xaml b/RMWPFUserInterface/Views/UserDisplayView.xaml index 7ad9ef6..a714060 100644 --- a/RMWPFUserInterface/Views/UserDisplayView.xaml +++ b/RMWPFUserInterface/Views/UserDisplayView.xaml @@ -32,7 +32,7 @@ + MinHeight="200" MinWidth="150" SelectedItem="SelectedUser"> @@ -42,5 +42,21 @@ + + + + + + + +