I'm using Entity Framework on .NET 3.5 and I can't for the life of me figure out how to write some Linq to traverse the following design:
Basically I am trying to figure out if a user has permission (EntityAction) for a specific EntityType. Now Users and Roles are maintained in Active Directory - but the system can do a lookup to find which Role/Groups a user belongs.
Let's assume I have the following data:
EntityType
EntityTypeId: 1
Name: Entity One
User
UserId: 1
AccountName: Andez
Role
RoleId: 1
AccountName: MyRole
EntityAction
EntityActionId: 1
Name: Just do something
RoleEntityActionAssociation
(association between Role and EntityAction)
EntityActionId: 1
RoleId: 1
I am storing group names for the user (from Active Directory) in a List:
List<string> groupNames = new List<string>();
Question
However I am trying to piece together some Linq to find out whether a User (or one of the Roles he is assigned in List groupNames) is associated with a particular EntityAction given an EntityType.
// get reference to the user
User user = context.Users.Where(x => x.AccountName == "Andez").FirstOrDefault();
// get reference to the entity type we want to query
EntityType et = context.EntityTypes.Where(x => x.Name == "Entity One").FirstOrDefault();
// get list of all entity actions for the user
var result = from ea in et.EntityActions
where ea.EntityActionId == 1 && (ea.Users.Contains(user) || ea.Roles.Count(r => groupNames.Contains(r.AccountName)) > 0)
select ea;
Of course my query does not work above - it does not return any results (result.ToList().Count == 0)
I need pointers on this please.
Thanks
