From 322e648919b9d37757e08434264ce517f8376ea7 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Tue, 24 Mar 2020 23:59:05 +0000 Subject: [PATCH] --- .../GlobalBizSettingsController.cs | 100 +++++++++++++++++ server/AyaNova/biz/BizRoles.cs | 11 ++ server/AyaNova/biz/GlobalBizSettingsBiz.cs | 101 ++++++++++++++++++ .../AyaNova/util/ServerGlobalBizSettings.cs | 2 +- 4 files changed, 213 insertions(+), 1 deletion(-) create mode 100644 server/AyaNova/Controllers/GlobalBizSettingsController.cs create mode 100644 server/AyaNova/biz/GlobalBizSettingsBiz.cs diff --git a/server/AyaNova/Controllers/GlobalBizSettingsController.cs b/server/AyaNova/Controllers/GlobalBizSettingsController.cs new file mode 100644 index 00000000..ecfb2876 --- /dev/null +++ b/server/AyaNova/Controllers/GlobalBizSettingsController.cs @@ -0,0 +1,100 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Routing; +using Microsoft.Extensions.Logging; +using Microsoft.AspNetCore.Authorization; +using Microsoft.EntityFrameworkCore; +using AyaNova.Models; +using AyaNova.Api.ControllerHelpers; +using AyaNova.Biz; +using System.Threading.Tasks; + +namespace AyaNova.Api.Controllers +{ + + [ApiController] + [ApiVersion("8.0")] + [Route("api/v{version:apiVersion}/[controller]")] + [Produces("application/json")] + [Authorize] + public class GlobalBizSettingsController : ControllerBase + { + private readonly AyContext ct; + private readonly ILogger log; + private readonly ApiServerState serverState; + + + + /// + /// ctor + /// + /// + /// + /// + public GlobalBizSettingsController(AyContext dbcontext, ILogger logger, ApiServerState apiServerState) + { + ct = dbcontext; + log = logger; + serverState = apiServerState; + } + + /// + /// Get GlobalBizSettings + /// + /// Global settings object + [HttpGet] + public async Task GetGlobalBizSettings() + { + if (serverState.IsClosed) + return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); + + //Instantiate the business object handler + GlobalBizSettingsBiz biz = GlobalBizSettingsBiz.GetBiz(ct, HttpContext); + + if (!Authorized.HasReadFullRole(HttpContext.Items, biz.BizType)) + return StatusCode(403, new ApiNotAuthorizedResponse()); + + if (!ModelState.IsValid) + return BadRequest(new ApiErrorResponse(ModelState)); + + var o = await biz.GetAsync(); + if (o == null) + return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); + + return Ok(ApiOkResponse.Response(o, !Authorized.HasModifyRole(HttpContext.Items, biz.BizType))); + } + + + /// + /// POST (replace) Global biz settings + /// + /// + /// nothing + [HttpPost] + public async Task ReplaceGlobalBizSettings([FromBody] GlobalBizSettings global) + { + if (!serverState.IsOpen) + return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason)); + + if (!ModelState.IsValid) + return BadRequest(new ApiErrorResponse(ModelState)); + + //Instantiate the business object handler + GlobalBizSettingsBiz biz = GlobalBizSettingsBiz.GetBiz(ct, HttpContext); + + if (!Authorized.HasModifyRole(HttpContext.Items, biz.BizType)) + return StatusCode(403, new ApiNotAuthorizedResponse()); + + try + { + if (!await biz.ReplaceAsync(global)) + return BadRequest(new ApiErrorResponse(biz.Errors)); + } + catch (DbUpdateConcurrencyException) + { + return StatusCode(409, new ApiErrorResponse(ApiErrorCode.CONCURRENCY_CONFLICT)); + } + return NoContent(); + } + + }//eoc +}//ens \ No newline at end of file diff --git a/server/AyaNova/biz/BizRoles.cs b/server/AyaNova/biz/BizRoles.cs index aeba77c4..6bee69a1 100644 --- a/server/AyaNova/biz/BizRoles.cs +++ b/server/AyaNova/biz/BizRoles.cs @@ -31,6 +31,17 @@ namespace AyaNova.Biz #region All roles initialization //CoreBizObject add here + //////////////////////////////////////////////////////////// + //GLOBAL BIZ SETTINGS + // + roles.Add(AyaType.Global, new BizRoleSet() + { + Change = AuthorizationRoles.BizAdminFull, + ReadFullRecord = AuthorizationRoles.BizAdminLimited + }); + + + //////////////////////////////////////////////////////////// //USER // diff --git a/server/AyaNova/biz/GlobalBizSettingsBiz.cs b/server/AyaNova/biz/GlobalBizSettingsBiz.cs new file mode 100644 index 00000000..001834db --- /dev/null +++ b/server/AyaNova/biz/GlobalBizSettingsBiz.cs @@ -0,0 +1,101 @@ +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using AyaNova.Util; +using AyaNova.Api.ControllerHelpers; +using AyaNova.Models; + +namespace AyaNova.Biz +{ + + internal class GlobalBizSettingsBiz : BizObject + { + + internal GlobalBizSettingsBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles) + { + ct = dbcontext; + UserId = currentUserId; + UserTranslationId = userTranslationId; + CurrentUserRoles = UserRoles; + BizType = AyaType.Global; + } + + internal static GlobalBizSettingsBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null) + { + if (httpContext != null) + return new GlobalBizSettingsBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items)); + else + return new GlobalBizSettingsBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull); + } + + + + + //////////////////////////////////////////////////////////////////////////////////////////////// + /// GET + + //Get one + internal async Task GetAsync(bool logTheGetEvent = true) + { + //first try to fetch from db + var ret = await ct.GlobalBizSettings.SingleOrDefaultAsync(m => m.Id == 1); + if (logTheGetEvent && ret != null) + { + //Log + await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, 1, BizType, AyaEvent.Retrieved), ct); + } + + //not in db then get the default + if (ret == null) + { + throw new System.Exception("GlobalBizSettingsBiz::GetAsync -> Global settings object not found in database!!"); + } + return ret; + } + + + + //////////////////////////////////////////////////////////////////////////////////////////////// + //UPDATE + // + + //put + internal async Task ReplaceAsync(GlobalBizSettings global) + { + var o = await ct.GlobalBizSettings.FirstOrDefaultAsync(m => m.Id == 1); + if (o == null) + throw new System.Exception("GlobalBizSettingsBiz::ReplaceAsync -> Global settings object not found in database!!"); + CopyObject.Copy(global, o); + Validate(o); + if (HasErrors) + return false; + await ct.SaveChangesAsync(); + //Log modification and save context + await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, 1, BizType, AyaEvent.Modified), ct); + //Update the static copy for the server + ServerGlobalBizSettings.Initialize(o); + return true; + } + + + + //////////////////////////////////////////////////////////////////////////////////////////////// + //VALIDATION + // + + //Can save or update? + private void Validate(GlobalBizSettings inObj) + { + + //currently nothing to validate + } + + + + + ///////////////////////////////////////////////////////////////////// + + }//eoc + + +}//eons + diff --git a/server/AyaNova/util/ServerGlobalBizSettings.cs b/server/AyaNova/util/ServerGlobalBizSettings.cs index 203d55b2..a6126221 100644 --- a/server/AyaNova/util/ServerGlobalBizSettings.cs +++ b/server/AyaNova/util/ServerGlobalBizSettings.cs @@ -37,7 +37,7 @@ namespace AyaNova.Util } } - + //We have the object, now copy the static values here SearchCaseSensitiveOnly = global.SearchCaseSensitiveOnly; }