Added basic user management
This commit is contained in:
parent
f63c2ddf2d
commit
90b9d3bb75
5 changed files with 201 additions and 5 deletions
|
@ -1,18 +1,22 @@
|
||||||
using LANCommander.Models;
|
using LANCommander.Data.Models;
|
||||||
|
using LANCommander.Models;
|
||||||
using LANCommander.Services;
|
using LANCommander.Services;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Identity;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace LANCommander.Controllers
|
namespace LANCommander.Controllers
|
||||||
{
|
{
|
||||||
[Authorize]
|
[Authorize(Roles = "Administrator")]
|
||||||
public class SettingsController : Controller
|
public class SettingsController : BaseController
|
||||||
{
|
{
|
||||||
private readonly SettingService SettingService;
|
private readonly SettingService SettingService;
|
||||||
|
private readonly UserManager<User> UserManager;
|
||||||
|
|
||||||
public SettingsController(SettingService settingService)
|
public SettingsController(SettingService settingService, UserManager<User> userManager)
|
||||||
{
|
{
|
||||||
SettingService = settingService;
|
SettingService = settingService;
|
||||||
|
UserManager = userManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IActionResult Index()
|
public IActionResult Index()
|
||||||
|
@ -34,5 +38,105 @@ namespace LANCommander.Controllers
|
||||||
|
|
||||||
return RedirectToAction(nameof(General));
|
return RedirectToAction(nameof(General));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<IActionResult> Users()
|
||||||
|
{
|
||||||
|
var users = new List<UserViewModel>();
|
||||||
|
|
||||||
|
foreach (var user in UserManager.Users)
|
||||||
|
{
|
||||||
|
users.Add(new UserViewModel()
|
||||||
|
{
|
||||||
|
Id = user.Id,
|
||||||
|
UserName = user.UserName,
|
||||||
|
Roles = await UserManager.GetRolesAsync(user)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return View(users);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IActionResult> DeleteUser(Guid id)
|
||||||
|
{
|
||||||
|
var user = await UserManager.FindByIdAsync(id.ToString());
|
||||||
|
var admins = await UserManager.GetUsersInRoleAsync("Administrator");
|
||||||
|
|
||||||
|
if (user.UserName == HttpContext.User.Identity.Name)
|
||||||
|
{
|
||||||
|
Alert("You cannot delete yourself!", "danger");
|
||||||
|
|
||||||
|
return RedirectToAction(nameof(Users));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (admins.Count == 1 && admins.First().Id == id)
|
||||||
|
{
|
||||||
|
Alert("You cannot delete the only admin user!", "danger");
|
||||||
|
|
||||||
|
return RedirectToAction(nameof(Users));
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await UserManager.DeleteAsync(user);
|
||||||
|
|
||||||
|
Alert("User successfully deleted!", "success");
|
||||||
|
|
||||||
|
return RedirectToAction(nameof(Users));
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
Alert("User could not be deleted!", "danger");
|
||||||
|
|
||||||
|
return RedirectToAction(nameof(Users));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IActionResult> PromoteUser(Guid id)
|
||||||
|
{
|
||||||
|
var user = await UserManager.FindByIdAsync(id.ToString());
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await UserManager.AddToRoleAsync(user, "Administrator");
|
||||||
|
|
||||||
|
Alert("User promoted to administrator!", "success");
|
||||||
|
|
||||||
|
return RedirectToAction(nameof(Users));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Alert("User could not be promoted!", "danger");
|
||||||
|
|
||||||
|
return RedirectToAction(nameof(Users));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IActionResult> DemoteUser(Guid id)
|
||||||
|
{
|
||||||
|
var user = await UserManager.FindByIdAsync(id.ToString());
|
||||||
|
var admins = await UserManager.GetUsersInRoleAsync("Administrator");
|
||||||
|
|
||||||
|
if (user.UserName == HttpContext.User.Identity.Name)
|
||||||
|
{
|
||||||
|
Alert("You cannot demote yourself!", "danger");
|
||||||
|
|
||||||
|
return RedirectToAction(nameof(Users));
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await UserManager.RemoveFromRoleAsync(user, "Administrator");
|
||||||
|
|
||||||
|
Alert("User successfully demoted!", "success");
|
||||||
|
|
||||||
|
return RedirectToAction(nameof(Users));
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
Alert("User could not be demoted!", "danger");
|
||||||
|
|
||||||
|
return RedirectToAction(nameof(Users));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
9
LANCommander/Models/UserViewModel.cs
Normal file
9
LANCommander/Models/UserViewModel.cs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
namespace LANCommander.Models
|
||||||
|
{
|
||||||
|
public class UserViewModel
|
||||||
|
{
|
||||||
|
public Guid Id { get; set; }
|
||||||
|
public string UserName { get; set; }
|
||||||
|
public IEnumerable<string> Roles { get; set; }
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
@model LANCommander.Models.LANCommanderSettings
|
@model LANCommander.Models.LANCommanderSettings
|
||||||
|
|
||||||
@{
|
@{
|
||||||
ViewData["Title"] = "Settings";
|
ViewData["Title"] = "Settings | Users";
|
||||||
}
|
}
|
||||||
|
|
||||||
<div class="container-xl">
|
<div class="container-xl">
|
||||||
|
|
82
LANCommander/Views/Settings/Users.cshtml
Normal file
82
LANCommander/Views/Settings/Users.cshtml
Normal file
|
@ -0,0 +1,82 @@
|
||||||
|
@model IEnumerable<LANCommander.Models.UserViewModel>
|
||||||
|
|
||||||
|
@{
|
||||||
|
ViewData["Title"] = "Settings | Users";
|
||||||
|
}
|
||||||
|
|
||||||
|
<div class="container-xl">
|
||||||
|
<!-- Page title -->
|
||||||
|
<div class="page-header d-print-none">
|
||||||
|
<div class="row align-items-center">
|
||||||
|
<div class="col">
|
||||||
|
<div class="page-pretitle">Settings</div>
|
||||||
|
<h2 class="page-title">
|
||||||
|
Users
|
||||||
|
</h2>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="page-body">
|
||||||
|
<div class="container-xl">
|
||||||
|
<div class="card">
|
||||||
|
<div class="row g-0">
|
||||||
|
@{
|
||||||
|
await Html.RenderPartialAsync("_SidebarPartial");
|
||||||
|
}
|
||||||
|
|
||||||
|
<div class="col d-flex flex-column">
|
||||||
|
<div class="card-body">
|
||||||
|
<h2 class="mb-4">Users</h2>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="table-responsive">
|
||||||
|
<table class="table table-vcenter table-mobile-md card-table">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Username</th>
|
||||||
|
<th>Role</th>
|
||||||
|
<th></th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
|
||||||
|
<tbody>
|
||||||
|
@foreach (var item in Model.OrderBy(u => u.UserName))
|
||||||
|
{
|
||||||
|
<tr>
|
||||||
|
<td>
|
||||||
|
@item.UserName
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
@String.Join(", ", item.Roles)
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<div class="btn-list flex-nowrap justify-content-end">
|
||||||
|
@if (!item.Roles.Any(r => r == "Administrator"))
|
||||||
|
{
|
||||||
|
<a asp-action="PromoteUser" asp-route-id="@item.Id" class="btn btn-ghost-primary">Promote</a>
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
<a asp-action="DemoteUser" asp-route-id="@item.Id" class="btn btn-ghost-primary">Demote</a>
|
||||||
|
}
|
||||||
|
<a asp-action="DeleteUser" asp-route-id="@item.Id" class="btn btn-danger">Delete</a>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
@section Scripts {
|
||||||
|
@{
|
||||||
|
await Html.RenderPartialAsync("_ValidationScriptsPartial");
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,6 +2,7 @@
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<div class="list-group list-group-transparent">
|
<div class="list-group list-group-transparent">
|
||||||
<a asp-action="General" asp-controller="Settings" class="list-group-item list-group-item-action d-flex align-items-center">General</a>
|
<a asp-action="General" asp-controller="Settings" class="list-group-item list-group-item-action d-flex align-items-center">General</a>
|
||||||
|
<a asp-action="Users" asp-controller="Settings" class="list-group-item list-group-item-action d-flex align-items-center">Users</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
Loading…
Add table
Reference in a new issue