/* * This is a sample Auth Provider Registration Handler * Used in the Headless Identity Demo * It will create a external user, associate them with the "Headless Demo Profile" * It will create or associate the users contact to an Account called "My Account" * It uses the email as the username * */ global class HeadlessDemoGoogleIDPRegistrationHandler implements Auth.RegistrationHandler{ static final string headless_account = 'My Account'; static final string headless_profile = 'Headless Demo Profile'; /* * Tries to find a user with a username matching the incoming email * If not it creates one, associates it to an Account and Profile * */ global User createUser(Id portalId, Auth.UserData data) { //Find an existing user, we are using username to map to email as your google email is a google username List users = [SELECT Id, firstName, lastName, email FROM User where Username =:data.email LIMIT 1]; User u = null; if (!users.isEmpty()) { u = users[0]; } //If no user is found we create one if (u == null) { u = new User(); 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]; } // Get the Profile Profile p = [SELECT Id FROM Profile WHERE Name =: headless_profile LIMIT 1]; //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 = p.Id; u.contactId = c.Id; } else { u.firstName = data.firstName; u.lastName = data.lastName; u.email = data.email; update u; } return u; } /* * Basic Update User Method * */ global void updateUser(Id userId, Id portalId, Auth.UserData data){ User u = new User(id=userId); u.email = data.email; u.lastName = data.lastName; u.firstName = data.firstName; update(u); } /* * This method handles filling user data that is required by Salesforce but is not passed in during registration */ 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(' '); alias = firstName; //Alias must be 8 characters or less if(alias.length() > 8) alias = alias.substring(0, 8); u.username = email; 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'; } }