2022-09-19 05:50:15 +03:00
|
|
|
using BlueWest.Domain;
|
2022-09-12 17:57:37 +03:00
|
|
|
using BlueWest.Data;
|
2022-09-11 01:22:04 +03:00
|
|
|
using BlueWest.WebApi.Context;
|
2022-09-17 22:13:35 +03:00
|
|
|
using BlueWest.WebApi.Context.Users;
|
2022-09-11 01:22:04 +03:00
|
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2022-09-12 17:57:37 +03:00
|
|
|
using Microsoft.AspNetCore.Cors;
|
2022-09-11 01:22:04 +03:00
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
namespace BlueWest.WebApi.Controllers
|
|
|
|
{
|
2022-09-17 22:13:35 +03:00
|
|
|
/// <inheritdoc />
|
2022-09-11 01:22:04 +03:00
|
|
|
[ApiController]
|
2022-09-12 17:57:37 +03:00
|
|
|
[Route("application/users")]
|
2022-09-11 01:22:04 +03:00
|
|
|
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
2022-09-12 17:57:37 +03:00
|
|
|
[Authorize(AuthenticationSchemes = CookieAuthenticationDefaults.AuthenticationScheme)]
|
|
|
|
[EnableCors(Constants.CorsPolicyName)]
|
2022-09-19 05:50:15 +03:00
|
|
|
[ServiceFilter(typeof(SessionAuthorizationFilter))]
|
2022-09-12 17:57:37 +03:00
|
|
|
|
2022-09-11 01:22:04 +03:00
|
|
|
public class ApplicationUserController : ControllerBase
|
|
|
|
{
|
2022-09-12 17:57:37 +03:00
|
|
|
private readonly ApplicationUserDbContext _dbContext;
|
2022-09-11 01:22:04 +03:00
|
|
|
|
2022-09-17 22:13:35 +03:00
|
|
|
/// <inheritdoc />
|
2022-09-11 01:22:04 +03:00
|
|
|
public ApplicationUserController(ApplicationUserDbContext context)
|
|
|
|
{
|
2022-09-12 17:57:37 +03:00
|
|
|
_dbContext = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
#region Users
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Get Application users
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
|
|
[HttpGet]
|
|
|
|
public ActionResult GetApplicationUsers(
|
|
|
|
int skip = 0,
|
|
|
|
int take = 50,
|
|
|
|
int orderDir = 1)
|
|
|
|
{
|
|
|
|
|
|
|
|
var (success, users) = _dbContext.GetUsers( skip, take, orderDir);
|
|
|
|
if (!success) return new NotFoundResult();
|
|
|
|
return Ok(users);
|
2022-09-11 01:22:04 +03:00
|
|
|
}
|
2022-09-12 17:57:37 +03:00
|
|
|
|
2022-09-11 01:22:04 +03:00
|
|
|
|
2022-09-12 17:57:37 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Updates a User
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="UserCode">The UserId ISO 3166 code</param>
|
|
|
|
/// <param name="UserToUpdate">User payload data</param>
|
|
|
|
/// <returns></returns>
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
|
|
[HttpPut("{UserCode}")]
|
2022-09-17 22:13:35 +03:00
|
|
|
public ActionResult UpdateApplicationUser(string applicationUserId, ApplicationUserUnique UserToUpdate)
|
2022-09-12 17:57:37 +03:00
|
|
|
{
|
2022-09-17 22:13:35 +03:00
|
|
|
var (success, updatedUser) = _dbContext.UpdateApplicationUser(UserToUpdate, applicationUserId);
|
|
|
|
|
2022-09-12 17:57:37 +03:00
|
|
|
if (success)
|
|
|
|
{
|
2022-09-17 22:13:35 +03:00
|
|
|
return Ok(updatedUser);
|
2022-09-12 17:57:37 +03:00
|
|
|
}
|
2022-09-17 22:13:35 +03:00
|
|
|
|
2022-09-12 17:57:37 +03:00
|
|
|
|
|
|
|
return new NotFoundResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
#region GetUserById
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Get User by Id
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="UserId">ISO 3166-1 UserId numeric code</param>
|
|
|
|
/// <returns></returns>
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
|
|
[HttpGet("{UserId}", Name = nameof(GetUserById))]
|
|
|
|
public ActionResult GetUserById(int UserId)
|
|
|
|
{
|
|
|
|
var (success, User) = _dbContext.GetOneUserById(UserId);
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
{
|
|
|
|
return Ok(User);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new NotFoundResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
*/
|
|
|
|
|
|
|
|
#region Roles
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Get Application users
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
|
|
[HttpGet("roles")]
|
|
|
|
public ActionResult GetApplicationRoles(
|
|
|
|
int skip = 0,
|
|
|
|
int take = 50,
|
|
|
|
int orderDir = 1)
|
|
|
|
{
|
|
|
|
|
|
|
|
var (success, users) = _dbContext.GetRoles( skip, take, orderDir);
|
|
|
|
if (!success) return new NotFoundResult();
|
|
|
|
return Ok(users);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2022-09-11 01:22:04 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-17 22:13:35 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Application Constants
|
|
|
|
/// </summary>
|
2022-09-12 17:57:37 +03:00
|
|
|
public static class Constants
|
|
|
|
{
|
2022-09-17 22:13:35 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Policy Name
|
|
|
|
/// </summary>
|
2022-09-12 17:57:37 +03:00
|
|
|
public const string CorsPolicyName = "_myAllowSpecificOrigins";
|
|
|
|
}
|
|
|
|
|