How user permission rules being applied to the applicable doctype?

Hi, I’ve been looking for the logic behind “User permission” doctype . how these rules being applied to the applicable doctype?

let me explain …

for simple App : library management app , I have
four doctypes : book , library member , library membership .
and i have library member role.
by user permission doctype > users with library member role will be allowed to see their library membership only

my question is "how these restricted rules being applied ? where its source code exist , in any files ? "

If you apply user permissions, only limited documents will be visible based on that. For example, if you apply a specific Employee (EMP1200) user permission, then the user will only be able to view documents related to that Employee. In this case, the system checks for the value of Employee link field in a document and only allows the document if the value of employee field is EMP1200.

For more info check: Documentation

Most of the permission related code are in the following files: