("LastName")
.HasColumnType("nvarchar(max)");
diff --git a/RentACar/Data/Models/User.cs b/RentACar/Data/Models/User.cs
index 63bfb69..5206ada 100644
--- a/RentACar/Data/Models/User.cs
+++ b/RentACar/Data/Models/User.cs
@@ -14,5 +14,7 @@ namespace Data.Entities
public string LastName { get; set; }
public string PersonalNumber { get; set; }
+
+ public bool IsAdmin { get; set; }
}
}
diff --git a/RentACar/WebApp/Areas/Identity/Pages/Account/Login.cshtml b/RentACar/WebApp/Areas/Identity/Pages/Account/Login.cshtml
index 8e4d327..857833a 100644
--- a/RentACar/WebApp/Areas/Identity/Pages/Account/Login.cshtml
+++ b/RentACar/WebApp/Areas/Identity/Pages/Account/Login.cshtml
@@ -14,9 +14,9 @@
-
-
-
+
+
+
diff --git a/RentACar/WebApp/Areas/Identity/Pages/Account/Login.cshtml.cs b/RentACar/WebApp/Areas/Identity/Pages/Account/Login.cshtml.cs
index 011e062..c5acdcc 100644
--- a/RentACar/WebApp/Areas/Identity/Pages/Account/Login.cshtml.cs
+++ b/RentACar/WebApp/Areas/Identity/Pages/Account/Login.cshtml.cs
@@ -44,8 +44,7 @@ namespace WebApp.Areas.Identity.Pages.Account
public class InputModel
{
[Required]
- [EmailAddress]
- public string Email { get; set; }
+ public string Username { get; set; }
[Required]
[DataType(DataType.Password)]
@@ -82,7 +81,7 @@ namespace WebApp.Areas.Identity.Pages.Account
{
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, set lockoutOnFailure: true
- var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: false);
+ var result = await _signInManager.PasswordSignInAsync(Input.Username, Input.Password, Input.RememberMe, lockoutOnFailure: false);
if (result.Succeeded)
{
_logger.LogInformation("User logged in.");
diff --git a/RentACar/WebApp/Areas/Identity/Pages/Account/Register.cshtml b/RentACar/WebApp/Areas/Identity/Pages/Account/Register.cshtml
index d9b50ab..7326a17 100644
--- a/RentACar/WebApp/Areas/Identity/Pages/Account/Register.cshtml
+++ b/RentACar/WebApp/Areas/Identity/Pages/Account/Register.cshtml
@@ -17,6 +17,36 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/RentACar/WebApp/Areas/Identity/Pages/Account/Register.cshtml.cs b/RentACar/WebApp/Areas/Identity/Pages/Account/Register.cshtml.cs
index 4ba281f..7d26258 100644
--- a/RentACar/WebApp/Areas/Identity/Pages/Account/Register.cshtml.cs
+++ b/RentACar/WebApp/Areas/Identity/Pages/Account/Register.cshtml.cs
@@ -52,6 +52,25 @@ namespace WebApp.Areas.Identity.Pages.Account
[Display(Name = "Email")]
public string Email { get; set; }
+ [Required]
+ public string UserName { get; set; }
+
+ [Required]
+ public string FirstName { get; set; }
+
+ [Required]
+ public string LastName { get; set; }
+ public bool IsAdmin { get; set; }
+
+ [Required]
+ [StringLength(10, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 10)]
+ public string EGN { get; set; }
+
+ [Required]
+ [DataType(DataType.PhoneNumber)]
+ [StringLength(10, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 10)]
+ public string PhoneNumber { get; set; }
+
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
@@ -76,25 +95,23 @@ namespace WebApp.Areas.Identity.Pages.Account
ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
if (ModelState.IsValid)
{
- var user = new User { UserName = Input.Email, Email = Input.Email };
+ var user = new User { Id = Guid.NewGuid().ToString(), UserName = Input.UserName, Email = Input.Email, FirstName = Input.FirstName, LastName = Input.LastName, PhoneNumber = Input.PhoneNumber, PersonalNumber = Input.EGN, IsAdmin = Input.IsAdmin};
var result = await _userManager.CreateAsync(user, Input.Password);
if (result.Succeeded)
{
- _logger.LogInformation("User created a new account with password.");
-
- var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
- code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
- var callbackUrl = Url.Page(
- "/Account/ConfirmEmail",
- pageHandler: null,
- values: new { area = "Identity", userId = user.Id, code = code, returnUrl = returnUrl },
- protocol: Request.Scheme);
-
-
- await _signInManager.SignInAsync(user, isPersistent: false);
- return LocalRedirect(returnUrl);
-
+ _logger.LogInformation("User created a new account with password.");
+ await _signInManager.SignInAsync(user, isPersistent: false);
+ if (Input.IsAdmin == true)
+ {
+ await _userManager.AddToRoleAsync(user, "Admin");
+ }
+ else
+ {
+ await _userManager.AddToRoleAsync(user, "User");
+ }
+ return LocalRedirect(returnUrl);
}
+
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
diff --git a/RentACar/WebApp/Controllers/HomeController.cs b/RentACar/WebApp/Controllers/HomeController.cs
index fa2b3b8..6b36d42 100644
--- a/RentACar/WebApp/Controllers/HomeController.cs
+++ b/RentACar/WebApp/Controllers/HomeController.cs
@@ -25,6 +25,11 @@ namespace API.Controllers
public IActionResult Privacy()
{
+ if(!this.User.Identity.IsAuthenticated)
+ {
+ return Redirect("");
+ }
+
return View();
}
diff --git a/RentACar/WebApp/Startup.cs b/RentACar/WebApp/Startup.cs
index 7e67b4c..8ed4d19 100644
--- a/RentACar/WebApp/Startup.cs
+++ b/RentACar/WebApp/Startup.cs
@@ -48,7 +48,7 @@ namespace API
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
- public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
+ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider serviceProvider)
{
if (env.IsDevelopment())
{
@@ -68,7 +68,7 @@ namespace API
app.UseAuthentication();
app.UseAuthorization();
-
+ CreateUserRoles(serviceProvider).Wait();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
@@ -77,5 +77,43 @@ namespace API
endpoints.MapRazorPages();
});
}
+
+ private async System.Threading.Tasks.Task CreateUserRoles(IServiceProvider serviceProvider)
+ {
+ var RoleManager = serviceProvider.GetRequiredService
>();
+ var UserManager = serviceProvider.GetRequiredService>();
+ string[] roleNames = { "Admin", "User"};
+ IdentityResult roleResult;
+ foreach (var roleName in roleNames)
+ {
+ var roleCheck = await RoleManager.RoleExistsAsync(roleName);
+ if (!roleCheck)
+ {
+ roleResult = await RoleManager.CreateAsync(new IdentityRole(roleName));
+ }
+ }
+
+ var user = new User();
+ user.UserName = "admin";
+ user.Id = Guid.NewGuid().ToString();
+ user.FirstName = "Admin";
+ user.LastName = "Admin";
+ user.PersonalNumber = "1234567890";
+ user.PhoneNumber = "0888888888";
+ user.Email = "admin@admin.admin";
+ user.IsAdmin = true;
+ string userPWD = "password";
+ var _user = await UserManager.FindByNameAsync(user.UserName);
+ if (_user == null)
+ {
+ IdentityResult chkUser = await UserManager.CreateAsync(user, userPWD);
+ if (chkUser.Succeeded)
+ {
+ await UserManager.AddToRoleAsync(user, "Admin");
+ }
+
+ }
+
+ }
}
}
diff --git a/RentACar/WebApp/Views/Cars/Index.cshtml b/RentACar/WebApp/Views/Cars/Index.cshtml
index bab159c..2937a34 100644
--- a/RentACar/WebApp/Views/Cars/Index.cshtml
+++ b/RentACar/WebApp/Views/Cars/Index.cshtml
@@ -5,9 +5,11 @@
}
Index
-
- New Car
+ @if (this.User.IsInRole("Admin"))
+ {
+ New car
+ }
@@ -34,32 +36,33 @@
-@foreach (var item in Model) {
-
- |
- @Html.DisplayFor(modelItem => item.Brand)
- |
-
- @Html.DisplayFor(modelItem => item.Model)
- |
-
- @Html.DisplayFor(modelItem => item.Year)
- |
-
- @Html.DisplayFor(modelItem => item.CountPassengerSeats)
- |
-
- @Html.DisplayFor(modelItem => item.Description)
- |
-
- @Html.DisplayFor(modelItem => item.PriceForDay)
- |
-
- Edit |
- Details |
- Delete
- |
-
-}
+ @foreach (var item in Model)
+ {
+
+ |
+ @Html.DisplayFor(modelItem => item.Brand)
+ |
+
+ @Html.DisplayFor(modelItem => item.Model)
+ |
+
+ @Html.DisplayFor(modelItem => item.Year)
+ |
+
+ @Html.DisplayFor(modelItem => item.CountPassengerSeats)
+ |
+
+ @Html.DisplayFor(modelItem => item.Description)
+ |
+
+ @Html.DisplayFor(modelItem => item.PriceForDay)
+ |
+
+ Edit |
+ Details |
+ Delete
+ |
+
+ }
diff --git a/RentACar/WebApp/Views/Shared/_Layout.cshtml b/RentACar/WebApp/Views/Shared/_Layout.cshtml
index 40f538c..009c8c3 100644
--- a/RentACar/WebApp/Views/Shared/_Layout.cshtml
+++ b/RentACar/WebApp/Views/Shared/_Layout.cshtml
@@ -27,6 +27,15 @@
Cars
+ @if (this.User.IsInRole("Admin"))
+ {
+
+ Create Car
+
+
+ Rents
+
+ }