How to restrict users from accessing other users profiles and profile data

I’m adding for the record a link to this snippet which was published in the meantime and which allows to restrict access to user profiles automatically on user creation.