Thursday 19 December 2013

Display Objects accessibility of user

Here in this example we are going to show the Users with the objects which are accessible to that user.

Controller(class)
============
public class Profileobjectpermissions{
public List<ObjectPermissions>  lstperimissions{get;set;}
public Map<String, Schema.SObjectType> Allobjectmap{get;set;}
public Map<String,List<String>> profcustobjectsmap{get;set;}
public Map<String,Integer> objectcount{get;set;}
public Map<String,Integer> userscount{get;set;}
public Map<String,Profile> mapprofile{get;set;}
public Map<String,List<String>> mapprofileobjest{get;set;}
public Profileobjectpermissions(){
    //Allobjectmap=new Map<String,Schema.SobjectType>();
    Allobjectmap=Schema.getGlobalDescribe();
    lstperimissions=new List<ObjectPermissions>();
    objectcount=new Map<String,Integer>();
    userscount=new Map<String,Integer>();
    profcustobjectsmap=new Map<String,List<String>>();
    mapprofile=new Map<String,Profile>();
    mapprofileobjest=new Map<String,List<String>>();
 
    lstperimissions=[SELECT PermissionsRead,SobjectType,ParentId,Parent.label,Parent.IsOwnedByProfile,Parent.ProfileId,Parent.Profile.name,Parent.Profile.UserLicense.Name FROM ObjectPermissions where Parent.Profile.UserLicense.Name='Salesforce' and PermissionsRead=true];
    List<Profile> profilelst=[select id,name,UserLicense.Name,(select id,name from Users) from Profile where UserLicense.Name='Salesforce'];
    if(lstperimissions!=null && lstperimissions!=null){
        for(ObjectPermissions objper:lstperimissions){
         
            if(objper.PermissionsRead==true && Allobjectmap.get(objper.SobjectType)!=null &&  Allobjectmap.get(objper.SobjectType).getDescribe().isCustom()==true){
                if(profcustobjectsmap.get(objper.Parent.Profile.name)!=null)
                        profcustobjectsmap.get(objper.Parent.Profile.name).add(Allobjectmap.get(objper.SobjectType).getDescribe().getLabel());
                else
                        profcustobjectsmap.put(objper.Parent.Profile.name,new List<String>{Allobjectmap.get(objper.SobjectType).getDescribe().getLabel()});
            }
         
        }
    }
    if(profilelst!=null && profilelst.size()>0){
        for(Profile profobj:profilelst){
                    System.debug('profileName:'+profobj.name);
                    mapprofile.put(profobj.Name,profobj);                
                    //Users count
                    if(profobj.users!=null && profobj.users.size()>0){
                        userscount.put(profobj.name,profobj.users.size());
                    }
                    else{
                        userscount.put(profobj.name,0);
                    }
                    //Object Count
                    if(profcustobjectsmap.size()>0 && profcustobjectsmap.get(profobj.name)!=null && profcustobjectsmap.get(profobj.name).size()>0)
                            objectcount.put(profobj.name,profcustobjectsmap.get(profobj.name).size());
                    else
                        objectcount.put(profobj.name,0);
                    if(profcustobjectsmap.size()>0 && profcustobjectsmap.get(profobj.Name)!=null){
                        mapprofileobjest.put(profobj.Name,profcustobjectsmap.get(profobj.Name));
                    }
                    else{
                         mapprofileobjest.put(profobj.Name,new List<String>{'No custom objects accessible'});
                    }
                 
        }
        System.debug('objectcount.keyset();;;'+objectcount.keyset());
    }
         
}
}
Page
====
<apex:page controller="Profileobjectpermissions">
    <apex:form >
        <apex:pageBlock title="Chatter  User permissions">
            <apex:pageBlockTable value="{!objectcount}" var="perm">              
                    <apex:column headerValue="Profile Name">
                        <apex:outputLink value="/{!mapprofile[perm].Id}" target="_blank">{!perm}</apex:outputLink>
                    </apex:column>
                    <apex:column headerValue="Object Count" value="{!objectcount[perm]}"/>
                    <apex:column headervalue="Users Count" value="{!userscount[perm]}"/>
                    <apex:column headerValue="Objects" value="{!mapprofileobjest[perm]}"/>
                </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>
===

Output:
=====

No comments:

Post a Comment

How to include a screen flow in a Lightning Web Component

 Hi, Assume  you have a flow called "Quick Contact Creation" and API Name for the same is "Quick_Contact_Creation". To i...