QueryInterceptor on all entities

688 views Asked by At

Is it possible to make a custom QueryInterceptor in a WCF Data Service on all entites instead of only one? This is a standard QueryInterceptor:

[QueryInterceptor("Products")]
public Expression<Func<Product, bool>> OnQueryProducts()
{
    var user = HttpContext.Current.User;
    if (user.IsInRole("Administrator"))
        return (Product p) => true;
    else
        return (Product p) => false;
}

I like to do something like this:

[QueryInterceptor("*")]
public Expression<Func<Object, bool>> OnQueryProducts()
{
    var user = HttpContext.Current.User;
    if (user.IsInRole("Administrator"))
        return (Object p) => true;
    else
        return (Object p) => false;
}

Is there any way or do I have to integrate one inceptor for all of my entities?

1

There are 1 answers

1
Adam Cooper On BEST ANSWER

Unfortunately you can't use a wild card with the QueryInterceptor however you can achieve the same result as in your example by overriding the OnStartProcessingRequest method of the DataService and checking the role of the user there.

protected override void OnStartProcessingRequest(ProcessRequestArgs args)
{
    var user = HttpContext.Current.User;

    // Only Administrator users are allowed access
    if (!user.IsInRole("Administrator"))
    {
        // Any other role throws a security exception
        throw new SecurityException("You do not have permission to access this Service");
    }

    base.OnStartProcessingRequest(args);
}