From 0f4aee9cc9a33745426b52794dbadb88b82ebf7e Mon Sep 17 00:00:00 2001 From: adrianaolarte Date: Tue, 13 Dec 2016 14:58:09 -0500 Subject: [PATCH] Update SimpleFacebookRegistrationHandler.cls --- SimpleFacebookRegistrationHandler.cls | 185 +++++++++----------------- 1 file changed, 66 insertions(+), 119 deletions(-) diff --git a/SimpleFacebookRegistrationHandler.cls b/SimpleFacebookRegistrationHandler.cls index 8bb63ea..82f840f 100644 --- a/SimpleFacebookRegistrationHandler.cls +++ b/SimpleFacebookRegistrationHandler.cls @@ -1,136 +1,66 @@ -/** - FaceBook Registration Handler example -**/ -global class SimpleFacebookRegistrationHandler implements Auth.RegistrationHandler{ - - private static final String ORG_SUFFIX = '.sso.badge.org'; - private static final String DEFAULT_ACCOUNTNAME = 'Customers'; - private static final String EXTERNAL_USER_PROFILE = 'Customers'; - private static final String INTERNAL_USER_PROFILE = 'Standard User'; - -/** - * Let anyone register as long as the required fields are supplied - * - * We require email, lastName, firstName - * - * @data - the user's info from the Auth Provider - **/ +//TODO:This autogenerated class includes the basics for a Registration +//Handler class. You will need to customize it to ensure it meets your needs and +//the data provided by the third party. + +global class AutocreatedRegHandler1481657532983 implements Auth.RegistrationHandler{ global boolean canCreateUser(Auth.UserData data) { - System.debug('canCreateUser was called for ' + (data != null ? data.email : 'null')); - Boolean retVal = (data != null - && data.email != null - && data.lastName != null - && data.firstName != null); - - System.debug('data.username='+data.username); - System.debug('data.email='+data.email); - System.debug('data.lastName='+data.lastName); - System.debug('data.firstName='+data.firstName); - - return retVal; + //TODO: Check whether we want to allow creation of a user with this data + //Set s = new Set{'usernamea', 'usernameb', 'usernamec'}; + //if(s.contains(data.username)) { + //return true; + //} + return false; } -/** - * Create the User - A required method to implement the Handler Interface - * - * @param portalId - Id of the Community - * @param data - Auth Provider user data describing the User to create - * - * @return User that has been initialized -**/ global User createUser(Id portalId, Auth.UserData data){ if(!canCreateUser(data)) { - // Returning null signals the auth framework we can't create the user + //Returning null or throwing an exception fails the SSO flow return null; } - - // Is this a Community Context? if(data.attributeMap.containsKey('sfdc_networkid')) { - System.debug('Registering Community user: ' + data.email); - Id contactId; - // To keep things modular, we're creating the Contact in a separate method - contactId = createContact(data); - System.debug('Created contact: '+ contactId); - - // You'd likely use other logic to assign the Profile - Profile p = [SELECT Id FROM profile WHERE name=:EXTERNAL_USER_PROFILE]; - System.debug('Found profile: '+ p); + //We have a community id, so create a user with community access + //TODO: Get an actual account + Account a = [SELECT Id FROM account WHERE name='Acme']; + Contact c = new Contact(); + c.accountId = a.Id; + c.email = data.email; + c.firstName = data.firstName; + c.lastName = data.lastName; + insert(c); - // Keeping it modular, we initialize the user in another method - User u = createUser(data,p); - - u.contactId = contactId; + //TODO: Customize the username and profile. Also check that the username doesn't already exist and + //possibly ensure there are enough org licenses to create a user. Must be 80 characters or less. + User u = new User(); + Profile p = [SELECT Id FROM profile WHERE name='Customer Portal User']; + u.username = data.username + '@acmecorp.com'; + u.email = data.email; + u.lastName = data.lastName; + u.firstName = data.firstName; + String alias = data.username; + //Alias must be 8 characters or less + if(alias.length() > 8) { + alias = alias.substring(0, 8); + } + u.alias = alias; + u.languagelocalekey = UserInfo.getLocale(); + u.localesidkey = UserInfo.getLocale(); + u.emailEncodingKey = 'UTF-8'; + u.timeZoneSidKey = 'America/Los_Angeles'; + u.profileId = p.Id; + u.contactId = c.Id; return u; } else { - //This is not a community, so we Assign an internal profile - Profile p = [SELECT Id FROM profile WHERE name=:INTERNAL_USER_PROFILE]; - System.debug('Found profile: '+ p); - - // Keeping it modular, we initialize the user in another method - User u = createUser(data,p); - - return u; - } -} - -/** - * Update the user - * @param portalId - Id of the Community - * @param data - Auth Provider user data describing the User to create - **/ -global void updateUser(Id userId, Id portalId, Auth.UserData data){ - System.debug('Update User called for: ' + data.email); - - User u = new User(id=userId); - u.email = data.email; - u.lastName = data.lastName; - u.firstName = data.firstName; - update(u); -} - /** - * Create a Contact - * - * @param data - Facebook provided context for the User - **/ - private Id createContact(Auth.UserData data){ - Contact contact = new Contact(); - contact.LastName = data.lastName; - contact.FirstName = data.firstName; - contact.Email = data.email; - - // set Account Id - if (data.attributemap.get('accountId') != null){ - contact.accountId = data.attributemap.get('accountId'); - } else { - List accounts = [select Id from Account where Name =:DEFAULT_ACCOUNTNAME]; - System.debug('Found account: ' + accounts); - - contact.accountId = accounts[0].Id; - } - insert contact; - - System.debug('Contact created for ' + data.email + ' id=' + contact.id); - - return contact.id; - } - - - /** - * Create and initialize the User but don't save it yet - * - * @param data - the provided User context from FaceBook - * @param p - the Profile we are going to assign to this user - * - * @return User that has been initialized but not Saved - **/ - private User createUser(Auth.UserData data, Profile p) { + //This is not a community, so create a regular standard user User u = new User(); - u.username = data.email + ORG_SUFFIX; + Profile p = [SELECT Id FROM profile WHERE name='Standard User']; + //TODO: Customize the username. Also check that the username doesn't already exist and + //possibly ensure there are enough org licenses to create a user. Must be 80 characters + //or less. + u.username = data.username + '@myorg.com'; u.email = data.email; u.lastName = data.lastName; u.firstName = data.firstName; - String alias = data.firstName + data.lastName; - + String alias = data.username; //Alias must be 8 characters or less if(alias.length() > 8) { alias = alias.substring(0, 8); @@ -142,5 +72,22 @@ global void updateUser(Id userId, Id portalId, Auth.UserData data){ u.timeZoneSidKey = 'America/Los_Angeles'; u.profileId = p.Id; return u; - } + } +} + +global void updateUser(Id userId, Id portalId, Auth.UserData data){ + User u = new User(id=userId); + //TODO: Customize the username. Must be 80 characters or less. + //u.username = data.username + '@myorg.com'; + u.email = data.email; + u.lastName = data.lastName; + u.firstName = data.firstName; + //String alias = data.username; + //Alias must be 8 characters or less + //if(alias.length() > 8) { + //alias = alias.substring(0, 8); + //} + //u.alias = alias; + update(u); +} }