-
Book Overview & Buying
-
Table Of Contents
Visualforce Development Cookbook - Second Edition
By :
When a Visualforce page is deployed to production, only users whose profiles have been given access via the security settings will be able to access the page. Any user with a profile that does not have access will receive an Insufficient Privileges error, which is not a good experience and can lead users to think that the page is crashing.
A better solution is to check whether the user has access to the page and if they do not, present a user-friendly message that explains the situation and directs them to where they can get more help.
In this recipe, we will create a launch page accessible to all profiles that checks whether the user has access to the protected page. If the user has access, they will be transferred to the protected page, while if they don't, they will receive an explanatory message.
This recipe requires a second user login. Ensure that this is not created with the System Administrator profile, as that profile has access to all Visualforce pages regardless of the security settings:
ProtectedContent in the Label field.ProtectedContent.page file from the code download into the Visualforce Markup area.
LaunchController.cls Apex class from the code download into the Apex Class area.Launch in the Label field.Launch.page file from the code download into the Visualforce Markup area.Log in using your second user credentials and open the following URL in your browser: https://<instance>/apex/Launch.
Here, <instance> is the Salesforce instance specific to your organization, for example, na6.salesforce.com.
The resulting page displays a friendly error message announcing that your user does not have access to the page, and renders a clickable link to request access.

The Launch page declaration contains an action attribute:
<apex:page controller="LaunchController" action="{!allowAccess}">
This invokes the allowAccess action method in the controller before the page is rendered:
public PageReference allowAccess()
{
PageReference pr=Page.ProtectedContent;
try
{
pr.getContent();
}
catch (Exception e)
{
pr=null;
}
return pr;
}
The allowAccess method attempts to retrieve the contents of the protected page programmatically. If the contents are retrieved successfully, it returns the page reference for the Protected page, which redirects the user to that page. If an exception occurs, the method returns null, which leaves the user on the Launch page and displays the friendly error message.