Skip to content

How to Add a Customer Group Restriction to Navigation Node

You have some exclusive customers. You offer some special sales for these customers. They should be able to see a navigation node on the page so they can visit the special campaign page. So, you have to display some navigation nodes for users who belong to a specific user group and hide them for other users. To accomplish this requirement we can use Search Restriction.

Search Restriction

Search restriction is a restriction type that you can apply for models. It has 3 main parts:

  1. Restricted Type (Model): Model that you want to apply restriction
  2. Apply On: User group for filtering the model
  3. Filter (Query): Custom query. This query will be added to end of the normal query of the model.

Filter query will be added at end of the your model’s SQL query. By this, we will be able to add some additional filters to the model’s query. What a great idea!

You can find search restrictions in the BackofficeSystemPersonalization

Implementation

Create user group

I have a user group called FamilySaleGroup. I want to make a navigation node visible for only users of this group.

User group impex:

INSERT_UPDATE UserGroup; uid[unique = true]; locname[lang = en];
                       ; FamilySaleGroup   ; Family Sale Group ;

Create navigation nodes

I have two navigation nodes. FamilySaleRootNode and as a sub-node FamilySaleSubNode

Impex:

INSERT_UPDATE CMSLinkComponent; $contentCV[unique = true]; uid[unique = true]          ; name            ; linkname[lang = $lang]; url       ; &linkRef; target(code)[default = 'sameWindow']
                              ;                          ; familySaleTopNavLink ; Family Sale     ; Family Sale           ; /family-sale     ; familySaleTopNavLink ;
                              ;                          ; familySaleMenLink    ; Family Sale Men ; Family Sale Men       ; /family-sale-men ; familySaleMenLink    ;

INSERT_UPDATE CMSNavigationNode; uid[unique = true]   ; $contentCV[unique = true];  title[lang = $lang]; name                   ; parent(uid, $contentCV); links(&linkRef)  
                               ; FamilySaleRootNode   ;                          ;  Family Sale        ; FamilySale Category    ; RootNodeOfSite     ; familySaleTopNavLink 
                               ; FamilySaleSubNode    ;                          ;  Family Sale Men    ; FamilySaleSub Category ; FamilySaleRootNode ; familySaleMenLink

Create Search Restriction

Go to backofficeSystemPersonalization

Create new by pressing + button

Give identifier familySaleNavigationNodeVisibility

Select CMSNavigationNode as Restricted Type

Set generate true

Set active true

Select customergroup as Apply On

Filter query:

({item:uid} in ('FamilySaleRootNode', 'FamilySaleSubNode') AND EXISTS ( {{ select {ug:PK} from {UserGroup as ug} where {ug:PK} IN (?session.user.groups) and {ug:uid} = 'FamilySaleGroup' }})) OR {item:uid} not in ('FamilySaleRootNode', 'FamilySaleSubNode')

Explanation of the query:

{item:uid} : item is your model which is defined in the Restricted Type. You can use any field of the model.

session.user.groups : You can access session variables in the query. I used groups variable to get current users groups.

If navigation node equals FamilySaleRootNode or FamilySaleSubNode and session user has group FamilySaleGroup display navigation node

or display navigation nodes that do not equal FamilySaleRootNode and FamilySaleSubNode

As you can see you can do anything that you do in the flexible query. You can use joins, conditions. You can build query in the hac → flexibleSearch and then you can copy to search restriction. If you have too many nodes, you may create better query by using parent-pk relation.

Result

An unauthorized user will see

An authorized user will see

Leave a Reply

Your email address will not be published. Required fields are marked *