How to check if the logged in user has access to a group in visualforce page
We can check whether the logged-in user has access to a group or not with the below code
Boolean hasUserAccess = false;
List<GroupMember> lstGroupMember = new List<GroupMember>();
lstGroupMember = [select GroupId,group.name, group.DeveloperName from GroupMember where groupId='00G0o000003nffh' and UserOrGroupId=:userInfo.getUserId()];
if(lstGroupMember.size() >0) {
hasUserAccess = true;
}
where '00G0o000003nffh' is the ID of the public group created.
Example:
Apex Class: LoggedInUserGroupAccessController
public class LoggedInUserGroupAccessController {
public Boolean hasUserAccess {get;set;}
public LoggedInUserGroupAccessController() {
hasUserAccess = false;
List<GroupMember> lstGroupMember = new List<GroupMember>();
lstGroupMember = [select GroupId,group.name, group.DeveloperName from GroupMember where groupId='00G0o000003nffh' and UserOrGroupId=:userInfo.getUserId()];
if(lstGroupMember.size() >0) {
hasUserAccess = true;
}
}
}
Visualforce Page: LoggedInUserAccessInVFPage
<apex:page controller="LoggedInUserGroupAccessController">
<apex:outputPanel rendered="{!if(hasUserAccess == true,true,false)}">Logged In User has access</apex:outputPanel>
</apex:page>
Output:
We can check whether the logged-in user has access to a group or not with the below code
Boolean hasUserAccess = false;
List<GroupMember> lstGroupMember = new List<GroupMember>();
lstGroupMember = [select GroupId,group.name, group.DeveloperName from GroupMember where groupId='00G0o000003nffh' and UserOrGroupId=:userInfo.getUserId()];
if(lstGroupMember.size() >0) {
hasUserAccess = true;
}
where '00G0o000003nffh' is the ID of the public group created.
Example:
Apex Class: LoggedInUserGroupAccessController
public class LoggedInUserGroupAccessController {
public Boolean hasUserAccess {get;set;}
public LoggedInUserGroupAccessController() {
hasUserAccess = false;
List<GroupMember> lstGroupMember = new List<GroupMember>();
lstGroupMember = [select GroupId,group.name, group.DeveloperName from GroupMember where groupId='00G0o000003nffh' and UserOrGroupId=:userInfo.getUserId()];
if(lstGroupMember.size() >0) {
hasUserAccess = true;
}
}
}
Visualforce Page: LoggedInUserAccessInVFPage
<apex:page controller="LoggedInUserGroupAccessController">
<apex:outputPanel rendered="{!if(hasUserAccess == true,true,false)}">Logged In User has access</apex:outputPanel>
</apex:page>
Output:
No comments:
Post a Comment