From afc5354d0fe99b1cdcbf0df915f365f76706f167 Mon Sep 17 00:00:00 2001 From: MarcEricMartel <74071476+MarcEricMartel@users.noreply.github.com> Date: Mon, 31 Oct 2022 07:12:25 -0700 Subject: [PATCH] Get api/Login -> WhoAmI --- .../Controllers/InventoryController.cs | 6 ++--- .../Controllers/LoginController.cs | 24 +++++++++++++------ .../Models/ReturnUserViewModel.cs | 20 ++++++++++++++++ .../Services/DatabaseCacheService.cs | 2 +- 4 files changed, 41 insertions(+), 11 deletions(-) create mode 100644 GrossesMitaines/GrossesMitainesAPI/Models/ReturnUserViewModel.cs diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs index f76b8ea..dd61767 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs @@ -16,7 +16,7 @@ using Microsoft.AspNetCore.Authorization; Authorize(AuthenticationSchemes = "Identity.Application")] public class InventoryController : Controller { #region Constants - private const int AMOUNT_SCROLL = 5; + private const int AMOUNT_SCROLL = 6; #endregion @@ -136,7 +136,7 @@ public class InventoryController : Controller { public ActionResult Delete(int? id) { int rid = 0; if (!id.HasValue) { - _logger.LogError(8, "Delete sans Id."); + _logger.LogError(8, "Tentative de vente sans Id."); return BadRequest(); } try { @@ -151,7 +151,7 @@ public class InventoryController : Controller { Product.States.Discontinued : Product.States.BackOrder; } else { - _logger.LogError(8, "Vente de produit pas en stock."); + _logger.LogError(8, $"Vente de produit pas en stock. Id Produit: {prod.Id}"); return BadRequest(); } _context.Products.Update(prod); diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/LoginController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/LoginController.cs index b57cfa8..55c7b1f 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/LoginController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/LoginController.cs @@ -7,28 +7,38 @@ using Microsoft.AspNetCore.Identity; using GrossesMitainesAPI.Data; using SignInResult = Microsoft.AspNetCore.Identity.SignInResult; using Microsoft.AspNetCore.Authentication.Cookies; +using System.Security.Principal; +using GrossesMitainesAPI.Models; [EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api"), Authorize(AuthenticationSchemes = "Identity.Application")] public class LoginController : Controller { - //private readonly UserManager _userMan; + private readonly UserManager _userMan; private readonly SignInManager _signInMan; - public LoginController(SignInManager signin) { + public LoginController(SignInManager signin, UserManager userman) { this._signInMan = signin; + this._userMan = userman; } - public class User { - public string username { get; set; } - public string password { get; set; } + public class LoginUser { + public string username { get; set; } = ""; + public string password { get; set; } = ""; + } + + [HttpGet, Route("Login")] + public ReturnUserViewModel WhoAmI() { + var user = _userMan.GetUserAsync(_signInMan.Context.User); + user.Wait(); + return new ReturnUserViewModel(user.Result); } [HttpPost, Route("Login"), AllowAnonymous] - public async Task Login(User user, bool rememberMe = false) { + public async Task Login(LoginUser user, bool rememberMe = false) { return await _signInMan.PasswordSignInAsync(user.username, user.password, rememberMe, false); } [HttpPost, Route("Logout")] - public void Logout() { _signInMan.SignOutAsync(); } + public void Logout() => _signInMan.SignOutAsync(); } diff --git a/GrossesMitaines/GrossesMitainesAPI/Models/ReturnUserViewModel.cs b/GrossesMitaines/GrossesMitainesAPI/Models/ReturnUserViewModel.cs new file mode 100644 index 0000000..110d4f4 --- /dev/null +++ b/GrossesMitaines/GrossesMitainesAPI/Models/ReturnUserViewModel.cs @@ -0,0 +1,20 @@ +using GrossesMitainesAPI.Data; + +namespace GrossesMitainesAPI.Models; +public class ReturnUserViewModel { + public string Username { get; set; } + public string Email { get; set; } + public string Phone { get; set; } + public bool EmailConfirmed { get; set; } + public bool PhoneConfirmed { get; set; } + public bool TwoFactorEnable { get; set; } + + public ReturnUserViewModel(InventoryUser user) { + Username = user.UserName; + Email = user.Email; + Phone = user.PhoneNumber; + EmailConfirmed = user.EmailConfirmed; + PhoneConfirmed = user.PhoneNumberConfirmed; + TwoFactorEnable = user.TwoFactorEnabled; + } +} diff --git a/GrossesMitaines/GrossesMitainesAPI/Services/DatabaseCacheService.cs b/GrossesMitaines/GrossesMitainesAPI/Services/DatabaseCacheService.cs index 8816985..419e705 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Services/DatabaseCacheService.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Services/DatabaseCacheService.cs @@ -92,7 +92,7 @@ public class DatabaseCacheService { #region Public Methods public bool isOk() { return _ok; } - public void askForRefresh() { _needUpd = true; } + public void askForRefresh() => _needUpd = true; public void addHit(uint id) { lock (_hits) { if (_hits.ContainsKey(id))