How to eliminate access to page based on the user ID? I want to control the access of a page based on the user ID and the value of some other fields.

For example, if I am logged in as user #5, I have access to user/5 but I don't have user to the page user/6 and administrator users have access to both pages.

In my case, imagine I have a custom user page and I want to give access only to the owner and to the user selected in a custom field.

I don't think I can achieve that using permissions and in the future I might have more restrictions based on the value of other fields. So ideally, I would like to have a function where I will get the uid and the value of the fields, I will do my checks and I will restrict the access or not.

By the way my pages are panel pages not nodes.

Any idea/help/suggestion?
You will probably need to add a custom hook_node_access code.  In this you can use the view operation to check:


Is the current user the owner (yes - done - allow access)
Is the user an admin (yes - done - allow access)
Does the node have your user access list field (No -done - allow access)
Is the current user's uid in the field values? (Yes - allow access / No -deny access)


This should handle the basic access issues.  You may need to look at how to hide / show links based on access as well.