2021-04-02 17:28:07 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
namespace AsbCloudWebApi.Controllers
|
|
|
|
|
{
|
|
|
|
|
[Route("/auth")]
|
|
|
|
|
[ApiController]
|
|
|
|
|
public class AuthController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly IAuthService authService;
|
2022-02-03 08:23:52 +05:00
|
|
|
|
private readonly IUserService userService;
|
2021-04-02 17:28:07 +05:00
|
|
|
|
|
2022-02-03 08:23:52 +05:00
|
|
|
|
public AuthController(IAuthService authService, IUserService userService)
|
2021-04-02 17:28:07 +05:00
|
|
|
|
{
|
|
|
|
|
this.authService = authService;
|
2022-02-03 08:23:52 +05:00
|
|
|
|
this.userService = userService;
|
2021-04-02 17:28:07 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Аутентификация пользователя
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="auth"></param>
|
2021-08-11 16:54:42 +05:00
|
|
|
|
/// <param name="token">Токен отмены задачи</param>
|
2021-04-02 17:28:07 +05:00
|
|
|
|
/// <response code="200">новый токен</response>
|
|
|
|
|
/// <response code="400">логин и пароль не подходят</response>
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
[HttpPost("login")]
|
2021-08-11 16:54:42 +05:00
|
|
|
|
[SwaggerOperation(OperationId = "login")]
|
2021-04-02 17:28:07 +05:00
|
|
|
|
[ProducesResponseType(typeof(UserTokenDto), (int)System.Net.HttpStatusCode.OK)]
|
2021-08-11 16:54:42 +05:00
|
|
|
|
public async Task<IActionResult> LoginAsync([FromBody] AuthDto auth, CancellationToken token = default)
|
2021-04-02 17:28:07 +05:00
|
|
|
|
{
|
2021-08-24 10:59:10 +05:00
|
|
|
|
var userToken = await authService.LoginAsync(auth.Login,
|
2021-08-11 17:26:02 +05:00
|
|
|
|
auth.Password, token).ConfigureAwait(false);
|
|
|
|
|
|
2021-04-02 17:28:07 +05:00
|
|
|
|
if (userToken is null)
|
2021-10-21 17:24:25 +05:00
|
|
|
|
Forbid();
|
2021-04-23 10:21:25 +05:00
|
|
|
|
|
2021-04-02 17:28:07 +05:00
|
|
|
|
return Ok(userToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Продление срока действия токена
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns code="200">новый токен</returns>
|
|
|
|
|
[Authorize]
|
|
|
|
|
[HttpGet("refresh")]
|
|
|
|
|
public IActionResult Refresh()
|
|
|
|
|
{
|
|
|
|
|
var newToken = authService.Refresh(User);
|
|
|
|
|
return Ok(newToken);
|
|
|
|
|
}
|
2021-09-07 09:49:25 +05:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-10-22 17:41:00 +05:00
|
|
|
|
/// Отправить заявку на регистрацию. Заявка подтверждается администратором.
|
2021-09-07 09:49:25 +05:00
|
|
|
|
/// </summary>
|
2021-10-22 17:41:00 +05:00
|
|
|
|
/// <param name="user">Информация о новом пользователе</param>
|
2021-09-07 09:49:25 +05:00
|
|
|
|
/// <returns code="200">Ок</returns>
|
|
|
|
|
[HttpPost]
|
2021-12-11 16:46:04 +05:00
|
|
|
|
public IActionResult Register(UserRegistrationDto user)
|
2021-09-07 09:49:25 +05:00
|
|
|
|
{
|
|
|
|
|
var code = authService.Register(user);
|
|
|
|
|
return code switch
|
|
|
|
|
{
|
|
|
|
|
0 => Ok(),
|
|
|
|
|
-1 => BadRequest("Логин должен быть длиннее 3х знаков."),
|
|
|
|
|
-2 => BadRequest("Пароль должен быть длиннее 3х знаков."),
|
2021-10-19 15:22:18 +05:00
|
|
|
|
-3 => BadRequest("Email не должен быть длиннее 255 знаков."),
|
|
|
|
|
-4 => BadRequest("Телефон не должен быть длиннее 50 знаков."),
|
|
|
|
|
-5 => BadRequest("Название должности не должно быть длиннее 255 символов."),
|
2021-10-25 12:04:34 +05:00
|
|
|
|
-6 => BadRequest("Пользователь с таким логином уже зарегистрирован."),
|
2021-09-07 09:49:25 +05:00
|
|
|
|
_ => BadRequest(),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Смена пароля пользователя. Доступна пользователю и администратору
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns code="200">Ок</returns>
|
|
|
|
|
[Authorize]
|
|
|
|
|
[HttpPut("{idUser}/ChangePassword")]
|
2022-04-11 18:00:34 +05:00
|
|
|
|
public IActionResult ChangePassword([FromRoute] int idUser, [FromBody] string newPassword)
|
2021-09-07 09:49:25 +05:00
|
|
|
|
{
|
2022-02-03 08:23:52 +05:00
|
|
|
|
var editorUserId = User.GetUserId();
|
2021-09-07 09:49:25 +05:00
|
|
|
|
|
2022-02-03 08:23:52 +05:00
|
|
|
|
if (editorUserId is null)
|
|
|
|
|
return Forbid();
|
2022-04-11 18:00:34 +05:00
|
|
|
|
|
2022-02-03 08:23:52 +05:00
|
|
|
|
if (!((editorUserId == idUser) || userService.HasPermission((int)editorUserId, "Auth.edit")))
|
|
|
|
|
return Forbid();
|
2021-09-07 09:49:25 +05:00
|
|
|
|
|
|
|
|
|
var code = authService.ChangePassword(idUser, newPassword);
|
|
|
|
|
return code switch
|
|
|
|
|
{
|
|
|
|
|
0 => Ok(),
|
|
|
|
|
-1 => BadRequest("Нет такого пользователя"),
|
|
|
|
|
_ => BadRequest(),
|
|
|
|
|
};
|
|
|
|
|
}
|
2021-04-02 17:28:07 +05:00
|
|
|
|
}
|
|
|
|
|
}
|