DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/AuthController.cs

101 lines
3.4 KiB
C#
Raw Permalink Normal View History

using AsbCloudApp.Data;
using AsbCloudApp.Data.User;
using AsbCloudApp.Repositories;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using System.Threading;
using System.Threading.Tasks;
2024-08-19 10:01:07 +05:00
namespace AsbCloudWebApi.Controllers;
/// <summary>
/// Авторизация
/// </summary>
[Route("/auth")]
[ApiController]
public class AuthController : ControllerBase
{
2024-08-19 10:01:07 +05:00
private readonly IAuthService authService;
private readonly IUserRepository userRepository;
public AuthController(IAuthService authService, IUserRepository userRepository)
{
this.authService = authService;
this.userRepository = userRepository;
}
2022-06-16 17:37:10 +05:00
/// <summary>
2024-08-19 10:01:07 +05:00
/// Аутентификация пользователя
2022-06-16 17:37:10 +05:00
/// </summary>
2024-08-19 10:01:07 +05:00
/// <param name="auth"></param>
/// <param name="token">Токен отмены задачи</param>
/// <response code="200">новый токен</response>
/// <response code="400">логин и пароль не подходят</response>
[AllowAnonymous]
[HttpPost("login")]
[SwaggerOperation(OperationId = "login")]
[ProducesResponseType(typeof(UserTokenDto), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> LoginAsync([FromBody] AuthDto auth, CancellationToken token)
{
2024-08-19 10:01:07 +05:00
var userToken = await authService.LoginAsync(auth.Login, auth.Password, token);
2024-08-19 10:01:07 +05:00
if (userToken is null)
Forbid();
2024-08-19 10:01:07 +05:00
return Ok(userToken);
}
2024-08-19 10:01:07 +05:00
/// <summary>
/// Продление срока действия токена
/// </summary>
/// <returns code="200">новый токен</returns>
[Authorize]
[HttpGet("refresh")]
[ProducesResponseType(typeof(UserTokenDto), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> RefreshAsync(CancellationToken token)
{
var userToken = await authService.RefreshAsync(User, token);
2024-08-19 10:01:07 +05:00
if (userToken is null)
Forbid();
2024-08-19 10:01:07 +05:00
return Ok(userToken);
}
2024-08-19 10:01:07 +05:00
/// <summary>
/// Отправить заявку на регистрацию. Заявка подтверждается администратором.
/// </summary>
/// <param name="user">Информация о новом пользователе</param>
/// <returns code="200">Ок</returns>
[HttpPost]
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
public IActionResult Register(UserRegistrationDto user)
{
authService.Register(user);
return Ok();
}
2024-08-19 10:01:07 +05:00
/// <summary>
/// Смена пароля пользователя. Доступна пользователю и администратору
/// </summary>
/// <returns code="200">Ок</returns>
[Authorize]
[HttpPut("{idUser}/ChangePassword")]
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
public IActionResult ChangePassword([FromRoute] int idUser, [FromBody] string newPassword)
{
var editorUserId = User.GetUserId();
2024-08-19 10:01:07 +05:00
if (editorUserId is null)
return Forbid();
2022-04-11 18:00:34 +05:00
2024-08-19 10:01:07 +05:00
if (!((editorUserId == idUser) || userRepository.HasPermission((int)editorUserId, "Auth.edit")))
return Forbid();
2024-08-19 10:01:07 +05:00
authService.ChangePassword(idUser, newPassword);
return Ok();
}
}