How to automatically assign specific role and restrict modules for new signups

Hi everyone,
I’m trying to customize the signup process in ERPNext so that whenever a new user signs up, they automatically get:
A fixed role (e.g. test)
Access to only one module (e.g. crm)
All other modules should be blocked automatically

I tried using a Server Script on the User (After Insert) event, but I initially got permission errors (Guest user can’t access Doctype)

Try doc.insert(ignore_permissions=True)in your server script.