Files
raven/server/AyaNova/Controllers/JobOperationsController.cs
2019-04-30 19:41:00 +00:00

134 lines
4.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Logging;
using AyaNova.Models;
using AyaNova.Api.ControllerHelpers;
using AyaNova.Biz;
namespace AyaNova.Api.Controllers
{
/// <summary>
/// JobOperations controller
/// </summary>
[ApiVersion("8.0")]
[Route("api/v{version:apiVersion}/[controller]")]
[Produces("application/json")]
[Authorize]
public class JobOperationsController : Controller
{
private readonly AyContext ct;
private readonly ILogger<JobOperationsController> log;
private readonly ApiServerState serverState;
/// <summary>
/// ctor
/// </summary>
/// <param name="dbcontext"></param>
/// <param name="logger"></param>
/// <param name="apiServerState"></param>
public JobOperationsController(AyContext dbcontext, ILogger<JobOperationsController> logger, ApiServerState apiServerState)
{
ct = dbcontext;
log = logger;
serverState = apiServerState;
}
/// <summary>
/// Get Operations jobs list
///
/// Required roles: OpsAdminFull, OpsAdminLimited, BizAdminFull, BizAdminLimited
///
/// This list cannot be filtered or queried as there are typically not many jobs
///
/// </summary>
/// <returns>List of operations jobs</returns>
[HttpGet]
public async Task<IActionResult> List()
{
//Open or opsOnly and user is opsadminfull or opsadminlimited
if (!serverState.IsOpenOrOpsOnly || (serverState.IsOpsOnly && !Authorized.HasAnyRole(HttpContext.Items, AuthorizationRoles.OpsAdminFull | AuthorizationRoles.OpsAdminLimited)))
{
return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason));
}
if (!Authorized.IsAuthorizedToReadFullRecord(HttpContext.Items, AyaType.JobOperations))
{
return StatusCode(403, new ApiNotAuthorizedResponse());
}
if (!ModelState.IsValid)
{
return BadRequest(new ApiErrorResponse(ModelState));
}
//Instantiate the business object handler
JobOperationsBiz biz = new JobOperationsBiz(ct, UserIdFromContext.Id(HttpContext.Items), UserRolesFromContext.Roles(HttpContext.Items));
List<JobOperationsFetchInfo> l = await biz.GetJobListAsync();
return Ok(ApiOkResponse.Response(l, true));
}
/// <summary>
/// Get Operations log for a job
///
/// Required roles: OpsAdminFull, OpsAdminLimited, BizAdminFull, BizAdminLimited
///
/// This list cannot be filtered or queried as there are typically not many jobs
///
/// </summary>
/// <param name="gid"></param>
/// <returns>A tag</returns>
[HttpGet("logs/{gid}")]
public async Task<IActionResult> GetLogs([FromRoute] Guid gid)
{
//Open or opsOnly and user is opsadminfull or opsadminlimited
if (!serverState.IsOpenOrOpsOnly || (serverState.IsOpsOnly && !Authorized.HasAnyRole(HttpContext.Items, AuthorizationRoles.OpsAdminFull | AuthorizationRoles.OpsAdminLimited)))
{
return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason));
}
if (!Authorized.IsAuthorizedToReadFullRecord(HttpContext.Items, AyaType.JobOperations))
{
return StatusCode(403, new ApiNotAuthorizedResponse());
}
if (!ModelState.IsValid)
{
return BadRequest(new ApiErrorResponse(ModelState));
}
//Instantiate the business object handler
JobOperationsBiz biz = new JobOperationsBiz(ct, UserIdFromContext.Id(HttpContext.Items), UserRolesFromContext.Roles(HttpContext.Items));
List<JobOperationsLogInfoItem> l = await biz.GetJobLogListAsync(gid);
return Ok(ApiOkResponse.Response(l, true));
}
//------------
}//eoc
}//eons