public override DbExpression Visit(DbScanExpression expression) { var column = EdmHelper.GetFactoryColumnName(expression.Target.ElementType); if (!string.IsNullOrEmpty(column)) { // Get the current expression var dbExpression = base.Visit(expression); // Get the current expression binding var currentExpressionBinding = DbExpressionBuilder.Bind(dbExpression); //FactoryId = @Paramname_1 // Create the variable reference in order to create the property var variableReferenceLeftSide = DbExpressionBuilder.Variable(currentExpressionBinding.VariableType, currentExpressionBinding.VariableName); // Create the property based on the variable in order to apply the equality var tenantPropertyLeftSide = DbExpressionBuilder.Property(variableReferenceLeftSide, column); // Create the parameter which is an object representation of a sql parameter. // We have to create a parameter and not perform a direct comparison with Equal function for example // as this logic is cached per query and called only once var tenantParameterLeftSide = DbExpressionBuilder.Parameter(tenantPropertyLeftSide.Property.TypeUsage, FactoryConstants.FactoryIdFilterParameterName); // Apply the equality between property and parameter. var filterExpressionLeftSide = DbExpressionBuilder.Equal(tenantPropertyLeftSide, tenantParameterLeftSide); //1 = @Paramname_2 // Create the variable reference in order to create the property var variableReferenceRightSide = DbExpressionBuilder.Variable(currentExpressionBinding.VariableType, currentExpressionBinding.VariableName); // Create the property based on the variable in order to apply the equality var tenantPropertyRightSide = DbExpression.FromInt32(1); // Create the parameter which is an object representation of a sql parameter. // We have to create a parameter and not perform a direct comparison with Equal function for example // as this logic is cached per query and called only once var tenantParameterRightSide = DbExpressionBuilder.Parameter(TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)), FactoryConstants.FactoryOverrideFilterParameterName); // Apply the equality between property and parameter. var filterExpressionRightSide = DbExpressionBuilder.Equal(tenantPropertyRightSide, tenantParameterRightSide); DbExpressionBuilder. var filterExpression = DbExpressionBuilder.Or(filterExpressionLeftSide, filterExpressionRightSide); // Apply the filtering to the initial query return(DbExpressionBuilder.Filter(currentExpressionBinding, filterExpression)); } return(base.Visit(expression)); }
public virtual 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; 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 FactoryQueryVisitor()); interceptionContext.Result = new DbQueryCommandTree( queryCommand.MetadataWorkspace, queryCommand.DataSpace, newQuery); return; } var insertCommand = interceptionContext.Result as DbInsertCommandTree; if (insertCommand != null) { var column = EdmHelper.GetFactoryColumnName(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("1")); // 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 }); var newInsertCommand = new DbInsertCommandTree( insertCommand.MetadataWorkspace, insertCommand.DataSpace, insertCommand.Target, finalSetClauses, insertCommand.Returning); interceptionContext.Result = newInsertCommand; return; } } } }