Portal - How the logged in user is mapped

I’m not able to find which piece of code maps the logged user to the customer or supplier in the portal.
For eg in Issues - Customer is mapped in the Issues doctype when the website user creates it. I’m trying to replicate the same for a custom doctype, but not able to.

I have mapped the website user as contact to the customer. Still the customer field (field type: link) in the doctype doesnt get populated, and also the user restrictions are not applied.

Generally speaking: linking is a manual process, if you want to get around that, use a custom script on your web form