feat(api): применены права ко всем контроллерам и методам

This commit is contained in:
Mikhail Trubnikov
2023-12-04 10:16:00 +10:00
parent e0b0297c15
commit 68e15021ec
12 changed files with 60 additions and 7 deletions

View File

@@ -1,5 +1,6 @@
using AutoMapper;
using FluentValidation;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using PARR.API.Contracts.V1;
@@ -10,6 +11,7 @@ using PARR.API.Contracts.V1.Responses.Base;
using PARR.API.Controllers.V1.Base;
using PARR.API.Extensions;
using PARR.API.Services.Interfaces;
using PARR.Constants;
using PARR.DAL.DomainModels;
using PARR.DAL.Models;
using PARR.DAL.Services.Interfaces;
@@ -25,18 +27,27 @@ namespace PARR.API.Controllers.V1
private readonly IAgentHistoryService agentHistoryService;
private readonly IUriService uriService;
private readonly IMapper mapper;
private readonly ITemplateService templateService;
private readonly IClientService clientService;
private readonly ILogger<AgentHistoryController> logger;
public AgentHistoryController(
IValidator<AgentHistoryRequest> validator,
IAgentHistoryService agentHistoryService,
IUriService uriService,
IMapper mapper
IMapper mapper,
ITemplateService templateService,
IClientService clientService,
ILogger<AgentHistoryController> logger
)
{
this.validator = validator;
this.agentHistoryService = agentHistoryService;
this.uriService = uriService;
this.mapper = mapper;
this.templateService = templateService;
this.clientService = clientService;
this.logger = logger;
}
@@ -44,6 +55,7 @@ namespace PARR.API.Controllers.V1
/// Получить историю работы агента постранично
/// </summary>
/// <returns></returns>
[Authorize(Roles = ParrRoles.Administrator.Role)]
[HttpGet(ApiRoutes.AgentHistory.GetAll)]
public async Task<IActionResult> GetAll([FromQuery] PaginationQuery paginationQuery, [FromQuery] AgentHistoryQuery request)
{
@@ -72,6 +84,7 @@ namespace PARR.API.Controllers.V1
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[Authorize(Roles = ParrRoles.Administrator.Role)]
[HttpGet(ApiRoutes.AgentHistory.Get)]
public async Task<IActionResult> GetById([FromRoute] Guid id)
{
@@ -90,6 +103,7 @@ namespace PARR.API.Controllers.V1
/// Добавить запись в историю работы агента
/// </summary>
/// <returns></returns>
[Authorize(Roles = ParrRoles.Agent.RoleOrAdmin)]
[HttpPost(ApiRoutes.AgentHistory.Create)]
public async Task<IActionResult> Create([FromBody] AgentHistoryRequest request)
{
@@ -97,7 +111,20 @@ namespace PARR.API.Controllers.V1
if (!resultValidate.IsValid)
return BadRequest(new Response(resultValidate.Errors));
//todo: проверять что этот этот шаблон привязан к этому серверу по ip???
var clientIp = clientService.GetClientIp()?.ToString();
var template = await templateService.Get().Include(t => t.Host).FirstOrDefaultAsync(t => t.Id == request.TemplateId);
if (template == null)
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { FieldName = nameof(request.TemplateId), Message = $"Не найден шаблон с Id: {request.TemplateId}" } }));
//проверять что этот этот шаблон привязан к этому серверу по ip
if (template.Host?.IP != clientIp)
{
logger.LogWarning($"Клиент с ip: {clientIp} пытается записать историю агента для templateId: {request.TemplateId}, " +
$"но у шаблона ip: {template.Host?.IP}, доступ запрещен так как их ip не равны.");
return Forbid();
}
var agentJournal = new AgentHistory
{