How to set custom permission for each user in each document?

I have this “Study Group” doctype which holds the information of the group and the list of members in that group (a child table), each member has a role in that group (leader, member).
How can I set permission for only the leaders can edit the information in their group while other members can only view it? (The leader cannot edit groups which they are not a leader of)