2023-09-21 20:51:16 +02:00
|
|
|
using System;
|
2023-09-17 21:41:31 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2023-09-06 21:06:13 +02:00
|
|
|
using System.Threading.Tasks;
|
2023-09-17 21:41:31 +02:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2023-09-06 21:06:13 +02:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2023-09-17 21:41:31 +02:00
|
|
|
using SharpRSS.API.Auth;
|
2023-09-10 21:32:25 +02:00
|
|
|
using SharpRSS.API.Contracts;
|
|
|
|
using SharpRSS.API.Contracts.DTO;
|
2023-09-17 21:41:31 +02:00
|
|
|
using SharpRSS.API.Contracts.Models.User;
|
2023-09-10 21:32:25 +02:00
|
|
|
using SharpRSS.API.Data;
|
|
|
|
using SharpRSS.API.Models;
|
|
|
|
using SharpRSS.API.Models.Auth;
|
2023-09-06 21:06:13 +02:00
|
|
|
|
|
|
|
namespace SharpRSS.API.Controllers
|
|
|
|
{
|
|
|
|
[ApiController]
|
2023-09-17 21:41:31 +02:00
|
|
|
[SessionAuthorize]
|
2023-09-06 21:06:13 +02:00
|
|
|
[Route("api/[controller]")]
|
|
|
|
public class AuthController : ControllerBase
|
|
|
|
{
|
2023-09-10 21:32:25 +02:00
|
|
|
public AuthController(AuthService authService)
|
2023-09-06 21:06:13 +02:00
|
|
|
{
|
2023-09-10 21:32:25 +02:00
|
|
|
_authService = authService;
|
|
|
|
}
|
|
|
|
|
|
|
|
private readonly AuthService _authService;
|
2023-09-17 21:41:31 +02:00
|
|
|
|
|
|
|
[HttpPost("[action]")]
|
|
|
|
[AllowAnonymous]
|
|
|
|
public async Task<ActionResult<string>> Authenticate(AuthenticateUser authenticateUser)
|
2023-09-21 20:51:16 +02:00
|
|
|
{ // Return test result
|
|
|
|
return Ok(new { Expires = DateTime.Now.Add(TimeSpan.FromDays(7)), SessionToken = Guid.NewGuid().ToString(), Released = DateTime.Now });
|
2023-09-17 21:41:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost("user")]
|
2023-09-21 20:51:16 +02:00
|
|
|
[SessionAuthorize("auth:user:create")]
|
2023-09-17 21:41:31 +02:00
|
|
|
public async Task<ActionResult<UserDto>> CreateUser(AuthenticateUser authenticateUser)
|
2023-09-10 21:32:25 +02:00
|
|
|
{
|
2023-09-17 21:41:31 +02:00
|
|
|
Result<User> result = await _authService.CreateUser(authenticateUser);
|
2023-09-21 20:51:16 +02:00
|
|
|
if (result.Success)
|
2023-09-10 21:32:25 +02:00
|
|
|
return Ok(Models.Auth.User.ToDto(result.Value ?? new User()));
|
|
|
|
return BadRequest(new ApiResult(result.Message, ApiResults.Error));
|
2023-09-06 21:06:13 +02:00
|
|
|
}
|
2023-09-17 21:41:31 +02:00
|
|
|
|
|
|
|
[HttpGet("user")]
|
2023-09-21 20:51:16 +02:00
|
|
|
[SessionAuthorize("auth:user:get")]
|
2023-09-17 21:41:31 +02:00
|
|
|
public async Task<ActionResult<ApiListResult<IEnumerable<UserDto>>>> GetUsers(int take, int skip)
|
|
|
|
{
|
|
|
|
var usersAuth = await _authService.GetUsers(take, skip);
|
|
|
|
List<UserDto> users = usersAuth.Value?.Select(Models.Auth.User.ToDto).ToList() ?? new List<UserDto>();
|
|
|
|
return Ok(new ApiListResult<IEnumerable<UserDto>>(users.Count, await _authService.UserCount(), users));
|
|
|
|
}
|
2023-09-06 21:06:13 +02:00
|
|
|
}
|
|
|
|
}
|