fixed registration crash
This commit is contained in:
@@ -20,6 +20,7 @@ namespace Data.Entities
|
|||||||
public DateTime StartDate { get; set; }
|
public DateTime StartDate { get; set; }
|
||||||
[Required]
|
[Required]
|
||||||
public DateTime EndDate { get; set; }
|
public DateTime EndDate { get; set; }
|
||||||
|
|
||||||
[ForeignKey("User")]
|
[ForeignKey("User")]
|
||||||
public string UserId { get; set; }
|
public string UserId { get; set; }
|
||||||
public virtual User User { get; set; }
|
public virtual User User { get; set; }
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Identity;
|
using Microsoft.AspNetCore.Identity;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace Data.Entities
|
namespace Data.Entities
|
||||||
{
|
{
|
||||||
@@ -16,4 +18,5 @@ namespace Data.Entities
|
|||||||
public string PersonalNumber { get; set; }
|
public string PersonalNumber { get; set; }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -65,6 +65,7 @@ namespace WebApp.Areas.Identity.Pages.Account
|
|||||||
[Display(Name = "Last Name")]
|
[Display(Name = "Last Name")]
|
||||||
public string LastName { get; set; }
|
public string LastName { get; set; }
|
||||||
|
|
||||||
|
[Phone]
|
||||||
[Display(Name = "Phone")]
|
[Display(Name = "Phone")]
|
||||||
public string PhoneNumber { get; set; }
|
public string PhoneNumber { get; set; }
|
||||||
|
|
||||||
@@ -81,6 +82,7 @@ namespace WebApp.Areas.Identity.Pages.Account
|
|||||||
[Display(Name = "Confirm password")]
|
[Display(Name = "Confirm password")]
|
||||||
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
|
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
|
||||||
public string ConfirmPassword { get; set; }
|
public string ConfirmPassword { get; set; }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task OnGetAsync(string returnUrl = null)
|
public async Task OnGetAsync(string returnUrl = null)
|
||||||
@@ -99,9 +101,6 @@ namespace WebApp.Areas.Identity.Pages.Account
|
|||||||
var result = await _userManager.CreateAsync(user, Input.Password);
|
var result = await _userManager.CreateAsync(user, Input.Password);
|
||||||
if (result.Succeeded)
|
if (result.Succeeded)
|
||||||
{
|
{
|
||||||
var defaultrole = _roleManager.FindByIdAsync("d3aa27bb-2866-4a7d-9f0d-30498859ae94").Result;
|
|
||||||
|
|
||||||
await _userManager.AddToRoleAsync(user, defaultrole.Name);
|
|
||||||
_logger.LogInformation("User created a new account with password.");
|
_logger.LogInformation("User created a new account with password.");
|
||||||
|
|
||||||
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
|
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
|
||||||
@@ -124,6 +123,7 @@ namespace WebApp.Areas.Identity.Pages.Account
|
|||||||
await _signInManager.SignInAsync(user, isPersistent: false);
|
await _signInManager.SignInAsync(user, isPersistent: false);
|
||||||
return LocalRedirect(returnUrl);
|
return LocalRedirect(returnUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
foreach (var error in result.Errors)
|
foreach (var error in result.Errors)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ namespace WebApp.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GET: Cars/Details/5
|
// GET: Cars/Details/5
|
||||||
|
|
||||||
public async Task<IActionResult> Details(int? id)
|
public async Task<IActionResult> Details(int? id)
|
||||||
{
|
{
|
||||||
if (id == null)
|
if (id == null)
|
||||||
@@ -43,7 +44,7 @@ namespace WebApp.Controllers
|
|||||||
|
|
||||||
return View(car);
|
return View(car);
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET: Cars/Create
|
// GET: Cars/Create
|
||||||
[Authorize(Roles = "Admin")]
|
[Authorize(Roles = "Admin")]
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
@@ -57,6 +58,7 @@ namespace WebApp.Controllers
|
|||||||
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
|
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
[ValidateAntiForgeryToken]
|
[ValidateAntiForgeryToken]
|
||||||
|
|
||||||
public async Task<IActionResult> Create([Bind("Id, Brand, Model, Year, CountPassengerSeats, Description, PriceForDay")] Car car)
|
public async Task<IActionResult> Create([Bind("Id, Brand, Model, Year, CountPassengerSeats, Description, PriceForDay")] Car car)
|
||||||
{
|
{
|
||||||
if (ModelState.IsValid)
|
if (ModelState.IsValid)
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ namespace WebApp.Controllers
|
|||||||
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
|
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
[ValidateAntiForgeryToken]
|
[ValidateAntiForgeryToken]
|
||||||
|
|
||||||
public async Task<IActionResult> Create([Bind("Id,CarId,StartDate,EndDate,UserId")] Rents rents)
|
public async Task<IActionResult> Create([Bind("Id,CarId,StartDate,EndDate,UserId")] Rents rents)
|
||||||
{
|
{
|
||||||
if (ModelState.IsValid)
|
if (ModelState.IsValid)
|
||||||
|
|||||||
@@ -1,152 +0,0 @@
|
|||||||
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;
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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));
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool UserExists(string id)
|
|
||||||
{
|
|
||||||
return _context.Users.Any(e => e.Id == id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -79,6 +79,7 @@ namespace API
|
|||||||
endpoints.MapRazorPages();
|
endpoints.MapRazorPages();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
<h1>Vehicle</h1>
|
<h1>Vehicle</h1>
|
||||||
|
<h2>Available cars</h2>
|
||||||
<p>
|
<p>
|
||||||
@if (this.User.IsInRole("Admin"))
|
@if (this.User.IsInRole("Admin"))
|
||||||
{
|
{
|
||||||
@@ -66,7 +67,7 @@
|
|||||||
@Html.DisplayFor(modelItem => item.PriceForDay)
|
@Html.DisplayFor(modelItem => item.PriceForDay)
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<a asp-controller="Rents" asp-action="Create">Select</a>
|
<a asp-controller="Rents" asp-action="Create" >Select</a>
|
||||||
</td>
|
</td>
|
||||||
@if (this.User.IsInRole("Admin"))
|
@if (this.User.IsInRole("Admin"))
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -5,7 +5,6 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
<h1>Rents</h1>
|
<h1>Rents</h1>
|
||||||
<h2>Available cars</h2>
|
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
<a asp-action="Create">Create New</a>
|
<a asp-action="Create">Create New</a>
|
||||||
|
|||||||
Reference in New Issue
Block a user