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 AutoMapper;
using FluentValidation; using FluentValidation;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using PARR.API.Contracts.V1; 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.Controllers.V1.Base;
using PARR.API.Extensions; using PARR.API.Extensions;
using PARR.API.Services.Interfaces; using PARR.API.Services.Interfaces;
using PARR.Constants;
using PARR.DAL.DomainModels; using PARR.DAL.DomainModels;
using PARR.DAL.Models; using PARR.DAL.Models;
using PARR.DAL.Services.Interfaces; using PARR.DAL.Services.Interfaces;
@@ -25,18 +27,27 @@ namespace PARR.API.Controllers.V1
private readonly IAgentHistoryService agentHistoryService; private readonly IAgentHistoryService agentHistoryService;
private readonly IUriService uriService; private readonly IUriService uriService;
private readonly IMapper mapper; private readonly IMapper mapper;
private readonly ITemplateService templateService;
private readonly IClientService clientService;
private readonly ILogger<AgentHistoryController> logger;
public AgentHistoryController( public AgentHistoryController(
IValidator<AgentHistoryRequest> validator, IValidator<AgentHistoryRequest> validator,
IAgentHistoryService agentHistoryService, IAgentHistoryService agentHistoryService,
IUriService uriService, IUriService uriService,
IMapper mapper IMapper mapper,
ITemplateService templateService,
IClientService clientService,
ILogger<AgentHistoryController> logger
) )
{ {
this.validator = validator; this.validator = validator;
this.agentHistoryService = agentHistoryService; this.agentHistoryService = agentHistoryService;
this.uriService = uriService; this.uriService = uriService;
this.mapper = mapper; this.mapper = mapper;
this.templateService = templateService;
this.clientService = clientService;
this.logger = logger;
} }
@@ -44,6 +55,7 @@ namespace PARR.API.Controllers.V1
/// Получить историю работы агента постранично /// Получить историю работы агента постранично
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
[Authorize(Roles = ParrRoles.Administrator.Role)]
[HttpGet(ApiRoutes.AgentHistory.GetAll)] [HttpGet(ApiRoutes.AgentHistory.GetAll)]
public async Task<IActionResult> GetAll([FromQuery] PaginationQuery paginationQuery, [FromQuery] AgentHistoryQuery request) public async Task<IActionResult> GetAll([FromQuery] PaginationQuery paginationQuery, [FromQuery] AgentHistoryQuery request)
{ {
@@ -72,6 +84,7 @@ namespace PARR.API.Controllers.V1
/// </summary> /// </summary>
/// <param name="id"></param> /// <param name="id"></param>
/// <returns></returns> /// <returns></returns>
[Authorize(Roles = ParrRoles.Administrator.Role)]
[HttpGet(ApiRoutes.AgentHistory.Get)] [HttpGet(ApiRoutes.AgentHistory.Get)]
public async Task<IActionResult> GetById([FromRoute] Guid id) public async Task<IActionResult> GetById([FromRoute] Guid id)
{ {
@@ -90,6 +103,7 @@ namespace PARR.API.Controllers.V1
/// Добавить запись в историю работы агента /// Добавить запись в историю работы агента
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
[Authorize(Roles = ParrRoles.Agent.RoleOrAdmin)]
[HttpPost(ApiRoutes.AgentHistory.Create)] [HttpPost(ApiRoutes.AgentHistory.Create)]
public async Task<IActionResult> Create([FromBody] AgentHistoryRequest request) public async Task<IActionResult> Create([FromBody] AgentHistoryRequest request)
{ {
@@ -97,7 +111,20 @@ namespace PARR.API.Controllers.V1
if (!resultValidate.IsValid) if (!resultValidate.IsValid)
return BadRequest(new Response(resultValidate.Errors)); 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 var agentJournal = new AgentHistory
{ {

View File

@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using PARR.API.Contracts.V1; using PARR.API.Contracts.V1;
using PARR.API.Contracts.V1.Requests.Queries; using PARR.API.Contracts.V1.Requests.Queries;
@@ -6,6 +7,7 @@ using PARR.API.Contracts.V1.Responses;
using PARR.API.Contracts.V1.Responses.Base; using PARR.API.Contracts.V1.Responses.Base;
using PARR.API.Controllers.V1.Base; using PARR.API.Controllers.V1.Base;
using PARR.API.Services.Interfaces; using PARR.API.Services.Interfaces;
using PARR.Constants;
using PARR.DAL.Contracts; using PARR.DAL.Contracts;
using PARR.DAL.Services.Interfaces; using PARR.DAL.Services.Interfaces;
using PARR.DAL.TransformServices; using PARR.DAL.TransformServices;
@@ -15,6 +17,7 @@ namespace PARR.API.Controllers.V1
/// <summary> /// <summary>
/// Выдает задания Агенту сервера /// Выдает задания Агенту сервера
/// </summary> /// </summary>
[Authorize(Roles = ParrRoles.Agent.RoleOrAdmin)]
public class AgentTaskController : BaseApiController public class AgentTaskController : BaseApiController
{ {
private readonly IClientService clientService; private readonly IClientService clientService;

View File

@@ -1,4 +1,5 @@
using FluentValidation; using FluentValidation;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using PARR.API.Contracts.V1; using PARR.API.Contracts.V1;
using PARR.API.Contracts.V1.Requests; using PARR.API.Contracts.V1.Requests;
@@ -8,10 +9,12 @@ using PARR.API.Services.Interfaces;
using PARR.API.Settings; using PARR.API.Settings;
using PARR.BLL.Domain.Mq; using PARR.BLL.Domain.Mq;
using PARR.BLL.Services.Interfaces; using PARR.BLL.Services.Interfaces;
using PARR.Constants;
using System.Text.Json; using System.Text.Json;
namespace PARR.API.Controllers.V1 namespace PARR.API.Controllers.V1
{ {
[Authorize(Roles = ParrRoles.Administrator.Role)]
public class GeneratorTemplateController : BaseApiController public class GeneratorTemplateController : BaseApiController
{ {
private readonly IValidator<GeneratorTemplateRequest> validator; private readonly IValidator<GeneratorTemplateRequest> validator;

View File

@@ -1,4 +1,5 @@
using AutoMapper; using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using PARR.API.Contracts.V1; using PARR.API.Contracts.V1;
@@ -8,11 +9,13 @@ using PARR.API.Contracts.V1.Responses.Base;
using PARR.API.Controllers.V1.Base; using PARR.API.Controllers.V1.Base;
using PARR.API.Extensions; using PARR.API.Extensions;
using PARR.BLL.Helpers; using PARR.BLL.Helpers;
using PARR.Constants;
using PARR.DAL.DomainModels; using PARR.DAL.DomainModels;
using PARR.DAL.Services.Interfaces; using PARR.DAL.Services.Interfaces;
namespace PARR.API.Controllers.V1 namespace PARR.API.Controllers.V1
{ {
[Authorize(Roles = ParrRoles.Administrator.Role)]
public class HostController : BaseApiController public class HostController : BaseApiController
{ {
private readonly IMapper mapper; private readonly IMapper mapper;

View File

@@ -1,5 +1,6 @@
using AutoMapper; using AutoMapper;
using FluentValidation; using FluentValidation;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using PARR.API.Contracts.V1; using PARR.API.Contracts.V1;
@@ -10,12 +11,14 @@ using PARR.API.Contracts.V1.Responses.Base;
using PARR.API.Controllers.V1.Base; using PARR.API.Controllers.V1.Base;
using PARR.API.Extensions; using PARR.API.Extensions;
using PARR.API.Services.Interfaces; using PARR.API.Services.Interfaces;
using PARR.Constants;
using PARR.DAL.DomainModels; using PARR.DAL.DomainModels;
using PARR.DAL.Models; using PARR.DAL.Models;
using PARR.DAL.Services.Interfaces; using PARR.DAL.Services.Interfaces;
namespace PARR.API.Controllers.V1 namespace PARR.API.Controllers.V1
{ {
[Authorize(Roles = ParrRoles.EsppRobot.RoleOrAdmin)]
public class RobotHistoryController : BaseApiController public class RobotHistoryController : BaseApiController
{ {
private readonly IValidator<RobotHistoryRequest> validator; private readonly IValidator<RobotHistoryRequest> validator;
@@ -54,7 +57,7 @@ namespace PARR.API.Controllers.V1
IQueryable<RobotHistory> query = robotHistoryService.Get() IQueryable<RobotHistory> query = robotHistoryService.Get()
.Include(t => t.RobotConfiguration) .Include(t => t.RobotConfiguration)
.ThenInclude(t=>t!.Template) .ThenInclude(t => t!.Template)
.Include(t => t.RobotHistoryLevel) .Include(t => t.RobotHistoryLevel)
.Include(t => t.StatusTask) .Include(t => t.StatusTask)
.OrderByDescending(t => t.DateCreated); .OrderByDescending(t => t.DateCreated);

View File

@@ -1,14 +1,17 @@
using AutoMapper; using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using PARR.API.Contracts.V1; using PARR.API.Contracts.V1;
using PARR.API.Contracts.V1.Responses; using PARR.API.Contracts.V1.Responses;
using PARR.API.Contracts.V1.Responses.Base; using PARR.API.Contracts.V1.Responses.Base;
using PARR.API.Controllers.V1.Base; using PARR.API.Controllers.V1.Base;
using PARR.Constants;
using PARR.DAL.Services.Interfaces; using PARR.DAL.Services.Interfaces;
namespace PARR.API.Controllers.V1 namespace PARR.API.Controllers.V1
{ {
[Authorize(Roles = ParrRoles.EsppRobot.RoleOrAdmin)]
public class RobotHistoryLevelController : BaseApiController public class RobotHistoryLevelController : BaseApiController
{ {
private readonly IMapper mapper; private readonly IMapper mapper;

View File

@@ -1,4 +1,5 @@
using AutoMapper; using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using PARR.API.Contracts.V1; using PARR.API.Contracts.V1;
@@ -13,6 +14,7 @@ using PARR.DAL.TransformServices;
namespace PARR.API.Controllers.V1 namespace PARR.API.Controllers.V1
{ {
[Authorize(Roles = ParrRoles.EsppRobot.RoleOrAdmin)]
public class RobotTaskController : BaseApiController public class RobotTaskController : BaseApiController
{ {
private readonly IMapper mapper; private readonly IMapper mapper;

View File

@@ -1,4 +1,5 @@
using AutoMapper; using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using PARR.API.Contracts.V1; using PARR.API.Contracts.V1;
@@ -6,12 +7,14 @@ using PARR.API.Contracts.V1.Requests;
using PARR.API.Contracts.V1.Responses; using PARR.API.Contracts.V1.Responses;
using PARR.API.Contracts.V1.Responses.Base; using PARR.API.Contracts.V1.Responses.Base;
using PARR.API.Controllers.V1.Base; using PARR.API.Controllers.V1.Base;
using PARR.Constants;
using PARR.DAL.Contracts; using PARR.DAL.Contracts;
using PARR.DAL.Models; using PARR.DAL.Models;
using PARR.DAL.Services.Interfaces; using PARR.DAL.Services.Interfaces;
namespace PARR.API.Controllers.V1 namespace PARR.API.Controllers.V1
{ {
[Authorize(Roles = ParrRoles.EsppRobot.RoleOrAdmin)]
public class RobotTaskRobotStatusController : BaseApiController public class RobotTaskRobotStatusController : BaseApiController
{ {
private readonly IRobotConfigurationService robotConfigurationService; private readonly IRobotConfigurationService robotConfigurationService;

View File

@@ -1,14 +1,17 @@
using AutoMapper; using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using PARR.API.Contracts.V1; using PARR.API.Contracts.V1;
using PARR.API.Contracts.V1.Responses; using PARR.API.Contracts.V1.Responses;
using PARR.API.Contracts.V1.Responses.Base; using PARR.API.Contracts.V1.Responses.Base;
using PARR.API.Controllers.V1.Base; using PARR.API.Controllers.V1.Base;
using PARR.Constants;
using PARR.DAL.Services.Interfaces; using PARR.DAL.Services.Interfaces;
namespace PARR.API.Controllers.V1 namespace PARR.API.Controllers.V1
{ {
[Authorize(Roles = ParrRoles.EsppRobot.RoleOrAdmin)]
public class TaskStatusController : BaseApiController public class TaskStatusController : BaseApiController
{ {
private readonly IMapper mapper; private readonly IMapper mapper;

View File

@@ -1,4 +1,5 @@
using AutoMapper; using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using PARR.API.Contracts.V1; using PARR.API.Contracts.V1;
@@ -17,6 +18,7 @@ using PARR.DAL.Services.Interfaces;
namespace PARR.API.Controllers.V1 namespace PARR.API.Controllers.V1
{ {
[Authorize(Roles = ParrRoles.Administrator.Role)]
public class TemplateController : BaseApiController public class TemplateController : BaseApiController
{ {
private readonly IMapper mapper; private readonly IMapper mapper;

View File

@@ -1,5 +1,6 @@
using AutoMapper; using AutoMapper;
using FluentValidation; using FluentValidation;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using PARR.API.Contracts.V1; using PARR.API.Contracts.V1;
using PARR.API.Contracts.V1.Requests; using PARR.API.Contracts.V1.Requests;
@@ -7,6 +8,7 @@ using PARR.API.Contracts.V1.Responses;
using PARR.API.Contracts.V1.Responses.Base; using PARR.API.Contracts.V1.Responses.Base;
using PARR.API.Controllers.V1.Base; using PARR.API.Controllers.V1.Base;
using PARR.API.Services.Interfaces; using PARR.API.Services.Interfaces;
using PARR.Constants;
using PARR.DAL.Services.Interfaces; using PARR.DAL.Services.Interfaces;
namespace PARR.API.Controllers.V1 namespace PARR.API.Controllers.V1
@@ -14,6 +16,7 @@ namespace PARR.API.Controllers.V1
/// <summary> /// <summary>
/// Контроллер по управлению ScheduleEsppId у шаблона /// Контроллер по управлению ScheduleEsppId у шаблона
/// </summary> /// </summary>
[Authorize(Roles = ParrRoles.EsppRobot.RoleOrAdmin)]
public class TemplateScheduleEspp : BaseApiController public class TemplateScheduleEspp : BaseApiController
{ {
private readonly ITemplateService templateService; private readonly ITemplateService templateService;

View File

@@ -4,12 +4,12 @@ using PARR.API.Contracts.V1;
using PARR.API.Contracts.V1.Responses.Base; using PARR.API.Contracts.V1.Responses.Base;
using PARR.API.Controllers.V1.Base; using PARR.API.Controllers.V1.Base;
using PARR.API.Services.Interfaces; using PARR.API.Services.Interfaces;
using PARR.Constants;
using PARR.DAL.CacheServices; using PARR.DAL.CacheServices;
using PARR.DAL.TransformServices; using PARR.DAL.TransformServices;
namespace PARR.API.Controllers.V1 namespace PARR.API.Controllers.V1
{ {
[Authorize]
public class TestController : BaseApiController public class TestController : BaseApiController
{ {
private readonly IClientService clientService; private readonly IClientService clientService;
@@ -28,8 +28,6 @@ namespace PARR.API.Controllers.V1
/// Получить мой ip /// Получить мой ip
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
[Authorize(Roles = ParrRoles.Administrator.Role)]
//[Authorize]
[HttpGet(ApiRoutes.Test.GetMyIp)] [HttpGet(ApiRoutes.Test.GetMyIp)]
public IActionResult GetMyIp() public IActionResult GetMyIp()
{ {