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

102 lines
3.7 KiB
C#
Raw 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;
namespace AsbCloudWebApi.Controllers
{
2022-06-16 17:37:10 +05:00
/// <summary>
/// Авторизация
/// </summary>
[Route("/auth")]
[ApiController]
public class AuthController : ControllerBase
{
private readonly IAuthService authService;
private readonly IUserRepository userRepository;
public AuthController(IAuthService authService, IUserRepository userRepository)
{
this.authService = authService;
this.userRepository = userRepository;
}
/// <summary>
/// Аутентификация пользователя
/// </summary>
/// <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)
{
var userToken = await authService.LoginAsync(auth.Login, auth.Password, token);
if (userToken is null)
Forbid();
2021-04-23 10:21:25 +05:00
return Ok(userToken);
}
/// <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);
if (userToken is null)
Forbid();
return Ok(userToken);
}
/// <summary>
2021-10-22 17:41:00 +05:00
/// Отправить заявку на регистрацию. Заявка подтверждается администратором.
/// </summary>
2021-10-22 17:41:00 +05:00
/// <param name="user">Информация о новом пользователе</param>
/// <returns code="200">Ок</returns>
[HttpPost]
2023-09-28 17:09:44 +05:00
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
public IActionResult Register(UserRegistrationDto user)
{
2023-09-28 17:09:44 +05:00
authService.Register(user);
return Ok();
}
/// <summary>
/// Смена пароля пользователя. Доступна пользователю и администратору
/// </summary>
/// <returns code="200">Ок</returns>
[Authorize]
[HttpPut("{idUser}/ChangePassword")]
2023-09-28 17:09:44 +05:00
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
2022-04-11 18:00:34 +05:00
public IActionResult ChangePassword([FromRoute] int idUser, [FromBody] string newPassword)
{
2022-02-03 08:23:52 +05:00
var editorUserId = User.GetUserId();
2022-02-03 08:23:52 +05:00
if (editorUserId is null)
return Forbid();
2022-04-11 18:00:34 +05:00
if (!((editorUserId == idUser) || userRepository.HasPermission((int)editorUserId, "Auth.edit")))
2022-02-03 08:23:52 +05:00
return Forbid();
2023-09-28 17:09:44 +05:00
authService.ChangePassword(idUser, newPassword);
return Ok();
}
}
}