-
-
Save wesley-chiSFDC/431b32a227ac831c5633624ff545cefa to your computer and use it in GitHub Desktop.
JIT Handler Round Robin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public with sharing class JitHandler implements Auth.SamlJitHandler { | |
public class JitException extends Exception {} | |
public User createUser(Id samlSsoProviderId, Id communityId, Id portalId, | |
String federationIdentifier, Map<String, String> attributes, String assertion) { | |
User u = new User(); | |
handleJit(true, u, samlSsoProviderId, communityId, portalId, | |
federationIdentifier, attributes, assertion); | |
return u; | |
} | |
public void updateUser(Id userId, Id samlSsoProviderId, Id communityId, Id portalId, | |
String federationIdentifier, Map<String, String> attributes, String assertion) { | |
User u = [SELECT Id, ProfileId, IsActive, UserName FROM User WHERE Id = :userId]; | |
handleJit(false, u, samlSsoProviderId, communityId, portalId, | |
federationIdentifier, attributes, assertion); | |
} | |
private void handleUser(boolean create, User u, Map<String, String> attributes, | |
String federationIdentifier, boolean isStandard) { | |
JitHelper.handleUser(create, u, attributes, federationIdentifier, isStandard); | |
} | |
private void handleJit(boolean create, User u, Id samlSsoProviderId, Id communityId, Id portalId, | |
String federationIdentifier, Map<String, String> attributes, String assertion) { | |
if (communityId != null || portalId != null) { | |
handleUser(create, u, attributes, federationIdentifier, false); | |
} else { | |
handleUser(create, u, attributes, federationIdentifier, true); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public with sharing class JitHelper { | |
public static void handleUser(boolean isNewUser, User user, Map<String, String> attributes, | |
String federationIdentifier, boolean isStandard) { | |
if (isNewUser) { | |
throw new JitHandler.JitException('No user found'); | |
} else { | |
Boolean hasLicensesAvailable = LicenseHelper.isLicenseAvailable(user.ProfileId, 2000010); | |
if (!user.isActive) { | |
if (!hasLicensesAvailable) { | |
List<User> usersToDeactivate = [ | |
SELECT Id, IsActive, Username, Name | |
FROM User | |
WHERE IsActive = true | |
AND (LastLoginDate != LAST_N_DAYS:30 OR LastLoginDate = null) | |
AND ProfileId = :user.ProfileId | |
ORDER BY LastLoginDate ASC | |
]; | |
if (!usersToDeactivate.isEmpty()) { | |
User deactivatedUser = usersToDeactivate[0]; | |
deactivatedUser.IsActive = false; | |
update deactivatedUser; | |
} | |
} | |
user.isActive = true; | |
try { | |
update user; | |
} catch(Exception e) { | |
throw new JitHandler.JitException('Error Activating User'); | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public with sharing class LicenseHelper { | |
public static Boolean isLicenseAvailable(Id profileId, Integer minimumAvailableLicenseThreshold) { | |
Integer availableLicenses = 0; | |
List<UserLicense> licenses = [ | |
SELECT Id, Name, MasterLabel, LicenseDefinitionKey, UsedLicenses, TotalLicenses, Status | |
FROM UserLicense | |
WHERE Status = 'Active' | |
AND Id IN ( | |
SELECT UserLicenseId | |
FROM Profile | |
WHERE Id = :profileId | |
) | |
]; | |
if (licenses.isEmpty()) { | |
throw new JitHandler.JitException('No licenses available'); | |
} | |
availableLicenses = licenses[0].TotalLicenses - licenses[0].UsedLicenses; | |
return availableLicenses > minimumAvailableLicenseThreshold; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment