ユーザーの個人情報表示設定への準拠
ポータルやサイトがある組織では、ユーザーの個人識別情報や連絡先情報が他のユーザーに表示されないようにする固有の設定が提供されます。これらの設定は、WITH SECURITY_ENFORCED 句や stripInaccessible メソッドなどの Apex セキュリティ機能があっても、Apex では適用されません。特定の項目がゲストや外部の承認済みユーザーに表示されないようにするには、以下のサンプルコードに従います。
ユーザーオブジェクトのユーザーの個人情報を非表示にする方法:
1public User[] fetchUserDetail(Set userIds) {
2 // Query all the fields of user which we are expected in user record to show that on UI or to
3 // perform some business logic.
4 User[] userRecords = [SELECT id, username, communitynickname, firstname, lastname, title
5 FROM User WHERE id IN :userIds];
6
7 for (User userRecord : userRecords) {
8 // User is not fetching his own record and is not standard user.
9 if(userRecord.id != UserInfo.getUserId() && !Auth.CommunitiesUtil.isInternalUser()) {
10 // clear-out all PII fields form user record which we have queried above.
11 userRecord.username = '';
12 userRecord.title = '';
13 }
14 }
15 return userRecords;
16}コミュニティまたはポータル内のユーザーの連絡先情報表示設定に準拠するために、特定の項目に関連付けられた設定がチェックされ、それに応じてデータの表示/非表示が決まります。Experience Cloud サイト内のユーザーの連絡先表示設定:
1public User[] fetchUserRecordRespectingFLVPreferences(Set<Id> userIds) {
2
3 //Fetch users records along with fields specific user preferences.
4 User[] userRecords = [SELECT email, UserPreferencesShowEmailToExternalUsers, UserPreferencesShowEmailToGuestUsers FROM User WHERE id IN :userIds];
5
6 // If context user is internal user then return result without any restriction.
7 if (Auth.CommunitiesUtil.isInternalUser()) {
8 return userRecords;
9 }
10
11 // If user is guest user then return result as per the user's UserPreference for the fields related to the Guest user visibility.
12 if (Auth.CommunitiesUtil.isGuestUser()){
13 return fetchUserRecordForGuestUser(userRecords);
14 }
15
16 // Return result as per the user's UserPreference for the fields related to the External user visibility
17 return fetchUserRecordForExternalUser(userRecords);
18
19}
20
21// Apply Field level visibilty logic by checking user's UserPreferences for the fields related to the External user visibility.
22public User[] fetchUserRecordForExternalUser(User[] userRecords) {
23
24 for(User userRecord : userRecords) {
25
26 //Clear field of user record when context user fetching other user's record and Field Level Visibility for that field is set to Restricted.
27 if(userRecord.id != UserInfo.getUserId() && !userRecord.UserPreferencesShowEmailToExternalUsers)
28 {
29 userRecord.email = '';
30 }
31
32 }
33
34 return userRecords;
35}
36
37// Apply Field level visibilty logic by checking user's UserPreferences for the fields related to the Guest user visibility.
38public User[] fetchUserRecordForGuestUser(User[] userRecords) {
39
40 for(User userRecord : userRecords) {
41
42 //Clear field of user record when context user fetching other user's record and user preference for that field is NOT set to public.
43 if(!userRecord.UserPreferencesShowEmailToGuestUsers)
44 {
45 userRecord.email = '';
46 }
47
48 }
49
50 return userRecords;
51
52}