This commit is contained in:
2020-04-05 23:33:10 +00:00
parent 837658f86c
commit 3e1323ee1d
6 changed files with 29 additions and 22 deletions

View File

@@ -62,27 +62,22 @@ namespace AyaNova.Api.Controllers
[Authorize]
public async Task<IActionResult> PostServerState([FromBody] ServerStateModel state)
{
if (!Authorized.HasModifyRole(HttpContext.Items, AyaType.ServerState))
{
return StatusCode(403, new ApiNotAuthorizedResponse());
}
if (serverState.IsClosed)//no state change allowed when closed
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
if (serverState.IsSystemLocked)//no state change allowed when system locked, must correct the problem first
{
return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason));
}
if (!Authorized.HasModifyRole(HttpContext.Items, AyaType.ServerState))
return StatusCode(403, new ApiNotAuthorizedResponse());
if (!ModelState.IsValid)
{
return BadRequest(new ApiErrorResponse(ModelState));
}
ApiServerState.ServerState desiredState;
if (!Enum.TryParse<ApiServerState.ServerState>(state.ServerState, true, out desiredState))
{
return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_INVALID_VALUE, null, "Invalid state - must be one of \"Closing\", \"Closed\", \"OpsOnly\" or \"Open\""));
}
return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_INVALID_VALUE, null, "Invalid state - must be one of \"OpsOnly\" or \"Open\""));
//don't allow a server to be set to closed, that's for internal ops only
if (desiredState == ApiServerState.ServerState.Closed)
return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_INVALID_VALUE, null, "Invalid state - must be one of \"OpsOnly\" or \"Open\""));
log.LogInformation($"ServerState change request by user {UserNameFromContext.Name(HttpContext.Items)} from current state of \"{serverState.GetState().ToString()}\" to \"{desiredState.ToString()}\"");
@@ -101,7 +96,7 @@ namespace AyaNova.Api.Controllers
public class ServerStateModel
{
/// <summary>
/// One of "Closed", "OpsOnly" or "Open"
/// "OpsOnly" or "Open"
/// </summary>
/// <returns></returns>
[Required]