2

I've an aura/Lightning Component Quick Action/button that checks to see if someone is part of the public group. If the user is part of the group they can run the quick action.

Originally there was only had a handful of users who were allowed access to run the action. Therefore I added the individuals to the public group. I was then able to check in the apex controller if the userId clicking the button matches, one of the UserOrGroupId's in the groupMember to check the user is part of the public group.

A request has now been made to open access up to more users via role and subordinates. Therefore I was going to add the role and subordinates to the public group.

How can I now check in in the apex controller if the user clicking the button/action is part of the public group when the only member of the group is now a role and subordinate?

Current code

    @AuraEnabled
    public static boolean rtwGroup() {
        boolean runCheck = false; 
        Group grp= [SELECT id, name, developerName FROM Group WHERE developerName = 'RTW_User' LIMIT 1];
        
        for(GroupMember gm : [SELECT Id, GroupId, UserOrGroupId 
                             FROM GroupMember 
                             WHERE GroupId =: grp.id]) {
                                  if(gm.UserOrGroupId == UserInfo.getUserId()) {
                                       runCheck = true;
                                  }
        }
       return runCheck;
    }

1 Answer 1

0

Steven I know is late but hopefully, this helps someone, I had a similar request, to check where the user is in a queue, that can have inside groups. roles, and roles with subordinates, for this I made some recursive methods, to check each of the members of the queue, it will be better to use providers to avoid extending the logic

private User oldUser;
private Map<id, Group> groupMap;
private Map<id, User> UserMap;
private Map<id,List<GroupMember>> groupMembers;
private Map<id,List<UserRole>> roleHierarchy;

public Boolean isUserOnQueue(Id queueId, Id UserId){
    this.oldUser = this.getUser(oldOwnerId);
    if(this.oldUser == null) {
        return false;
    }

    return isMember(newOwnerId);
}

private Boolean isMember(Id groupId) {
    Group groupRecord = this.getGroupObject(groupId);
    if(groupRecord == null) {
        return false;
    }

    switch on groupRecord.Type {
        when 'Regular', 'Queue' {
            return isMemberFromGroup(groupId);
        } when 'RoleAndSubordinatesInternal' {
            return isMemberFromRole(groupRecord.RelatedId, true);
        } when 'Role' {
            return isMemberFromRole(groupRecord.RelatedId, false);
        }
    }

    return false;
}

private Boolean isMemberFromGroup(Id groupId) {
    for(GroupMember member : this.getGroupMembers(groupId)) {
        if(member.UserOrGroupId.getSobjectType() == Group.getSObjectType()) {
            if(this.isMember(member.UserOrGroupId) == true) {
                return true;
            }
        } else if(oldUser.Id == member.UserOrGroupId) {
            return true;
        }
    }

    return false;
}

private Boolean isMemberFromRole(Id roleId, Boolean withSubordinates) {

    if(oldUser.UserRoleID == roleId) {
            return true;
    }
    if(withSubordinates) {
        for(UserRole role : this.getRoleHierarchy(roleId)) {
            if(this.isMemberFromRole(role.Id, withSubordinates) == true) {
                return true;
            }
        }
    }

    return false;
}

private User getUser(ID userId) {
    if(this.UserMap == null) {
        this.UserMap = new Map<Id, User>([select id,UserRoleID from User]);
    }
    return this.UserMap.get(userId);
}

private Group getGroupObject(Id groupId) {
    if(this.groupMap == null) {
        this.groupMap = new Map<Id, User>([select id,Type,RelatedId from Group]);
    }
    return this.groupMap.get(userId);
}

private List<GroupMember> getGroupMembers(Id groupId) {
    if(this.groupMembers == null) {
        List<groupMember> tempList = [select id, userOrGroupId, GroupId, Group.Name, Group.DeveloperName, Group.Type from groupMember]);
        for(groupMember member:tempList) {
            List<groupMember> currentMembers = this.groupMembers.get(groupMember.GroupId) ?? new List<groupMember>();
            this.currentMembers.add(member);
            this.groupMembers.put(groupMember.GroupId, this.currentMembers)
        }
    }
    return this.groupMembers.get(userId);
}

private List<UserRole> getRoleHierarchy(Id roleId) {
    if(this.roleHierarchy == null) {
        Map roleMap = new Map<Id, Role>([SELECT Id, Name, DeveloperName, ParentRoleId FROM UserRole]);
        for (UserRole role : roleMap.values()) {
            if (!this.roleHierarchy.containsKey(role.Id)) {
                this.roleHierarchy.put(role.Id, new List<UserRole>());
            }

            if (role.ParentRoleId == null) { continue; }

            if (!this.roleHierarchy.containsKey(role.ParentRoleId)) {
                this.roleHierarchy.put(role.ParentRoleId, new List<UserRole>());
            }
            this.roleHierarchy.get(role.ParentRoleId).add(role);
        }
    }
    return this.roleHierarchy.get(roleId)
}

You must log in to answer this question.

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