2024-05-29 02:22:44 +04:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2024-05-29 14:53:23 +04:00
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
2024-05-29 02:22:44 +04:00
|
|
|
|
using PolyclinicContracts.BindingModels;
|
|
|
|
|
using PolyclinicContracts.BusinessLogicsContracts;
|
|
|
|
|
using PolyclinicContracts.SearchModels;
|
|
|
|
|
using PolyclinicDataModels.Enums;
|
|
|
|
|
using PolyclinicDataModels.Models;
|
|
|
|
|
using PolyclinicWebAppImplementer.Models;
|
|
|
|
|
|
|
|
|
|
namespace PolyclinicWebAppImplementer.Controllers
|
|
|
|
|
{
|
|
|
|
|
public class UserController : Controller
|
|
|
|
|
{
|
|
|
|
|
private readonly IUserLogic _userLogic;
|
|
|
|
|
public UserController(IUserLogic userLogic)
|
|
|
|
|
{
|
|
|
|
|
_userLogic = userLogic;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public IActionResult Login(LoginModel model)
|
|
|
|
|
{
|
|
|
|
|
var errors = new List<string>();
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public IActionResult Register(RegisterModel model)
|
|
|
|
|
{
|
|
|
|
|
var errors = new List<string>();
|
|
|
|
|
if (HttpContext.Request.Method == "POST")
|
|
|
|
|
{
|
|
|
|
|
if (_userLogic.ReadElement(new UserSearchModel { Email = model.Email }) != null)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
FIO = model.FIO,
|
|
|
|
|
Email = model.Email,
|
|
|
|
|
Password = model.Password,
|
|
|
|
|
Role = UserRole.Исполнитель
|
|
|
|
|
};
|
|
|
|
|
_userLogic.Create(user);
|
|
|
|
|
return RedirectToAction("Login");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return View(model);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public IActionResult Logout()
|
|
|
|
|
{
|
|
|
|
|
LoginManager.LogginedUser = null;
|
|
|
|
|
return RedirectToAction("Login");
|
|
|
|
|
}
|
2024-05-29 14:23:16 +04:00
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[HttpPost]
|
2024-05-29 14:53:23 +04:00
|
|
|
|
public IActionResult Privacy(UserPrivacyModel model)
|
2024-05-29 14:23:16 +04:00
|
|
|
|
{
|
2024-05-29 14:53:23 +04:00
|
|
|
|
var currentUser = LoginManager.LogginedUser;
|
|
|
|
|
if (currentUser == null)
|
2024-05-29 14:23:16 +04:00
|
|
|
|
{
|
|
|
|
|
return RedirectToAction("Login");
|
|
|
|
|
}
|
2024-05-29 14:53:23 +04:00
|
|
|
|
if (HttpContext.Request.Method == "POST")
|
|
|
|
|
{
|
|
|
|
|
var errors = new List<string>();
|
|
|
|
|
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);
|
|
|
|
|
}
|
2024-05-29 14:23:16 +04:00
|
|
|
|
}
|
2024-05-29 02:22:44 +04:00
|
|
|
|
}
|
|
|
|
|
}
|