Initial commit

This commit is contained in:
2026-07-14 20:40:37 +02:00
commit ffd8a087c8
119 changed files with 85507 additions and 0 deletions
@@ -0,0 +1,10 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace TemplateWeb.Areas.Admin.Controllers;
[Area("Admin")]
[Authorize(Roles = "Admin")]
public abstract class AdminBaseController : Controller
{
}
@@ -0,0 +1,11 @@
using Microsoft.AspNetCore.Mvc;
namespace TemplateWeb.Areas.Admin.Controllers;
public class DashboardController : AdminBaseController
{
public IActionResult Index()
{
return View();
}
}
@@ -0,0 +1,57 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using TemplateWeb.Entities;
using TemplateWeb.Models;
namespace TemplateWeb.Areas.Admin.Controllers;
public class UsersController : AdminBaseController
{
private readonly UserManager<UserEntity> _userManager;
private readonly RoleManager<IdentityRole> _roleManager;
public UsersController(UserManager<UserEntity> userManager, RoleManager<IdentityRole> roleManager)
{
_userManager = userManager;
_roleManager = roleManager;
}
public async Task<IActionResult> Index()
{
var allRoles = _roleManager.Roles.Select(r => r.Name!).ToList();
var users = _userManager.Users.ToList();
var viewModels = new List<UserWithRolesViewModel>();
foreach (var user in users)
{
var roles = await _userManager.GetRolesAsync(user);
viewModels.Add(new UserWithRolesViewModel { UserEntity = user, Roles = roles, AllRoles = allRoles });
}
return View(viewModels);
}
public async Task<IActionResult> Details(string id)
{
var user = await _userManager.FindByIdAsync(id);
if (user == null) return NotFound();
var roles = await _userManager.GetRolesAsync(user);
var allRoles = _roleManager.Roles.Select(r => r.Name!).ToList();
return View(new UserWithRolesViewModel { UserEntity = user, Roles = roles, AllRoles = allRoles });
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> UpdateRoles(string id, List<string> selectedRoles)
{
var user = await _userManager.FindByIdAsync(id);
if (user == null) return NotFound();
var currentRoles = await _userManager.GetRolesAsync(user);
await _userManager.RemoveFromRolesAsync(user, currentRoles);
if (selectedRoles.Count > 0)
await _userManager.AddToRolesAsync(user, selectedRoles);
TempData["SuccessMessage"] = "Roles updated successfully.";
return RedirectToAction(nameof(Details), new { id });
}
}
@@ -0,0 +1,19 @@
@{
ViewData["Title"] = "Dashboard";
}
<h1>Admin Dashboard</h1>
<p>Welcome to the administration area.</p>
<div class="row mt-4">
<div class="col-md-4">
<div class="card text-white bg-primary mb-3">
<div class="card-header">Users</div>
<div class="card-body">
<h5 class="card-title">Manage system users</h5>
<p class="card-text">View and edit user details.</p>
<a asp-controller="Users" asp-action="Index" class="btn btn-light">Go to Users</a>
</div>
</div>
</div>
</div>
@@ -0,0 +1,69 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>@ViewData["Title"] - Admin Panel</title>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css"/>
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true"/>
<style>
body { display: flex; min-height: 100vh; flex-direction: column; }
.wrapper { display: flex; flex: 1; }
#sidebar { min-width: 250px; max-width: 250px; background: #343a40; color: #fff; transition: all 0.3s; }
#sidebar .sidebar-header { padding: 20px; background: #3c444b; }
#sidebar ul.components { padding: 20px 0; border-bottom: 1px solid #47748b; }
#sidebar ul p { color: #fff; padding: 10px; }
#sidebar ul li a { padding: 10px; font-size: 1.1em; display: block; color: #fff; text-decoration: none; }
#sidebar ul li a:hover { color: #343a40; background: #fff; }
#content { width: 100%; padding: 20px; }
</style>
</head>
<body>
<header>
<nav class="navbar navbar-expand-sm navbar-dark bg-dark border-bottom box-shadow">
<div class="container-fluid">
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">TemplateWeb <small class="text-muted">Admin</small></a>
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
<ul class="navbar-nav flex-grow-1">
</ul>
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link text-light" asp-area="" asp-controller="Home" asp-action="Index">Back to Site</a>
</li>
</ul>
</div>
</div>
</nav>
</header>
<div class="wrapper">
<!-- Sidebar -->
<nav id="sidebar">
<div class="sidebar-header">
<h3>Admin Panel</h3>
</div>
<ul class="list-unstyled components">
<li class="@(ViewContext.RouteData.Values["controller"]?.ToString() == "Dashboard" ? "active" : "")">
<a asp-area="Admin" asp-controller="Dashboard" asp-action="Index">Dashboard</a>
</li>
<li class="@(ViewContext.RouteData.Values["controller"]?.ToString() == "Users" ? "active" : "")">
<a asp-area="Admin" asp-controller="Users" asp-action="Index">Users</a>
</li>
<!-- Add more models here easily -->
</ul>
</nav>
<!-- Page Content -->
<div id="content">
<main role="main" class="pb-3">
@RenderBody()
</main>
</div>
</div>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
@await RenderSectionAsync("Scripts", required: false)
</body>
</html>
@@ -0,0 +1,62 @@
@model UserWithRolesViewModel
@{
ViewData["Title"] = "User Details";
}
<div class="mb-4">
<a asp-action="Index" class="btn btn-secondary">&larr; Back to List</a>
</div>
@if (TempData["SuccessMessage"] != null)
{
<div class="alert alert-success">@TempData["SuccessMessage"]</div>
}
<div class="card mb-4">
<div class="card-header bg-primary text-white">
<h3 class="card-title mb-0">User Information: @Model.UserEntity.UserName</h3>
</div>
<div class="card-body">
<dl class="row">
<dt class="col-sm-3">ID</dt>
<dd class="col-sm-9">@Model.UserEntity.Id</dd>
<dt class="col-sm-3">Username</dt>
<dd class="col-sm-9">@Model.UserEntity.UserName</dd>
<dt class="col-sm-3">Email</dt>
<dd class="col-sm-9">@Model.UserEntity.Email</dd>
<dt class="col-sm-3">Roles</dt>
<dd class="col-sm-9">
@foreach (var role in Model.Roles)
{
<span class="badge @(role == "Admin" ? "bg-danger" : "bg-info")">@role</span>
}
</dd>
</dl>
</div>
</div>
<div class="card">
<div class="card-header">
<h5 class="mb-0">Edit Roles</h5>
</div>
<div class="card-body">
<form asp-action="UpdateRoles" asp-route-id="@Model.UserEntity.Id" method="post">
@Html.AntiForgeryToken()
<div class="d-flex gap-4 flex-wrap mb-3">
@foreach (var role in Model.AllRoles)
{
<div class="form-check">
<input class="form-check-input" type="checkbox"
name="selectedRoles" value="@role" id="role_@role"
@(Model.Roles.Contains(role) ? "checked" : "") />
<label class="form-check-label" for="role_@role">@role</label>
</div>
}
</div>
<button type="submit" class="btn btn-primary">Save Roles</button>
</form>
</div>
</div>
@@ -0,0 +1,39 @@
@model IEnumerable<UserWithRolesViewModel>
@{
ViewData["Title"] = "User Management";
}
<div class="d-flex justify-content-between align-items-center mb-4">
<h1>Users</h1>
</div>
<table class="table table-striped table-hover">
<thead class="table-dark">
<tr>
<th>ID</th>
<th>Username</th>
<th>Email</th>
<th>Roles</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach (var vm in Model)
{
<tr>
<td>@vm.UserEntity.Id</td>
<td>@vm.UserEntity.UserName</td>
<td>@vm.UserEntity.Email</td>
<td>
@foreach (var role in vm.Roles)
{
<span class="badge @(role == "Admin" ? "bg-danger" : "bg-info")">@role</span>
}
</td>
<td>
<a asp-action="Details" asp-route-id="@vm.UserEntity.Id" class="btn btn-sm btn-outline-primary">Details</a>
</td>
</tr>
}
</tbody>
</table>
@@ -0,0 +1,3 @@
@using TemplateWeb
@using TemplateWeb.Models
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@@ -0,0 +1,3 @@
@{
Layout = "_AdminLayout";
}