Try our conversational search powered by Generative AI!

Mahdi Shahbazi
Jan 4, 2017
  3061
(1 votes)

How to Check Visitor Group Criteria In Code

If you want to check if a contentAreaItem is match current user all visitor group criteria you can use the following Methods:

  public static bool IsMatchCriteria(ContentAreaItem contentAreaItem)
        {
            var result = true;
            contentAreaItem.AllowedRoles.ForEach(role =>
            {
                    result = result && IsMatchCriteria(role);
            });
            return result;
        }
 

        public static bool IsMatchCriteria(string guid)
        {
            var visitorGroupGuidId = new Guid(guid);
            var visitorGroupRepository = ServiceLocator.Current.GetInstance<IVisitorGroupRepository>();
            var user = HttpContext.Current.User;

            var vgHelper = new VisitorGroupHelper();
            var visitorGroup = visitorGroupRepository.Load(visitorGroupGuidId);
            return vgHelper.IsPrincipalInGroup(user, visitorGroup.Name);
        }

[Pasting files is not allowed][Pasting files is not allowed][Pasting files is not allowed][Pasting files is not allowed]

Jan 04, 2017

Comments

Aria Zanganeh
Aria Zanganeh Jan 4, 2017 08:04 PM

Great job mate!

valdis
valdis Jan 4, 2017 09:42 PM

just my 2 cent here:

a) why not convert "role" from IsMatchCriteria method to Guid already and pass in that, instead of convert string to Guid?

b) I would try to avoid using ServiceLocator in this context, but instead - would require from "above" - somebody has to inject that dependency in order to test for visitor group match.

Please login to comment.
Latest blogs
From Procrastination to Proficiency: Navigating Your Journey to Web Experimentation Certification

Hey there, Optimizely enthusiasts!   Join me in celebrating a milestone – I'm officially a certified web experimentation expert! It's an exhilarati...

Silvio Pacitto | May 17, 2024

GPT-4o Now Available for Optimizely via the AI-Assistant plugin!

I am excited to announce that GPT-4o is now available for Optimizely users through the Epicweb AI-Assistant integration. This means you can leverag...

Luc Gosso (MVP) | May 17, 2024 | Syndicated blog

The downside of being too fast

Today when I was tracking down some changes, I came across this commit comment Who wrote this? Me, almost 5 years ago. I did have a chuckle in my...

Quan Mai | May 17, 2024 | Syndicated blog

Optimizely Forms: Safeguarding Your Data

With the rise of cyber threats and privacy concerns, safeguarding sensitive information has become a top priority for businesses across all...

K Khan | May 16, 2024