2022-11-03 12:45:59 -04:00
|
|
|
|
namespace GrossesMitainesAPI.Controllers;
|
|
|
|
|
|
|
|
|
|
#region Dependencies
|
|
|
|
|
using GrossesMitainesAPI.Data;
|
2022-11-02 21:00:48 -04:00
|
|
|
|
using GrossesMitainesAPI.Models;
|
2022-11-03 12:45:59 -04:00
|
|
|
|
using Microsoft.AspNet.Identity;
|
2022-11-01 14:07:49 -04:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Cors;
|
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2022-11-03 12:45:59 -04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using System.Net.Mime;
|
|
|
|
|
|
|
|
|
|
#endregion
|
2022-11-01 14:07:49 -04:00
|
|
|
|
|
|
|
|
|
[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]"),
|
|
|
|
|
Authorize(AuthenticationSchemes = "Identity.Application", Roles = "Administrateur")]
|
|
|
|
|
public class UserController : Controller {
|
2022-11-03 12:45:59 -04:00
|
|
|
|
#region DI Fields
|
|
|
|
|
private readonly Microsoft.AspNetCore.Identity.UserManager<InventoryUser> _userMan;
|
2022-11-01 14:07:49 -04:00
|
|
|
|
private readonly SignInManager<InventoryUser> _signInMan;
|
2022-11-03 12:45:59 -04:00
|
|
|
|
private readonly InventoryContext _context;
|
2022-11-02 21:00:48 -04:00
|
|
|
|
private readonly ILogger<UserController> _logger;
|
2022-11-01 14:07:49 -04:00
|
|
|
|
|
2022-11-03 12:45:59 -04:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Ctor
|
|
|
|
|
public UserController(ILogger<UserController> logger,
|
|
|
|
|
SignInManager<InventoryUser> signin,
|
|
|
|
|
Microsoft.AspNetCore.Identity.UserManager<InventoryUser> userman,
|
|
|
|
|
InventoryContext context) {
|
2022-11-02 21:00:48 -04:00
|
|
|
|
_logger = logger;
|
|
|
|
|
_signInMan = signin;
|
|
|
|
|
_userMan = userman;
|
2022-11-03 12:45:59 -04:00
|
|
|
|
_context = context;
|
2022-11-01 14:07:49 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-03 12:45:59 -04:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region API Methods
|
2022-11-02 21:00:48 -04:00
|
|
|
|
[HttpPost, AllowAnonymous]
|
2022-11-03 12:45:59 -04:00
|
|
|
|
public ActionResult<ReturnUserViewModel> Post(SignUpUserModel sign) {
|
2022-11-02 21:00:48 -04:00
|
|
|
|
InventoryUser usr;
|
|
|
|
|
try {
|
2022-11-08 11:18:33 -05:00
|
|
|
|
usr = new() {
|
2022-11-06 12:10:41 -05:00
|
|
|
|
FirstName = sign.FirstName,
|
|
|
|
|
LastName = sign.LastName,
|
2022-11-08 11:18:33 -05:00
|
|
|
|
UserName = sign.FirstName + sign.LastName,
|
|
|
|
|
NormalizedUserName = (sign.FirstName + sign.LastName).ToUpper(),
|
2022-11-06 12:10:41 -05:00
|
|
|
|
Email = sign.Email,
|
2022-11-08 11:18:33 -05:00
|
|
|
|
NormalizedEmail = sign.Email.ToUpper(),
|
2022-11-06 12:10:41 -05:00
|
|
|
|
PhoneNumber = sign.Phone
|
|
|
|
|
};
|
2022-11-03 12:45:59 -04:00
|
|
|
|
} catch (Exception e){
|
|
|
|
|
return BadRequest($"Erreur utilisateur: {e.Message}");
|
2022-11-02 21:00:48 -04:00
|
|
|
|
}
|
2022-11-03 12:45:59 -04:00
|
|
|
|
|
2022-11-06 12:10:41 -05:00
|
|
|
|
try {
|
2022-11-08 11:18:33 -05:00
|
|
|
|
usr.Adresses = new();
|
2022-11-06 12:10:41 -05:00
|
|
|
|
usr.Adresses.Add(new AddressModel() {
|
|
|
|
|
CivicNumber = sign.CivicNumber,
|
2022-11-08 11:18:33 -05:00
|
|
|
|
Appartment = sign.Appartment is not null && sign.Appartment != ""? sign.Appartment: null,
|
2022-11-06 12:10:41 -05:00
|
|
|
|
Street = sign.Street,
|
|
|
|
|
City = sign.City,
|
|
|
|
|
Province = sign.Province,
|
|
|
|
|
Country = sign.Country,
|
|
|
|
|
PostalCode = sign.PostalCode
|
|
|
|
|
});
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return BadRequest($"Erreur adresse: {e.Message}");
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-02 21:00:48 -04:00
|
|
|
|
try {
|
|
|
|
|
usr.PasswordHash = new PasswordHasher<InventoryUser>().HashPassword(usr, sign.Password);
|
2022-11-03 12:45:59 -04:00
|
|
|
|
} catch (Exception e){
|
|
|
|
|
return BadRequest($"Erreur de mot de passe: {e.Message}");
|
2022-11-02 21:00:48 -04:00
|
|
|
|
}
|
|
|
|
|
try {
|
2022-11-03 12:45:59 -04:00
|
|
|
|
var t1 = _userMan.CreateAsync(usr);
|
|
|
|
|
t1.Wait();
|
|
|
|
|
var t2 = _userMan.AddToRoleAsync(usr, "Client");
|
|
|
|
|
t2.Wait();
|
2022-11-02 21:00:48 -04:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return BadRequest(e.Message);
|
|
|
|
|
}
|
2022-11-03 12:45:59 -04:00
|
|
|
|
|
2022-11-02 21:00:48 -04:00
|
|
|
|
return new ReturnUserViewModel(usr, "Client");
|
|
|
|
|
}
|
2022-11-03 12:45:59 -04:00
|
|
|
|
|
|
|
|
|
[HttpGet, Route("Adresses")]
|
|
|
|
|
public async Task<ActionResult<AddressModel>> GetAddresses(bool? all) {
|
|
|
|
|
IList<string> roles;
|
|
|
|
|
string id;
|
|
|
|
|
try { // Trouver les rôles de l'utilisateur, assumer non-admin si impossible à trouver.
|
|
|
|
|
roles = await _userMan.GetRolesAsync(await _userMan.GetUserAsync(_signInMan.Context.User));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
_logger.LogError(10, e.Message);
|
|
|
|
|
roles = new List<string>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
id = _signInMan.Context.User.Identity.GetUserId();
|
|
|
|
|
if (all is not null && all == true && roles.Contains("Administrateur"))
|
|
|
|
|
return Ok(_context.Addresses.Include("AspNetUser").ToList());
|
|
|
|
|
else return Ok(_context.Users.Include("Adresses").Where(x => x.Id == id).ToList());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
_logger.LogError(10, e.Message);
|
|
|
|
|
return BadRequest(e.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2022-11-01 14:07:49 -04:00
|
|
|
|
}
|
|
|
|
|
|