59 lines
2.0 KiB
C#
59 lines
2.0 KiB
C#
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using PSTW_CentralSystem.DBContext;
|
|
using PSTW_CentralSystem.Models;
|
|
namespace PSTW_CentralSystem.Controllers.API
|
|
{
|
|
[ApiController]
|
|
[Route("[controller]")]
|
|
|
|
public class IdentityAPI : ControllerBase
|
|
{
|
|
private readonly ILogger<IdentityAPI> _logger;
|
|
private readonly IdentityDBContext _authDbContext;
|
|
private readonly UserManager<UserModel> _userManager;
|
|
|
|
public IdentityAPI(ILogger<IdentityAPI> logger, IdentityDBContext authDbContext, UserManager<UserModel> userManager)
|
|
{
|
|
_logger = logger;
|
|
_authDbContext = authDbContext;
|
|
_userManager = userManager;
|
|
}
|
|
|
|
[HttpPost("GetUserInformation/")]
|
|
public async Task<IActionResult> GetUserInformation()
|
|
{
|
|
try
|
|
{
|
|
var user = await _userManager.GetUserAsync(User);
|
|
var userRole = await _userManager.GetRolesAsync(user!);
|
|
if (user == null)
|
|
{
|
|
return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
|
|
}
|
|
|
|
var userInfo = await _authDbContext.Users.Include(u => u.Department).Select(u => new
|
|
{
|
|
id = u.Id,
|
|
email = u.NormalizedEmail,
|
|
company = u.Department!.Company!.CompanyName,
|
|
department =u.Department,
|
|
role = userRole,
|
|
}).Where(u => u.id == user.Id).FirstOrDefaultAsync();
|
|
|
|
if (userInfo == null)
|
|
{
|
|
return NotFound("User not found");
|
|
}
|
|
|
|
return Ok(new { UserInfo = userInfo });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode(500, $"An error occurred: {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
}
|