0

I'm not certain how to make the below class, which is called from a trigger on Chatter posts, (it prevents users from posting in Chatter, unless related to a group, or a subset of Objects) also evaluate for the user profile.

Basically, there are 'superusers' that should be able to post anywhere, while the rest are held to the below rules. So the logic would be "if the current user is "Business Super User" profile, then ignore the rest of the class. I believe I can evaluate the current user profile as the first IF statement?

public with sharing class ChatterPostValidation {
    public static Boolean isChatterPostAllowed(FeedItem feedItem) {
        //ParentName is the name of where the post is going
        //e.g. "CollaborationGroup" means it's being posted to a chatter group
        String ParentName = feedItem.ParentId.getSObjectType().getDescribe().getName();

        //check if place being posted is one of the following:
        //chatter group, account (firm), contact, deal     
        if ('CollaborationGroup'.Equals(ParentName)) {
                return true;
        }
        if ('Account'.Equals(ParentName)            ||
            'Contact'.Equals(ParentName)            ||
            'Investment__c'.Equals(ParentName)) {
            return true;
        }
        return false;
    }
}

2 Answers 2

4

You should use the UserInfo class

Id profileId=userinfo.getProfileId();
String profileName=[Select Id,Name from Profile where Id=:profileId].Name;
system.debug('ProfileName' + profileName);
if(profile == 'BusinessSuperUser'){
   //Code Statements
}

To help you testing it, you should use the runAs method.

7
  • So, if I want to ignore the remaining class if it's a specific profile, my logic is: if(profile != 'BusinessSuperUser'){ //Code Statements }
    – HomerJ
    Jan 27, 2015 at 22:59
  • 1
    Yes, that's correct. Jan 27, 2015 at 23:01
  • 2
    You should really have a statically cached whitelist methinks.
    – Adrian Larson
    Jan 27, 2015 at 23:04
  • @AdrianLarson Could you explain? I could use the training.
    – HomerJ
    Jan 27, 2015 at 23:06
  • Nevermind. I see below.
    – HomerJ
    Jan 27, 2015 at 23:07
3

You can absolutely whitelist/exempt certain Profiles from validation.

public with sharing class ChatterPostValidation {
    public static Set<Id> exemptProfileIds
    {
        get
        {
            if (exemptProfileIds == null)
            {
                exemptProfileIds = new Set<Id>();
                for (Profile exemptProfile : [SELECT Id FROM Profile]) // WHERE ...
                {
                    exemptProfileIds.add(exemptProfile.Id);
                }
            }
            return exemptProfileIds;
        }
        private set;
    }
    public static Boolean isChatterPostAllowed(FeedItem feedItem) {
        if (exemptProfileIds.contains(UserInfo.getProfileId())) return true
        // rest of check
    }
}
4
  • Hi Adrian, if I'm not wrong the use of this lazy loading pattern is that we would have only one SOQL in an execution context. Please correct me if I'm wrong.
    – Jarvis
    Nov 20, 2016 at 4:19
  • That's correct. You could call it a thousand times and it would query just once.
    – Adrian Larson
    Nov 20, 2016 at 4:54
  • Hi Adrian, how different is lazy loading than singleton pattern. Here I see that there would be only one object instantiated just like in singleton class.
    – Jarvis
    Nov 22, 2016 at 3:16
  • It's not that different. I guess the singleton pattern is a special type of lazy loading.
    – Adrian Larson
    Nov 22, 2016 at 15:41

You must log in to answer this question.

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