Changing permission on node access that linked with other nodes
You can do this in hook_node_access().

You didn't mentioned if this is for D7 or D8, but this hook is available in both.

This hook provides the node and the account object. So you have all data you need to calculate the permission.