From 9a5b50286bf61db0e909e09dc56addbb49e60317 Mon Sep 17 00:00:00 2001 From: Oleg Shabunov Date: Fri, 21 Jun 2024 14:29:50 +0400 Subject: [PATCH] authentication in ShopApp --- ...irViewModel.cs => ShopRepairsViewModel.cs} | 2 +- AutoWorkshopRestApi/ApiConfig.cs | 12 --- .../Controllers/ShopController.cs | 57 +++------- AutoWorkshopRestApi/Program.cs | 2 - AutoWorkshopRestApi/appsettings.json | 3 +- AutoWorkshopShopApp/ApiClient.cs | 26 ++--- .../Controllers/HomeController.cs | 102 +++++++++++------- AutoWorkshopShopApp/Views/Home/Shop.cshtml | 2 +- AutoWorkshopShopApp/appsettings.json | 3 +- 9 files changed, 95 insertions(+), 114 deletions(-) rename AutoWorkshopContracts/ViewModels/{ShopRepairViewModel.cs => ShopRepairsViewModel.cs} (84%) delete mode 100644 AutoWorkshopRestApi/ApiConfig.cs diff --git a/AutoWorkshopContracts/ViewModels/ShopRepairViewModel.cs b/AutoWorkshopContracts/ViewModels/ShopRepairsViewModel.cs similarity index 84% rename from AutoWorkshopContracts/ViewModels/ShopRepairViewModel.cs rename to AutoWorkshopContracts/ViewModels/ShopRepairsViewModel.cs index 13cbe0a..092e406 100644 --- a/AutoWorkshopContracts/ViewModels/ShopRepairViewModel.cs +++ b/AutoWorkshopContracts/ViewModels/ShopRepairsViewModel.cs @@ -1,6 +1,6 @@ namespace AutoWorkshopContracts.ViewModels { - public class ShopRepairViewModel + public class ShopRepairsViewModel { public ShopViewModel Shop { get; set; } = new(); diff --git a/AutoWorkshopRestApi/ApiConfig.cs b/AutoWorkshopRestApi/ApiConfig.cs deleted file mode 100644 index 26247db..0000000 --- a/AutoWorkshopRestApi/ApiConfig.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace AutoWorkshopRestApi -{ - public class ApiConfig - { - public static string? ShopPassword; - - public static void LoadData(IConfiguration Configuration) - { - ShopPassword = Configuration["ShopApiPassword"]; - } - } -} diff --git a/AutoWorkshopRestApi/Controllers/ShopController.cs b/AutoWorkshopRestApi/Controllers/ShopController.cs index 16247d5..2824779 100644 --- a/AutoWorkshopRestApi/Controllers/ShopController.cs +++ b/AutoWorkshopRestApi/Controllers/ShopController.cs @@ -20,18 +20,8 @@ namespace AutoWorkshopRestApi.Controllers } [HttpGet] - public bool Authentication(string Password) + public List? GetShopList() { - return CheckPassword(Password); - } - - [HttpGet] - public List? GetShopList(string Password) - { - if (!CheckPassword(Password)) - { - return null; - } try { return _shopLogic.ReadList(null); @@ -44,16 +34,16 @@ namespace AutoWorkshopRestApi.Controllers } [HttpGet] - public ShopRepairViewModel? GetShop(int ShopId, string Password) + public ShopRepairsViewModel? GetShop(int ShopId) { - if (!CheckPassword(Password)) - { - return null; - } try { var Shop = _shopLogic.ReadElement(new ShopSearchModel { Id = ShopId }); - return new ShopRepairViewModel + + if (Shop == null) + return null; + + return new ShopRepairsViewModel { Shop = Shop, ShopRepairs = Shop.ShopRepairs.ToDictionary(x => x.Key, x => new RepairCount @@ -77,12 +67,8 @@ namespace AutoWorkshopRestApi.Controllers } [HttpPost] - public void CreateShop(ShopBindingModel Model, string Password) + public void CreateShop(ShopBindingModel Model) { - if (!CheckPassword(Password)) - { - return; - } try { _shopLogic.Create(Model); @@ -95,12 +81,8 @@ namespace AutoWorkshopRestApi.Controllers } [HttpPost] - public void UpdateShop(ShopBindingModel Model, string Password) + public void UpdateShop(ShopBindingModel Model) { - if (!CheckPassword(Password)) - { - return; - } try { _shopLogic.Update(Model); @@ -112,16 +94,12 @@ namespace AutoWorkshopRestApi.Controllers } } - [HttpDelete] - public void DeleteShop(int ShopId, string Password) + [HttpPost] + public void DeleteShop(ShopBindingModel Model) { - if (!CheckPassword(Password)) - { - return; - } try { - _shopLogic.Delete(new ShopBindingModel { Id = ShopId }); + _shopLogic.Delete(Model); } catch (Exception ex) { @@ -131,12 +109,8 @@ namespace AutoWorkshopRestApi.Controllers } [HttpPost] - public void MakeSypply(SupplyBindingModel Model, string Password) + public void MakeSupply(SupplyBindingModel Model) { - if (!CheckPassword(Password)) - { - return; - } try { _shopLogic.MakeSupply(Model); @@ -147,10 +121,5 @@ namespace AutoWorkshopRestApi.Controllers throw; } } - - private bool CheckPassword(string Password) - { - return ApiConfig.ShopPassword == Password; - } } } diff --git a/AutoWorkshopRestApi/Program.cs b/AutoWorkshopRestApi/Program.cs index dca012c..c1ea8fb 100644 --- a/AutoWorkshopRestApi/Program.cs +++ b/AutoWorkshopRestApi/Program.cs @@ -3,7 +3,6 @@ using AutoWorkshopContracts.BusinessLogicContracts; using AutoWorkshopContracts.BusinessLogicsContracts; using AutoWorkshopContracts.StoragesContracts; using AutoWorkshopDatabaseImplement.Implements; -using AutoWorkshopRestApi; using Microsoft.OpenApi.Models; var Builder = WebApplication.CreateBuilder(args); @@ -34,7 +33,6 @@ Builder.Services.AddSwaggerGen(c => }); var App = Builder.Build(); -ApiConfig.LoadData(Builder.Configuration); if (App.Environment.IsDevelopment()) { diff --git a/AutoWorkshopRestApi/appsettings.json b/AutoWorkshopRestApi/appsettings.json index 98abc52..10f68b8 100644 --- a/AutoWorkshopRestApi/appsettings.json +++ b/AutoWorkshopRestApi/appsettings.json @@ -5,6 +5,5 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*", - "ShopApiPassword": "8841" + "AllowedHosts": "*" } diff --git a/AutoWorkshopShopApp/ApiClient.cs b/AutoWorkshopShopApp/ApiClient.cs index 38b2586..591693f 100644 --- a/AutoWorkshopShopApp/ApiClient.cs +++ b/AutoWorkshopShopApp/ApiClient.cs @@ -9,10 +9,21 @@ namespace AutoWorkshopShopApp private static readonly HttpClient _client = new(); public static string? Password { get; set; } + public static bool IsAuthenticated { get; private set; } = false; - public static void Connect(IConfiguration Configuration) + public static bool TryAuthenticate(string Password) + { + if (Password == ApiClient.Password) + { + IsAuthenticated = true; + } + return IsAuthenticated; + } + + public static void Connect(IConfiguration Configuration) { - _client.BaseAddress = new Uri(Configuration["IPAddress"]); + Password = Configuration["Password"]; + _client.BaseAddress = new Uri(Configuration["IPAddress"]); _client.DefaultRequestHeaders.Accept.Clear(); _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); } @@ -45,16 +56,5 @@ namespace AutoWorkshopShopApp throw new Exception(Result); } } - - public static void DeleteRequest(string RequestUrl) - { - var Response = _client.DeleteAsync(RequestUrl); - var Result = Response.Result.Content.ReadAsStringAsync().Result; - - if (!Response.Result.IsSuccessStatusCode) - { - throw new Exception(Result); - } - } } } diff --git a/AutoWorkshopShopApp/Controllers/HomeController.cs b/AutoWorkshopShopApp/Controllers/HomeController.cs index 2b51c13..6960675 100644 --- a/AutoWorkshopShopApp/Controllers/HomeController.cs +++ b/AutoWorkshopShopApp/Controllers/HomeController.cs @@ -17,11 +17,11 @@ namespace AutoWorkshopShopApp.Controllers public IActionResult Index() { - if (ApiClient.Password == null) + if (!ApiClient.IsAuthenticated) { return Redirect("~/Home/Enter"); } - return View(ApiClient.GetRequest>($"api/shop/getshoplist?password={ApiClient.Password}")); + return View(ApiClient.GetRequest>("api/shop/getshoplist")); } [HttpGet] @@ -33,20 +33,22 @@ namespace AutoWorkshopShopApp.Controllers [HttpPost] public void Enter(string Password) { - bool ResOut = ApiClient.GetRequest($"/api/shop/authentication?password={Password}"); - if (!ResOut) - { - Response.Redirect("../Home/Enter"); - return; - } - ApiClient.Password = Password; + if (string.IsNullOrEmpty(Password)) + { + throw new Exception("Введите пароль"); + } + if (!ApiClient.TryAuthenticate(Password)) + { + throw new Exception("Неверный пароль"); + } + Response.Redirect("Index"); - } + } [HttpGet] public IActionResult Create() { - if (ApiClient.Password == null) + if (!ApiClient.IsAuthenticated) { return Redirect("~/Home/Enter"); } @@ -56,16 +58,21 @@ namespace AutoWorkshopShopApp.Controllers [HttpPost] public void Create(int Id, string ShopName, string Address, DateTime OpeningDate, int MaxCount) { - if (string.IsNullOrEmpty(ShopName) || string.IsNullOrEmpty(Address)) + if (!ApiClient.IsAuthenticated) + { + throw new Exception("Вход только авторизованным"); + } + + if (string.IsNullOrEmpty(ShopName) || string.IsNullOrEmpty(Address)) { throw new Exception("Название или адрес не может быть пустым"); } - if (OpeningDate == default(DateTime)) - { - throw new Exception("Дата открытия не может быть пустой"); - } + if (MaxCount <= 0) + { + throw new Exception("Вместимость магазина должна быть больше нуля"); + } - ApiClient.PostRequest($"api/shop/createshop?password={ApiClient.Password}", new ShopBindingModel + ApiClient.PostRequest("api/shop/createshop", new ShopBindingModel { Id = Id, ShopName = ShopName, @@ -79,25 +86,31 @@ namespace AutoWorkshopShopApp.Controllers [HttpGet] public IActionResult Update(int Id) { - if (ApiClient.Password == null) - { - return Redirect("~/Home/Enter"); - } - return View("Shop", ApiClient.GetRequest($"api/shop/getshop?shopId={Id}&password={ApiClient.Password}")); + if (!ApiClient.IsAuthenticated) + { + return Redirect("~/Home/Enter"); + } + return View("Shop", ApiClient.GetRequest($"api/shop/getshop?shopId={Id}")); } [HttpPost] public void Update(int Id, string ShopName, string Address, DateTime OpeningDate, int MaxCount) { - if (string.IsNullOrEmpty(ShopName) || string.IsNullOrEmpty(Address)) + if (!ApiClient.IsAuthenticated) + { + throw new Exception("Вход только авторизованным"); + } + + if (string.IsNullOrEmpty(ShopName) || string.IsNullOrEmpty(Address)) { throw new Exception("Название или адрес не может быть пустым"); } - if (OpeningDate == default(DateTime)) - { - throw new Exception("Дата открытия не может быть пустой"); - } - ApiClient.PostRequest($"api/shop/updateshop?password={ApiClient.Password}", new ShopBindingModel + if (MaxCount <= 0) + { + throw new Exception("Вместимость магазина должна быть больше нуля"); + } + + ApiClient.PostRequest("api/shop/updateshop", new ShopBindingModel { Id = Id, ShopName = ShopName, @@ -105,33 +118,46 @@ namespace AutoWorkshopShopApp.Controllers OpeningDate = OpeningDate, RepairsMaxCount = MaxCount }); - Response.Redirect("../Index"); + Response.Redirect("Index"); } [HttpPost] public void Delete(int Id) { - ApiClient.DeleteRequest($"api/shop/deleteshop?shopId={Id}&password={ApiClient.Password}"); - Response.Redirect("../Index"); + if (!ApiClient.IsAuthenticated) + { + throw new Exception("Вход только авторизованным"); + } + + ApiClient.PostRequest("api/shop/deleteshop", new ShopBindingModel + { + Id = Id, + }); + Response.Redirect("Index"); } [HttpGet] public IActionResult Supply() { - if (ApiClient.Password == null) - { - return Redirect("~/Home/Enter"); - } + if (!ApiClient.IsAuthenticated) + { + return Redirect("~/Home/Enter"); + } - ViewBag.Shops = ApiClient.GetRequest>($"api/shop/getshoplist?password={ApiClient.Password}"); - ViewBag.Repairs = ApiClient.GetRequest>($"api/main/getrepairlist"); + ViewBag.Shops = ApiClient.GetRequest>("api/shop/getshoplist"); + ViewBag.Repairs = ApiClient.GetRequest>("api/main/getrepairlist"); return View(); } [HttpPost] public void Supply(int Shop, int Repair, int Count) { - ApiClient.PostRequest($"api/shop/makesypply?password={ApiClient.Password}", new SupplyBindingModel + if (!ApiClient.IsAuthenticated) + { + throw new Exception("Вход только авторизованным"); + } + + ApiClient.PostRequest("api/shop/makesupply", new SupplyBindingModel { ShopId = Shop, RepairId = Repair, diff --git a/AutoWorkshopShopApp/Views/Home/Shop.cshtml b/AutoWorkshopShopApp/Views/Home/Shop.cshtml index 6456ac9..006add8 100644 --- a/AutoWorkshopShopApp/Views/Home/Shop.cshtml +++ b/AutoWorkshopShopApp/Views/Home/Shop.cshtml @@ -1,7 +1,7 @@ @using AutoWorkshopDataModels.Models; @using AutoWorkshopContracts.ViewModels; -@model ShopRepairViewModel +@model ShopRepairsViewModel @{ ViewData["Title"] = "Shop"; diff --git a/AutoWorkshopShopApp/appsettings.json b/AutoWorkshopShopApp/appsettings.json index c34e306..40434b5 100644 --- a/AutoWorkshopShopApp/appsettings.json +++ b/AutoWorkshopShopApp/appsettings.json @@ -6,5 +6,6 @@ } }, "AllowedHosts": "*", - "IPAddress": "http://localhost:5224/" + "IPAddress": "http://localhost:5224/", + "Password": "admin" }