New Posts New Posts RSS Feed: FilterQuery
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

FilterQuery

 Post Reply Post Reply
Author
gregweb View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 10-Sep-2009
Location: Clearwater, Fl
Posts: 253
Post Options Post Options   Quote gregweb Quote  Post ReplyReply Direct Link To This Post Topic: FilterQuery
    Posted: 26-Dec-2010 at 6:32pm
From another post:
 
"Using the EntityServerQueryInterceptor, you can force it to always add a Where IsDeleted == false, so no matter what query you execute, it will only return entities that satisfy this condition."
 
How would I do this?  I don't see a Force() method that takes a string.
 
I need to use this for a security filter that will be applied to all queries.  This is what I have so far, but it is specfic to type ContactEntity.
 
protected override bool FilterQuery()
        {
            if (this.Query.ElementType == typeof(ContactEntity)) 
            {
                var currentUser = this.GetCurrentAppUserEntity();
                if (currentUser != null)
                {
                    QueryFilters.AddFilter<ContactEntity>(q => q.Where(c => c.OwnerId == currentUser.Id));
                }
                else
                {
                    //Will return no records.
                    QueryFilters.AddFilter<ContactEntity>(q => q.Where(c => c.OwnerId == -1));
                }
                    
            }
            
            return base.FilterQuery();
        }
 
Greg
Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post Posted: 28-Dec-2010 at 12:27pm
Hi Greg,
 
If you want to apply a filter to all queries (do you mean all queriable entities?) you can create the filter dynamically with PredicateDescription:
 
protected override bool FilterQuery() {
  var typesList = this.EntityManager.MetadataStore.GetEntityTypes();
  foreach (var type in typesList) {
    var meta= IdeaBlade.EntityModel.EntityMetadataStore.Instance.GetEntityMetadata(type);
    var oId = meta.DataProperties["OwnerId"]; 
    if (oId != null) { // you might not want to filter entities with no "OwnerId" field
      var currentUser = this.GetCurrentAppUserEntity();
      if (currentUser != null) {
        var filter = new PredicateDescription(type, "OwnerId", FilterOperator.IsEqualTo, currentUser.Id);
        this.QueryFilters.AddFilter(filter);
        //QueryFilters.AddFilter<ContactEntity>(q => q.Where(c => c.OwnerId == currentUser.Id));
      } else {
        //Will return no records.
        var filter = new PredicateDescription(type, "OwnerId", FilterOperator.IsEqualTo, -1);
        this.QueryFilters.AddFilter(filter);
        //QueryFilters.AddFilter<ContactEntity>(q => q.Where(c => c.OwnerId == -1));
      }
    }
  }
  return base.FilterQuery();
}
 
I hope this helps,
 
Silvio.
Back to Top
gregweb View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 10-Sep-2009
Location: Clearwater, Fl
Posts: 253
Post Options Post Options   Quote gregweb Quote  Post ReplyReply Direct Link To This Post Posted: 29-Dec-2010 at 5:42am
Hi Silvio,
 
Thanks very much, you are very knowledgable!
 
Greg
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down