2024-07-04 11:02:45 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
2023-06-21 12:33:18 +05:00
|
|
|
|
using AsbCloudApp.Data.User;
|
2022-10-27 11:22:39 +05:00
|
|
|
|
using AsbCloudApp.Repositories;
|
2021-04-02 17:28:07 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2021-08-24 10:59:10 +05:00
|
|
|
|
using Swashbuckle.AspNetCore.Annotations;
|
2021-08-11 16:54:42 +05:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-04-02 17:28:07 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
namespace AsbCloudWebApi.Controllers;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Авторизация
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Route("/auth")]
|
|
|
|
|
[ApiController]
|
|
|
|
|
public class AuthController : ControllerBase
|
2021-04-02 17:28:07 +05:00
|
|
|
|
{
|
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)
|
2021-04-02 17:28:07 +05:00
|
|
|
|
{
|
2024-08-19 10:01:07 +05:00
|
|
|
|
var userToken = await authService.LoginAsync(auth.Login, auth.Password, token);
|
2021-04-02 17:28:07 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
if (userToken is null)
|
|
|
|
|
Forbid();
|
2021-04-02 17:28:07 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
return Ok(userToken);
|
|
|
|
|
}
|
2021-04-02 17:28:07 +05:00
|
|
|
|
|
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);
|
2022-10-24 10:36:53 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
if (userToken is null)
|
|
|
|
|
Forbid();
|
2022-10-24 10:36:53 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
return Ok(userToken);
|
|
|
|
|
}
|
2021-09-07 09:49:25 +05:00
|
|
|
|
|
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();
|
|
|
|
|
}
|
2021-09-07 09:49:25 +05:00
|
|
|
|
|
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();
|
2021-09-07 09:49:25 +05:00
|
|
|
|
|
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();
|
2021-09-07 09:49:25 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
authService.ChangePassword(idUser, newPassword);
|
|
|
|
|
return Ok();
|
2021-04-02 17:28:07 +05:00
|
|
|
|
}
|
|
|
|
|
}
|