public async Task ReturnsMappedData() { var uniqueData = UNIQUE_PREFIX + nameof(ReturnsMappedData); using var app = _appFactory.Create(); var parentDirectoryId = await app.TestData.PageDirectories().AddAsync(uniqueData); var addDirectoryCommand = app.TestData.PageDirectories().CreateAddCommand(uniqueData, parentDirectoryId); var contentRepository = app.Services.GetContentRepositoryWithElevatedPermissions(); await contentRepository .PageDirectories() .AddAsync(addDirectoryCommand); var query = new GetPatchableCommandByIdQuery <UpdatePageDirectoryCommand>(addDirectoryCommand.OutputPageDirectoryId); var command = await contentRepository.ExecuteQueryAsync(query); using (new AssertionScope()) { command.Should().NotBeNull(); command.Name.Should().Be(addDirectoryCommand.Name); command.PageDirectoryId.Should().Be(addDirectoryCommand.OutputPageDirectoryId); } }
public async Task <UpdateCustomEntityDraftVersionCommand> ExecuteAsync(GetPatchableCommandByIdQuery <UpdateCustomEntityDraftVersionCommand> query, IExecutionContext executionContext) { var dbResult = await _dbContext .CustomEntityVersions .Include(v => v.CustomEntity) .AsNoTracking() .FilterActive() .FilterByCustomEntityId(query.Id) .Where(v => v.WorkFlowStatusId == (int)WorkFlowStatus.Draft) .SingleOrDefaultAsync(); if (dbResult == null) { return(null); } _permissionValidationService.EnforceCustomEntityPermission <CustomEntityReadPermission>(dbResult.CustomEntity.CustomEntityDefinitionCode, executionContext.UserContext); var command = new UpdateCustomEntityDraftVersionCommand() { CustomEntityDefinitionCode = dbResult.CustomEntity.CustomEntityDefinitionCode, CustomEntityId = dbResult.CustomEntityId, Title = dbResult.Title }; var definitionQuery = new GetCustomEntityDefinitionSummaryByCodeQuery(command.CustomEntityDefinitionCode); var definition = await _queryExecutor.ExecuteAsync(definitionQuery, executionContext); EntityNotFoundException.ThrowIfNull(definition, command.CustomEntityDefinitionCode); command.Model = (ICustomEntityDataModel)_dbUnstructuredDataSerializer.Deserialize(dbResult.SerializedData, definition.DataModelType); return(command); }
public async Task <UpdateRoleCommand> ExecuteAsync(GetPatchableCommandByIdQuery <UpdateRoleCommand> query, IExecutionContext executionContext) { var role = await _internalRoleRepository.GetByIdAsync(query.Id); if (role == null) { return(null); } var command = new UpdateRoleCommand() { RoleId = role.RoleId, Title = role.Title }; command.Permissions = role .Permissions .Select(p => new PermissionCommandData() { EntityDefinitionCode = p.GetUniqueIdentifier(), PermissionCode = p is IEntityPermission ? ((IEntityPermission)p).EntityDefinition.EntityDefinitionCode : null }) .ToArray(); return(command); }
public async Task <UpdatePageCommand> ExecuteAsync(GetPatchableCommandByIdQuery <UpdatePageCommand> query, IExecutionContext executionContext) { var dbResult = await _dbContext .Pages .AsNoTracking() .Include(p => p.PageTags) .ThenInclude(t => t.Tag) .FilterActive() .FilterById(query.Id) .SingleOrDefaultAsync(); if (dbResult == null) { return(null); } var command = new UpdatePageCommand() { PageId = dbResult.PageId, Tags = dbResult .PageTags .Select(t => t.Tag.TagText) .OrderBy(t => t) .ToArray() }; return(command); }
public async Task <UpdateDocumentAssetCommand> ExecuteAsync(GetPatchableCommandByIdQuery <UpdateDocumentAssetCommand> query, IExecutionContext executionContext) { var dbResult = await _dbContext .DocumentAssets .Include(a => a.DocumentAssetTags) .ThenInclude(a => a.Tag) .AsNoTracking() .FilterById(query.Id) .SingleOrDefaultAsync(); if (dbResult == null) { return(null); } var result = new UpdateDocumentAssetCommand() { Description = dbResult.Description, DocumentAssetId = dbResult.DocumentAssetId, Title = dbResult.Title }; result.Tags = dbResult .DocumentAssetTags .Select(t => t.Tag.TagText) .OrderBy(t => t) .ToArray(); return(result); }
public async Task ReturnsMappedData() { var uniqueData = UNIQUE_PREFIX + nameof(ReturnsMappedData); using var app = _appFactory.Create(); var directoryId = await app.TestData.PageDirectories().AddAsync(uniqueData); var addPageCommand = app.TestData.Pages().CreateAddCommand(uniqueData, directoryId); addPageCommand.Tags = new string[] { UNIQUE_PREFIX + "1", app.SeededEntities.TestTag.TagText }; var contentRepository = app.Services.GetContentRepositoryWithElevatedPermissions(); await contentRepository .Pages() .AddAsync(addPageCommand); var query = new GetPatchableCommandByIdQuery <UpdatePageCommand>(addPageCommand.OutputPageId); var command = await contentRepository.ExecuteQueryAsync(query); using (new AssertionScope()) { command.Should().NotBeNull(); command.PageId.Should().Be(addPageCommand.OutputPageId); command.Tags.Should().OnlyContain(t => addPageCommand.Tags.Contains(t)); } }
/// <summary> /// Patches a command to modify the current state and then executes it. /// </summary> /// <typeparam name="TCommand">Type of command to patch and execute.</typeparam> /// <param name="id"> /// The integer database identifier of the entity associated with /// patchable command. /// </param> /// <param name="commandPatcher"> /// An action to configure or "patch" a command that's been initialized /// with existing data. /// </param> public static async Task PatchCommandAsync <TCommand>(this IDomainRepository repository, int id, Action <TCommand> commandPatcher) where TCommand : IPatchableByIdCommand { var query = new GetPatchableCommandByIdQuery <TCommand>(id); var command = await repository.ExecuteQueryAsync(query); commandPatcher(command); await repository.ExecuteCommandAsync(command); }
public async Task WhenRootDirectory_Throws() { using var app = _appFactory.Create(); var rootDirectoryId = await app.TestData.PageDirectories().GetRootDirectoryIdAsync(); var query = new GetPatchableCommandByIdQuery <UpdatePageDirectoryCommand>(rootDirectoryId); var contentRepository = app.Services.GetContentRepositoryWithElevatedPermissions(); await contentRepository .Awaiting(r => r.ExecuteQueryAsync(query)) .Should() .ThrowAsync <NotPermittedException>(); }
public async Task <JsonResult> RunCommandAsync <TCommand>(int id, IDelta <TCommand> delta) where TCommand : class, IPatchableByIdCommand { var query = new GetPatchableCommandByIdQuery <TCommand>(id); var command = await _queryExecutor.ExecuteAsync(query); if (delta != null) { delta.Patch(command); } return(await RunCommandAsync(command)); }
public async Task <JsonResult> Get(int customEntityVersionPageBlockId, CustomEntityVersionPageBlocksActionDataType dataType = CustomEntityVersionPageBlocksActionDataType.RenderDetails) { if (dataType == CustomEntityVersionPageBlocksActionDataType.UpdateCommand) { var updateCommandQuery = new GetPatchableCommandByIdQuery <UpdateCustomEntityVersionPageBlockCommand>(customEntityVersionPageBlockId); return(await _apiResponseHelper.RunQueryAsync(updateCommandQuery)); } var query = new GetCustomEntityVersionPageBlockRenderDetailsByIdQuery() { CustomEntityVersionPageBlockId = customEntityVersionPageBlockId, PublishStatus = PublishStatusQuery.Latest }; return(await _apiResponseHelper.RunQueryAsync(query)); }
public async Task <UpdateUserCommand> ExecuteAsync(GetPatchableCommandByIdQuery <UpdateUserCommand> query, IExecutionContext executionContext) { var dbUser = await _dbContext .Users .AsNoTracking() .FilterNotDeleted() .FilterNotSystemAccount() .FilterById(query.Id) .SingleOrDefaultAsync(); if (dbUser == null) { return(null); } if (dbUser.UserAreaCode == CofoundryAdminUserArea.Code) { _permissionValidationService.EnforceCurrentUserOrHasPermission <CofoundryUserReadPermission>(query.Id, executionContext.UserContext); } else { _permissionValidationService.EnforceCurrentUserOrHasPermission <NonCofoundryUserReadPermission>(query.Id, executionContext.UserContext); } var userArea = _userAreaDefinitionRepository.GetByCode(dbUser.UserAreaCode); var user = new UpdateUserCommand() { Email = dbUser.Email, FirstName = dbUser.FirstName, LastName = dbUser.LastName, RequirePasswordChange = dbUser.RequirePasswordChange, RoleId = dbUser.RoleId, UserId = dbUser.UserId, IsAccountVerified = dbUser.AccountVerifiedDate.HasValue, IsActive = !dbUser.DeactivatedDate.HasValue }; if (!userArea.UseEmailAsUsername) { user.Username = dbUser.Username; } return(user); }
public async Task <JsonResult> PatchDraft(int pageId, [FromBody] IDelta <UpdatePageDraftVersionCommand> delta) { // Custom patching because we may need to create a draft version first var query = new GetPatchableCommandByIdQuery <UpdatePageDraftVersionCommand>(pageId); var command = await _domainRepository.ExecuteQueryAsync(query); if (command == null) { var createDraftCommand = new AddPageDraftVersionCommand(); createDraftCommand.PageId = pageId; await _domainRepository.ExecuteCommandAsync(createDraftCommand); command = await _domainRepository.ExecuteQueryAsync(query); } delta.Patch(command); return(await _apiResponseHelper.RunCommandAsync(command)); }
public async Task <UpdatePageDirectoryAccessRuleSetCommand> ExecuteAsync(GetPatchableCommandByIdQuery <UpdatePageDirectoryAccessRuleSetCommand> query, IExecutionContext executionContext) { var dbPageDirectory = await _dbContext .PageDirectories .AsNoTracking() .Include(r => r.AccessRules) .FilterById(query.Id) .SingleOrDefaultAsync(); if (dbPageDirectory == null) { return(null); } var violationAction = EnumParser.ParseOrNull <AccessRuleViolationAction>(dbPageDirectory.AccessRuleViolationActionId); if (!violationAction.HasValue) { throw new InvalidOperationException($"{nameof(AccessRuleViolationAction)} of value {dbPageDirectory.AccessRuleViolationActionId} could not be parsed on a page directory with an id of {dbPageDirectory.PageDirectoryId}."); } var command = new UpdatePageDirectoryAccessRuleSetCommand() { PageDirectoryId = dbPageDirectory.PageDirectoryId, UserAreaCodeForSignInRedirect = dbPageDirectory.UserAreaCodeForSignInRedirect, ViolationAction = violationAction.Value }; command.AccessRules = dbPageDirectory .AccessRules .Select(r => new UpdatePageDirectoryAccessRuleSetCommand.AddOrUpdatePageDirectoryAccessRuleCommand() { PageDirectoryAccessRuleId = r.PageDirectoryAccessRuleId, UserAreaCode = r.UserAreaCode, RoleId = r.RoleId }) .ToList(); return(command); }
public async Task <UpdatePageVersionBlockCommand> ExecuteAsync(GetPatchableCommandByIdQuery <UpdatePageVersionBlockCommand> query, IExecutionContext executionContext) { var dbResult = await _dbContext .PageVersionBlocks .AsNoTracking() .Where(b => b.PageVersionBlockId == query.Id) .Select(b => new { PageBlock = b, BlockTypeFileName = b.PageBlockType.FileName }) .SingleOrDefaultAsync(); if (dbResult == null) { return(null); } var result = Map(dbResult.PageBlock, dbResult.BlockTypeFileName); return(result); }
public async Task <UpdatePageDraftVersionCommand> ExecuteAsync(GetPatchableCommandByIdQuery <UpdatePageDraftVersionCommand> query, IExecutionContext executionContext) { var command = await _dbContext .PageVersions .AsNoTracking() .FilterActive() .FilterByPageId(query.Id) .Where(p => p.WorkFlowStatusId == (int)WorkFlowStatus.Draft) .Select(v => new UpdatePageDraftVersionCommand { MetaDescription = v.MetaDescription, OpenGraphDescription = v.OpenGraphDescription, OpenGraphImageId = v.OpenGraphImageId, OpenGraphTitle = v.OpenGraphTitle, PageId = v.PageId, ShowInSiteMap = !v.ExcludeFromSitemap, Title = v.Title }) .SingleOrDefaultAsync(); return(command); }
public async Task <UpdatePageDirectoryCommand> ExecuteAsync(GetPatchableCommandByIdQuery <UpdatePageDirectoryCommand> query, IExecutionContext executionContext) { var dbResult = await _dbContext .PageDirectories .AsNoTracking() .FilterById(query.Id) .SingleOrDefaultAsync(); EntityNotFoundException.ThrowIfNull(dbResult, query.Id); if (!dbResult.ParentPageDirectoryId.HasValue) { throw new NotPermittedException("The root directory cannot be updated."); } var command = new UpdatePageDirectoryCommand() { Name = dbResult.Name, PageDirectoryId = dbResult.PageDirectoryId }; return(command); }
public async Task ReturnsMappedData() { var uniqueData = UNIQUE_PREFIX + nameof(ReturnsMappedData); using var app = _appFactory.Create(); var directoryId = await app.TestData.PageDirectories().AddAsync(uniqueData); var addPageCommand = app.TestData.Pages().CreateAddCommand(uniqueData, directoryId); addPageCommand.MetaDescription = uniqueData + " Meta"; addPageCommand.OpenGraphDescription = uniqueData + "OG Desc"; addPageCommand.OpenGraphImageId = app.SeededEntities.TestImageId; addPageCommand.OpenGraphTitle = uniqueData + "OG Title"; var contentRepository = app.Services.GetContentRepositoryWithElevatedPermissions(); await contentRepository .Pages() .AddAsync(addPageCommand); var query = new GetPatchableCommandByIdQuery <UpdatePageDraftVersionCommand>(addPageCommand.OutputPageId); var command = await contentRepository.ExecuteQueryAsync(query); using (new AssertionScope()) { command.Should().NotBeNull(); command.PageId.Should().Be(addPageCommand.OutputPageId); command.MetaDescription.Should().Be(command.MetaDescription); command.OpenGraphDescription.Should().Be(command.OpenGraphDescription); command.OpenGraphImageId.Should().Be(command.OpenGraphImageId); command.OpenGraphTitle.Should().Be(command.OpenGraphTitle); command.Publish.Should().BeFalse(); command.PublishDate.Should().BeNull(); command.ShowInSiteMap.Should().Be(command.ShowInSiteMap); command.Title.Should().Be(command.Title); } }
public async Task <UpdateCustomEntityVersionPageBlockCommand> ExecuteAsync(GetPatchableCommandByIdQuery <UpdateCustomEntityVersionPageBlockCommand> query, IExecutionContext executionContext) { var dbResult = await _dbContext .CustomEntityVersionPageBlocks .AsNoTracking() .Where(b => b.CustomEntityVersionPageBlockId == query.Id) .Select(b => new { PageBlock = b, PageBlockTypeFileName = b.PageBlockType.FileName, CustomEntityDefinitionCode = b.CustomEntityVersion.CustomEntity.CustomEntityDefinitionCode }) .SingleOrDefaultAsync(); if (dbResult == null) { return(null); } _permissionValidationService.EnforceCustomEntityPermission <CustomEntityReadPermission>(dbResult.CustomEntityDefinitionCode, executionContext.UserContext); var result = Map(dbResult.PageBlock, dbResult.PageBlockTypeFileName); return(result); }
public IEnumerable <IPermissionApplication> GetPermissions(GetPatchableCommandByIdQuery <UpdateImageAssetCommand> query) { yield return(new ImageAssetUpdatePermission()); }
public IEnumerable <IPermissionApplication> GetPermissions(GetPatchableCommandByIdQuery <UpdateDocumentAssetCommand> query) { yield return(new DocumentAssetReadPermission()); }
public IEnumerable <IPermissionApplication> GetPermissions(GetPatchableCommandByIdQuery <UpdateRoleCommand> command) { yield return(new RoleReadPermission()); }
public IEnumerable <IPermissionApplication> GetPermissions(GetPatchableCommandByIdQuery <UpdatePageDraftVersionCommand> query) { yield return(new PageReadPermission()); }
public IEnumerable <IPermissionApplication> GetPermissions(GetPatchableCommandByIdQuery <UpdatePageDirectoryCommand> command) { yield return(new PageDirectoryReadPermission()); }