What is page access ?
In salesforce, we can apply security to visualforce pages based on profiles.
How to enforce them in visualforce ?
It's is enforced automatically when user tries the access the restricted page. But, if you want to identify the access before you redirect the user, there's a solution for that.
How to do that ?
Apex profile getContent() method on PageReference class. This applies the profile permissions before returning the content. So, we could use that to determine the access.
Where is the code ?
For illustration, i have given a textbox to accept the page name & then check the access based on that.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class testcls9_08122016 { | |
public string pageName { get; set; } | |
public Boolean getHasAccess() { | |
try { | |
PageReference newPage = new PageReference('/apex/'+pageName); | |
newPage.getContent(); | |
} catch(Exception e) { | |
return False; | |
} | |
return True; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<apex:page controller="testcls9_08122016"> | |
<apex:form> | |
<apex:inputText value="{!pageName}" html-placeholder="Enter page name to check access"/> | |
<apex:commandButton value="check" rerender="MessageArea"/> | |
<apex:outputPanel id="MessageArea"> | |
<apex:outputPanel layout="block" rendered="{!HasAccess}"> | |
Yes, Page {!pageName} has access | |
</apex:outputPanel> | |
<apex:outputPanel layout="block" rendered="{!!HasAccess}"> | |
Sorry, You don't have Page {!pageName} access | |
</apex:outputPanel> | |
</apex:outputPanel> | |
</apex:form> | |
</apex:page> |
No comments:
Post a Comment
Thanks for reading my blog !