UsersController returns! Design updates

This commit is contained in:
Dimitar Byalkov
2022-04-08 22:09:31 +03:00
parent c9777042c0
commit 8935dbdc99
10 changed files with 361 additions and 98 deletions

View File

@@ -20,14 +20,14 @@ namespace WebApp.Controllers
{
_context = context;
}
[Authorize]
public async Task<IActionResult> Index()
{
return View(await _context.Cars.ToListAsync());
}
// GET: Cars/Details/5
[Authorize]
public async Task<IActionResult> Details(int? id)
{
if (id == null)
@@ -58,7 +58,7 @@ namespace WebApp.Controllers
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize(Roles = "Admin")]
public async Task<IActionResult> Create([Bind("Id, Brand, Model, Year, CountPassengerSeats, Description, PriceForDay")] Car car)
{
if (ModelState.IsValid)
@@ -147,6 +147,7 @@ namespace WebApp.Controllers
// POST: Cars/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
[Authorize(Roles = "Admin")]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var car = await _context.Cars.FindAsync(id);

View File

@@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Data;
using Data.Entities;
using Microsoft.AspNetCore.Authorization;
namespace WebApp.Controllers
{
@@ -20,12 +21,14 @@ namespace WebApp.Controllers
}
// GET: Rents
[Authorize(Roles = "Admin")]
public async Task<IActionResult> Index()
{
return View(await _context.Rents.ToListAsync());
}
// GET: Rents/Details/5
[Authorize]
public async Task<IActionResult> Details(int? id)
{
if (id == null)
@@ -42,13 +45,9 @@ namespace WebApp.Controllers
return View(rents);
}
public IActionResult DateSelect(DateTime startDate, DateTime endDate)
{
return RedirectToAction(nameof(Index));
}
// GET: Rents/Create
[Authorize]
public IActionResult Create(int id)
{
return View();
@@ -59,13 +58,15 @@ namespace WebApp.Controllers
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Car.Id,StartDate,EndDate,UserId")] Rents rents)
[Authorize]
public async Task<IActionResult> Create([Bind("Id,Car.Id,StartDate,EndDate,User.Id")] Rents rents)
{
if (ModelState.IsValid)
{
var car = _context.Cars.FirstOrDefault(car => car.Id == rents.Car.Id);
rents.Car = car;
var user = _context.Users.FirstOrDefault(user => user.Id == rents.User.Id);
rents.User = user;
_context.Add(rents);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
@@ -74,6 +75,7 @@ namespace WebApp.Controllers
}
// GET: Rents/Edit/5
[Authorize(Roles = "Admin")]
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
@@ -94,6 +96,7 @@ namespace WebApp.Controllers
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize(Roles = "Admin")]
public async Task<IActionResult> Edit(int id, [Bind("Id,CarId,StartDate,EndDate,UserId")] Rents rents)
{
if (id != rents.Id)
@@ -125,6 +128,7 @@ namespace WebApp.Controllers
}
// GET: Rents/Delete/5
[Authorize(Roles = "Admin")]
public async Task<IActionResult> Delete(int? id)
{
if (id == null)
@@ -145,6 +149,7 @@ namespace WebApp.Controllers
// POST: Rents/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
[Authorize(Roles = "Admin")]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var rents = await _context.Rents.FindAsync(id);

View File

@@ -0,0 +1,195 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Data;
using Data.Entities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
namespace WebApp.Controllers
{
public class UserWithRoles
{
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string PersonalNumber { get; set; }
public string PhoneNumber { get; set; }
public string Role { get; set; }
}
public class UsersController : Controller
{
private readonly RentACarDbContext _context;
public UsersController(RentACarDbContext context)
{
_context = context;
}
// GET: Users
[Authorize(Roles = "Admin")]
public async Task<IActionResult> Index()
{
ICollection<User> users = await _context.Users.ToListAsync();
ICollection<UserWithRoles> usersWithRole = new List<UserWithRoles>();
//foreach (var item in users)
//{
// var userRole = await _context.UserRoles.FirstOrDefaultAsync(userRoles => userRoles.UserId == item.Id);
// var role = await _context.Roles.FirstOrDefaultAsync(role => role.Id == userRole.RoleId);
// usersWithRole.Add(
// new UserWithRoles()
// {
// UserName = item.UserName,
// FirstName = item.FirstName,
// LastName = item.LastName,
// Email = item.Email,
// PersonalNumber = item.PersonalNumber,
// PhoneNumber = item.PhoneNumber,
// Role = role.Name
// }
// );
//}
return View(users);
}
// GET: Users/Edit/5
[Authorize(Roles = "Admin")]
public async Task<IActionResult> Edit(string id)
{
if (id == null)
{
return NotFound();
}
var user = await _context.Users.FindAsync(id);
if (user == null)
{
return NotFound();
}
return View(user);
}
// GET: Rents/Create
[Authorize(Roles = "Admin")]
public IActionResult Create(int id)
{
return View();
}
// POST: Rents/Create
// To protect from overposting attacks, enable the specific properties you want to bind to.
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize(Roles = "Admin")]
public async Task<IActionResult> Create([Bind("FirstName,LastName,PersonalNumber,Id,UserName,NormalizedUserName,Email,NormalizedEmail,EmailConfirmed,PasswordHash,SecurityStamp,ConcurrencyStamp,PhoneNumber,PhoneNumberConfirmed,TwoFactorEnabled,LockoutEnd,LockoutEnabled,AccessFailedCount")] User user)
{
user.Id = Guid.NewGuid().ToString();
PasswordHasher<string> passwordHasher = new PasswordHasher<string>();
user.PasswordHash = passwordHasher.HashPassword(user.Id, user.PasswordHash);
if (ModelState.IsValid)
{
_context.Add(user);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(user);
}
// POST: Users/Edit/5
// To protect from overposting attacks, enable the specific properties you want to bind to.
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize(Roles = "Admin")]
public async Task<IActionResult> Edit(string id, [Bind("FirstName,LastName,PersonalNumber,Id,UserName,NormalizedUserName,Email,NormalizedEmail,EmailConfirmed,PasswordHash,SecurityStamp,ConcurrencyStamp,PhoneNumber,PhoneNumberConfirmed,TwoFactorEnabled,LockoutEnd,LockoutEnabled,AccessFailedCount")] User user)
{
if (id != user.Id)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(user);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!UserExists(user.Id))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(user);
}
// GET: Users/Delete/5
[Authorize(Roles = "Admin")]
public async Task<IActionResult> Delete(string id)
{
if (id == null)
{
return NotFound();
}
var user = await _context.Users
.FirstOrDefaultAsync(m => m.Id == id);
if (user == null)
{
return NotFound();
}
return View(user);
}
// POST: Users/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
[Authorize(Roles = "Admin")]
public async Task<IActionResult> DeleteConfirmed(string id)
{
var user = await _context.Users.FindAsync(id);
_context.Users.Remove(user);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
// GET: Users/Details/5
[Authorize(Roles = "Admin")]
public async Task<IActionResult> Details(string? id)
{
if (id == null)
{
return NotFound();
}
var user = await _context.Users
.FirstOrDefaultAsync(m => m.Id == id);
if (user == null)
{
return NotFound();
}
return View(user);
}
private bool UserExists(string id)
{
return _context.Users.Any(e => e.Id == id);
}
}
}

View File

@@ -11,11 +11,12 @@
{
<a type="button" class="btn btn-primary" asp-area="" asp-controller="Cars" asp-action="Index">Manage cars</a>
<a type="button" class="btn btn-primary" asp-area="" asp-controller="Rents" asp-action="Index">Manage rents</a>
<a type="button" class="btn btn-primary" asp-area="" asp-controller="Users" asp-action="Index">Manage users</a>
}
else
{
<a type="button" class="btn btn-primary" asp-area="" asp-controller="Cars" asp-action="Index">See all cars</a>
<a type="button" class="btn btn-primary" asp-area="" asp-controller="Rents" asp-action="Index">Rent a car</a>
<a type="button" class="btn btn-primary" asp-area="" asp-controller="Rents" asp-action="Create">Rent a car</a>
}
}
else

View File

@@ -27,8 +27,11 @@
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
<a type="button" class="btn btn-secondary" asp-action="Index">Back to List</a>
</div>
@if (this.User.IsInRole("Admin"))
{
<a type="button" class="btn btn-secondary" asp-action="Index">Back to List</a>
}
</div>
</form>
</div>
</div>

View File

@@ -20,13 +20,22 @@
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Cars" asp-action="Index">Cars</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Rents" asp-action="Index">Rents</a>
</li>
@if (this.User.Identity.Name != null)
{
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Cars" asp-action="Index">Cars</a>
</li>
@if (this.User.IsInRole("Admin"))
{
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Rents" asp-action="Index">Rents</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Users" asp-action="Index">Users</a>
</li>
}
}
</ul>
<partial name="_LoginPartial" />
</div>

View File

@@ -0,0 +1,79 @@
@model Data.Entities.User
@{
ViewData["Title"] = "Create";
}
<h1>Create user</h1>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="FirstName" class="control-label"></label>
<input asp-for="FirstName" class="form-control" />
<span asp-validation-for="FirstName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="LastName" class="control-label"></label>
<input asp-for="LastName" class="form-control" />
<span asp-validation-for="LastName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="PersonalNumber" class="control-label"></label>
<input asp-for="PersonalNumber" class="form-control" />
<span asp-validation-for="PersonalNumber" class="text-danger"></span>
</div>
<input type="hidden" asp-for="Id" />
<div class="form-group">
<label asp-for="UserName" class="control-label"></label>
<input asp-for="UserName" class="form-control" />
<span asp-validation-for="UserName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Email" class="control-label"></label>
<input asp-for="Email" class="form-control" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="form-group form-check">
<label class="form-check-label">
<input class="form-check-input" asp-for="EmailConfirmed" /> @Html.DisplayNameFor(model => model.EmailConfirmed)
</label>
</div>
<div class="form-group">
<label asp-for="PasswordHash" class="control-label"></label>
<input asp-for="PasswordHash" class="form-control" />
<span asp-validation-for="PasswordHash" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="PhoneNumber" class="control-label"></label>
<input asp-for="PhoneNumber" class="form-control" />
<span asp-validation-for="PhoneNumber" class="text-danger"></span>
</div>
<div class="form-group form-check">
<label class="form-check-label">
<input class="form-check-input" asp-for="PhoneNumberConfirmed" /> @Html.DisplayNameFor(model => model.PhoneNumberConfirmed)
</label>
</div>
<div class="form-group">
<label asp-for="LockoutEnd" class="control-label"></label>
<input asp-for="LockoutEnd" class="form-control" />
<span asp-validation-for="LockoutEnd" class="text-danger"></span>
</div>
<div class="form-group form-check">
<label class="form-check-label">
<input class="form-check-input" asp-for="LockoutEnabled" /> @Html.DisplayNameFor(model => model.LockoutEnabled)
</label>
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
</div>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

View File

@@ -4,11 +4,10 @@
ViewData["Title"] = "Delete";
}
<h1>Delete</h1>
<h1>Delete user</h1>
<h3>Are you sure you want to delete this?</h3>
<div>
<h4>User</h4>
<hr />
<dl class="row">
<dt class = "col-sm-2">
@@ -117,7 +116,7 @@
<form asp-action="Delete">
<input type="hidden" asp-for="Id" />
<input type="submit" value="Delete" class="btn btn-danger" /> |
<a asp-action="Index">Back to List</a>
<input type="submit" value="Delete" class="btn btn-danger" />
<a type="button" class="btn btn-secondary" asp-action="Index">Back to List</a>
</form>
</div>

View File

@@ -4,9 +4,7 @@
ViewData["Title"] = "Edit";
}
<h1>Edit</h1>
<h4>User</h4>
<h1>Edit user</h1>
<hr />
<div class="row">
<div class="col-md-4">
@@ -33,21 +31,11 @@
<input asp-for="UserName" class="form-control" />
<span asp-validation-for="UserName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="NormalizedUserName" class="control-label"></label>
<input asp-for="NormalizedUserName" class="form-control" />
<span asp-validation-for="NormalizedUserName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Email" class="control-label"></label>
<input asp-for="Email" class="form-control" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="NormalizedEmail" class="control-label"></label>
<input asp-for="NormalizedEmail" class="form-control" />
<span asp-validation-for="NormalizedEmail" class="text-danger"></span>
</div>
<div class="form-group form-check">
<label class="form-check-label">
<input class="form-check-input" asp-for="EmailConfirmed" /> @Html.DisplayNameFor(model => model.EmailConfirmed)
@@ -58,16 +46,6 @@
<input asp-for="PasswordHash" class="form-control" />
<span asp-validation-for="PasswordHash" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="SecurityStamp" class="control-label"></label>
<input asp-for="SecurityStamp" class="form-control" />
<span asp-validation-for="SecurityStamp" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ConcurrencyStamp" class="control-label"></label>
<input asp-for="ConcurrencyStamp" class="form-control" />
<span asp-validation-for="ConcurrencyStamp" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="PhoneNumber" class="control-label"></label>
<input asp-for="PhoneNumber" class="form-control" />
@@ -78,11 +56,6 @@
<input class="form-check-input" asp-for="PhoneNumberConfirmed" /> @Html.DisplayNameFor(model => model.PhoneNumberConfirmed)
</label>
</div>
<div class="form-group form-check">
<label class="form-check-label">
<input class="form-check-input" asp-for="TwoFactorEnabled" /> @Html.DisplayNameFor(model => model.TwoFactorEnabled)
</label>
</div>
<div class="form-group">
<label asp-for="LockoutEnd" class="control-label"></label>
<input asp-for="LockoutEnd" class="form-control" />
@@ -94,21 +67,13 @@
</label>
</div>
<div class="form-group">
<label asp-for="AccessFailedCount" class="control-label"></label>
<input asp-for="AccessFailedCount" class="form-control" />
<span asp-validation-for="AccessFailedCount" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
<input class="btn btn-primary" type="submit" value="Save" />
<a type="button" class="btn btn-secondary" asp-action="Index">Back to List</a>
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

View File

@@ -1,62 +1,68 @@
@model IEnumerable<Data.Entities.User>
@{
ViewData["Title"] = "Index";
ViewData["Title"] = "Users";
}
<h1>Index</h1>
<h1>Users</h1>
<p>
<a type="button" class="btn btn-primary" asp-action="Create">Create user</a>
</p>
<table class="table">
<table class="table table-striped">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.FirstName)
First name
</th>
<th>
@Html.DisplayNameFor(model => model.LastName)
Last name
</th>
<th>
@Html.DisplayNameFor(model => model.PersonalNumber)
ID number
</th>
<th>
@Html.DisplayNameFor(model => model.UserName)
Username
</th>
<th>
@Html.DisplayNameFor(model => model.Email)
E-mail
</th>
<th>
@Html.DisplayNameFor(model => model.PhoneNumber)
Phone number
</th>
<th>
Actions
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.PersonalNumber)
</td>
<td>
@Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.DisplayFor(modelItem => item.PhoneNumber)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
</td>
</tr>
}
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.PersonalNumber)
</td>
<td>
@Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.DisplayFor(modelItem => item.PhoneNumber)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
</td>
</tr>
}
</tbody>
</table>