14

I have a Force.com site portal written completely with custom Visualforce, HTML and CSS.

I have a component which is used to display the navigation links at the top of the page. This navigation is built using and

  • tags which is pretty common.

    <ul id="hdrnav">
                    <li><a href="/apex/billing" id="billnav">Billing</a></li>
                    <li><a href="/apex/deposits" id="depositsnav">Deposits</a></li>  
                    <li><a href="/apex/checks" id="checksnav">Checks</a></li>   
    
                </ul>
    

    Now I want to be able to render these

  • links based on the user's security as each of these links display information from different custom objects.

    I know we can check a user's access to a record using something like:

    {!$ObjectType.objectname.accessible}
    

    So how can I achieve something like not displaying the

  • link for deposits if the user has no access to the deposit custom object.

    • Are you on Force.com Sites or Site.com? – joshbirk Nov 6 '12 at 17:09
    • Force.com sites. Editing. – Richard N Nov 6 '12 at 17:15
    • Just bind in output pannel .Should be good what Josh suggesting – Mohith Shrivastava Nov 6 '12 at 18:02
    18

    Off the cuff, I believe you can overload the use of the <apex:variable> tag along with it's rendered flag to show/hide individual elements.

    <apex:variable var="foo" rendered="{!$ObjectType.objectname.accessible} >
        <li> bar ... </li>
    </apex:variable>
    
    • Well this "off the cuff" solution works for me :) – Richard N Nov 7 '12 at 16:33
    • Marking as solution as this works and will be easy to implement. – Richard N Nov 7 '12 at 16:39
    • This worked for me, however I had to add the value='foo' attribute. – Ricky Hewitt Sep 3 '13 at 21:19
    • I added the same value and it worked like magic! Thanks for the awesome code Kevin! – Sam Jun 9 '15 at 9:52
    8

    This is a bit off the cuff, but I think you can wrap it in a panel:

    <apex:outputPanel rendered="{!$ObjectType.objectname.accessible}" layout="none"><!-- content --> </apex:outputPanel>
    

    Would that work in your case?

    • Thanks I did think of this but unfortunately it wont work in my case. – Richard N Nov 6 '12 at 18:59
    • Upvoting as this will also help in general cases. – Richard N Nov 7 '12 at 16:38
    5

    One option would be to compose them server side, then display with <apex:dataList> or <apex:repeat>. Useful especially if the list will grow in future?

    To do that build some wrapper apex class (or even something as simple as Map<String, String>) that would hold the URL and display name. Anything you can do in VF with {!$ObjectType.Account.accessible} you can check with Account.sObjectType.getDescribe().isAccessible()

    • 2
      This answer gets my vote as it keeps the business logic in the controller where it belongs, while the page simply iterates the collection it is given and renders it. – Bob Buzzard Nov 6 '12 at 17:46
    • @BobBuzzard Thx :) I'm torn about that one to be honest. Sometimes I do a lot of magic in rendered if it's just for display related, not manipulating the data. Plus I think describes used on the page don't count towards the governor limit? – eyescream Nov 6 '12 at 17:51
    • Thanks for letting me how I can do this in the controller as well. The answers here will help folks no doubt. For my specific purposes and for its simplicity to implement, I will go with Kevin's solution. Another thing is I know VF will render <Apex:DataList> as <ul> and <li>. I wish Salesforce gives us new apex equivalents of <ul> and <li>. I have some formatting CSS on these elements as well. Thank you. Upvoting. – Richard N Nov 7 '12 at 16:37
    1

    If the VF level or Controller level is not an option (the two first things I would try) - then you could also potentially control it via jQuery. Give the LI's you want to hide a specific class, and then tie it back to a function which checks if the user has the permission, and then shows the LI (instead of the other way around).

    • Its nice to know I could have tried this which was really next on my list. But Kevin's solution works for me and its easier to make that change. Thank you. – Richard N Nov 7 '12 at 16:34
    • Upvoting this solution as JQuery can also be used for conditional checking. – Richard N Nov 7 '12 at 16:39

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

    Not the answer you're looking for? Browse other questions tagged or ask your own question.