From f89a6e3db9eccd71cb7394193ec0b23f29bd88ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=91=D0=B0=D0=BA=D0=B0?= =?UTF-8?q?=D0=BB=D1=8C=D1=81=D0=BA=D0=B0=D1=8F?= Date: Wed, 29 May 2024 23:07:34 +0400 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD=D1=8B=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BD=D1=82=D1=80=D0=BE=D0=BB=D0=BB=D0=B5=D1=80?= =?UTF-8?q?=D1=8B=20=D0=B4=D0=BB=D1=8F=20=D1=8E=D0=B7=D0=B5=D1=80=D0=BE?= =?UTF-8?q?=D0=B2=20(=D1=80=D0=B5=D0=B3=D0=B8=D1=81=D1=82=D1=80=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F,=20=D0=BB=D0=B8=D1=87=D0=BD=D1=8B=D0=B5=20?= =?UTF-8?q?=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D0=B5,=20=D0=B2=D1=8B=D1=85?= =?UTF-8?q?=D0=BE=D0=B4,=20=D1=80=D0=B5=D0=B3=D0=B8=D1=81=D1=82=D1=80?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8F)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/HomeController.cs | 114 +++++++++++++++++- .../Models/UserPrivacyModel.cs | 2 +- 2 files changed, 111 insertions(+), 5 deletions(-) diff --git a/Polyclinic/PolyclinicWebAppSuretor/Controllers/HomeController.cs b/Polyclinic/PolyclinicWebAppSuretor/Controllers/HomeController.cs index 57fe5fb..7bef273 100644 --- a/Polyclinic/PolyclinicWebAppSuretor/Controllers/HomeController.cs +++ b/Polyclinic/PolyclinicWebAppSuretor/Controllers/HomeController.cs @@ -1,10 +1,12 @@ using Microsoft.AspNetCore.Mvc; +using Microsoft.IdentityModel.Tokens; using PolyclinicBusinessLogic.BusinessLogics; using PolyclinicBusinessLogic.OfficePackage; using PolyclinicContracts.BindingModels; using PolyclinicContracts.BusinessLogicsContracts; using PolyclinicContracts.SearchModels; using PolyclinicContracts.ViewModels; +using PolyclinicDataModels.Enums; using PolyclinicDataModels.Models; using PolyclinicWebAppSuretor.Models; using System.Diagnostics; @@ -51,6 +53,12 @@ namespace PolyclinicWebAppSuretor.Controllers return View(); } + /// + /// USER + /// + /// + /// + [HttpGet] [HttpPost] public IActionResult Register(RegisterModel model) @@ -70,14 +78,112 @@ namespace PolyclinicWebAppSuretor.Controllers model.FIO = model.FIO; return View(model); } - /*var user = new UserViewModel { - Email - };*/ + var user = new UserBindingModel + { + FIO = model.FIO, + Email = model.Email, + Password = model.Password, + Role = UserRole.Поручитель + }; + _userLogic.Create(user); return RedirectToAction("Login"); } else { - return View(); + return View(model); + } + } + + [HttpGet] + [HttpPost] + public IActionResult Login(LoginModel model) + { + var errors = new List(); + if (HttpContext.Request.Method == "POST") + { + var user = _userLogic.ReadElement(new UserSearchModel { Email = model.Email, Password = model.Password }); + if (user == null) + { + errors.Add("Неверные логин или пароль"); + } + else if (user.Role != UserRole.Поручитель) + { + errors.Add("Пользователь имеет неразрешенную роль"); + } + if (errors.Count > 0) + { + model = new LoginModel + { + Errors = errors + }; + return View(model); + } + LoginManager.LogginedUser = user; + return RedirectToAction("", "Home"); + } + else + { + model = new(); + return View(model); + } + } + + [HttpPost] + public IActionResult Logout() + { + LoginManager.LogginedUser = null; + return RedirectToAction("Login"); + } + + [HttpGet] + [HttpPost] + public IActionResult Privacy(UserPrivacyModel model) + { + var currentUser = LoginManager.LogginedUser; + if (currentUser == null) + { + return RedirectToAction("Login"); + } + if (HttpContext.Request.Method == "POST") + { + var errors = new List(); + var checkedUser = _userLogic.ReadElement(new UserSearchModel { Email = model.Email }); + if (checkedUser != null && checkedUser.Id != LoginManager.LogginedUser.Id) + { + errors.Add("Пользователь с таким Email уже есть"); + } + if (model.Password != model.ConfirmPassword) + { + errors.Add("Пароли не совпадают"); + } + if (errors.Count > 0) + { + model.Errors = errors; + model.Password = string.Empty; + model.ConfirmPassword = string.Empty; + return View(model); + } + var user = new UserBindingModel + { + Id = currentUser.Id, + FIO = model.FIO, + Email = model.Email, + Password = model.Password.IsNullOrEmpty() ? LoginManager.LogginedUser.Password : model.Password, + }; + _userLogic.Update(user); + LoginManager.LogginedUser = _userLogic.ReadElement(new UserSearchModel { Id = model.Id }); + return RedirectToAction("Privacy"); + } + else + { + model = new() + { + Id = currentUser.Id, + FIO = currentUser.FIO, + Email = currentUser.Email, + Role = currentUser.Role + }; + return View(model); } } diff --git a/Polyclinic/PolyclinicWebAppSuretor/Models/UserPrivacyModel.cs b/Polyclinic/PolyclinicWebAppSuretor/Models/UserPrivacyModel.cs index d3990d2..dc444f1 100644 --- a/Polyclinic/PolyclinicWebAppSuretor/Models/UserPrivacyModel.cs +++ b/Polyclinic/PolyclinicWebAppSuretor/Models/UserPrivacyModel.cs @@ -3,7 +3,7 @@ using System.ComponentModel; namespace PolyclinicWebAppSuretor.Models { - public class UserPrivacyModel + public class UserPrivacyModel : RegisterModel { public int Id { get; set; }