New Posts New Posts RSS Feed: QueryInterceptor exception
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

QueryInterceptor exception

 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: QueryInterceptor exception
    Posted: 28-Apr-2011 at 8:47am
I have a QueryInterceptor that looks like the below.  Basically, all it is doing is getting the current AppUserEntity and using that in the Filter.  This all works fine until I put the [RequiresAuthentication] attribute on the AppUserEntity.  When I do that, I get an EntityServerError that says : Parameter 'principal' cannot be null.  The error occurs during the execution of AppUserEntity query.
public class QueryInterceptor : EntityServerQueryInterceptor
    {
        protected override bool FilterQuery()
        {
            if (this.Query.ElementType == typeof(EmailAccountEntity))
            {
                var currentUser = this.GetCurrentAppUserEntity();
                if (currentUser != null)
                {
                    QueryFilters.AddFilter<EmailAccountEntity>(q => q.Where(c => c.OwnedBy == currentUser.Id));
                }
                else
                {
                    //Will return no records.
                    QueryFilters.AddFilter<EmailAccountEntity>(q => q.Where(c => c.OwnedBy == -1));
                }
            }
            return base.FilterQuery();
        }
        protected AppUserEntity GetCurrentAppUserEntity()
        {
            string name = this.Principal.Identity.Name;
            IEntityQuery<AppUserEntity> query =
                this.EntityManager.GetQuery<AppUserEntity>()
                .Where(user => user.AspNetDbUserName == name );
           
            var appUsers = this.EntityManager.ExecuteQuery<AppUserEntity>(query);  //This is where the error occurs.
           
            return appUsers.FirstOrDefault();
             
        }
    }
 
I like having the [RequiresAuthentication] attribute because it ensure that no-one is going to be getting to those entities without being logged in.  I could manually do this in the QueryInterceptor, but would prefer the attribute as it's less error prone.
 
I did some testing and any Entity type that is queried in the QueryInterceptor throws the same error if the RequiresAuthentication attribute is used on it.
 
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: 02-May-2011 at 5:29pm
Hi Greg,
 
I was able to reproduce the issue here.
 
I am filing a bug on this and will keep you updated on its status.
 
In the meantime a workaround would be checking if Principal is authenticated instead of setting the [RequiresAuthentication] attribute:
 
protected Employee GetCurrentAppUserEntity() {
  string name = this.Principal.Identity.Name;
  if (this.Principal.Identity.IsAuthenticated) {
    IEntityQuery<Employee> query =
      this.EntityManager.GetQuery<Employee>()
      .Where(user => user.FirstName == name);
    var appUsers = this.EntityManager.ExecuteQuery<Employee>(query);
    return appUsers.FirstOrDefault();
  }
  return null;
}
 
Regards,
   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: 03-May-2011 at 8:22am
Great, thanks very much.
 
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: 09-Jun-2011 at 11:16am
Hi Greg,
 
I want to let you know that this issue has been fixed and will be available in DevForce 6.1.1.0. (coming up shortly)
 
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: 09-Jun-2011 at 12:45pm
Looking forward to it.
 
Greg
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down