この文章は Salesforce 機械翻訳システムを使用して翻訳されました。詳細はこちらをご参照ください。
英語に切り替える

Newer Version Available

This content describes an older version of this product. View Latest

PolicyCondition インターフェース

トランザクションセキュリティポリシーに基づいて、特定のイベントの発生時に実行するアクションを指定するクラスを実装できるようにする Apex インターフェースです。

名前空間

TxnSecurity

使用方法

evaluate メソッドは、トランザクションセキュリティポリシーで監視されているイベントが発生するとコールされます。通常の実装では、最初にイベントから関心のある項目を選択します。その項目をテストして、監視されている条件を満たすどうか確認します。条件を満たす場合、メソッドは true を返します。

たとえば、同じユーザが複数回ログインしていないかチェックするトランザクションセキュリティポリシーがあるとします。ログインイベントごとに、メソッドは、ログインしているユーザにすでに進行中のログインセッションがあるかどうかをチェックし、ある場合は true を返します。

PolicyCondition メソッド

PolicyCondition のメソッドは次のとおりです。

evaluate(event)

トランザクションセキュリティポリシーに対してイベントを評価します。イベントでポリシーがトリガされる場合、true が返されます。

署名

public Boolean evaluate(TxnSecurity.Event event)

パラメータ

event
型: TxnSecurity.Event
トランザクションセキュリティポリシーに対して確認するイベント。

戻り値

型: Boolean

ポリシーがトリガされると、True が返されます。たとえば、ユーザをシングルログインセッションに限定するポリシーがあるとします。あるユーザが 2 つ目のログインをしようとすると、ポリシーのアクションによって現在のセッションを終了するよう求められます。また、Salesforce システム管理者にもメール通知が送信されます。evaluate() メソッドは、ログインイベントのみを確認し、ユーザの 2 つ目のログインである場合に True を返します。トランザクションセキュリティシステムはアクションと通知を実行しますが、evaluate() メソッドは実行しません。

PolicyCondition の実装例

このサンプルは、TxnSecurity.PolicyCondition インターフェースの実装例です。この例では、localhost からログインがあるとトリガされるポリシーを実装します。

1global class BlockLocalhostCondition implements TxnSecurity.PolicyCondition {
2
3  public boolean evaluate(TxnSecurity.Event e) {
4    // Get the IP address.
5    String sourceIp = e.data.get('SourceIp');
6    // If it’s localhost the policy is triggered and true is returned.
7    if(sourceIp != null && sourceIp.equals('127.0.0.1')){
8      return true; 
9    } else {
10      return false;
11    }
12  }
13}

次の例では、実装をテストしています。

1@isTest
2public class TestLogin {
3  public static testMethod void testLocalhostLogin() {
4    Map<String, String> eventData = new Map<String, String>();
5
6    /* Insert localhost IP address into the event data map */
7    eventData.put('SourceIp', '127.0.0.1');
8
9    TxnSecurity.Event e = new TxnSecurity.Event(
10         '00Dxxx123123123' /* organizationId */,
11         '005xxx123123123'/* userId */,
12         'AuthSession' /* entityName */ ,
13         'Login' /* action */,
14         'LoginHistory' /* resourceType */,
15         '01pR00000009D2H' /* entityId */,
16          Datetime.newInstance(2015, 9, 15) /* timeStamp */,
17          eventData /* data - Map containing information about the event */ );
18
19    /* We are unit testing a PolicyCondition that triggers
20       when an event is generated from localhost */
21    BlockLocalhostCondition condition = new BlockLocalhostCondition();
22
23    /* Assert that the condition is triggered */
24    System.assertEquals(true, condition.evaluate(e));
25  }
26
27  public static testMethod void testNonLocalhostLogin() {
28    Map<String, String> eventData = new Map<String, String>();
29
30    /* Insert non-localhost IP address into the event data map */
31    eventData.put('SourceIp', '1.1.1.1');
32
33    TxnSecurity.Event e = new TxnSecurity.Event(
34         '00Dxxx123123123' /* organizationId */,
35         '005xxx123123123'/* userId */,
36         'AuthSession' /* entityName */ ,
37         'Login' /* action */,
38         'LoginHistory' /* resourceName */,
39         '01pR00000009D2H' /* entityId */,
40          Datetime.newInstance(2015, 9, 15) /* timeStamp */,
41          eventData /* data - Map containing information about the event */ );
42
43    /* We are unit testing a PolicyCondition that triggers
44       when an event is generated from localhost */
45    BlockLocalhostCondition condition = new BlockLocalhostCondition();
46
47    /* Assert that the condition is NOT triggered */
48    System.assertEquals(false, condition.evaluate(e));
49  }
50}