How do I implement access checks based on referring nodes in the grants system?
Given that we don't have an exhaustive list of requirements &amp; Q&amp;A isn't suited well for long discussions, it will be impossible to definitely say yes.

The grant system isn't designed around any sort idea of hierarchy. Is it possible to rewrite? It depends if you can refactor your existing logic around what the grant system supplies.

You've framed your question as a dilemma, but you really need both hooks to use the grant system:


hook_node_grants need to assign realm/gid grants that correspond to the $user and the available lesson nid they can access.
hook_node_access_records needs to fire against a lesson $node


So if you can:


Map the realm/gid grant keys to match to courses the user has access to (e.g. mymodule_course_realm/123 where 123 some group identifier that maps to a group of users that have course access, thus lesson nid access)
Trigger lesson save events upon course updates (e.g. using hook_node_update, hook_node_insert, etc. to look for update courses, then doing dummy saves on the lesson nodes) so that hook_node_access_records can fire against lesson nodes that have a changed relationship. (or build custom logic where you insert grants directly via NodeGrantDatabaseStorage::write())


You can probably refactor around the hooks the grant system provides.