From 4eb8bb735338d63d0985db6e7cd5c2f022f93e5b Mon Sep 17 00:00:00 2001 From: MarcEricMartel <74071476+MarcEricMartel@users.noreply.github.com> Date: Tue, 1 Nov 2022 11:07:49 -0700 Subject: [PATCH] Correction --- .../Controllers/InventoryController.cs | 62 +++++++++---------- .../Controllers/InvoiceController.cs | 21 +++++++ .../Controllers/LoginController.cs | 35 +++++++---- .../Controllers/ProductController.cs | 2 +- .../Controllers/UserController.cs | 25 ++++++++ .../grosses-mitaines-ui/src/pages/Login.js | 14 ++--- 6 files changed, 108 insertions(+), 51 deletions(-) create mode 100644 GrossesMitaines/GrossesMitainesAPI/Controllers/InvoiceController.cs create mode 100644 GrossesMitaines/GrossesMitainesAPI/Controllers/UserController.cs diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs index 65484b1..259468c 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs @@ -132,37 +132,37 @@ public class InventoryController : Controller { } } // Inventory/Delete => Décrémenter un produit. Va aller chercher directement dans la BD. - [EnableCors("_myAllowSpecificOrigins"), HttpDelete(Name = "Inventory"), AllowAnonymous] - public ActionResult Delete(int? id) { - int rid = 0; - if (!id.HasValue) { - _logger.LogError(8, "Tentative de vente sans Id."); - return BadRequest(); - } - try { - ProductModel prod = _context.Products.First(x => x.Id == id); - rid = prod.Id; - if (prod.Quantity > 0) { - prod.Quantity = prod.Quantity - 1; - prod.Sales = prod.Sales + 1; - prod.LastSale = DateTime.Now; - if (prod.Quantity == 0) - prod.Status = prod.Status == ProductModel.States.Clearance ? - ProductModel.States.Discontinued : - ProductModel.States.BackOrder; - } else { - _logger.LogError(8, $"Vente de produit pas en stock. Id Produit: {prod.Id}"); - return BadRequest(); - } - _context.Products.Update(prod); - _context.SaveChanges(); - } catch (Exception e) { - _logger.LogError(8, e.Message); - return BadRequest(); - } - _cache.askForRefresh(); - return rid; - } + //[EnableCors("_myAllowSpecificOrigins"), HttpDelete(Name = "Inventory"), AllowAnonymous] + //public ActionResult Delete(int? id) { + // int rid = 0; + // if (!id.HasValue) { + // _logger.LogError(8, "Tentative de vente sans Id."); + // return BadRequest(); + // } + // try { + // ProductModel prod = _context.Products.First(x => x.Id == id); + // rid = prod.Id; + // if (prod.Quantity > 0) { + // prod.Quantity = prod.Quantity - 1; + // prod.Sales = prod.Sales + 1; + // prod.LastSale = DateTime.Now; + // if (prod.Quantity == 0) + // prod.Status = prod.Status == ProductModel.States.Clearance ? + // ProductModel.States.Discontinued : + // ProductModel.States.BackOrder; + // } else { + // _logger.LogError(8, $"Vente de produit pas en stock. Id Produit: {prod.Id}"); + // return BadRequest(); + // } + // _context.Products.Update(prod); + // _context.SaveChanges(); + // } catch (Exception e) { + // _logger.LogError(8, e.Message); + // return BadRequest(); + // } + // _cache.askForRefresh(); + // return rid; + //} #endregion } diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/InvoiceController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/InvoiceController.cs new file mode 100644 index 0000000..9485c24 --- /dev/null +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/InvoiceController.cs @@ -0,0 +1,21 @@ +using GrossesMitainesAPI.Data; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Cors; +using Microsoft.AspNetCore.Mvc; + +namespace GrossesMitainesAPI.Controllers; +[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]"), + Authorize(AuthenticationSchemes = "Identity.Application", Roles = "Administrateur")] +public class InvoiceController : Controller { + private readonly ILogger _logger; + private readonly InventoryContext _context; + + public InvoiceController(ILogger logger, InventoryContext context) { + this._logger = logger; + this._context = context; + } + + + +} + diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/LoginController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/LoginController.cs index 7298bac..296da5c 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/LoginController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/LoginController.cs @@ -1,5 +1,6 @@ namespace GrossesMitainesAPI.Controllers; +#region Dependencies using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Authorization; @@ -10,44 +11,54 @@ using Microsoft.AspNetCore.Authentication.Cookies; using System.Security.Principal; using GrossesMitainesAPI.Models; +#endregion + [EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api"), Authorize(AuthenticationSchemes = "Identity.Application")] public class LoginController : Controller { + #region DI Fields private readonly UserManager _userMan; - private readonly RoleManager _roleMan; private readonly SignInManager _signInMan; - public LoginController(SignInManager signin, UserManager userman, RoleManager roleMan) { + #endregion + + #region Ctor + public LoginController(SignInManager signin, UserManager userman) { this._signInMan = signin; this._userMan = userman; - this._roleMan = roleMan; } + #endregion + + #region Utility Classes public class LoginUser { public string email { get; set; } = ""; public string password { get; set; } = ""; } - [HttpGet, Route("Login")] - public ReturnUserViewModel WhoAmI() { - var user = _userMan.GetUserAsync(_signInMan.Context.User); - user.Wait(); - var roles = _userMan.GetRolesAsync(user.Result); - roles.Wait(); + #endregion + #region API Methods + [HttpGet, Route("WhoAmI")] + public async Task WhoAmI() { + var user = await _userMan.GetUserAsync(_signInMan.Context.User); + var roles = await _userMan.GetRolesAsync(user); string role = ""; - if (roles.Result.Contains("Administrateur")) + if (roles.Contains("Administrateur")) role = "Administrateur"; else role = "Client"; - return new ReturnUserViewModel(user.Result, role); + return new ReturnUserViewModel(user, role); } [HttpPost, Route("Login"), AllowAnonymous] public async Task Login(LoginUser user, bool rememberMe = false) { - return await _signInMan.PasswordSignInAsync(await _userMan.FindByEmailAsync(user.email), user.password, rememberMe, false); + var User = await _userMan.FindByEmailAsync(user.email); + return await _signInMan.PasswordSignInAsync(User, user.password, rememberMe, false); } [HttpPost, Route("Logout")] public void Logout() => _signInMan.SignOutAsync(); + + #endregion } diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs index 05e8b20..1fdfd59 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs @@ -21,7 +21,7 @@ using GrossesMitainesAPI.Services; /// l'éxécution d'une modification de la BD. /// [EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]"), - Authorize(AuthenticationSchemes = "Identity.Application")] + Authorize(AuthenticationSchemes = "Identity.Application", Roles = "Administrateur")] public class ProductController : ControllerBase { #region DI Fields private readonly ILogger _logger; diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/UserController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/UserController.cs new file mode 100644 index 0000000..81df983 --- /dev/null +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/UserController.cs @@ -0,0 +1,25 @@ +using GrossesMitainesAPI.Data; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Cors; +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Mvc; + +namespace GrossesMitainesAPI.Controllers; +[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]"), + Authorize(AuthenticationSchemes = "Identity.Application", Roles = "Administrateur")] +public class UserController : Controller { + private readonly UserManager _userMan; + private readonly SignInManager _signInMan; + + public UserController(SignInManager signin, UserManager userman) { + this._signInMan = signin; + this._userMan = userman; + } + + + + + + +} + diff --git a/GrossesMitaines/grosses-mitaines-ui/src/pages/Login.js b/GrossesMitaines/grosses-mitaines-ui/src/pages/Login.js index 53ca390..40dd38c 100644 --- a/GrossesMitaines/grosses-mitaines-ui/src/pages/Login.js +++ b/GrossesMitaines/grosses-mitaines-ui/src/pages/Login.js @@ -3,7 +3,7 @@ import { Button } from "react-bootstrap"; const Login = () => { - const [adresse, setAdresse] = useState(""); + const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [returnmess, returnMessage] = useState(""); @@ -17,14 +17,13 @@ const Login = () => { 'Accept': 'text/json', 'Content-Type': 'text/json' }, - body: JSON.stringify({ username, password }) + body: JSON.stringify({ email, password }) }); - // Partie de display d'erreur ou de redirection (faudrait checker pour se faire un state de connexion avec un cookie pour react). - + if (response.status === 200) { var rep = await response.json(); if (rep.succeeded === true) { - const confirm = await fetch(`https://localhost:7292/api/Login`, { + const confirm = await fetch(`https://localhost:7292/api/WhoAmI`, { method: 'GET', credentials: 'include', headers: { @@ -32,6 +31,7 @@ const Login = () => { 'Content-Type': 'text/json' } }); + // TODO: Redirection vers Home et ajout du cookie pour React pour le layout. console.log(await confirm.json()) returnMessage("WOOHOO!"); } @@ -52,8 +52,8 @@ const Login = () => { setAdresse(e.target.value)} /> + value={email} + onChange={(e) => setEmail(e.target.value)} />