/* ----------------------------------------------------------------------------------------------------------------------------------------------------------
Name: LoginAccessMarketProfileEventCondition.cls
Description: Update Transactional Secuirty Policy
Date Version Author Summary of Changes
----------- ---------- --------------- ------------------------------------------------------------------------------------------------
Nov 2024 1.0 Rushikesh Shinde US-ID_0016982 Transaction Security Policy - LoginAccessMarketProfile - Cleanup and simplification
---------------------------------------------------------------------------------------------------------------------------------------------------------- */
global class LoginAccessMarketProfileEventCondition1 implements TxnSecurity.EventCondition {
static final boolean AllowLogin = false;
static final boolean RestricLogin = false;
public Boolean evaluate(SObject event) {
system.debug('TestKK-Event' + event);
switch on event {
when LoginEvent loginEvent {
return evaluateNext(loginEvent);
}
when null {
return false;
}
when else {
return false;
}
}
}
public Boolean evaluateNext(LoginEvent LoginObj) {
// Login user details
system.debug('TestHB' + LoginObj);
List<User> userList = [SELECT ProfileId, Profile.Name, Profile.PermissionsApiUserOnly, OpCo__c FROM User WHERE Id = :LoginObj.UserId];
// Added for the SR-00362417. IF Login Session is not related to Application then allow login as external Application/Integration
if (!LoginObj.LoginType.contains('Application')) {
return AllowLogin;
}
boolean value = evaluateThis(userList, LoginObj.LoginURL);
return value;
}
public Boolean evaluateThis(List<User> userList, String LoginURL) {
// Bypass if user is bypassed in DataManagementSCM
DataManagementSCM__c dmscmCSRecord = DataManagementSCM__c.getInstance(userList[0].Id);
DataManagementSCM__c dmscmCSRecordOrgWide = DataManagementSCM__c.getOrgDefaults();
if ((dmscmCSRecord != null && dmscmCSRecord.ByPassLoginAccessMarketProfilePolicyCond__c) ||(dmscmCSRecordOrgWide != null && dmscmCSRecordOrgWide.ByPassLoginAccessMarketProfilePolicyCond__c))
{
system.debug('TestKK3');
return AllowLogin;
}
// Bypass if user has API User Only permission
Boolean profilePermissionsApiEnabled = (Boolean) userList[0] .Profile.PermissionsApiUserOnly;
if (profilePermissionsApiEnabled) {
return AllowLogin;
}
// Bypass if user has LoginAccessPolicyBypass Custom Permission
Boolean hasLoginAccessPolicyBypassPermission = false;
hasLoginAccessPolicyBypassPermission = doesRunningUserHavePermission(userList[0],'LoginAccessPolicyBypass');
if (hasLoginAccessPolicyBypassPermission) {
system.debug('TestKK2' + hasLoginAccessPolicyBypassPermission);
return AllowLogin;
}
// Bypass if USer is SSO And URl is Not ( login or test)
if (((!LoginURL.contains(GEN_Constants.SandboxLoginURL)) || (!LoginURL.contains(GEN_Constants.ProdLoginURL)))) {
system.debug('TestKK17');
return AllowLogin;
}
// Bypass if user has LoginAccessPolicyBypass Custom Permission Non SSO
if (((LoginURL.contains(GEN_Constants.SandboxLoginURL)) ||(LoginURL.contains(GEN_Constants.ProdLoginURL))) && hasLoginAccessPolicyBypassPermission) {
system.debug('TestKK2' + hasLoginAccessPolicyBypassPermission);
return AllowLogin;
}
else {
system.debug('TestKK18');
return RestricLogin ;
}
}
public static Boolean doesRunningUserHavePermission(User user, String apiName) {
Set<Id> accessiblePermissionIds = new Set<Id>();
for(SetupEntityAccess access : [
SELECT SetupEntityId FROM SetupEntityAccess
WHERE SetupEntityType = 'CustomPermission' AND ParentId IN (
SELECT PermissionSetId FROM PermissionSetAssignment
WHERE AssigneeId = : user.Id // UserInfo.getUserId()
)
]){
accessiblePermissionIds.add(access.SetupEntityId);
system.debug('TestKK --accessiblePermissionIds'+accessiblePermissionIds);
}
return 0 < [
SELECT count() FROM CustomPermission
WHERE Id IN : accessiblePermissionIds
AND DeveloperName = : apiName ];
}
}