Search Results based on User Roles

Aden Tutton asked on November 28, 2017 14:43

Hi All,

I have a group of search results which currently gather pages from a number of categories branching off a parent page (breeds of animal). What I would like to do is to filter the search results based on the aubscription a user has, so if they only have one type of subscription then only those results would show in the search results. I have split the smart search results into separate search indexes but an struggling to find the logic to assign the correct index to the search results based on the user role

Below is my current code in the indexes field of my web part

{% 
Transformations["Vetstream.ContentTransformations.Search_SetSuffix"].TransformationCode;
Transformations["Vetstream.ContentTransformations.Includes_LoginRegister"].TransformationCode; 
#%}

{% 
MembershipCodeNames = "AnnualSubscriptionRole" + suffix;
TrialMembershipCodeNames = "TrialSubscriptionRole" + suffix;

if (MembershipContext.AuthenticatedUser.HasMembership(MembershipCodeNames) || MembershipContext.AuthenticatedUser.HasMembership(TrialMembershipCodeNames)) {
    index = suffix + "Results";
}
#%}

Thanks!

Aden

Recent Answers


Dragoljub Ilic answered on November 28, 2017 15:04

Hi Aden,

If I understand you well, you want to choose correct index based on current user role? If that is the case, you can use this piece of code to check if current user is in specific role: MembershipContext.AuthenticatedUser.IsInRole("YourRoleName", SiteContext.CurrentSiteName)

I think that this, in combination with simple if-else statement will be enough to solve your problem.

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on November 28, 2017 16:09 (last edited on December 10, 2019 02:31)

Are you using portal model or ACX? If i understand correctly you want to use a chosen index for the smart search results web part. You can try this approach:

{% 
MembershipCodeNames = "AnnualSubscriptionRole" + suffix;
TrialMembershipCodeNames = "TrialSubscriptionRole" + suffix;
seachIndex = "defaultsearchIndex"; // some default index if there no trial or other subscription
if(MembershipContext.AuthenticatedUser.HasMembership(MembershipCodeNames)) {seachIndex ="membershipSearchIndexName"}
if(MembershipContext.AuthenticatedUser.HasMembership(TrialMembershipCodeNames)) {seachIndex ="trialSearchIndexName"}
return seachIndex;
|(identity)GlobalAdministrator%}

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on November 28, 2017 18:05 (last edited on December 10, 2019 02:31)

Diregard my previous post. I've taken some time to investigate the issue. I know It is kind of crazy. You probably should read more on Membership management

I've checked this on the version 9. For some reason you can find your assignments in OtherBindings, i.e. {% CurrentUser.OtherBindings.MembershipUsers|(identity)GlobalAdministrator%}

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.