-
Notifications
You must be signed in to change notification settings - Fork 31
/
MyFaceBookRegistrationHandler.cls
149 lines (124 loc) · 4.73 KB
/
MyFaceBookRegistrationHandler.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
FaceBook Registration Handler example
@Author - Bobby White
@email [email protected]
@company Salesforce
@date March 16, 2016
**/
global class MyFaceBookRegistrationHandler implements Auth.RegistrationHandler{
private static final String ORG_SUFFIX = '.sso.badge.org';
public static final String DEFAULT_ACCOUNTNAME = 'Acme';
/**
* 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
**/
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;
}
/**
* 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
return null;
}
// Is this a Community Context?
if(data.attributeMap.containsKey('sfdc_networkid')) {
System.debug('Registering Community user: ' + data.email);
// To keep things modular, we're creating the PersonAccount in a separate method
Id contactId = createPersonAccountContact(data);
// You'd likely use other logic to assign the Profile
Profile p = [SELECT Id FROM profile WHERE name='Customer Community User'];
// Keeping it modular, we initialize the user in another method
User u = createUser(data,p);
u.contactId = contactId;
return u;
} else {
//This is not a community, so we Assign an internal profile
Profile p = [SELECT Id FROM profile WHERE name='Standard User'];
// 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 PersonAccount for the contact
*
* @param data - Facebook provided context for this User
**/
private Id createPersonAccountContact(Auth.UserData data) {
Account person = new Account();
person.LastName = data.lastName;
person.FirstName = data.FirstName;
person.personEmail = data.email;
person.RecordTypeId = [Select Id From RecordType
Where SobjectType='Account'
AND isPersonType=true LIMIT 1].id;
insert person;
System.debug('Person Account created for ' + data.email + ' id=' + person.id);
/**
* This next step is necessary to get a valid contact Id,
* it won't exist until the PersonAcct is saved
**/
Account a = [Select PersonContactId From Account Where Id = :person.Id];
return a.PersonContactId;
}
/**
* 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) {
User u = new User();
u.username = data.email + ORG_SUFFIX;
u.email = data.email;
u.lastName = data.lastName;
u.firstName = data.firstName;
String alias = data.firstName + data.lastName;
//Alias must be 8 characters or less
if(alias.length() > 8) {
alias = alias.substring(0, 8);
}
u.alias = alias;
u.languagelocalekey = UserInfo.getLanguage();
u.localesidkey = UserInfo.getLocale();
u.emailEncodingKey = 'UTF-8';
u.timeZoneSidKey = 'America/Los_Angeles';
u.profileId = p.Id;
return u;
}
}