/* * Sample Headless Self Registration Handler * Used for the Headless Demo App */ global class HeadlessSelfRegistrationHandler implements Auth.HeadlessSelfRegistrationHandler{ static final string headless_account = 'My Account'; // Creates a Standard salesforce or a community user global User createUser(Id profileId, Auth.UserData data, String customUserDataMap, String experienceId, String password){ User u = new User(); //Ensures the user will save as all required fields are pre-filled in with dummy values prepareUserData(data, u); //Get the Account, and create it if one is not already present. Account a; List accounts = [SELECT Id FROM account WHERE name='My Account']; if(accounts.isEmpty()) { a = new Account(name = headless_account); insert(a); } else { a = accounts[0]; } handleCustomData(customUserDataMap); // Create the Contact Contact c = new Contact(); c.accountId = a.Id; c.firstName = u.firstName; c.lastName = u.lastName; insert(c); //Associate the Contact to the user along with the profile. u.profileId = profileId; u.contactId = c.Id; return u; } /* * We support the ability to pass in complex structures in the custom user data map * You can build apex classes that represent your complex structure * Then deserialize that structure into your apex class * In this case we have a class at the bottom of this file called "ContactInformation" * This class will deserialize the incoming request and print out the fields * */ void handleCustomData(String customUserDataMap) { System.debug('Custom Data: ' + customUserDataMap); ContactInformation contactInfo = null; try { contactInfo = (HeadlessSelfRegistrationHandler.ContactInformation)JSON.deserialize(customUserDataMap, HeadlessSelfRegistrationHandler.ContactInformation.class); System.debug('ContactInfo.mobilePhone: ' + contactInfo.mobilePhone); System.debug('ContactInfo.streetAddress: ' + contactInfo.streetAddress); System.debug('ContactInfo.city: ' + contactInfo.city); System.debug('ContactInfo.state: ' + contactInfo.state); } catch (Exception e) { System.debug('JSON was not formed correctly for the apex class'); } } /* * This method handles filling user data that is required by Salesforce but is not passed in during registration * It is not strictly necesary but helpful as it centralizes the management of unnecesary fields to the IDP instead of the client. */ void prepareUserData(Auth.UserData data, User u){ String name, firstName, lastName, username, alias, email; System.debug('----> Passed In User Information'); system.debug('Email: ' + data.email); system.debug('First Name: ' + data.firstName); system.debug('Last Name: ' + data.lastName); for(string key : data.attributeMap.keySet()) { system.debug('key: ' + key + ' value: ' + data.attributeMap.get(key)); } // Initialize the attributes essential for creating a new user with dummy values // in case they will not be provided by the Auth Provider firstName = 'change-me'; lastName = 'change-me'; email = 'change@me.com'; if(data.email != null && data.email != '') email = data.email; if(data.firstName != null && data.firstName != '') firstName = data.firstName; if(data.LastName != null && data.lastName != '') lastName = data.lastName; if(data.attributeMap.containsKey('full_name')) name = data.attributeMap.get('full_name'); if(data.attributeMap.containsKey('name')) name = data.attributeMap.get('name'); if(firstName == 'change-me' && name != '') firstName = name.substringBefore(' '); if(lastName == 'change-me' && name.substringAfter(' ') != '') lastName = name.substringAfter(' '); // Generate a random username Integer rand = Math.round(Math.random()*100000000); if(data.attributeMap.containsKey('username')){ username = data.attributeMap.get('username'); }else{ username = lastName + '.' + rand + '@social-sign-on.com'; } alias = firstName; //Alias must be 8 characters or less if(alias.length() > 8) alias = alias.substring(0, 8); u.username = username; u.email = email; u.lastName = lastName; u.firstName = firstName; u.alias = alias; u.languagelocalekey = UserInfo.getLocale(); u.localesidkey = UserInfo.getLocale(); u.emailEncodingKey = 'UTF-8'; u.timeZoneSidKey = 'America/Los_Angeles'; } /* * Apex Class Representation of Contact Information * which was passed in the custom data map * */ global class ContactInformation { String mobilePhone; String streetAddress; String city; String state; Boolean privacyPolicy; } }