Error when user permissions are added for any doctype

When we give a user permission for a child document, it is not being applied correctly to all the documents.
For example, if we grant user permission for the “IT” department (where IS Group = 0), which is a child of the “All” Departments (where IS Group = 1) parent, no records of the department are being displayed. This issue occurs because the filter is set for the field “Parent Department” with the value “IT,” which is not a parent department on its own.
Tracing the issue:
• Create 2 user permissions for any user for doctype department.
• Now login with the user and go to department list u will see empty list. Below image is a reference of filters applied on department list

• Here where condition is applied on parent_department field with 2 departments I have added in user permissions. Which is returning empty list.
• In case if one of the department which I have added has is_group = 1 then the above condition is getting applied returning all the departments with that particular parent_department.
This issue seems to be a critical bug which is currently causing us issue. We have customized the code to remove links with same option and parent, let us know if anything needed