Last active
August 29, 2015 14:00
-
-
Save magnet88jp/11215682 to your computer and use it in GitHub Desktop.
This file contains 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 SGDev1Util1 { | |
private static Map<Id, Set<Id>> userSetFromRoleMap; | |
private static Map<Id, Set<Id>> subordinateRoleSetFromRoleMap; | |
private static Map<Id, Id> parentRoleFromRoleMap; | |
private static Map<Id, Set<GroupMember>> groupMemberSetFromGroupMap; | |
private static List<UserRole> roleList; | |
private static Map<Id, Group> groupMap; | |
private static Map<Id, GroupMember> groupMemberFromGroupMap; | |
public SGDev1Util1() { | |
userSetFromRoleMap = new Map<Id, Set<Id>>(); | |
subordinateRoleSetFromRoleMap = new Map<Id, Set<Id>>(); | |
parentRoleFromRoleMap = new Map<Id, Id>(); | |
groupMemberSetFromGroupMap = new Map<Id, Set<GroupMember>>(); | |
roleList = [SELECT Id, ParentRoleId FROM UserRole]; | |
groupMap = new Map<Id, Group>([SELECT Id, RelatedId, Type FROM Group]); | |
// make userSetFromRoleMap | |
for(User user : [ | |
SELECT Id, UserRoleId FROM User | |
] ) { | |
Set<Id> userSet = userSetFromRoleMap.get(user.UserRoleId); | |
if(userSet == null) { | |
userSet = new Set<Id>(); | |
} | |
userSet.add(user.Id); | |
userSetFromRoleMap.put(user.UserRoleId, userSet); | |
} | |
// make parentRoleFromRoleMap | |
for(UserRole role : roleList) { | |
parentRoleFromRoleMap.put(role.Id, role.ParentRoleId); | |
} | |
// make subordinateRoleSetFromRoleMap | |
for(UserRole role : roleList) { | |
subordinateRoleSetFromRoleMap.put(role.Id, getSubordinateRoleSet(role.Id)); | |
} | |
// groupMemberFromGroupMap | |
groupMemberFromGroupMap = new Map<Id, GroupMember>(); | |
for(GroupMember groupMember : [ | |
SELECT GroupId, UserOrGroupId FROM GroupMember | |
] ) { | |
groupMemberFromGroupMap.put(groupMember.GroupId, groupMember); | |
} | |
// make groupMemberSetFromGroupMap | |
for(Group userGroup : [ | |
SELECT Id FROM Group | |
] ) { | |
Set<GroupMember> groupMemberSet = groupMemberSetFromGroupMap.get(userGroup.Id); | |
if(groupMemberSet == null) { | |
groupMemberSet = new Set<GroupMember>(); | |
} | |
groupMemberSet.add(groupMemberFromGroupMap.get(userGroup.Id)); | |
groupMemberSetFromGroupMap.put(userGroup.Id, groupMemberSet); | |
} | |
} | |
private static Set<Id> getSubordinateRoleSet(Id checkId) { | |
Set<Id> result = new Set<Id>(); | |
for(UserRole role : roleList) { | |
if(checkSubordinateRole(checkId, role.Id)) { | |
result.add(role.Id); | |
} | |
} | |
return result; | |
} | |
private static Boolean checkSubordinateRole(Id checkId, Id currentId){ | |
if(checkId == currentId) { | |
return True; | |
} | |
Id parentId = parentRoleFromRoleMap.get(currentId); | |
if(parentId == null) { | |
return False; | |
} else if(parentId == checkId) { | |
return True; | |
} else { | |
return checkSubordinateRole(checkId, parentId); | |
} | |
} | |
public Set<Id> getUserIdsFromPublicGroup(Set<Id> groupIds) { | |
return GetUserIdsFromGroup(groupIds); | |
} | |
public static Set<id> GetUserIdsFromGroup(Set<Id> groupIds) { | |
// store the results in a set so we don't get duplicates | |
Set<Id> result=new Set<Id>(); | |
String userType = Schema.SObjectType.User.getKeyPrefix(); | |
String groupType = Schema.SObjectType.Group.getKeyPrefix(); | |
String roleType = Schema.SObjectType.UserRole.getKeyPrefix(); | |
Set<Id> groupIdProxys = new Set<Id>(); | |
// make GroupMemberSet | |
Set<GroupMember> groupMemberSet = new Set<GroupMember>(); | |
for(Id groupId : groupIds) { | |
groupMemberSet.addAll(groupMemberSetFromGroupMap.get(groupId)); | |
} | |
// Loop through all group members in a group | |
for(GroupMember m : groupMemberSet) { | |
if(m == null) { | |
continue; | |
} | |
// If the user or group id is a user | |
if(((String)m.UserOrGroupId).startsWith(userType)) { | |
result.add(m.UserOrGroupId); | |
} | |
// If the user or group id is a group | |
// Note: there may be a problem with governor limits if this is called too many times | |
else if (((String)m.UserOrGroupId).startsWith(groupType)) { | |
// Call this function again but pass in the group found within this group | |
groupIdProxys.add(m.UserOrGroupId); | |
// Check RelatedId | |
Group userGroup = groupMap.get(m.UserOrGroupId); | |
if( ((String)userGroup.RelatedId).startsWith(roleType) ) { | |
if(((String)userGroup.Type).equals('Role')) { | |
result.addAll( userSetFromRoleMap.get(userGroup.RelatedId) ); | |
} else if(((String)userGroup.Type).equals('RoleAndSubordinates')){ | |
for(Id roleId : subordinateRoleSetFromRoleMap.get(userGroup.RelatedId)) { | |
result.addAll(userSetFromRoleMap.get(roleId)); | |
} | |
} | |
} | |
} | |
} | |
if(groupIdProxys.size() > 0) { | |
result.addAll(GetUserIdsFromGroup(groupIdProxys)); | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment