Skip to content

Instantly share code, notes, and snippets.

@dcinzona
Forked from wesley-chiSFDC/JitHandler.cls
Created June 20, 2024 13:03
Show Gist options
  • Save dcinzona/ae50158d65b6de0eb7dfa2f2f32e8329 to your computer and use it in GitHub Desktop.
Save dcinzona/ae50158d65b6de0eb7dfa2f2f32e8329 to your computer and use it in GitHub Desktop.
JIT Handler Round Robin
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);
}
}
}
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');
}
}
}
}
}
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