How to dynamically allow access based on role

I am trying to allow access rights to a custom doctype based on a field in the doctype.
I have a custom doctype with a field that is a link to a Role
When the user creates a new document of that doctype they can select a link to a role.
Only users who have the selected role should be able to access the document.
Imagine a role called Public and a role called Private
If a user creates a new document and selects the Private role in the role field only users who have the Private role attached to their accounts should be able to access the document.
I do not want to have alter any users accounts and add user permissions.
I am aware of creating custom user permissions but this is not a solution for regular users creating documents.

Hi you can start studying the Database Tables

so far I was able to hide/unhide the modules by batch and by Role

I haven’t updated this for awhile. I created this when ERPNext was still v7.

I’m planning to update this in few weeks from now. Next updates will include

  1. User Permissions
  2. Sort Order of Modules
  3. Globally Change Background
  4. Make it work for v8-v9

Would be glad if you can contribute

Cheers,
John

Thanks John

I appreciate the answer.
What I am trying to do is not so much hide things as recreate what most people understand as groups.
If I create a new doctype with a field name Role that is a link to a Role I would like anyone with that role to access the document if it has a link to a role that they have.
I have gone through the erpnext permission system and it doesn’t seem to offer any straightforward way to do that.
I can add a customer user permission per user to access documents but I dont want to do that. Also documents would be created by regular users who would not have the rights to go and alter permissions.

Marcus