public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { if (interceptionContext.DbContexts.Contains(_context, ReferenceEquals)) { _commandTrees.Add(interceptionContext.Result); } }
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { var dbCommandTreeKind = interceptionContext.Result.CommandTreeKind; var context = interceptionContext.DbContexts.First(); var auditContenxt = new LogContext(); switch (dbCommandTreeKind) { case DbCommandTreeKind.Insert: var addedEntries = context.ChangeTracker.Entries() .Where(e => e.State == EntityState.Added && e.IsAttr<AuditableAttribute>()) .ToList(); foreach (var entry in addedEntries) { auditContenxt.ApplyAuditLog(context, entry); } break; case DbCommandTreeKind.Update: case DbCommandTreeKind.Delete: var entries = context.ChangeTracker.Entries().Where( e => (e.State == EntityState.Deleted || e.State == EntityState.Modified) && e.IsAttr<AuditableAttribute>()).ToList(); foreach (var entry in entries) { auditContenxt.ApplyAuditLog(context, entry); } break; } }
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { if (interceptionContext.OriginalResult.DataSpace == DataSpace.SSpace) { var queryCommand = interceptionContext.Result as DbQueryCommandTree; if (queryCommand != null) { var newQuery = queryCommand.Query.Accept(new SoftDeleteQueryVisitor()); interceptionContext.Result = new DbQueryCommandTree( queryCommand.MetadataWorkspace, queryCommand.DataSpace, newQuery ); } var deleteCommand = interceptionContext.OriginalResult as DbDeleteCommandTree; if (deleteCommand != null) { var column = SoftDeleteAttribute.GetSoftDeleteColumnName(deleteCommand.Target.VariableType.EdmType); if (column != null) { var setClause = DbExpressionBuilder.SetClause( deleteCommand.Target.VariableType.Variable(deleteCommand.Target.VariableName).Property(column), DbExpression.FromBoolean(true)); var update = new DbUpdateCommandTree( deleteCommand.MetadataWorkspace, deleteCommand.DataSpace, deleteCommand.Target, deleteCommand.Predicate, new List<DbModificationClause> { setClause }.AsReadOnly(), null); interceptionContext.Result = update; } } } }
public void New_interception_context_has_no_state() { var interceptionContext = new DbCommandTreeInterceptionContext(); Assert.Empty(interceptionContext.ObjectContexts); Assert.Empty(interceptionContext.DbContexts); Assert.Equal(null, interceptionContext.Result); Assert.Equal(null, interceptionContext.OriginalResult); }
public void TreeCreated(DbCommandTree commandTree, DbCommandTreeInterceptionContext interceptionContext) { Assert.NotEmpty(interceptionContext.DbContexts); if (_context == null || interceptionContext.DbContexts.Contains(_context)) { _log.Add(Tuple.Create(commandTree, interceptionContext)); } }
/// <summary> /// This method is called after a new DbCommandTree has been created. /// It collects only DbQueryCommandTrees. /// </summary> /// <param name="interceptionContext">Represents contextual information associated with calls into IDbCommandTreeInterceptor implementations.</param> public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { if (interceptionContext.Result.CommandTreeKind == DbCommandTreeKind.Query && interceptionContext.Result.DataSpace == DataSpace.SSpace) { if (interceptionContext.ObjectContexts.Contains(_objectContext, ReferenceEquals)) { DbQueryCommandTree = (DbQueryCommandTree)interceptionContext.Result; } } }
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { if (interceptionContext.OriginalResult.DataSpace == DataSpace.SSpace) { var queryCommand = interceptionContext.Result as DbQueryCommandTree; if (queryCommand != null) { var newQuery = queryCommand.Query.Accept(new SoftDeleteQueryVisitor()); interceptionContext.Result = new DbQueryCommandTree( queryCommand.MetadataWorkspace, queryCommand.DataSpace, newQuery); } var deleteCommand = interceptionContext.OriginalResult as DbDeleteCommandTree; if (deleteCommand != null) { //Check if POCO has system Status flag if ( ((EntityType)deleteCommand.Target.VariableType.EdmType).DeclaredMembers.All( x => x.Name != "SystemStatus")) { return; } // Just because the entity has the soft delete annotation doesn't mean that // this particular table has the column. This occurs in situation like TPT // inheritance mapping and entity splitting where one type maps to multiple // tables. // If the table doesn't have the column we just want to leave the row unchanged // since it will be joined to the table that does have the column during query. // We can't no-op, so we just generate an UPDATE command that doesn't set anything. var setClauses = new List <DbModificationClause>(); var table = (EntityType)deleteCommand.Target.VariableType.EdmType; if (table.Properties.Any(p => p.Name == "SystemStatus")) { setClauses.Add(DbExpressionBuilder.SetClause( deleteCommand.Target.VariableType.Variable(deleteCommand.Target.VariableName) .Property("SystemStatus"), DbExpression.FromInt32((int)SystemStatus.Deleted))); } var update = new DbUpdateCommandTree( deleteCommand.MetadataWorkspace, deleteCommand.DataSpace, deleteCommand.Target, deleteCommand.Predicate, setClauses.AsReadOnly(), null); interceptionContext.Result = update; } } }
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { if (interceptionContext.OriginalResult.DataSpace == System.Data.Entity.Core.Metadata.Edm.DataSpace.SSpace) { if (interceptionContext.Result is DbQueryCommandTree) { var queryCommand = (DbQueryCommandTree)interceptionContext.Result; var newQuery = queryCommand.Query.Accept(new StringTrimmerQueryVisitor()); interceptionContext.Result = new DbQueryCommandTree(queryCommand.MetadataWorkspace, queryCommand.DataSpace, newQuery); } } }
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { if (interceptionContext.OriginalResult.DataSpace == DataSpace.SSpace) { var queryCommand = interceptionContext.Result as DbQueryCommandTree; if (queryCommand != null) { var newQuery = queryCommand.Query.Accept(new SoftDeleteQueryVisitor()); interceptionContext.Result = new DbQueryCommandTree(queryCommand.MetadataWorkspace, queryCommand.DataSpace, newQuery); } } }
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { if (interceptionContext.OriginalResult.DataSpace != DataSpace.SSpace) { return; } if (interceptionContext.Result.CommandTreeKind == DbCommandTreeKind.Insert) { InterceptInsertStatement(interceptionContext); } }
/// <summary> /// In case of an insert command we always assign the correct value to the tenantId /// </summary> private static bool InterceptInsertCommand(DbCommandTreeInterceptionContext interceptionContext, int userId) { var shoolcontextList = interceptionContext.DbContexts.OfType <SchoolContext>(); if (shoolcontextList.Any(x => x.IsSeeding)) { return(true); } var insertCommand = interceptionContext.Result as DbInsertCommandTree; if (insertCommand != null) { var column = TenantAwareAttribute.GetTenantColumnName(insertCommand.Target.VariableType.EdmType); if (!string.IsNullOrEmpty(column)) { // Create the variable reference in order to create the property var variableReference = DbExpressionBuilder.Variable(insertCommand.Target.VariableType, insertCommand.Target.VariableName); // Create the property to which will assign the correct value var tenantProperty = DbExpressionBuilder.Property(variableReference, column); // Create the set clause, object representation of sql insert command var tenantSetClause = DbExpressionBuilder.SetClause(tenantProperty, DbExpression.FromInt32(userId)); // Remove potential assignment of tenantId for extra safety var filteredSetClauses = insertCommand.SetClauses.Cast <DbSetClause>() .Where(sc => ((DbPropertyExpression)sc.Property).Property.Name != column); // Construct the final clauses, object representation of sql insert command values var finalSetClauses = new ReadOnlyCollection <DbModificationClause>(new List <DbModificationClause>(filteredSetClauses) { tenantSetClause }); // Construct the new command var newInsertCommand = new DbInsertCommandTree( insertCommand.MetadataWorkspace, insertCommand.DataSpace, insertCommand.Target, finalSetClauses, insertCommand.Returning); interceptionContext.Result = newInsertCommand; // True means an interception successfully happened so there is no need to continue return(true); } } return(false); }
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { if (interceptionContext.OriginalResult.DataSpace == DataSpace.SSpace) { var queryCommand = interceptionContext.Result as DbQueryCommandTree; if (queryCommand != null) { var newQuery = queryCommand.Query.Accept(new SoftDeleteQueryVisitor()); interceptionContext.Result = new DbQueryCommandTree( queryCommand.MetadataWorkspace, queryCommand.DataSpace, newQuery); } var deleteCommand = interceptionContext.OriginalResult as DbDeleteCommandTree; if (deleteCommand != null) { var column = SoftDeleteAttribute.GetSoftDeleteColumnName(deleteCommand.Target.VariableType.EdmType); if (column != null) { // Just because the entity has the soft delete annotation doesn't mean that // this particular table has the column. This occurs in situation like TPT // inheritance mapping and entity splitting where one type maps to multiple // tables. // If the table doesn't have the column we just want to leave the row unchanged // since it will be joined to the table that does have the column during query. // We can't no-op, so we just generate an UPDATE command that doesn't set anything. var setClauses = new List<DbModificationClause>(); var table = (EntityType)deleteCommand.Target.VariableType.EdmType; if (table.Properties.Any(p => p.Name == column)) { setClauses.Add(DbExpressionBuilder.SetClause( DbExpressionBuilder.Property(DbExpressionBuilder.Variable(deleteCommand.Target.VariableType, deleteCommand.Target.VariableName), column), DbExpression.FromBoolean(true))); } var update = new DbUpdateCommandTree( deleteCommand.MetadataWorkspace, deleteCommand.DataSpace, deleteCommand.Target, deleteCommand.Predicate, setClauses.AsReadOnly(), null); interceptionContext.Result = update; } } } }
/// <summary> /// In case of an update command we always filter based on the userId /// </summary> private static bool InterceptUpdate(DbCommandTreeInterceptionContext interceptionContext) { var updateCommand = interceptionContext.Result as DbUpdateCommandTree; if (updateCommand != null) { var column = UserAwareAttribute.GetUserColumnName(updateCommand.Target.VariableType.EdmType); if (!string.IsNullOrEmpty(column)) { // Validate user Security.ValidateCurrentUser(); // Get the userId (throw an exception if there is none) var userId = GetCurrentUserId(); // Create the variable reference in order to create the property var variableReference = DbExpressionBuilder.Variable(updateCommand.Target.VariableType, updateCommand.Target.VariableName); // Create the property to which will assign the correct value var userProperty = DbExpressionBuilder.Property(variableReference, column); // Create the userId where predicate, object representation of sql where userId = value statement var userIdWherePredicate = DbExpressionBuilder.Equal(userProperty, DbExpression.FromInt32(userId)); // Remove potential assignment of userId for extra safety var filteredSetClauses = updateCommand.SetClauses.Cast <DbSetClause>() .Where(sc => ((DbPropertyExpression)sc.Property).Property.Name != column); // Construct the final clauses, object representation of sql insert command values var finalSetClauses = new ReadOnlyCollection <DbModificationClause>(new List <DbModificationClause>(filteredSetClauses)); // The initial predicate is the sql where statement var initialPredicate = updateCommand.Predicate; // Add to the initial statement the userId statement which translates in sql AND UserId = 'value' var finalPredicate = initialPredicate.And(userIdWherePredicate); var newUpdateCommand = new DbUpdateCommandTree( updateCommand.MetadataWorkspace, updateCommand.DataSpace, updateCommand.Target, finalPredicate, finalSetClauses, updateCommand.Returning); interceptionContext.Result = newUpdateCommand; // True means an interception successfully happened so there is no need to continue return(true); } } return(false); }
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { if (interceptionContext.OriginalResult.DataSpace != DataSpace.SSpace) { return; } var queryCommand = interceptionContext.Result as DbQueryCommandTree; if (queryCommand != null) { var newQuery = queryCommand.Query.Accept(new SoftDeleteQueryVisitor()); interceptionContext.Result = new DbQueryCommandTree( queryCommand.MetadataWorkspace, queryCommand.DataSpace, newQuery); } var deleteCommand = interceptionContext.OriginalResult as DbDeleteCommandTree; if (deleteCommand == null) { return; } var column = SoftDeleteAttribute.GetSoftDeleteColumnName(deleteCommand.Target.VariableType.EdmType); if (column == null) { return; } var setClauses = new List <DbModificationClause>(); var table = (EntityType)deleteCommand.Target.VariableType.EdmType; if (table.Properties.Any(p => p.Name == column)) { setClauses.Add(DbExpressionBuilder.SetClause( deleteCommand.Target.VariableType.Variable(deleteCommand.Target.VariableName).Property(column), DbExpression.FromBoolean(true))); } var update = new DbUpdateCommandTree( deleteCommand.MetadataWorkspace, deleteCommand.DataSpace, deleteCommand.Target, deleteCommand.Predicate, setClauses.AsReadOnly(), null); interceptionContext.Result = update; }
/// <summary> /// In case of query command change the query by adding a filtering based on userId /// </summary> static bool InterceptQueryCommand(DbCommandTreeInterceptionContext interceptionContext) { if (interceptionContext.Result is DbQueryCommandTree queryCommand) { var newQuery = queryCommand.Query.Accept(new UserQueryVisitor()); interceptionContext.Result = new DbQueryCommandTree( queryCommand.MetadataWorkspace, queryCommand.DataSpace, newQuery); return(true); } return(false); }
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { var dataSpace = interceptionContext.OriginalResult.DataSpace; if (dataSpace != DataSpace.SSpace) { return; } if (interceptionContext.Result is DbQueryCommandTree queryCommand) { interceptionContext.Result = HandleQueryCommand(queryCommand); } }
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { if (interceptionContext.OriginalResult.DataSpace != DataSpace.SSpace) { return; } var deleteCommand = interceptionContext.OriginalResult as DbDeleteCommandTree; if (deleteCommand != null) { interceptionContext.Result = HandleDeleteCommand(deleteCommand); } }
/// <summary> /// In case of an update command we always filter based on the tenantId /// </summary> private static bool InterceptUpdate(DbCommandTreeInterceptionContext interceptionContext, string tenantValue) { var updateCommand = interceptionContext.Result as DbUpdateCommandTree; if (updateCommand == null) { return(false); } var column = TenantAttribute.GetTenantColumnName(updateCommand.Target.VariableType.EdmType); if (string.IsNullOrEmpty(column)) { return(false); } // Create the variable reference in order to create the property var variableReference = updateCommand.Target.VariableType.Variable(updateCommand.Target.VariableName); // Create the property to which will assign the correct value var tenantProperty = variableReference.Property(column); // Create the tenantId where predicate, object representation of sql where tenantId = value statement var tenantIdWherePredicate = tenantProperty.Equal(DbExpression.FromString(tenantValue)); // Remove potential assignment of tenantId for extra safety var filteredSetClauses = updateCommand.SetClauses.Cast <DbSetClause>() .Where(sc => ((DbPropertyExpression)sc.Property).Property.Name != column); // Construct the final clauses, object representation of sql insert command values var finalSetClauses = new ReadOnlyCollection <DbModificationClause>(new List <DbModificationClause>(filteredSetClauses)); // The initial predicate is the sql where statement var initialPredicate = updateCommand.Predicate; // Add to the initial statement the tenantId statement which translates in sql AND TenantId = 'value' var finalPredicate = initialPredicate.And(tenantIdWherePredicate); var newUpdateCommand = new DbUpdateCommandTree( updateCommand.MetadataWorkspace, updateCommand.DataSpace, updateCommand.Target, finalPredicate, finalSetClauses, updateCommand.Returning); interceptionContext.Result = newUpdateCommand; // True means an interception successfully happened so there is no need to continue return(true); }
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { if (interceptionContext.OriginalResult.DataSpace != DataSpace.SSpace) return; var queryCommand = interceptionContext.Result as DbQueryCommandTree; if (queryCommand == null) return; var newQuery = queryCommand.Query.Accept(new JobOpportunityIsActiveVisitor()); interceptionContext.Result = new DbQueryCommandTree( queryCommand.MetadataWorkspace, queryCommand.DataSpace, newQuery); }
/// <summary> /// In case of query command change the query by adding a filtering based on userId /// </summary> static bool InterceptQueryCommand(DbCommandTreeInterceptionContext interceptionContext) { var queryCommand = interceptionContext.Result as DbQueryCommandTree; if (queryCommand != null) { var newQuery = queryCommand.Query.Accept(new UserQueryVisitor()); interceptionContext.Result = new DbQueryCommandTree( queryCommand.MetadataWorkspace, queryCommand.DataSpace, newQuery); return true; } return false; }
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { if (interceptionContext.OriginalResult.DataSpace == DataSpace.SSpace && interceptionContext.Result is DbQueryCommandTree queryCommand) { var newQuery = queryCommand.Query.Accept(new GuidToStringComparisonRewriter()); if (newQuery != queryCommand.Query) { interceptionContext.Result = new DbQueryCommandTree( queryCommand.MetadataWorkspace, queryCommand.DataSpace, newQuery); } } }
public void TreeCreated(DbCommandTreeInterceptionContext context) { if (context.OriginalResult.DataSpace == DataSpace.SSpace) { switch (context.Result.CommandTreeKind) { case DbCommandTreeKind.Insert: context.Result = InsertCommandTree(context.Result); break; case DbCommandTreeKind.Update: context.Result = UpdateCommandTree(context.Result); break; } } }
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { if (ShouldHandle(interceptionContext)) { switch (interceptionContext.OriginalResult) { case DbInsertCommandTree insertCommand: interceptionContext.Result = HandleInsertCommand(insertCommand); return; case DbUpdateCommandTree updateCommand: interceptionContext.Result = HandleUpdateCommand(updateCommand); return; } } }
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { if (interceptionContext.OriginalResult.DataSpace == DataSpace.SSpace) { var queryCommand = interceptionContext.Result as DbQueryCommandTree; if (queryCommand != null) { var context = (DataContext)interceptionContext.DbContexts.Single(c => c is DataContext); if (context.SoftDeleteFilterIsActive) { var newQuery = queryCommand.Query.Accept(new SoftDeleteQueryVisitor()); interceptionContext.Result = new DbQueryCommandTree( queryCommand.MetadataWorkspace, queryCommand.DataSpace, newQuery); } } var deleteCommand = interceptionContext.OriginalResult as DbDeleteCommandTree; if (deleteCommand != null) { var column = SoftDeleteAttribute.GetSoftDeleteColumnName(deleteCommand.Target.VariableType.EdmType); if (column != null) { var setClause = DbExpressionBuilder.SetClause( DbExpressionBuilder.Property( DbExpressionBuilder.Variable(deleteCommand.Target.VariableType, deleteCommand.Target.VariableName), column), DbExpression.FromBoolean(true)); var update = new DbUpdateCommandTree( deleteCommand.MetadataWorkspace, deleteCommand.DataSpace, deleteCommand.Target, deleteCommand.Predicate, new List <DbModificationClause> { setClause }.AsReadOnly(), null); interceptionContext.Result = update; } } } }
public void Result_can_be_mutated() { var interceptionContext = new DbCommandTreeInterceptionContext(); Assert.Null(interceptionContext.Result); Assert.Null(interceptionContext.OriginalResult); var commandTree = new Mock<DbCommandTree>().Object; interceptionContext.MutableData.SetExecuted(commandTree); Assert.Same(commandTree, interceptionContext.Result); Assert.Same(commandTree, interceptionContext.OriginalResult); var commandTree2 = new Mock<DbCommandTree>().Object; interceptionContext.Result = commandTree2; Assert.Same(commandTree2, interceptionContext.Result); Assert.Same(commandTree, interceptionContext.OriginalResult); }
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { if (interceptionContext.OriginalResult.DataSpace == DataSpace.CSpace) { var queryCommand = interceptionContext.Result as DbQueryCommandTree; if (queryCommand != null) { var context = interceptionContext.DbContexts.FirstOrDefault(); if (context != null) { var newQuery = queryCommand.Query.Accept(new FilterQueryVisitor(context)); interceptionContext.Result = new DbQueryCommandTree( queryCommand.MetadataWorkspace, queryCommand.DataSpace, newQuery); } } } }
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { if (interceptionContext.OriginalResult.DataSpace == DataSpace.CSpace) { var queryCommand = interceptionContext.Result as DbQueryCommandTree; if (queryCommand != null) { var context = interceptionContext.DbContexts.FirstOrDefault(); if (context != null) { var newQuery = queryCommand.Query.Accept(new CategoryVisitor(context)); interceptionContext.Result = new DbQueryCommandTree( queryCommand.MetadataWorkspace, queryCommand.DataSpace, newQuery); } } } }
/// <summary> /// In case of an insert command we always assign the correct value to the tenantId /// </summary> private static bool InterceptInsertCommand(DbCommandTreeInterceptionContext interceptionContext, string userId) { var insertCommand = interceptionContext.Result as DbInsertCommandTree; if (insertCommand != null) { var column = TenantAwareAttribute.GetTenantColumnName(insertCommand.Target.VariableType.EdmType); if (!string.IsNullOrEmpty(column)) { // Create the variable reference in order to create the property var variableReference = DbExpressionBuilder.Variable(insertCommand.Target.VariableType, insertCommand.Target.VariableName); // Create the property to which will assign the correct value var tenantProperty = DbExpressionBuilder.Property(variableReference, column); // Create the set clause, object representation of sql insert command var tenantSetClause = DbExpressionBuilder.SetClause(tenantProperty, DbExpression.FromString(userId)); // Remove potential assignment of tenantId for extra safety var filteredSetClauses = insertCommand.SetClauses.Cast<DbSetClause>() .Where(sc => ((DbPropertyExpression)sc.Property).Property.Name != column); // Construct the final clauses, object representation of sql insert command values var finalSetClauses = new ReadOnlyCollection<DbModificationClause>(new List<DbModificationClause>(filteredSetClauses) { tenantSetClause }); // Construct the new command var newInsertCommand = new DbInsertCommandTree( insertCommand.MetadataWorkspace, insertCommand.DataSpace, insertCommand.Target, finalSetClauses, insertCommand.Returning); interceptionContext.Result = newInsertCommand; // True means an interception successfully happened so there is no need to continue return true; } } return false; }
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { if (interceptionContext.OriginalResult.DataSpace == DataSpace.SSpace) { // Check that there is an authenticated user in this context var identity = Thread.CurrentPrincipal.Identity as ClaimsIdentity; if (identity == null) { return; } var userClaim = identity.Claims.SingleOrDefault(c => c.Type == "Tenant"); if (userClaim == null) { return; } // In case of query command change the query by adding a filtering based on tenantId var queryCommand = interceptionContext.Result as DbQueryCommandTree; if (queryCommand != null) { var newQuery = queryCommand.Query.Accept(new TenantQueryVisitor()); interceptionContext.Result = new DbQueryCommandTree( queryCommand.MetadataWorkspace, queryCommand.DataSpace, newQuery); return; } var userId = System.Int32.Parse(userClaim.Value); //int userId = 1; if (InterceptInsertCommand(interceptionContext, userId)) { return; } if (InterceptUpdate(interceptionContext, userId)) { return; } InterceptDeleteCommand(interceptionContext, userId); } }
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { if (interceptionContext.OriginalResult.DataSpace == DataSpace.SSpace) { //This was Rowan Miller's example for filtering out IsDeleteds, but I'm going to try Jimmy Bogard's method instead. var queryCommand = interceptionContext.Result as DbQueryCommandTree; if (queryCommand != null) { var newQuery = queryCommand.Query.Accept(new SoftDeleteQueryVisitor()); interceptionContext.Result = new DbQueryCommandTree( queryCommand.MetadataWorkspace, queryCommand.DataSpace, newQuery); } var deleteCommand = interceptionContext.OriginalResult as DbDeleteCommandTree; if (deleteCommand != null) { var column = SoftDeleteAttribute.GetSoftDeleteColumnName(deleteCommand.Target.VariableType.EdmType); if (column != null) { var setClause = DbExpressionBuilder.SetClause( DbExpressionBuilder.Property( DbExpressionBuilder.Variable(deleteCommand.Target.VariableType, deleteCommand.Target.VariableName), column), DbExpression.FromBoolean(true)); var update = new DbUpdateCommandTree( deleteCommand.MetadataWorkspace, deleteCommand.DataSpace, deleteCommand.Target, deleteCommand.Predicate, new List <DbModificationClause> { setClause }.AsReadOnly(), null); interceptionContext.Result = update; } } } }
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { if (interceptionContext.OriginalResult.DataSpace == DataSpace.SSpace) { // Check that there is an authenticated user in this context var identity = Thread.CurrentPrincipal.Identity as ClaimsIdentity; if (identity == null) { return; } var userIdclaim = identity.Claims.SingleOrDefault(c => c.Type == ClaimTypes.NameIdentifier); if (userIdclaim == null) { return; } // In case of query command change the query by adding a filtering based on tenantId var queryCommand = interceptionContext.Result as DbQueryCommandTree; if (queryCommand != null) { var newQuery = queryCommand.Query.Accept(new TenantQueryVisitor()); interceptionContext.Result = new DbQueryCommandTree( queryCommand.MetadataWorkspace, queryCommand.DataSpace, newQuery); return; } var userId = userIdclaim.Value; if (InterceptInsertCommand(interceptionContext, userId)) { return; } if (InterceptUpdate(interceptionContext, userId)) { return; } InterceptDeleteCommand(interceptionContext, userId); } }
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { if (interceptionContext.OriginalResult.DataSpace != DataSpace.SSpace) { return; } var context = (ContextBase)interceptionContext.DbContexts.Where(c => typeof(ContextBase) == c.GetType().BaseType.BaseType).FirstOrDefault(); if (context == null || (context != null && context.options == null) || (context != null && context.options != null && !context.options.IncludeIsDelete)) { return; } var queryCommand = interceptionContext.Result as DbQueryCommandTree; if (queryCommand != null) { interceptionContext.Result = HandleQueryCommand(queryCommand); } }
public void Cloning_the_interception_context_preserves_contextual_information_but_not_mutable_state() { var objectContext = new ObjectContext(); var dbContext = CreateDbContext(objectContext); var interceptionContext = new DbCommandTreeInterceptionContext(); interceptionContext.MutableData.SetExecuted(new Mock<DbCommandTree>().Object); interceptionContext = interceptionContext .WithDbContext(dbContext) .WithObjectContext(objectContext) .AsAsync(); Assert.Equal(new[] { objectContext }, interceptionContext.ObjectContexts); Assert.Equal(new[] { dbContext }, interceptionContext.DbContexts); Assert.True(interceptionContext.IsAsync); Assert.Null(interceptionContext.Result); Assert.Null(interceptionContext.OriginalResult); }
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { if (interceptionContext.OriginalResult.DataSpace != DataSpace.SSpace) { return; } var queryCommand = interceptionContext.Result as DbQueryCommandTree; if (queryCommand == null) { return; } var newQuery = queryCommand.Query.Accept(new JobOpportunityIsActiveVisitor()); interceptionContext.Result = new DbQueryCommandTree( queryCommand.MetadataWorkspace, queryCommand.DataSpace, newQuery); }
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { if (interceptionContext.OriginalResult.DataSpace == DataSpace.SSpace) { var insertCommand = interceptionContext.Result as DbInsertCommandTree; if (insertCommand != null) { var namesToIgnore = new List<string> { "ValidFrom", "ValidTo" }; var props = new List<DbModificationClause>(insertCommand.SetClauses); props = props.Where(_ => !namesToIgnore.Contains((((_ as DbSetClause)?.Property as DbPropertyExpression)?.Property as EdmProperty)?.Name)).ToList(); var newSetClauses = new ReadOnlyCollection<DbModificationClause>(props); var newCommand = new DbInsertCommandTree( insertCommand.MetadataWorkspace, insertCommand.DataSpace, insertCommand.Target, newSetClauses, insertCommand.Returning); interceptionContext.Result = newCommand; }; } }
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { if (interceptionContext.OriginalResult.DataSpace != DataSpace.SSpace) { return; } var insertCommand = interceptionContext.Result as DbInsertCommandTree; if (insertCommand != null) { interceptionContext.Result = HandleInsertCommand(insertCommand); } var updateCommand = interceptionContext.OriginalResult as DbUpdateCommandTree; if (updateCommand != null) { interceptionContext.Result = HandleUpdateCommand(updateCommand); } }
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { if (interceptionContext.OriginalResult.DataSpace == DataSpace.SSpace) { if (InterceptQueryCommand(interceptionContext)) { return; } if (InterceptInsertCommand(interceptionContext)) { return; } if (InterceptUpdate(interceptionContext)) { return; } InterceptDeleteCommand(interceptionContext); } }
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { if (interceptionContext.OriginalResult.DataSpace == DataSpace.SSpace) { var queryCommand = interceptionContext.Result as DbQueryCommandTree; if (queryCommand != null) { var context = interceptionContext.DbContexts.FirstOrDefault(); if (context != null) { var newQuery = queryCommand.Query.Accept(new DynamicFilterQueryVisitor(context)); interceptionContext.Result = new DbQueryCommandTree( queryCommand.MetadataWorkspace, queryCommand.DataSpace, newQuery); } } // Can also check for other command types such as DbDeleteCommandTree and DbUpdateCommandTree to change // their behaviors as well. See https://github.com/rowanmiller/Demo-TechEd2014/blob/master/FakeEstate.ListingManager/Models/EFHelpers/SoftDeleteInterceptor.cs } }
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { Log(interceptionContext.OriginalResult.GetType().Name); if (interceptionContext.OriginalResult is DbQueryCommandTree) { } else if (interceptionContext.OriginalResult is DbFunctionCommandTree) { } else if (interceptionContext.OriginalResult is DbInsertCommandTree) { } else if (interceptionContext.OriginalResult is DbModificationCommandTree) { } else if (interceptionContext.OriginalResult is DbQueryCommandTree) { } else if (interceptionContext.OriginalResult is DbUpdateCommandTree) { } }
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { var dbCommandTreeKind = interceptionContext.Result.CommandTreeKind; var context = interceptionContext.DbContexts.First(); using (var auditContext = AuditLogStorage.Current.GetConnection()) { switch (dbCommandTreeKind) { case DbCommandTreeKind.Update: case DbCommandTreeKind.Delete: var entries = context.ChangeTracker.Entries().Where( e => (e.State == EntityState.Deleted || e.State == EntityState.Modified) && e.IsAttr <AuditableAttribute>()).ToList(); foreach (var entry in entries) { ApplyAuditLog(auditContext, context, entry); } break; } } }
/// <summary> /// In case of a delete command we always filter based on the userId /// </summary> private static void InterceptDeleteCommand(DbCommandTreeInterceptionContext interceptionContext) { var deleteCommand = interceptionContext.Result as DbDeleteCommandTree; if (deleteCommand != null) { var column = UserAwareAttribute.GetUserColumnName(deleteCommand.Target.VariableType.EdmType); if (!string.IsNullOrEmpty(column)) { // Validate user Security.ValidateCurrentUser(); // Get the userId (throw an exception if there is none) var userId = GetCurrentUserId(); // Create the variable reference in order to create the property var variableReference = DbExpressionBuilder.Variable(deleteCommand.Target.VariableType, deleteCommand.Target.VariableName); // Create the property to which will assign the correct value var userProperty = DbExpressionBuilder.Property(variableReference, column); var userIdWherePredicate = DbExpressionBuilder.Equal(userProperty, DbExpression.FromInt32(userId)); // The initial predicate is the sql where statement var initialPredicate = deleteCommand.Predicate; // Add to the initial statement the userId statement which translates in sql AND userId = 'value' var finalPredicate = initialPredicate.And(userIdWherePredicate); var newDeleteCommand = new DbDeleteCommandTree( deleteCommand.MetadataWorkspace, deleteCommand.DataSpace, deleteCommand.Target, finalPredicate); interceptionContext.Result = newDeleteCommand; } } }
private void InterceptInsertStatement(DbCommandTreeInterceptionContext interceptionContext) { var insertCommand = interceptionContext.Result as DbInsertCommandTree; List <DbModificationClause> finalSetClauses = new List <DbModificationClause>((IEnumerable <DbModificationClause>)insertCommand.SetClauses); //TENANT AWARE string column = "TenantId"; DbExpression newValue = "JustMe"; // Here we should insert the right value // TODO: Need to check if this entity is a Multitenant entity in the right way // You can use the attribute like in the original sample if ( interceptionContext.ObjectContexts.Count() == 1 && interceptionContext.ObjectContexts.First().DefaultContainerName == "HistoryContext") { return; } finalSetClauses.Add( GetInsertSetClause(column, newValue, insertCommand)); // Construct the final clauses, object representation of sql insert command values // In insert probably you can avoid to change the newInstanceAfterInsert because you are using a Guid for the entity ID that is always unique (it does not matter the tenant). var newInsertCommand = new DbInsertCommandTree( insertCommand.MetadataWorkspace, insertCommand.DataSpace, insertCommand.Target, new ReadOnlyCollection <DbModificationClause>(finalSetClauses), insertCommand.Returning); interceptionContext.Result = newInsertCommand; }
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { }
public abstract void TreeCreated(DbCommandTree commandTree, DbCommandTreeInterceptionContext interceptionContext);
/// <summary> /// In case of an update command we always filter based on the userId /// </summary> static bool InterceptUpdate(DbCommandTreeInterceptionContext interceptionContext) { var updateCommand = interceptionContext.Result as DbUpdateCommandTree; if (updateCommand != null) { var column = UserAwareAttribute.GetUserColumnName(updateCommand.Target.VariableType.EdmType); if (!string.IsNullOrEmpty(column)) { // Get the userId (throw an exception if there is none) var userId = GetCurrentUserId(); // Create the variable reference in order to create the property var variableReference = DbExpressionBuilder.Variable(updateCommand.Target.VariableType, updateCommand.Target.VariableName); // Create the property to which will assign the correct value var userProperty = DbExpressionBuilder.Property(variableReference, column); // Create the userId where predicate, object representation of sql where userId = value statement var userIdWherePredicate = DbExpressionBuilder.Equal(userProperty, DbExpression.FromInt32(userId)); // Remove potential assignment of userId for extra safety var filteredSetClauses = updateCommand.SetClauses.Cast<DbSetClause>() .Where(sc => ((DbPropertyExpression)sc.Property).Property.Name != column); // Construct the final clauses, object representation of sql insert command values var finalSetClauses = new ReadOnlyCollection<DbModificationClause>(new List<DbModificationClause>(filteredSetClauses)); // The initial predicate is the sql where statement var initialPredicate = updateCommand.Predicate; // Add to the initial statement the userId statement which translates in sql AND UserId = 'value' var finalPredicate = initialPredicate.And(userIdWherePredicate); var newUpdateCommand = new DbUpdateCommandTree( updateCommand.MetadataWorkspace, updateCommand.DataSpace, updateCommand.Target, finalPredicate, finalSetClauses, updateCommand.Returning); interceptionContext.Result = newUpdateCommand; // True means an interception successfully happened so there is no need to continue return true; } } return false; }
/// <summary> /// In case of a delete command we always filter based on the userId /// </summary> static void InterceptDeleteCommand(DbCommandTreeInterceptionContext interceptionContext) { var deleteCommand = interceptionContext.Result as DbDeleteCommandTree; if (deleteCommand != null) { var column = UserAwareAttribute.GetUserColumnName(deleteCommand.Target.VariableType.EdmType); if (!string.IsNullOrEmpty(column)) { // Get the userId (throw an exception if there is none) var userId = GetCurrentUserId(); // Create the variable reference in order to create the property var variableReference = DbExpressionBuilder.Variable(deleteCommand.Target.VariableType, deleteCommand.Target.VariableName); // Create the property to which will assign the correct value var userProperty = DbExpressionBuilder.Property(variableReference, column); var userIdWherePredicate = DbExpressionBuilder.Equal(userProperty, DbExpression.FromInt32(userId)); // The initial predicate is the sql where statement var initialPredicate = deleteCommand.Predicate; // Add to the initial statement the userId statement which translates in sql AND userId = 'value' var finalPredicate = initialPredicate.And(userIdWherePredicate); var newDeleteCommand = new DbDeleteCommandTree( deleteCommand.MetadataWorkspace, deleteCommand.DataSpace, deleteCommand.Target, finalPredicate); interceptionContext.Result = newDeleteCommand; } } }
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { Assert.NotEmpty(interceptionContext.DbContexts); if (_context == null || interceptionContext.DbContexts.Contains(_context)) { _log.Add(Tuple.Create(interceptionContext.Result, interceptionContext)); } }