Newer Version Available
stripInaccessible メソッドによるセキュリティの適用
stripInaccessible メソッドを使用して、項目およびオブジェクトレベルのデータ保護を適用します。このメソッドを使用してクエリおよびサブクエリの結果からユーザがアクセスできない項目を除外できます。また、このメソッドを使用して、DML 操作の前にアクセスできない sObject 項目を削除して例外を回避し、信頼されないソースから非逐次化された sObject を適切な状態にすることもできます。
項目およびオブジェクトレベルのデータ保護には、Security および SObjectAccessDecision クラスを介してアクセスします。アクセスの検査は、作成、参照、更新、または更新/挿入という指定された操作のコンテキストで、現在のユーザの項目レベルの権限に基づいて行われます。stripInaccessible メソッドは、ソースレコードに現在のユーザの項目レベルセキュリティチェックに失敗した項目がないかを確認し、sObject のリストを作成します。返されるリストは、現在のユーザがアクセスできない項目が除外されている以外は、ソースレコードと同じです。getRecords メソッドによって返される sObject には、stripInaccessible メソッドの sourceRecords パラメータ内の sObject と同じ順序でレコードが含まれています。照会されない項目は、例外が発生することなく、戻りリストで null になります。
削除されたアクセスできない項目を特定するには、isSet メソッドを使用できます。たとえば、戻りリストには取引先責任者オブジェクトが含まれおり、カスタム項目 social_security_number__c はユーザからアクセスできません。このカスタム項目は、項目レベルのアクセスチェックに失敗するため、項目が設定されず、isSet は false を返します。
1SObjectAccessDecision securityDecision = Security.stripFields(sourceRecords);
2Contact c = securityDecision.getRecords()[0];
3System.debug(c.isSet('social_security_number__c')); // prints "false"stripInaccessible メソッドを使用可能ないくつかの例を次に示します。
例
このコード例では、アクセスできない項目がクエリ結果から削除されます。キャンペーンデータの表示テーブルには、常に BudgetedCost を表示する必要があります。ActualCost は、この項目を読み取る権限を持つユーザのみに表示される必要があります。
1Security.SObjectAccessDecision securityDecision =
2 Security.stripInaccessible(AccessType.READABLE,
3 [SELECT Name, BudgetedCost, ActualCost FROM Campaign];
4 );
5
6 // Construct the output table
7 if (securityDecision.getRemovedFields().get('Campaign').contains('ActualCost')) {
8 for (Campaign c : securityDecision.getRecords()) {
9 //System.debug Output: Name, BudgetedCost
10 }
11 } else {
12 for (Campaign c : securityDecision.getRecords()) {
13 //System.debug Output: Name, BudgetedCost, ActualCost
14 }
15}例
このコード例では、アクセスできない項目がサブクエリ結果から削除されます。Contacts オブジェクトの Phone 項目を参照する権限を持たないユーザ。
1List<Account> accountsWithContacts =
2 [SELECT Id, Name, Phone,
3 (SELECT Id, LastName, Phone FROM Account.Contacts)
4 FROM Account];
5
6 // Strip fields that are not readable
7 SObjectAccessDecision decision = Security.stripInaccessible(
8 AccessType.READABLE,
9 accountsWithContacts);
10
11// Print stripped records
12 for (Integer i = 0; i < accountsWithContacts.size(); i++)
13 {
14 System.debug('Insecure record access: '+accountsWithContacts[i]);
15 System.debug('Secure record access: '+decision.getRecords()[i]);
16 }
17
18// Print modified indexes
19 System.debug('Records modified by stripInaccessible: '+decision.getModifiedIndexes());
20
21// Print removed fields
22 System.debug('Fields removed by stripInaccessible: '+decision.getRemovedFields());例
このコード例では、DML 操作の前にアクセスできない項目が sObject から削除されます。取引先の Rating を作成する権限を持っていないユーザでも、取引先を作成できます。このメソッドでは、Rating は設定されず、例外は発生しません。
1List<Account> newAccounts = new List<Account>();
2Account a = new Account(Name='Acme Corporation');
3Account b = new Account(Name='Blaze Comics', Rating=’Warm’);
4newAccounts.add(a);
5newAccounts.add(b);
6
7SObjectAccessDecision securityDecision = Security.stripInaccessible(
8AccessType.CREATABLE,
9newAccounts);
10
11// No exceptions are thrown and no rating is set
12insert securityDecision.getRecords();
13
14System.debug(securityDecision.getRemovedFields().get('Account')); // Prints "Rating"
15System.debug(securityDecision.getModifiedIndexes()); // Prints "1"例
このコード例では、信頼されないソースからの並列化された sObject をサニタイズします。ユーザには、取引先の AnnualRevenue を更新する権限がありません。
1String jsonInput =
2'[' +
3'{' +
4'"Name": "InGen",' +
5'"AnnualRevenue": "100"' +
6'},' +
7'{' +
8'"Name": "Octan"' +
9'}' +
10']';
11
12List<Account> accounts = (List<Account>)JSON.deserializeStrict(jsonInput, List<Account>.class);
13SObjectAccessDecision securityDecision = Security.stripInaccessible(
14AccessType.UPDATABLE,
15accounts);
16
17// Secure update
18update securityDecision.getRecords(); // Doesn’t update AnnualRevenue field
19System.debug(String.join(securityDecision.getRemovedFields().get('Account'), ', ')); // Prints "AnnualRevenue"
20System.debug(String.join(securityDecision.getModifiedIndexes(), ', ')); // Prints "0”