Newer Version Available
PolicyCondition Interface
Namespace
Usage
The evaluate method is called upon the occurrence of an event monitored by a transaction security policy. A typical implementation first selects the item of interest from the event. Then the item is tested to see if it meets the condition being monitored. If the condition is met, the method returns true.
For example, imagine a transaction security policy that checks for the same user logging in more than once. For each login event, the method would check if the user logging in already has a login session in progress, and if so, true is returned.
PolicyCondition Methods
The following is the method for PolicyCondition.
evaluate(event)
Signature
public Boolean evaluate(TxnSecurity.Event event)
Parameters
- event
- Type: TxnSecurity.Event
- The event to check against the transaction security policy.
Return Value
Type: Boolean
When the policy is triggered, True is returned. For example, let’s suppose the policy is to limit users to a single login session. If anyone tries to log in a second time, the policy’s action requires that they end their current session. The policy also sends an email notification to the Salesforce admin. The evaluate() method only checks the login event, and returns True if it’s the user’s second login. The Transaction Security system performs the action and notification, and not the evaluate() method.
PolicyCondition Example Implementation
This sample is an example implementation of the TxnSecurity.PolicyCondition interface. This example implements a policy that triggers when there’s a login from 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}The following example tests the implementation:
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}