Click here to Skip to main content
15,884,598 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I started creating a role-based security system in my WinForm application so I began with Form navigation (Permission Navigation) and this is my entity's
C#
public partial class User
{
    public User()
    {
        UsersToRoles = new HashSet<UsersToRole>();
    }

    public string Login { get; set; } = null!;
    public string PasswordUser { get; set; } = null!;
    public string? FullName { get; set; }
    public string? Email { get; set; }
    public int Id { get; set; }

    public virtual ICollection<UsersToRole> UsersToRoles { get; set; }
}

public partial class Role
{
    public Role()
    {
        UsersToRoles = new HashSet<UsersToRole>();
        PermissionNavigations = new HashSet<PermissionNavigation>();
    }

    public int Id { get; set; }
    public string Name { get; set; } = null!;

    public virtual ICollection<UsersToRole> UsersToRoles { get; set; }
    public virtual ICollection<PermissionNavigation> PermissionNavigations { get; set; }
}

public partial class UsersToRole
{
    public int Id { get; set; }
    public int IdUser { get; set; }
    public int IdRole { get; set; }

    public virtual Role IdRoleNavigation { get; set; } = null!;
    public virtual User IdUserNavigation { get; set; } = null!;
}

public partial class Navigation
{
    public Navigation()
    {
        PermissionNavigations = new HashSet<PermissionNavigation>();
    }

    public int Id { get; set; }
    public string Page { get; set; } = null!;
    public string Forms { get; set; } = null!;

    public virtual ICollection<PermissionNavigation> PermissionNavigations { get; set; }
}

public partial class PermissionNavigation
{
    public int Id { get; set; }
    public int IdRole { get; set; }
    public int IdNavigation { get; set; }

    public virtual Navigation IdNavigationNavigation { get; set; } = null!;
    public virtual Role IdRoleNavigation { get; set; } = null!;
}

This is my geniric GetAllIncluding method
C#
public async Task<IEnumerable<T>> GetAllIncluding(params Expression<Func<T, object>>[] includeProperties)
   {
       try
       {
           IQueryable<T> query = dbSet;
           foreach (Expression<Func<T, object>> includeProperty in includeProperties)
           {
               query = query.Include<T, object>(includeProperty);
           }
           return await query.ToListAsync();
       }
       catch (Exception ex)
       {
           throw new Exception($"{nameof(GetAllIncluding)} properties could not be included properly: {ex.Message}");
       }
   }

And this is how I use it in my PermissionNavigationService
C#
public async Task<IEnumerable<PermissionNavigationDto?>> 
GetAllPermissionNavigationDetailsByUserAsync(int idUser)
    {
        var permissionNavigation = await unitOfWork.PermissionNavigations.GetAllIncluding(
                           x => x.IdNavigationNavigation,
                           x => x.IdRoleNavigation,
                           x => x.IdRoleNavigation.UsersToRoles.Where(x=>x.IdUser== idUser));

        return mapper.Map<IEnumerable<PermissionNavigationDto?>>(permissionNavigation);
    }

The question is: What can be done to get all Permission Navigation related to specific user
I am looking to something's like this but in ef core
SQL
SELECT PermissionNavigation.[Id]
      ,PermissionNavigation.[IdRole]
      ,Roles.Name
      ,Navigation.Forms
      ,[IdNavigation]
      ,UsersToRoles.IdUser
  FROM [SIM].[dbo].[PermissionNavigation]
  INNER JOIN Roles on Roles.Id=IdRole
  INNER JOIN Navigation on Navigation.id=IdNavigation
  INNER JOIN UsersToRoles on UsersToRoles.IdRole=PermissionNavigation.[IdRole]
  WHERE UsersToRoles.IdUser=@IdUser


What I have tried:

I know that this line of code only filtering UsersToRoles entity not PermissionNavigation entity
C#
x => x.IdRoleNavigation.UsersToRoles.Where(x=>x.IdUser== idUser)
Posted
Updated 16-May-22 3:41am
v2

1 solution

I decided to go this way:
When the user has successfully logged in, I catch the Id then I make a call to get all roles related to that user after that I make another call to get all permission navigation using role Id that I got earlier.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900