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
{

View File

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

View File

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

View File

@@ -1,4 +1,5 @@
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
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.Extensions;
using PARR.BLL.Helpers;
using PARR.Constants;
using PARR.DAL.DomainModels;
using PARR.DAL.Services.Interfaces;
namespace PARR.API.Controllers.V1
{
[Authorize(Roles = ParrRoles.Administrator.Role)]
public class HostController : BaseApiController
{
private readonly IMapper mapper;

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,12 +11,14 @@ 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;
namespace PARR.API.Controllers.V1
{
[Authorize(Roles = ParrRoles.EsppRobot.RoleOrAdmin)]
public class RobotHistoryController : BaseApiController
{
private readonly IValidator<RobotHistoryRequest> validator;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,5 +1,6 @@
using AutoMapper;
using FluentValidation;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using PARR.API.Contracts.V1;
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.Controllers.V1.Base;
using PARR.API.Services.Interfaces;
using PARR.Constants;
using PARR.DAL.Services.Interfaces;
namespace PARR.API.Controllers.V1
@@ -14,6 +16,7 @@ namespace PARR.API.Controllers.V1
/// <summary>
/// Контроллер по управлению ScheduleEsppId у шаблона
/// </summary>
[Authorize(Roles = ParrRoles.EsppRobot.RoleOrAdmin)]
public class TemplateScheduleEspp : BaseApiController
{
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.Controllers.V1.Base;
using PARR.API.Services.Interfaces;
using PARR.Constants;
using PARR.DAL.CacheServices;
using PARR.DAL.TransformServices;
namespace PARR.API.Controllers.V1
{
[Authorize]
public class TestController : BaseApiController
{
private readonly IClientService clientService;
@@ -28,8 +28,6 @@ namespace PARR.API.Controllers.V1
/// Получить мой ip
/// </summary>
/// <returns></returns>
[Authorize(Roles = ParrRoles.Administrator.Role)]
//[Authorize]
[HttpGet(ApiRoutes.Test.GetMyIp)]
public IActionResult GetMyIp()
{