3

Home page components get added to a homepage layout that are assigned per profile, now if I want to hide the component for specific users in the profile is it possible to do so ?

eg: sysadmin has user A , user B, user C.

If I have to hide the homepage component (nothing but a IFRAME where src = apex page) for user C and show it only to A and B is it even possible?

Is there some way to hide it using Java script?

I was able to find some reference and got down to this

<div class="iframe">
<iframe src="/apex/page" style="width:1000px;height:400px;"></iframe>
<style type="text/css">
.iframe{display:none !important;} // hide the iframe itself 
h2{display:none !important; //salesforce always uses h2 for custom component headings thus this takes cares of hiding the component name, bad approach if there are other custom components, alternative solutions would be most helpful :) 
</style>
</div>

*the above script works awesome and hides the custom component but i want to scrap the user name of the user from the top of the page and hide the component.*

By inspecting the element i found the class name to be :

**<h1 class="currentStatusUserName"><a href="/_ui/core/userprofile/UserProfilePage">username test user</a>**
</h1>

can someone point me how to scrap the username using jScript to actually compare who logged in and hide the custom homepage component?

Any help will be appreciated.

thanks a lot!!

*UPDATE : Harcoded user Id to make the component only visible for specific users *

Code for refrence :

    <div class="iframe">
    <iframe src="/apex/page" style="width:1000px;height:400px;"></iframe>
    </div>  
    <script>
// moved from the top div and found that the class name for username on the home page as
currentStatusUserName and used  querySelectorAll since IE8 does not understand getElementsByClassName 
    var userid   = UserContext.userId;
    var div_hide_1 = document.querySelectorAll('.iframe')[0];
    var div_hide_2 = document.getElementsByTagName('h2');

    if(userid   == 'someuserid'){
    }
    else{
        for(i=0;i<div_hide_2.length;i++){
            if(div_hide_2[i].innerHTML == 'Component Test'){
                div_hide_1.style.display = 'none';
                div_hide_2[i].style.display  = 'none';
            }
        }
    }
    </script>
2
  • I do not have a direct answer, but I'd like to think at the very least, with Custom_Field_1__c on the User database, you could mark C with it (or vice versa the other two) and filter the component based on that field (?). Not knowledgeable enough to give an actual code solution, sorry.
    – AMM
    Mar 19, 2013 at 21:09
  • @AMM wish I could easily get the user info using some other technique since I cannot use any ajax on home page components :(
    – Rao
    Mar 19, 2013 at 22:23

3 Answers 3

3

Messy. Real shame that stuff like {!$User.Username}, {!$UserRole.Name} doesn't work in the HTML components.

If you have to scrape - try to do it by user id, not by name of the user. I've recently fixed an interesting bug where one trigger didn't work for Hungarian users - trigger was looking for FirstName LastName and Hungarian locale flips the order like in Asian languages ;)

There's one javascript variable you might reuse without all the hassle of traversing DOM:

var id = UserContext.userId;
console.log(id);

Combine with an array with user Ids and you're getting somewhere. I'd be tempted to explore cookies route too (kind of user preferences) but that means you'd have to assign them somehow in pure JS (cookie served from Visualforce will come from different domain) and you'd need to think how to set cookie if null before relying on its' value so it works ok when new user / new browser is used...

Is AJAX really banned? One query for example to test Group membership (or flag on User), set the cookie and subsequently rely only on the cookie could work and be fairly OK in maintenance.


If you can live with having ugly placeholders in the sidebar use <apex:page rendered="false">. Put a boolean condition there (if your page has a controller it can be as complex as you need) and on the home page the iframe will still be displayed, just with blank content.

3
  • That helped a lot @eyescream :) !!! As you said the userid seems to be best of the worst route. Thanks for the solution.
    – Rao
    Mar 20, 2013 at 15:28
  • In the rendered="false" approach the empty frame lives on every screen along with the name of the component. The whole point was to restrict access only to a few users
    – Rao
    Mar 20, 2013 at 15:31
  • Glad I could help :)
    – eyescream
    Mar 20, 2013 at 15:46
2

As horrible as this sounds, I'd suggest that the best option would be to simply have more profiles to do this. Even though it means more profile maintenance it would be the more reliable and supported option.

Poking around with javascript and the like to manipulate the DOM is only going to cause problems if the DOM changes and you never know when that might happen.

1
  • I only wish I could do that. The problem is that I need to enable this component for multiple users from different profiles and cannot consolidated into one profile/ I cannot create one profile_clone specific to each user :(!!! Having said that I tried to put the apex page as a VF component inside a dashboard, since dashboards are not expandable the VF page looks crappy inside the dashboard :( thats the whole reason i am doing this crappy design :/
    – Rao
    Mar 19, 2013 at 23:42
0

Ideally you would have the apex page hide itself, since it has the context of who the running user is. Trouble is cross-site scripting (XSS) safe guards are going to prevent javascript from your visualforce page from modifying the parent page.

However, if you're in control of both domains (which you are) you can use Post Messaging to pass stringified functions between the two domains, allowing you to do things like hide a home page component in the salesforce.com domain from a script in the visual.force.com domain. I've personally never had the opportunity to try this out, but I've seen it done successfully, and once mastered could be a pretty fantastic technique to have in your toolbelt.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .