Creating a Processor for the getSearchUserIdentities Pipeline
Creating a Processor for the getSearchUserIdentities Pipeline
The getSearchUserIdentities pipeline allows for additional user identities to pass when performing a search query against the search index.
This feature is especially useful when querying external content as it uses a different security model than Sitecore (Active Directory, SharePoint, Salesforce, etc).
In this example, there’s a Sitecore instance that wants to present external content. This external content consists of files secured with Active Directory; they’re not stored in Sitecore but are already indexed by Coveo. Different methods exist to achieve this behavior. The approach presented here is to take an Active Directory account that can access the file and inject it to the Sitecore identities. In other words, when performing a search query, Sitecore uses two identities: the current Sitecore identity and the Active Directory identity allowed to read the files. To achieve this task, you have to implement a pipeline processor. Here is a code sample to do it.
using Coveo.Framework.Processor;
using Coveo.Framework.Security;
using Coveo.SearchProvider.Rest.Pipelines;
namespace Tutorials.Lib.GetSearchUserIdentities
{
/// <summary>
/// This processor injects an additional user identity at query time.
/// </summary>
public class AddSpecificUserIdentity : IProcessor<GetSearchUserIdentitiesArgs>
{
/// <summary>
/// The process method is called when the <b>getSearchUserIdentities</b> pipeline is invoked.
/// </summary>
/// <param name="p_Args">The arguments contains the active user and the list of user identities used to perform the search query.</param>
public void Process(GetSearchUserIdentitiesArgs p_Args)
{
// In this case, we have an Active Directory account (PublicContent) that can see
// every public item. Adding this PublicContent identity at query time will allow
// the website to present the public items to everybody, regardless of the
// Sitecore user that's currently in use.
p_Args.Identities.Add(new Identity {
Name = "PublicContent",
SecurityProviderName = "Active Directory",
Type = IdentityType.User
});
}
}
}
-
Build the code. In this sample, it generates the
Tutorials.Lib.dllfile. -
Copy the
Tutorials.Lib.dllfile in the website bin folder. -
Edit the
Coveo.SearchProvider.Rest.Custom.configfile to register the processor. Sitecore will then be able to use it. To do so, add this code under thegetSearchUserIdentitiesnode.<processor type="Tutorials.Lib.GetSearchUserIdentities.AddSpecificUserIdentity, Tutorials.Lib" />You may have to copy the
getSearchUserIdentitiesnode from theCoveo.SearchProvider.Rest.configfile. Note that you’re discouraged from modifying this file, as it may cause upgrading issues. -
Sitecore can now retrieve the content that’s granted to the PublicContent user.
NoteTo validate which user identities are passed to the search index, you can use the
debug=1query string parameter when calling the/coveo/restendpoint.For example,
http://sitecoreInstance/coveo/rest?debug=1returns a JSON document containing theuserIdentitiesattribute. The content of this attribute looks like this."userIdentities": [ { "name": "extranet\Anonymous", "provider": "Sitecore Security Provider", "type": "User" },{ "name": "PublicContent", "provider": "Active Directory", "type": "User" } ]