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;
|
|
|
|
|
|
|
|
|
|
public AuthController(IAuthService authService)
|
|
|
|
|
{
|
|
|
|
|
this.authService = authService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
|
BadRequest();//"wrong login or password"
|
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>
|
|
|
|
|
/// Регистрация пользователя. Доступна администратору
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns code="200">Ок</returns>
|
|
|
|
|
[Authorize]
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public IActionResult Register(UserDto user)
|
|
|
|
|
{
|
|
|
|
|
const string roleName = "Администратор";
|
|
|
|
|
if (!User.IsInRole(roleName))
|
|
|
|
|
return Forbid($"You mast be an {roleName}.");
|
|
|
|
|
|
|
|
|
|
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-09-07 09:49:25 +05:00
|
|
|
|
_ => BadRequest(),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Смена пароля пользователя. Доступна пользователю и администратору
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns code="200">Ок</returns>
|
|
|
|
|
[Authorize]
|
|
|
|
|
[HttpPut("{idUser}/ChangePassword")]
|
|
|
|
|
public IActionResult ChangePassword([FromRoute]int idUser, [FromBody]string newPassword)
|
|
|
|
|
{
|
|
|
|
|
const string roleName = "Администратор";
|
|
|
|
|
var allow = (User.GetUserId() == idUser) || User.IsInRole(roleName);
|
|
|
|
|
|
|
|
|
|
if (!allow)
|
|
|
|
|
return Forbid($"You mast be an {roleName} or user with id:{idUser}.");
|
|
|
|
|
|
|
|
|
|
var code = authService.ChangePassword(idUser, newPassword);
|
|
|
|
|
return code switch
|
|
|
|
|
{
|
|
|
|
|
0 => Ok(),
|
|
|
|
|
-1 => BadRequest("Нет такого пользователя"),
|
|
|
|
|
_ => BadRequest(),
|
|
|
|
|
};
|
|
|
|
|
}
|
2021-04-02 17:28:07 +05:00
|
|
|
|
}
|
|
|
|
|
}
|