Setting permissions programmatically in concrete5.7

Permalink
Hi,

I would like to programmatically set custom block permissions. I need to set them in a way that a specific user group has permission to add only a set of certain blocks. (e.g. users in group1 could only add Content and Form block) This works nice if it is done from the dashboard, but doing it programmatically (using the concrete5 API) seems to be tricky. I have tried to reverse engineer the dashboard view that is used to set the block permissions but the code is scattered into different element, dialogs, controllers, etc and it is really difficult to figure out how it actually works. There's a bunch of classes in the API that deal with block permissions but I haven't been able to figure out how they should be used. Is there any documentation (in any form) about the concrete's permissions system?