function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
dtillerydtillery 

Determining page accessibility from visualforce/apex

Hey guys,

 

I have a visualforce page, and I'm including several other visualforce pages within it.  Each of these pages have access restrictions based on different profiles (some people can use only 1 of the included pages, some can use 2, etc...).  If I include them all on the same page, then users will get the standard Access Restricted message for pages that aren't available to them.

 

Instead I'd rather not render the page at all if a user's profile does not give them access to it, so they don't see the error message.  I could do some silly "If user.profile == A or B or C..." logic, but I'd rather not have to edit the code every time some new profile needs access to a new page.

 

Basically I'm wondering if there's a way in either the Visualforce page or the controller to tell if a person has access to a page, sort of like how you can check for Object accessibility with {!$ObjectType.objectname.accessible}.

 

Also open to other suggestions if there's a better way.  I'm new to the force ecosystem so I may just be missing something simple.

 

Thanks!

rtuttlertuttle

I mentioned this on Twitter but wanted to make sure I made my idea more clear (more clear than 140 characters anyway).

 

I am not 100% sure this will work, but it just might because of the security model in SFDC.  There is access to pages via the ApexPage metadata type in SOQL.  If the security model controls the visibility via SOQL in the same way it controls page access then you should be able to query ApexPage and return a null value if they don't have access.

 

example: (below code uses a general exception catch, mostly cause I don't recall the exact exception off the top of my head, I recommend debugging and putting in the proper exception if this method works)

public Boolean showPageX {
  get {
    if(showPageX == null) {
      showPageX = false;
      try {
        ApexPage p = [select Id from ApexPage where name='PageX'];
        if(p != null) showPageX = true;
      } catch(Exception e) { // probably an assignment error due to non existing page, don't show it }
    }
    return showPageX;
  }
  private set;
}

 

 Edit: Fixed formatting with code block

 

-Richard

tom_patrostom_patros

Not really a solution per se, but I'm wondering if the new Dynamic Visualforce Components functionality would be a good approach for this.

 

Just curious: why do need to include entire pages? Would components work?