0

I am new to TSP's and am wanting to exclude a list of users from triggering the TSP during their LoginEvents. My code is below:

    global class EnhancedOSEvent implements TxnSecurity.EventCondition {
    public boolean evaluate(SObject event) {
        switch on event{
            when LoginEvent loginEvent {
                return evaluate(loginEvent);
            }
            when null {
                return false;   
            }
            
            when else {
                return false;
            }
        }
    }
    
    List<MechId__mdt> mechids = [SELECT Id, Label, Username__c FROM MechId__mdt];
    
    public boolean evaluate(LoginEvent loginEvent) {
    
        String platform = loginEvent.Platform;
        String username = loginEvent.Username; // exclude the username if it matches that of one in the list
        
        // warn about access from Threatening OS
        if (platform.contains('Linux') || platform.contains('Ubuntu')) {
            return true;
        }
        return false; 
    }
}

The goal is to exclude those in the list "mechids" in the if statement. I am wanting to do so by comparing LoginEvent.Username to the MechId__MDT.Label (which contains the username of the users wanting to exclude.) I have attempted to add the below in the if:

        if (platform.contains('Linux') || platform.contains('Ubuntu') && username != MechId__mdt.Label IN :mechids))

and

if (platform.contains('Linux') || platform.contains('Ubuntu') && username != MechId__mdt.Label)

But neither work. Any insight is appreciated!

1
  • Shouldn't Username__c contain the Username you want to filter by?
    – Adrian Larson
    Mar 10, 2023 at 17:41

3 Answers 3

2

You can use either Set.contains or List.contains here. Note that you can simplify a lot of your conditions. Rather than:

if (condition)
    return true;
return false;

Simply:

return condition;

You also have a redundant when block. A cleaned up version of your code:

global class EnhancedOSEvent implements TxnSecurity.EventCondition {
    public Boolean evaluate(SObject event) {
        switch on event{
            when LoginEvent loginEvent {
                return evaluate(loginEvent);
            }
            when else {
                return false;
            }
        }
    }
    
    public Boolean evaluate(LoginEvent loginEvent) {
        List<String> blockedPlatforms = new List<String> { 'Linux', 'Ubuntu' };
        List<String> allowedUsers = new List<String>();
        for (MechId__mdt user : [SELECT Username__c FROM MechId__mdt])
            allowedUsers.add(user.Username__c);
        
        return blockedPlatforms.contains(platform) &&
            !allowedUsers.contains(loginEvent.Username);
    }
}
2
  • I went with this route but it does not work as expected. I can verify the allowedUsers is filling the list correctly after debugging but it doesn't stop the warning email from coming through, thoughts? Mar 13, 2023 at 13:55
  • @ForrestFairway You just needed to invert the condition on allowedUsers. See the updated code.
    – Adrian Larson
    Mar 13, 2023 at 14:53
0

You can't use SOQL-like stuff in Apex, you'll need to actually create a list of values:

Set<String> mechIds = new Set<String>();
for(MechId__mdt setting: mechids) {
  mechIds.add(setting.Label);
}
if ((platform.contains('Linux') || platform.contains('Ubuntu')) && 
    !mechIds.contains(username)) { ...
0

Final working code below:

global class EnhancedOSEvent implements TxnSecurity.EventCondition {
public boolean evaluate(SObject event) {
    switch on event{
        when LoginEvent loginEvent {
            return evaluate(loginEvent);
        }
        
        when else {
            return false;
        }
    }
}

public boolean evaluate(LoginEvent loginEvent) {

// gather usernames from mechId__mdt
List<MechId__mdt> allowedUsers = [SELECT Username__c FROM MechId__mdt];
List<String> blockedPlatforms = new List<String> { 'Linux', 'Ubuntu' };

for (MechId__mdt user : allowedUsers) {
    if (user.Username__c == loginEvent.Username) {
        return false;
    }
}
    
    if (blockedPlatforms.contains(loginEvent.Platform)) {
        return true;
    }
    return false;
}
}
1
  • to be honest, I am still slightly unsure why the above samples wouldn't work until I broke out the if statements. Mar 13, 2023 at 19:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .