Entity structure several many many queries

I have a simple security model where there is:

  • Users
  • Roles
  • Tracks

and many of the many links between these tables, so the user must perform roles and roles along the way. I'm trying to write a function, so that from the username and path it will return a bool value based on whether the user has access to this path. How can I do this with entity infrastructure? I currently have:

var rolesForUser = _entities.Users
         .Include("Roles")
         .Where(u => u.Login.Equals(username))
         .Select(u => u.Roles);

if(rolesForUser.Count() == 0) return false;

var authentications = _entities.WebPaths
         .Where(p => p.Path == path)
         .WhereIn(p => p.Roles, rolesForUser);

return (authentications.Count() > 0);

which uses the WhereIn extension method , however this can only be compared to primitives, so this is not working at the moment. Any suggestions are welcome.

+3
source share
1 answer

, PredicateBuilder.

:

var predicate = PredicateBuilder.False<WebPath>();
foreach (var role in from roles in rolesForUser 
                     from r in roles.Role
                     select r)
{
  predicate = predicate.Or (p => p.roles.Any(r => r.Id == role.Id));
}

var authentications = _entities.WebPaths.AsExpandable()
         .Where(p => p.Path == path)
         .Where(predicate);
return (authentications.Count() > 0);
+1

Source: https://habr.com/ru/post/1727233/


All Articles