PredicateExpression on deep levels?

Posts   
 
    
netrom
User
Posts: 15
Joined: 03-Jul-2008
# Posted on: 03-Jul-2008 11:26:29   

Hi,

I am trying to find out what permissions a specific user has on projects he is assigned to.

A snip of the DB looks like this:

User 1:N Membership M:1 Group 1:N RessourcePermission M:1 Ressource 1:1 Project.

So I need to be able to read details on User, RessourcePermission and Project.

I have tried to "begin" from both User and Project perspective, but in the code below i tried a perspective from RessourcePermission.


EntityCollection<RessourcePermissionEntity> rpc = new EntityCollection<RessourcePermissionEntity>();
RelationPredicateBucket bucket = new RelationPredicateBucket();

bucket.Relations.Add(RessourcePermissionEntity.Relations.GroupEntityUsingGroupId);
bucket.Relations.Add(GroupEntity.Relations.MembershipEntityUsingGroupId);
bucket.Relations.Add(MembershipEntity.Relations.UserEntityUsingUserId);
bucket.PredicateExpression.Add(MembershipFields.UserId == "mos");

//Branch to user
IPrefetchPath2 pref = new PrefetchPath2(EntityType.RessourcePermissionEntity);
pref.Add(RessourcePermissionEntity.PrefetchPathGroup).SubPath.Add(GroupEntity.PrefetchPathUserCollectionViaMembership);

//Branch to project
pref.Add(RessourcePermissionEntity.PrefetchPathRessource).SubPath.Add(RessourceEntity.PrefetchPathProject);

DataAccessAdapter daa = new DataAccessAdapter();
daa.FetchEntityCollection(rpc, bucket, pref);

foreach (RessourcePermissionEntity rpe in rpc)
{
        Debug.WriteLine(string.Format("{0} is member of group: {1} and has permission: {2} on project: {3}", rpe.Group.UserCollectionViaMembership[0].Name, rpe.Group.Name,
                        rpe.Permissions.ToString(), rpe.Ressource.Project.Name));
}

What happens, that I don't like, is that when I try to print out the rpe.Group.UserCollectionViaMembership[0].Name - of course it returns a list. It seems though that the collection in UserCollectionViaMembership has all users from same group as user "mos". I'd like it to have only users of "mos".

I hope someone is able to point me in the right direction or tell me what I have misunderstood simple_smile

Walaa avatar
Walaa
Support Team
Posts: 14994
Joined: 21-Aug-2005
# Posted on: 03-Jul-2008 15:18:42   

If you want to filter the prefethed UserCollectionViaMembership, then add a filter to the prefetchPath

.....SubPath.Add(GroupEntity.PrefetchPathUserCollectionViaMembership, 0, new PredicateExpression(MembershipFields.UserId == "mos"));
netrom
User
Posts: 15
Joined: 03-Jul-2008
# Posted on: 03-Jul-2008 16:32:12   

Ah, thanks a lot. I wasn't aware of that.

Adding your code gives me the following error:

An exception was caught during the execution of a retrieval query: The multi-part identifier "PermissionsTest.dbo.Membership.UserID" could not be bound..

Usually when I get that error I just need to add some relations to my RelationPredicateBucket, but it doesn't seem to help in this case. Any pointers?

Walaa avatar
Walaa
Support Team
Posts: 14994
Joined: 21-Aug-2005
# Posted on: 03-Jul-2008 19:10:18   

You are right, I've missed that.

Please use the following overload of the Add() method to specify the relation/join.

public IPrefetchPathElement2 Add( 
   IPrefetchPathElement2 elementToAdd,
   int maxAmountOfItemsToReturn,
   IPredicateExpression additionalFilter,
   IRelationCollection additionalFilterRelations
)
netrom
User
Posts: 15
Joined: 03-Jul-2008
# Posted on: 04-Jul-2008 10:07:05   

Hi Walaa,

Thanks a lot for your help. Now it works perfectly! simple_smile