public async Task <ActionResult> GetMessageBoxInstanceList(int instanceOwnerPartyId, [FromQuery] string state, [FromQuery] string language) { string[] allowedStates = { "active", "archived", "deleted" }; string[] acceptedLanguages = { "en", "nb", "nn" }; string languageId = "nb"; if (string.IsNullOrEmpty(state)) { return(BadRequest($"State is empty. Please provide on of: {string.Join(", ", allowedStates)}")); } state = state.ToLower(); if (!allowedStates.Contains(state)) { return(BadRequest($"Invalid instance state. Please provide on of: {string.Join(", ", allowedStates)}")); } if (language != null && acceptedLanguages.Contains(language.ToLower())) { languageId = language; } List <Instance> allInstances = await _instanceRepository.GetInstancesInStateOfInstanceOwner(instanceOwnerPartyId, state); if (allInstances.Count <= 0) { return(Ok(new List <MessageBoxInstance>())); } // removing properties only used for active messageBoxInstances if (!state.Equals("active")) { allInstances.ForEach(i => i.DueBefore = null); } List <MessageBoxInstance> authorizedInstances = await _authorizationHelper.AuthorizeMesseageBoxInstances(HttpContext.User, allInstances); List <string> appIds = authorizedInstances.Select(i => InstanceHelper.GetAppId(i)).Distinct().ToList(); List <TextResource> texts = await _textRepository.Get(appIds, languageId); InstanceHelper.ReplaceTextKeys(authorizedInstances, texts, languageId); return(Ok(authorizedInstances)); }
public async void AuthorizeMesseageBoxInstances_TC01_EmptyList() { // Arrange List <MessageBoxInstance> expected = new List <MessageBoxInstance>(); List <Instance> instances = new List <Instance>(); // Act List <MessageBoxInstance> actual = await _authzHelper.AuthorizeMesseageBoxInstances(CreateUserClaims(3), instances); // Assert Assert.Equal(expected, actual); }
public async Task <ActionResult> GetMessageBoxInstanceList(int instanceOwnerPartyId, [FromQuery] string state, [FromQuery] string language) { string[] allowedStates = { "active", "archived", "deleted" }; string[] acceptedLanguages = { "en", "nb", "nn" }; string languageId = "nb"; if (string.IsNullOrEmpty(state)) { return(BadRequest($"State is empty. Please provide on of: {string.Join(", ", allowedStates)}")); } state = state.ToLower(); if (!allowedStates.Contains(state)) { return(BadRequest($"Invalid instance state. Please provide on of: {string.Join(", ", allowedStates)}")); } if (language != null && acceptedLanguages.Contains(language.ToLower())) { languageId = language; } List <Instance> allInstances = await _instanceRepository.GetInstancesInStateOfInstanceOwner(instanceOwnerPartyId, state); if (allInstances.Count <= 0) { return(Ok(new List <MessageBoxInstance>())); } List <MessageBoxInstance> autorizedInstances = await _authorizationHelper.AuthorizeMesseageBoxInstances(HttpContext.User, allInstances); List <string> appIds = autorizedInstances.Select(i => InstanceHelper.GetAppId(i)).Distinct().ToList(); Dictionary <string, Dictionary <string, string> > appTitles = await _applicationRepository.GetAppTitles(appIds); List <MessageBoxInstance> messageBoxInstances = InstanceHelper.AddTitleToInstances(autorizedInstances, appTitles, languageId); return(Ok(messageBoxInstances)); }
public async Task <ActionResult> SearchMessageBoxInstances( [FromQuery(Name = "instanceOwner.partyId")] int instanceOwnerPartyId, [FromQuery] string appId, [FromQuery] bool includeActive, [FromQuery] bool includeArchived, [FromQuery] bool includeDeleted, [FromQuery] string lastChanged, [FromQuery] string created, [FromQuery] string searchString, [FromQuery] string archiveReference, [FromQuery] string language) { string[] acceptedLanguages = { "en", "nb", "nn" }; string languageId = "nb"; if (language != null && acceptedLanguages.Contains(language.ToLower())) { languageId = language.ToLower(); } Dictionary <string, StringValues> queryParams = QueryHelpers.ParseQuery(Request.QueryString.Value); if (!string.IsNullOrEmpty(archiveReference)) { if ((includeActive == includeArchived) && (includeActive == includeDeleted)) { includeActive = false; includeDeleted = true; includeArchived = true; } else if (includeActive && !includeArchived && !includeDeleted) { return(Ok(new List <MessageBoxInstance>())); } else if (includeActive && (includeArchived || includeDeleted)) { includeActive = false; } } GetStatusFromQueryParams(includeActive, includeArchived, includeDeleted, queryParams); queryParams.Add("sortBy", "desc:lastChanged"); if (!string.IsNullOrEmpty(searchString)) { StringValues applicationIds = await MatchStringToAppTitle(searchString); if (!applicationIds.Any() || (!string.IsNullOrEmpty(appId) && !applicationIds.Contains(appId))) { return(Ok(new List <MessageBoxInstance>())); } else if (string.IsNullOrEmpty(appId)) { queryParams.Add("appId", applicationIds); } queryParams.Remove(nameof(searchString)); } InstanceQueryResponse queryResponse = await _instanceRepository.GetInstancesFromQuery(queryParams, string.Empty, 100); if (queryResponse?.Exception != null) { if (queryResponse.Exception.StartsWith("Unknown query parameter")) { return(BadRequest(queryResponse.Exception)); } return(StatusCode(500, queryResponse.Exception)); } if (queryResponse == null || queryResponse.Count <= 0) { return(Ok(new List <MessageBoxInstance>())); } List <Instance> allInstances = queryResponse.Instances; allInstances.RemoveAll(i => i.VisibleAfter > DateTime.UtcNow); allInstances.ForEach(i => { if (i.Status.IsArchived || i.Status.IsSoftDeleted) { i.DueBefore = null; } }); List <MessageBoxInstance> authorizedInstances = await _authorizationHelper.AuthorizeMesseageBoxInstances(HttpContext.User, allInstances); List <string> appIds = authorizedInstances.Select(i => InstanceHelper.GetAppId(i)).Distinct().ToList(); List <TextResource> texts = await _textRepository.Get(appIds, languageId); InstanceHelper.ReplaceTextKeys(authorizedInstances, texts, languageId); return(Ok(authorizedInstances)); }