If you prefer to use the Beans instead of direct SQL access, you’ll find some bits setting Security groups associations here:
–> https://gist.github.com/pgorod/3ce7868a290df0ef4f8c9de74f12a6cd
You basically set a relationship between the record and the security group.