Across multiple tables...

Posts   
 
    
nmarun
User
Posts: 27
Joined: 31-Jul-2006
# Posted on: 31-Jul-2006 22:53:24   

Hi Developers,

I'm relatively new to LLBLGen Pro although I happen to know how powerful it is.

Here's my question:

I'm having 4 tables all 'related' by foreign keys:


[b]UserRole[/b]
PK      UserID
PK, FK   RoleID

[b]Role[/b]
PK       RoleID
             Description

[b]RolePermission[/b]
PK, FK   RoleID
PK, FK   PermissionID

[b]Permission[/b]
PK      PermissionID
            Description

(sorry..not sure how to upload an image.. image contains the relations diagram).

I need to pull out all the permissions for a particular UserID.

I AM able to get all the permissions for a given UserID, but I think I can do it in a better way. I guess I can do this by adding some relations and stuff like that.

Here's the code I'm currently using.


private void GetPermissions(string strObjectGUID)
{
                IPredicateExpression urCollectionFilter = new PredicateExpression();
    urCollectionFilter.Add(UserRoleFields.UserId==strObjectGUID);
    UserRoleCollection urCollection = new UserRoleCollection();
    urCollection.GetMulti(urCollectionFilter);

    IPredicateExpression rCollectionFilter = new PredicateExpression();

    for(int i=0; i<urCollection.Count; i++ )
    {
        UserRoleEntity urEntity = urCollection[i];
        rCollectionFilter.AddWithOr(RoleFields.RoleId == urEntity.RoleId );
    }

    RoleCollection rCollection = new RoleCollection();
    rCollection.GetMulti(rCollectionFilter);

    IPredicateExpression rpCollectionFilter = new PredicateExpression();
    for(int i=0; i<rCollection.Count; i++)
    {
        RoleEntity rEntity = rCollection[i];
        rpCollectionFilter.AddWithOr(RolePermissionFields.RoleId == rEntity.RoleId);
    }

    RolePermissionCollection rpCollection = new RolePermissionCollection();
    rpCollection.GetMulti(rpCollectionFilter);

    IPredicateExpression pFilter = new PredicateExpression();
    for(int i=0; i<rpCollection.Count; i++)
    {
        RolePermissionEntity rpEntity = rpCollection[i];
        pFilter.AddWithOr(PermissionFields.PermissionId == rpEntity.PermissionId);
    }

    PermissionCollection pCollection = new PermissionCollection();
    pCollection.GetMulti(pFilter);

    StringBuilder sb = new StringBuilder();
    sb.Append("<b>User has following permissions:</b><p/>");
    for(int i=0; i<pCollection.Count; i++)
    {
        PermissionEntity pEntity = pCollection[i];
        sb.Append(pEntity.Description);
        sb.Append("<br>");
    }

    lblPermissions.Text = sb.ToString();
}

Please help me refine my code and learn to utilize LLBLGen Pro's abilities to the fullest.

I did make basic search in the forum before posting, but was not able to find things similar to my problem.

Thanks in advance for any help. Arun

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 01-Aug-2006 03:08:32   

Here is how I would fetch a collection of permissions for a given user.

IPredicateExpression filter = new PredicateExpression();
filter.Add(UserRoleFields.UserID == strObjectGUID);
IRelationCollection relations = new RelationCollection();
relations.Add(UserRoleEntity.Relations.RoleEntityUsingRoleID);
relations.Add(RoleEntity.Relations.RolePermissionEntityUsingRoleID);
relations.Add(RolePermissionEntity.Relations.PermissionEntityUsingPermissionID);
PermissionCollection permissions = new PermissionCollection();
permissions.GetMulti(filter, relations);

Now you have a collection that contains all permissions for the user.

nmarun
User
Posts: 27
Joined: 31-Jul-2006
# Posted on: 01-Aug-2006 05:00:15   

Thanks a ton bclubb.. looks awesome and it worked.. I LOVE LLBLGEN Pro!!!!! and I'm just getting started

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 01-Aug-2006 09:50:44   

smile

Frans Bouma | Lead developer LLBLGen Pro