Base solution for your next web application
Spring Sale!
Time Offer!
XXdays : XXhrs : XXmin : XXsec
Open Closed

Return all user id with specific permission? #2393

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)
  • 0
    ismcagdas created
    Support Team


    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))
            u =>
                u.Name.Contains(input.Filter) ||
                u.Surname.Contains(input.Filter) ||
                u.UserName.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);