Skip to content

Instantly share code, notes, and snippets.

@metadaddy
Last active December 24, 2015 08:59
Show Gist options
  • Select an option

  • Save metadaddy/6774317 to your computer and use it in GitHub Desktop.

Select an option

Save metadaddy/6774317 to your computer and use it in GitHub Desktop.
public class PortalHandler implements Auth.RegistrationHandler{
// createUser is called when there is no existing user linked to the incoming
//
public User createUser(Id portalId, Auth.UserData data){
User u;
// Use incoming email for username, since we're working with a portal user
// Look for a existing user with same email address
List<User> l = [SELECT Id, ContactId FROM User WHERE UserName = :data.email];
if (l.size() > 0) {
u = l[0];
System.debug('Found existing user record for '+data.username);
// Update existing record
u.Email = data.email;
u.LastName = data.lastName;
u.FirstName = data.firstName;
// Useful to save the Facebook ID in a custom field
u.Facebook_ID__c = data.identifier;
System.debug('Updating user record for '+data.username);
update(u);
} else {
// For simplicity, just put all contacts on the sForce account
Account a = [SELECT Id FROM Account WHERE Name='sForce'];
Contact c = new Contact();
c.AccountId = a.Id;
c.Email = data.email;
c.FirstName = data.firstName;
c.LastName = data.lastName;
insert(c);
u = new User();
// In my org, 'Customer Portal with API' is a clone of
// 'High Volume Customer Portal' with API access enabled
Profile p = [SELECT Id FROM profile WHERE name='Customer Portal with API'];
u.UserName = data.email;
u.Email = data.email;
u.LastName = data.lastName;
u.FirstName = data.firstName;
u.Facebook_ID__c = data.identifier;
u.Alias = (data.username != null) ? data.username : data.identifier;
if (u.Alias.length() > 8) {
u.Alias = u.Alias.substring(0, 8);
}
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;
System.debug('Returning new user record for '+data.username);
}
return u;
}
public 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;
u.Facebook_ID__c = data.identifier;
System.debug('Updating user record for '+data.username);
update(u);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment