Mahdi Shahbazi
Jan 4, 2017
  3103
(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
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024