ConfigurableSelfRegHandler インターフェース
名前空間
使用方法
コミュニティのセルフ登録は、[管理] ワークスペースの [ログイン & 登録] (L&R) ページで宣言的に設定します。設定可能なセルフ登録設定と組み合わせた場合、ハンドラクラスでプログラムでユーザ項目 (カスタム項目を含む) を入力できるほか、ユーザを作成してログインする方法を決定できます。
[設定可能なセルフ登録ページ] 登録ページを選択した場合、セルフ登録フォームで収集するユーザ項目 (名、姓、ユーザ名、ニックネーム、モバイル、メールなど) を選択します。また、ユーザの本人確認で使用する検証方法も決定します。この方法では、メールまたはモバイルを使用できます。またこのどちらも使用しないこともできます。Salesforce は、コミュニティメンバーの作成ロジックが含まれる Auth.ConfigurableSelfRegHandler ハンドラを生成します。ユーザの作成方法と、収集したユーザ情報の使用方法を変更するには、このハンドラを変更します。
メールまたは電話番号が、登録している外部ユーザに固有のものとなるようにカスタムロジックを追加することをお勧めします。たとえば、一意のカスタム項目を追加し、メールまたは電話番号のコピーをその項目に書き込みます。また、ユーザの作成方法も変更できます。デフォルトでは、ユーザは、L&R ページで選択された取引先に関連付けられた取引先責任者として作成されます。
生成された ConfigurableSelfRegHandler は [設定] の [Apex クラス] ページに配置され、AutocreatedConfigSelfReg で始まります (AutocreatedConfigSelfReg1532475901849 など)。
例については、「ConfigurableSelfRegHandler の実装例」を参照してください。詳細は、『Salesforce External Identity Implementation Guide (Salesforce External Identity 実装ガイド)』を参照してください。
ConfigurableSelfRegHandler メソッド
ConfigurableSelfRegHandler のメソッドは次のとおりです。
createUser(accountId, profileId, registrationAttributes, password)
署名
public Id createUser(Id accountId, Id profileId, Map<Schema.SObjectField,String> registrationAttributes, String password)
パラメータ
- accountId
- 型: Id
- 新規ユーザが関連付けられるデフォルトのアカウント。この値は、[ログイン & 登録] ページの [登録ページ設定] の [アカウント] 項目設定から取得されます。
- profileID
- 型: Id
- 新規ユーザに割り当てるプロファイル。この値は、[ログイン & 登録] ページの [登録ページ設定] の [プロファイル] 項目設定から取得されます。
- registrationAttributes
- 型: Map<Schema.sObjectField,String>
- セルフ登録ページで登録ユーザが入力した属性の対応付け。登録種別が [設定可能なセルフ登録ページ] の場合、セルフ登録ページに表示される項目は [ログイン & 登録] ページで選択した [ユーザの項目] から取得されます。
- password
- 型: String
- ユーザが入力したパスワード ([ログイン & 登録] ページで [パスワードを含める] が選択されている場合)。パスワードはユーザの作成に必要であるため、パスワードが入力されていない場合はハンドラで生成する必要があります。
ConfigurableSelfRegHandler の実装例
システム管理者が [ログイン & 登録] (L&R) ページで設定可能なセルフ登録ハンドラを設定するときに検証方法として [メール] を選択している場合、検証はメールによって実行されます。訪問者がログインページのサインアップリンクをクリックすると、Salesforce はメールアドレスの入力を要求した後、指定されたメールアドレスにワンタイムパスワードを送信します。検証ページで訪問者が確認コードの入力に成功したら、ユーザが作成され、ログインされます。同様に、システム管理者が L&R ページで検証方法として [テキストメッセージ] を選択している場合、訪問者は電話番号の入力を要求されます。Salesforce は、SMS を介してチャレンジ (確認コード) をユーザに送信します。成功した場合、ユーザが作成されてログインされます。ユーザを作成する前に検証を要求することで、組織に存在する不要なダミーユーザの数を減らすことができます。
Auth.ConfigurableSelfRegHandler クラスには、ユーザの作成に必要なユーザ項目がユーザから提供されない場合に、その項目を生成するためのロジックが含まれます。ハンドラは、タイムスタンプを追加することで値が一意となるようにデフォルト値を生成します。外部ユーザのメールアドレスと電話番号も一意であることを確認するようにハンドラを変更することができます。
1global class AutocreatedConfigSelfReg implements Auth.ConfigurableSelfRegHandler {
2
3 private final Long CURRENT_TIME = Datetime.now().getTime();
4 private final String[] UPPERCASE_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
5 private final String[] LOWERCASE_CHARS = 'abcdefghijklmnopqrstuvwxyz'.split('');
6 private final String[] NUMBER_CHARS = '1234567890'.split('');
7 private final String[] SPECIAL_CHARS = '!#$%-_=+<>'.split('');
8
9 // This method is called once after verification (if any was configured).
10 // This method should create a user and insert it.
11 // Password can be null.
12 // Return null or throw an exception to fail creation.
13 global Id createUser(Id accountId, Id profileId, Map<SObjectField, String> registrationAttributes, String password) {
14 User u = new User();
15 u.ProfileId = profileId;
16 for (SObjectField field : registrationAttributes.keySet()) {
17 String value = registrationAttributes.get(field);
18 u.put(field, value);
19 }
20
21 u = handleUnsetRequiredFields(u);
22 generateContact(u, accountId);
23 if (String.isBlank(password)) {
24 password = generateRandomPassword();
25 }
26 Site.validatePassword(u, password, password);
27 if (u.contactId == null) {
28 return Site.createExternalUser(u, accountId, password);
29 }
30 u.languagelocalekey = UserInfo.getLocale();
31 u.localesidkey = UserInfo.getLocale();
32 u.emailEncodingKey = 'UTF-8';
33 u.timeZoneSidKey = UserInfo.getTimezone().getID();
34 insert u;
35 System.setPassword(u.Id, password);
36 return u.id;
37 }
38 // Method to autogenerate a password if one isn't passed in.
39 // By setting a password for a user, we won't send a
40 // welcome email to set the password.
41 private String generateRandomPassword() {
42 String[] characters = new List<String>(UPPERCASE_CHARS);
43 characters.addAll(LOWERCASE_CHARS);
44 characters.addAll(NUMBER_CHARS);
45 characters.addAll(SPECIAL_CHARS);
46 String newPassword = '';
47 Boolean needsUpper = true, needsLower = true, needsNumber = true, needsSpecial = true;
48 while (newPassword.length() < 50) {
49 Integer randomInt = generateRandomInt(characters.size());
50 String c = characters[randomInt];
51 if (needsUpper && c.isAllUpperCase()) {
52 needsUpper = false;
53 } else if (needsLower && c.isAllLowerCase()) {
54 needsLower = false;
55 } else if (needsNumber && c.isNumeric()) {
56 needsNumber = false;
57 } else if (needsSpecial && !c.isAlphanumeric()) {
58 needsSpecial = false;
59 }
60 newPassword += c;
61 }
62 newPassword = addMissingPasswordRequirements(newPassword, needsLower, needsUpper, needsNumber, needsSpecial);
63 return newPassword;
64 }
65
66 private String addMissingPasswordRequirements(String password, Boolean addLowerCase, Boolean addUpperCase, Boolean addNumber, Boolean addSpecial) {
67 if (addLowerCase) {
68 password += LOWERCASE_CHARS[generateRandomInt(LOWERCASE_CHARS.size())];
69 }
70 if (addUpperCase) {
71 password += UPPERCASE_CHARS[generateRandomInt(UPPERCASE_CHARS.size())];
72 }
73 if (addNumber) {
74 password += NUMBER_CHARS[generateRandomInt(NUMBER_CHARS.size())];
75 }
76 if (addSpecial) {
77 password += SPECIAL_CHARS[generateRandomInt(SPECIAL_CHARS.size())];
78 }
79 return password;
80 }
81 // Generates a random number from 0 up to, but not including, max.
82 private Integer generateRandomInt(Integer max) {
83 return Math.mod(Math.abs(Crypto.getRandomInteger()), max);
84 }
85
86 // Loops over required fields that were not passed in to
87 // set to some default value.
88 private User handleUnsetRequiredFields(User u) {
89 if (String.isBlank(u.LastName)){
90 u.LastName = generateLastName();
91 }
92 if (String.isBlank(u.Username)) {
93 u.Username = generateUsername();
94 }
95 if (String.isBlank(u.Email)) {
96 u.Email = generateEmail();
97 }
98 if (String.isBlank(u.Alias)) {
99 u.Alias = generateAlias();
100 }
101 if (String.isBlank(u.CommunityNickname)) {
102 u.CommunityNickname = generateCommunityNickname();
103 }
104 return u;
105 }
106 // Method to construct a contact for a user.
107 private void generateContact(User u, Id accountId) {
108 // Add logic here if you want to build your own
109 // contact for the use.
110 }
111 // Default implementation to try to provide uniqueness.
112 private String generateAlias() {
113 String timeString = String.valueOf(CURRENT_TIME);
114 return timeString.substring(timeString.length() - 8);
115 }
116 // Default implementation to try to provide uniqueness.
117 private String generateLastName() {
118 return 'ExternalUser' + CURRENT_TIME;
119 }
120 // Default implementation to try to provide uniqueness.
121 private String generateUsername() {
122 return 'externaluser' + CURRENT_TIME + '@company.com';
123 }
124 // Default implementation to try to provide uniqueness.
125 private String generateEmail() {
126 return 'externaluser' + CURRENT_TIME + '@company.com';
127 }
128 // Default implementation to try to provide uniqueness.
129 private String generateCommunityNickname() {
130 return 'ExternalUser' + CURRENT_TIME;
131 }
132}