Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Return all user id with specific permission? #2393


User avatar
0
omital created

Hi, How can I retrieve all userIds that have specific permisstion? for example all userids that have permission 'BaseTable.Acc.Bank.Edit'.


1 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    You can use below code for this. You can modify it according to your needs.

    var query = UserManager.Users
        .Include(u => u.Roles)
        .WhereIf(input.Role.HasValue, u => u.Roles.Any(r => r.RoleId == input.Role.Value))
        .WhereIf(
            !input.Filter.IsNullOrWhiteSpace(),
            u =>
                u.Name.Contains(input.Filter) ||
                u.Surname.Contains(input.Filter) ||
                u.UserName.Contains(input.Filter) ||
                u.EmailAddress.Contains(input.Filter)
        );
    
    if (!input.Permission.IsNullOrWhiteSpace())
    {
        query = (from user in query
                 join ur in _userRoleRepository.GetAll() on user.Id equals ur.UserId into urJoined
                 from ur in urJoined.DefaultIfEmpty()
                 join up in _userPermissionRepository.GetAll() on new { UserId = user.Id, Name = input.Permission } equals new { up.UserId, up.Name } into upJoined
                 from up in upJoined.DefaultIfEmpty()
                 join rp in _rolePermissionRepository.GetAll() on new { RoleId = ur.RoleId, Name = input.Permission } equals new { rp.RoleId, rp.Name } into rpJoined
                 from rp in rpJoined.DefaultIfEmpty()
                 where (up != null && up.IsGranted) || (up == null && rp != null)
                 group user by user into userGrouped
                 select userGrouped.Key);
    }