diff --git a/StudentHouseDashboard/Data/UserRepository.cs b/StudentHouseDashboard/Data/UserRepository.cs index d869b63..acdd681 100644 --- a/StudentHouseDashboard/Data/UserRepository.cs +++ b/StudentHouseDashboard/Data/UserRepository.cs @@ -125,10 +125,10 @@ namespace Data using (SqlConnection conn = SqlConnectionHelper.CreateConnection()) { string sql = "UPDATE Users " + - "SET Name = 'Deleted User @id', Password = '0'" + + "SET Name = 'Deleted User ' + @id, Password = '0'" + "WHERE ID = @id;"; SqlCommand cmd = new SqlCommand(sql, conn); - cmd.Parameters.AddWithValue("@id", id); + cmd.Parameters.AddWithValue("@id", id.ToString()); int writer = cmd.ExecuteNonQuery(); if (writer == 1) diff --git a/StudentHouseDashboard/WebApp/Pages/ChangePassword.cshtml b/StudentHouseDashboard/WebApp/Pages/ChangePassword.cshtml new file mode 100644 index 0000000..2562326 --- /dev/null +++ b/StudentHouseDashboard/WebApp/Pages/ChangePassword.cshtml @@ -0,0 +1,29 @@ +@page +@model WebApp.Pages.ChangePasswordModel +@{ +} +@if (ViewData["confirm"] != null) +{ + +} + +
+
+ + + +
+
+ + + +
+
+ + + +
+ +
\ No newline at end of file diff --git a/StudentHouseDashboard/WebApp/Pages/ChangePassword.cshtml.cs b/StudentHouseDashboard/WebApp/Pages/ChangePassword.cshtml.cs new file mode 100644 index 0000000..e22d79f --- /dev/null +++ b/StudentHouseDashboard/WebApp/Pages/ChangePassword.cshtml.cs @@ -0,0 +1,56 @@ +using Logic; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.RazorPages; +using Models; +using System.ComponentModel.DataAnnotations; +using System.Security.Claims; + +namespace WebApp.Pages +{ + [Authorize] + public class ChangePasswordModel : PageModel + { + [BindProperty] + [DataType(DataType.Password)] + [Required(ErrorMessage = "Current password is required.")] + public string Password { get; set; } + [BindProperty] + [DataType(DataType.Password)] + [Required(ErrorMessage = "New password is required.")] + public string NewPassword { get; set; } + [BindProperty] + [DataType(DataType.Password)] + [Required(ErrorMessage = "Confirmation Password is required.")] + [Compare("NewPassword", ErrorMessage = "Confirmation field not matching. Check your new password for mistakes.")] + public string ConfirmPassword { get; set; } + public void OnGet() + { + } + public void OnPost() + { + UserManager userManager = new UserManager(); + User user = userManager.GetUserById(int.Parse(User.FindFirstValue("id"))); + if (NewPassword == null) + { + ViewData["confirm"] = "New password not entered. Password not changed."; + return; + } + if (NewPassword != ConfirmPassword) + { + ViewData["confirm"] = "Password fields do not match. Password not changed."; + return; + } + if (BCrypt.Net.BCrypt.Verify(Password, user.Password)) + { + NewPassword = BCrypt.Net.BCrypt.HashPassword(NewPassword); + userManager.UpdateUser(user.ID, user.Name, NewPassword, user.Role); + ViewData["confirm"] = "Password successfully changed."; + } + else + { + ViewData["confirm"] = "Current password is not correct. Password not changed."; + } + } + } +} diff --git a/StudentHouseDashboard/WebApp/Pages/CreateAnnouncement.cshtml.cs b/StudentHouseDashboard/WebApp/Pages/CreateAnnouncement.cshtml.cs index 4243ee0..9ac90be 100644 --- a/StudentHouseDashboard/WebApp/Pages/CreateAnnouncement.cshtml.cs +++ b/StudentHouseDashboard/WebApp/Pages/CreateAnnouncement.cshtml.cs @@ -1,4 +1,5 @@ using Logic; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Models; @@ -6,6 +7,7 @@ using System.Security.Claims; namespace WebApp.Pages { + [Authorize] public class CreateAnnouncementModel : PageModel { [BindProperty] diff --git a/StudentHouseDashboard/WebApp/Pages/DeleteAnnouncement.cshtml.cs b/StudentHouseDashboard/WebApp/Pages/DeleteAnnouncement.cshtml.cs index b98ddcd..c8d57d8 100644 --- a/StudentHouseDashboard/WebApp/Pages/DeleteAnnouncement.cshtml.cs +++ b/StudentHouseDashboard/WebApp/Pages/DeleteAnnouncement.cshtml.cs @@ -1,4 +1,5 @@ using Logic; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Models; @@ -6,6 +7,7 @@ using System.Security.Claims; namespace WebApp.Pages { + [Authorize] public class DeleteAnnouncementModel : PageModel { [BindProperty] diff --git a/StudentHouseDashboard/WebApp/Pages/EditAnnouncement.cshtml.cs b/StudentHouseDashboard/WebApp/Pages/EditAnnouncement.cshtml.cs index 69f7134..3000016 100644 --- a/StudentHouseDashboard/WebApp/Pages/EditAnnouncement.cshtml.cs +++ b/StudentHouseDashboard/WebApp/Pages/EditAnnouncement.cshtml.cs @@ -1,4 +1,5 @@ using Logic; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Models; @@ -6,6 +7,7 @@ using System.Security.Claims; namespace WebApp.Pages { + [Authorize] public class EditAnnouncementModel : PageModel { [BindProperty] diff --git a/StudentHouseDashboard/WebApp/Pages/Shared/_Layout.cshtml b/StudentHouseDashboard/WebApp/Pages/Shared/_Layout.cshtml index e05c50b..012f34f 100644 --- a/StudentHouseDashboard/WebApp/Pages/Shared/_Layout.cshtml +++ b/StudentHouseDashboard/WebApp/Pages/Shared/_Layout.cshtml @@ -50,6 +50,9 @@ Create user } +