November Happy Hour will be moved to Thursday December 5th.

Mahdi Shahbazi
Jan 4, 2017
  3195
(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
Optimizely SaaS CMS + Coveo Search Page

Short on time but need a listing feature with filters, pagination, and sorting? Create a fully functional Coveo-powered search page driven by data...

Damian Smutek | Nov 21, 2024 | Syndicated blog

Optimizely SaaS CMS DAM Picker (Interim)

Simplify your Optimizely SaaS CMS workflow with the Interim DAM Picker Chrome extension. Seamlessly integrate your DAM system, streamlining asset...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Optimizely CMS Roadmap

Explore Optimizely CMS's latest roadmap, packed with developer-focused updates. From SaaS speed to Visual Builder enhancements, developer tooling...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Set Default Culture in Optimizely CMS 12

Take control over culture-specific operations like date and time formatting.

Tomas Hensrud Gulla | Nov 15, 2024 | Syndicated blog

I'm running Optimizely CMS on .NET 9!

It works 🎉

Tomas Hensrud Gulla | Nov 12, 2024 | Syndicated blog

Recraft's image generation with AI-Assistant for Optimizely

Recraft V3 model is outperforming all other models in the image generation space and we are happy to share: Recraft's new model is now available fo...

Luc Gosso (MVP) | Nov 8, 2024 | Syndicated blog