authentication in ShopApp
This commit is contained in:
parent
a6264c5b1b
commit
9a5b50286b
@ -1,6 +1,6 @@
|
||||
namespace AutoWorkshopContracts.ViewModels
|
||||
{
|
||||
public class ShopRepairViewModel
|
||||
public class ShopRepairsViewModel
|
||||
{
|
||||
public ShopViewModel Shop { get; set; } = new();
|
||||
|
@ -1,12 +0,0 @@
|
||||
namespace AutoWorkshopRestApi
|
||||
{
|
||||
public class ApiConfig
|
||||
{
|
||||
public static string? ShopPassword;
|
||||
|
||||
public static void LoadData(IConfiguration Configuration)
|
||||
{
|
||||
ShopPassword = Configuration["ShopApiPassword"];
|
||||
}
|
||||
}
|
||||
}
|
@ -20,18 +20,8 @@ namespace AutoWorkshopRestApi.Controllers
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public bool Authentication(string Password)
|
||||
public List<ShopViewModel>? GetShopList()
|
||||
{
|
||||
return CheckPassword(Password);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public List<ShopViewModel>? 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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())
|
||||
{
|
||||
|
@ -5,6 +5,5 @@
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"ShopApiPassword": "8841"
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<List<ShopViewModel>>($"api/shop/getshoplist?password={ApiClient.Password}"));
|
||||
return View(ApiClient.GetRequest<List<ShopViewModel>>("api/shop/getshoplist"));
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
@ -33,20 +33,22 @@ namespace AutoWorkshopShopApp.Controllers
|
||||
[HttpPost]
|
||||
public void Enter(string Password)
|
||||
{
|
||||
bool ResOut = ApiClient.GetRequest<bool>($"/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<ShopRepairViewModel>($"api/shop/getshop?shopId={Id}&password={ApiClient.Password}"));
|
||||
if (!ApiClient.IsAuthenticated)
|
||||
{
|
||||
return Redirect("~/Home/Enter");
|
||||
}
|
||||
return View("Shop", ApiClient.GetRequest<ShopRepairsViewModel>($"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<List<ShopViewModel>>($"api/shop/getshoplist?password={ApiClient.Password}");
|
||||
ViewBag.Repairs = ApiClient.GetRequest<List<RepairViewModel>>($"api/main/getrepairlist");
|
||||
ViewBag.Shops = ApiClient.GetRequest<List<ShopViewModel>>("api/shop/getshoplist");
|
||||
ViewBag.Repairs = ApiClient.GetRequest<List<RepairViewModel>>("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,
|
||||
|
@ -1,7 +1,7 @@
|
||||
@using AutoWorkshopDataModels.Models;
|
||||
@using AutoWorkshopContracts.ViewModels;
|
||||
|
||||
@model ShopRepairViewModel
|
||||
@model ShopRepairsViewModel
|
||||
|
||||
@{
|
||||
ViewData["Title"] = "Shop";
|
||||
|
@ -6,5 +6,6 @@
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"IPAddress": "http://localhost:5224/"
|
||||
"IPAddress": "http://localhost:5224/",
|
||||
"Password": "admin"
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user